1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2020 Facebook */
4 #include <linux/bits.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>
27 #define PCI_VENDOR_ID_FACEBOOK 0x1d9b
28 #define PCI_DEVICE_ID_FACEBOOK_TIMECARD 0x0400
30 #define PCI_VENDOR_ID_CELESTICA 0x18d4
31 #define PCI_DEVICE_ID_CELESTICA_TIMECARD 0x1008
33 #define PCI_VENDOR_ID_OROLIA 0x1ad7
34 #define PCI_DEVICE_ID_OROLIA_ARTCARD 0xa000
36 static struct class timecard_class = {
65 #define OCP_CTRL_ENABLE BIT(0)
66 #define OCP_CTRL_ADJUST_TIME BIT(1)
67 #define OCP_CTRL_ADJUST_OFFSET BIT(2)
68 #define OCP_CTRL_ADJUST_DRIFT BIT(3)
69 #define OCP_CTRL_ADJUST_SERVO BIT(8)
70 #define OCP_CTRL_READ_TIME_REQ BIT(30)
71 #define OCP_CTRL_READ_TIME_DONE BIT(31)
73 #define OCP_STATUS_IN_SYNC BIT(0)
74 #define OCP_STATUS_IN_HOLDOVER BIT(1)
76 #define OCP_SELECT_CLK_NONE 0
77 #define OCP_SELECT_CLK_REG 0xfe
92 #define TOD_CTRL_PROTOCOL BIT(28)
93 #define TOD_CTRL_DISABLE_FMT_A BIT(17)
94 #define TOD_CTRL_DISABLE_FMT_B BIT(16)
95 #define TOD_CTRL_ENABLE BIT(0)
96 #define TOD_CTRL_GNSS_MASK GENMASK(3, 0)
97 #define TOD_CTRL_GNSS_SHIFT 24
99 #define TOD_STATUS_UTC_MASK GENMASK(7, 0)
100 #define TOD_STATUS_UTC_VALID BIT(8)
101 #define TOD_STATUS_LEAP_ANNOUNCE BIT(12)
102 #define TOD_STATUS_LEAP_VALID BIT(16)
130 #define PPS_STATUS_FILTER_ERR BIT(0)
131 #define PPS_STATUS_SUPERV_ERR BIT(1)
144 struct irig_master_reg {
153 #define IRIG_M_CTRL_ENABLE BIT(0)
155 struct irig_slave_reg {
164 #define IRIG_S_CTRL_ENABLE BIT(0)
166 struct dcf_master_reg {
174 #define DCF_M_CTRL_ENABLE BIT(0)
176 struct dcf_slave_reg {
184 #define DCF_S_CTRL_ENABLE BIT(0)
206 struct frequency_reg {
211 struct board_config_reg {
212 u32 mro50_serial_activate;
215 #define FREQ_STATUS_VALID BIT(31)
216 #define FREQ_STATUS_ERROR BIT(30)
217 #define FREQ_STATUS_OVERRUN BIT(29)
218 #define FREQ_STATUS_MASK GENMASK(23, 0)
220 struct ptp_ocp_flash_info {
227 struct ptp_ocp_firmware_header {
229 __be16 pci_vendor_id;
230 __be16 pci_device_id;
236 #define OCP_FIRMWARE_MAGIC_HEADER "OCPC"
238 struct ptp_ocp_i2c_info {
240 unsigned long fixed_rate;
245 struct ptp_ocp_ext_info {
247 irqreturn_t (*irq_fcn)(int irq, void *priv);
248 int (*enable)(void *priv, u32 req, bool enable);
251 struct ptp_ocp_ext_src {
254 struct ptp_ocp_ext_info *info;
258 enum ptp_ocp_sma_mode {
263 struct ptp_ocp_sma_connector {
264 enum ptp_ocp_sma_mode mode;
271 struct ocp_attr_group {
273 const struct attribute_group *group;
276 #define OCP_CAP_BASIC BIT(0)
277 #define OCP_CAP_SIGNAL BIT(1)
278 #define OCP_CAP_FREQ BIT(2)
280 struct ptp_ocp_signal {
290 struct ptp_ocp_serial_port {
295 #define OCP_BOARD_ID_LEN 13
296 #define OCP_SERIAL_LEN 6
299 struct pci_dev *pdev;
302 struct ocp_reg __iomem *reg;
303 struct tod_reg __iomem *tod;
304 struct pps_reg __iomem *pps_to_ext;
305 struct pps_reg __iomem *pps_to_clk;
306 struct board_config_reg __iomem *board_config;
307 struct gpio_reg __iomem *pps_select;
308 struct gpio_reg __iomem *sma_map1;
309 struct gpio_reg __iomem *sma_map2;
310 struct irig_master_reg __iomem *irig_out;
311 struct irig_slave_reg __iomem *irig_in;
312 struct dcf_master_reg __iomem *dcf_out;
313 struct dcf_slave_reg __iomem *dcf_in;
314 struct tod_reg __iomem *nmea_out;
315 struct frequency_reg __iomem *freq_in[4];
316 struct ptp_ocp_ext_src *signal_out[4];
317 struct ptp_ocp_ext_src *pps;
318 struct ptp_ocp_ext_src *ts0;
319 struct ptp_ocp_ext_src *ts1;
320 struct ptp_ocp_ext_src *ts2;
321 struct ptp_ocp_ext_src *ts3;
322 struct ptp_ocp_ext_src *ts4;
323 struct ocp_art_gpio_reg __iomem *art_sma;
324 struct img_reg __iomem *image;
325 struct ptp_clock *ptp;
326 struct ptp_clock_info ptp_info;
327 struct platform_device *i2c_ctrl;
328 struct platform_device *spi_flash;
329 struct clk_hw *i2c_clk;
330 struct timer_list watchdog;
331 const struct attribute_group **attr_group;
332 const struct ptp_ocp_eeprom_map *eeprom_map;
333 struct dentry *debug_root;
337 struct ptp_ocp_serial_port gnss_port;
338 struct ptp_ocp_serial_port gnss2_port;
339 struct ptp_ocp_serial_port mac_port; /* miniature atomic clock */
340 struct ptp_ocp_serial_port nmea_port;
344 u8 board_id[OCP_BOARD_ID_LEN];
345 u8 serial[OCP_SERIAL_LEN];
346 bool has_eeprom_data;
350 u32 ts_window_adjust;
352 struct ptp_ocp_signal signal[4];
353 struct ptp_ocp_sma_connector sma[4];
354 const struct ocp_sma_op *sma_op;
357 #define OCP_REQ_TIMESTAMP BIT(0)
358 #define OCP_REQ_PPS BIT(1)
360 struct ocp_resource {
361 unsigned long offset;
364 int (*setup)(struct ptp_ocp *bp, struct ocp_resource *r);
366 unsigned long bp_offset;
367 const char * const name;
370 static int ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r);
371 static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r);
372 static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r);
373 static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r);
374 static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r);
375 static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r);
376 static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv);
377 static irqreturn_t ptp_ocp_signal_irq(int irq, void *priv);
378 static int ptp_ocp_ts_enable(void *priv, u32 req, bool enable);
379 static int ptp_ocp_signal_from_perout(struct ptp_ocp *bp, int gen,
380 struct ptp_perout_request *req);
381 static int ptp_ocp_signal_enable(void *priv, u32 req, bool enable);
382 static int ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr);
384 static int ptp_ocp_art_board_init(struct ptp_ocp *bp, struct ocp_resource *r);
386 static const struct ocp_attr_group fb_timecard_groups[];
388 static const struct ocp_attr_group art_timecard_groups[];
390 struct ptp_ocp_eeprom_map {
394 const void * const tag;
397 #define EEPROM_ENTRY(addr, member) \
399 .len = sizeof_field(struct ptp_ocp, member), \
400 .bp_offset = offsetof(struct ptp_ocp, member)
402 #define BP_MAP_ENTRY_ADDR(bp, map) ({ \
403 (void *)((uintptr_t)(bp) + (map)->bp_offset); \
406 static struct ptp_ocp_eeprom_map fb_eeprom_map[] = {
407 { EEPROM_ENTRY(0x43, board_id) },
408 { EEPROM_ENTRY(0x00, serial), .tag = "mac" },
412 static struct ptp_ocp_eeprom_map art_eeprom_map[] = {
413 { EEPROM_ENTRY(0x200 + 0x43, board_id) },
414 { EEPROM_ENTRY(0x200 + 0x63, serial) },
418 #define bp_assign_entry(bp, res, val) ({ \
419 uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset; \
420 *(typeof(val) *)addr = val; \
423 #define OCP_RES_LOCATION(member) \
424 .name = #member, .bp_offset = offsetof(struct ptp_ocp, member)
426 #define OCP_MEM_RESOURCE(member) \
427 OCP_RES_LOCATION(member), .setup = ptp_ocp_register_mem
429 #define OCP_SERIAL_RESOURCE(member) \
430 OCP_RES_LOCATION(member), .setup = ptp_ocp_register_serial
432 #define OCP_I2C_RESOURCE(member) \
433 OCP_RES_LOCATION(member), .setup = ptp_ocp_register_i2c
435 #define OCP_SPI_RESOURCE(member) \
436 OCP_RES_LOCATION(member), .setup = ptp_ocp_register_spi
438 #define OCP_EXT_RESOURCE(member) \
439 OCP_RES_LOCATION(member), .setup = ptp_ocp_register_ext
441 /* This is the MSI vector mapping used.
450 * 8: HWICAP (notused)
453 * 11: Signal Generator 1
454 * 12: Signal Generator 2
455 * 13: Signal Generator 3
456 * 14: Signal Generator 4
462 * 11: Orolia TS0 (GNSS)
468 static struct ocp_resource ocp_fb_resource[] = {
470 OCP_MEM_RESOURCE(reg),
471 .offset = 0x01000000, .size = 0x10000,
474 OCP_EXT_RESOURCE(ts0),
475 .offset = 0x01010000, .size = 0x10000, .irq_vec = 1,
476 .extra = &(struct ptp_ocp_ext_info) {
478 .irq_fcn = ptp_ocp_ts_irq,
479 .enable = ptp_ocp_ts_enable,
483 OCP_EXT_RESOURCE(ts1),
484 .offset = 0x01020000, .size = 0x10000, .irq_vec = 2,
485 .extra = &(struct ptp_ocp_ext_info) {
487 .irq_fcn = ptp_ocp_ts_irq,
488 .enable = ptp_ocp_ts_enable,
492 OCP_EXT_RESOURCE(ts2),
493 .offset = 0x01060000, .size = 0x10000, .irq_vec = 6,
494 .extra = &(struct ptp_ocp_ext_info) {
496 .irq_fcn = ptp_ocp_ts_irq,
497 .enable = ptp_ocp_ts_enable,
501 OCP_EXT_RESOURCE(ts3),
502 .offset = 0x01110000, .size = 0x10000, .irq_vec = 15,
503 .extra = &(struct ptp_ocp_ext_info) {
505 .irq_fcn = ptp_ocp_ts_irq,
506 .enable = ptp_ocp_ts_enable,
510 OCP_EXT_RESOURCE(ts4),
511 .offset = 0x01120000, .size = 0x10000, .irq_vec = 16,
512 .extra = &(struct ptp_ocp_ext_info) {
514 .irq_fcn = ptp_ocp_ts_irq,
515 .enable = ptp_ocp_ts_enable,
518 /* Timestamp for PHC and/or PPS generator */
520 OCP_EXT_RESOURCE(pps),
521 .offset = 0x010C0000, .size = 0x10000, .irq_vec = 0,
522 .extra = &(struct ptp_ocp_ext_info) {
524 .irq_fcn = ptp_ocp_ts_irq,
525 .enable = ptp_ocp_ts_enable,
529 OCP_EXT_RESOURCE(signal_out[0]),
530 .offset = 0x010D0000, .size = 0x10000, .irq_vec = 11,
531 .extra = &(struct ptp_ocp_ext_info) {
533 .irq_fcn = ptp_ocp_signal_irq,
534 .enable = ptp_ocp_signal_enable,
538 OCP_EXT_RESOURCE(signal_out[1]),
539 .offset = 0x010E0000, .size = 0x10000, .irq_vec = 12,
540 .extra = &(struct ptp_ocp_ext_info) {
542 .irq_fcn = ptp_ocp_signal_irq,
543 .enable = ptp_ocp_signal_enable,
547 OCP_EXT_RESOURCE(signal_out[2]),
548 .offset = 0x010F0000, .size = 0x10000, .irq_vec = 13,
549 .extra = &(struct ptp_ocp_ext_info) {
551 .irq_fcn = ptp_ocp_signal_irq,
552 .enable = ptp_ocp_signal_enable,
556 OCP_EXT_RESOURCE(signal_out[3]),
557 .offset = 0x01100000, .size = 0x10000, .irq_vec = 14,
558 .extra = &(struct ptp_ocp_ext_info) {
560 .irq_fcn = ptp_ocp_signal_irq,
561 .enable = ptp_ocp_signal_enable,
565 OCP_MEM_RESOURCE(pps_to_ext),
566 .offset = 0x01030000, .size = 0x10000,
569 OCP_MEM_RESOURCE(pps_to_clk),
570 .offset = 0x01040000, .size = 0x10000,
573 OCP_MEM_RESOURCE(tod),
574 .offset = 0x01050000, .size = 0x10000,
577 OCP_MEM_RESOURCE(irig_in),
578 .offset = 0x01070000, .size = 0x10000,
581 OCP_MEM_RESOURCE(irig_out),
582 .offset = 0x01080000, .size = 0x10000,
585 OCP_MEM_RESOURCE(dcf_in),
586 .offset = 0x01090000, .size = 0x10000,
589 OCP_MEM_RESOURCE(dcf_out),
590 .offset = 0x010A0000, .size = 0x10000,
593 OCP_MEM_RESOURCE(nmea_out),
594 .offset = 0x010B0000, .size = 0x10000,
597 OCP_MEM_RESOURCE(image),
598 .offset = 0x00020000, .size = 0x1000,
601 OCP_MEM_RESOURCE(pps_select),
602 .offset = 0x00130000, .size = 0x1000,
605 OCP_MEM_RESOURCE(sma_map1),
606 .offset = 0x00140000, .size = 0x1000,
609 OCP_MEM_RESOURCE(sma_map2),
610 .offset = 0x00220000, .size = 0x1000,
613 OCP_I2C_RESOURCE(i2c_ctrl),
614 .offset = 0x00150000, .size = 0x10000, .irq_vec = 7,
615 .extra = &(struct ptp_ocp_i2c_info) {
617 .fixed_rate = 50000000,
618 .data_size = sizeof(struct xiic_i2c_platform_data),
619 .data = &(struct xiic_i2c_platform_data) {
621 .devices = (struct i2c_board_info[]) {
622 { I2C_BOARD_INFO("24c02", 0x50) },
623 { I2C_BOARD_INFO("24mac402", 0x58),
624 .platform_data = "mac" },
630 OCP_SERIAL_RESOURCE(gnss_port),
631 .offset = 0x00160000 + 0x1000, .irq_vec = 3,
632 .extra = &(struct ptp_ocp_serial_port) {
637 OCP_SERIAL_RESOURCE(gnss2_port),
638 .offset = 0x00170000 + 0x1000, .irq_vec = 4,
639 .extra = &(struct ptp_ocp_serial_port) {
644 OCP_SERIAL_RESOURCE(mac_port),
645 .offset = 0x00180000 + 0x1000, .irq_vec = 5,
646 .extra = &(struct ptp_ocp_serial_port) {
651 OCP_SERIAL_RESOURCE(nmea_port),
652 .offset = 0x00190000 + 0x1000, .irq_vec = 10,
655 OCP_SPI_RESOURCE(spi_flash),
656 .offset = 0x00310000, .size = 0x10000, .irq_vec = 9,
657 .extra = &(struct ptp_ocp_flash_info) {
658 .name = "xilinx_spi", .pci_offset = 0,
659 .data_size = sizeof(struct xspi_platform_data),
660 .data = &(struct xspi_platform_data) {
665 .devices = &(struct spi_board_info) {
666 .modalias = "spi-nor",
672 OCP_MEM_RESOURCE(freq_in[0]),
673 .offset = 0x01200000, .size = 0x10000,
676 OCP_MEM_RESOURCE(freq_in[1]),
677 .offset = 0x01210000, .size = 0x10000,
680 OCP_MEM_RESOURCE(freq_in[2]),
681 .offset = 0x01220000, .size = 0x10000,
684 OCP_MEM_RESOURCE(freq_in[3]),
685 .offset = 0x01230000, .size = 0x10000,
688 .setup = ptp_ocp_fb_board_init,
693 #define OCP_ART_CONFIG_SIZE 144
694 #define OCP_ART_TEMP_TABLE_SIZE 368
696 struct ocp_art_gpio_reg {
703 static struct ocp_resource ocp_art_resource[] = {
705 OCP_MEM_RESOURCE(reg),
706 .offset = 0x01000000, .size = 0x10000,
709 OCP_SERIAL_RESOURCE(gnss_port),
710 .offset = 0x00160000 + 0x1000, .irq_vec = 3,
711 .extra = &(struct ptp_ocp_serial_port) {
716 OCP_MEM_RESOURCE(art_sma),
717 .offset = 0x003C0000, .size = 0x1000,
719 /* Timestamp associated with GNSS1 receiver PPS */
721 OCP_EXT_RESOURCE(ts0),
722 .offset = 0x360000, .size = 0x20, .irq_vec = 12,
723 .extra = &(struct ptp_ocp_ext_info) {
725 .irq_fcn = ptp_ocp_ts_irq,
726 .enable = ptp_ocp_ts_enable,
730 OCP_EXT_RESOURCE(ts1),
731 .offset = 0x380000, .size = 0x20, .irq_vec = 8,
732 .extra = &(struct ptp_ocp_ext_info) {
734 .irq_fcn = ptp_ocp_ts_irq,
735 .enable = ptp_ocp_ts_enable,
739 OCP_EXT_RESOURCE(ts2),
740 .offset = 0x390000, .size = 0x20, .irq_vec = 10,
741 .extra = &(struct ptp_ocp_ext_info) {
743 .irq_fcn = ptp_ocp_ts_irq,
744 .enable = ptp_ocp_ts_enable,
748 OCP_EXT_RESOURCE(ts3),
749 .offset = 0x3A0000, .size = 0x20, .irq_vec = 14,
750 .extra = &(struct ptp_ocp_ext_info) {
752 .irq_fcn = ptp_ocp_ts_irq,
753 .enable = ptp_ocp_ts_enable,
757 OCP_EXT_RESOURCE(ts4),
758 .offset = 0x3B0000, .size = 0x20, .irq_vec = 15,
759 .extra = &(struct ptp_ocp_ext_info) {
761 .irq_fcn = ptp_ocp_ts_irq,
762 .enable = ptp_ocp_ts_enable,
765 /* Timestamp associated with Internal PPS of the card */
767 OCP_EXT_RESOURCE(pps),
768 .offset = 0x00330000, .size = 0x20, .irq_vec = 11,
769 .extra = &(struct ptp_ocp_ext_info) {
771 .irq_fcn = ptp_ocp_ts_irq,
772 .enable = ptp_ocp_ts_enable,
776 OCP_SPI_RESOURCE(spi_flash),
777 .offset = 0x00310000, .size = 0x10000, .irq_vec = 9,
778 .extra = &(struct ptp_ocp_flash_info) {
779 .name = "spi_altera", .pci_offset = 0,
780 .data_size = sizeof(struct altera_spi_platform_data),
781 .data = &(struct altera_spi_platform_data) {
784 .devices = &(struct spi_board_info) {
785 .modalias = "spi-nor",
791 OCP_I2C_RESOURCE(i2c_ctrl),
792 .offset = 0x350000, .size = 0x100, .irq_vec = 4,
793 .extra = &(struct ptp_ocp_i2c_info) {
794 .name = "ocores-i2c",
795 .fixed_rate = 400000,
796 .data_size = sizeof(struct ocores_i2c_platform_data),
797 .data = &(struct ocores_i2c_platform_data) {
801 .devices = &(struct i2c_board_info) {
802 I2C_BOARD_INFO("24c08", 0x50),
808 OCP_SERIAL_RESOURCE(mac_port),
809 .offset = 0x00190000, .irq_vec = 7,
810 .extra = &(struct ptp_ocp_serial_port) {
815 OCP_MEM_RESOURCE(board_config),
816 .offset = 0x210000, .size = 0x1000,
819 .setup = ptp_ocp_art_board_init,
824 static const struct pci_device_id ptp_ocp_pcidev_id[] = {
825 { PCI_DEVICE_DATA(FACEBOOK, TIMECARD, &ocp_fb_resource) },
826 { PCI_DEVICE_DATA(CELESTICA, TIMECARD, &ocp_fb_resource) },
827 { PCI_DEVICE_DATA(OROLIA, ARTCARD, &ocp_art_resource) },
830 MODULE_DEVICE_TABLE(pci, ptp_ocp_pcidev_id);
832 static DEFINE_MUTEX(ptp_ocp_lock);
833 static DEFINE_IDR(ptp_ocp_idr);
835 struct ocp_selector {
840 static const struct ocp_selector ptp_ocp_clock[] = {
841 { .name = "NONE", .value = 0 },
842 { .name = "TOD", .value = 1 },
843 { .name = "IRIG", .value = 2 },
844 { .name = "PPS", .value = 3 },
845 { .name = "PTP", .value = 4 },
846 { .name = "RTC", .value = 5 },
847 { .name = "DCF", .value = 6 },
848 { .name = "REGS", .value = 0xfe },
849 { .name = "EXT", .value = 0xff },
853 #define SMA_DISABLE BIT(16)
854 #define SMA_ENABLE BIT(15)
855 #define SMA_SELECT_MASK GENMASK(14, 0)
857 static const struct ocp_selector ptp_ocp_sma_in[] = {
858 { .name = "10Mhz", .value = 0x0000 },
859 { .name = "PPS1", .value = 0x0001 },
860 { .name = "PPS2", .value = 0x0002 },
861 { .name = "TS1", .value = 0x0004 },
862 { .name = "TS2", .value = 0x0008 },
863 { .name = "IRIG", .value = 0x0010 },
864 { .name = "DCF", .value = 0x0020 },
865 { .name = "TS3", .value = 0x0040 },
866 { .name = "TS4", .value = 0x0080 },
867 { .name = "FREQ1", .value = 0x0100 },
868 { .name = "FREQ2", .value = 0x0200 },
869 { .name = "FREQ3", .value = 0x0400 },
870 { .name = "FREQ4", .value = 0x0800 },
871 { .name = "None", .value = SMA_DISABLE },
875 static const struct ocp_selector ptp_ocp_sma_out[] = {
876 { .name = "10Mhz", .value = 0x0000 },
877 { .name = "PHC", .value = 0x0001 },
878 { .name = "MAC", .value = 0x0002 },
879 { .name = "GNSS1", .value = 0x0004 },
880 { .name = "GNSS2", .value = 0x0008 },
881 { .name = "IRIG", .value = 0x0010 },
882 { .name = "DCF", .value = 0x0020 },
883 { .name = "GEN1", .value = 0x0040 },
884 { .name = "GEN2", .value = 0x0080 },
885 { .name = "GEN3", .value = 0x0100 },
886 { .name = "GEN4", .value = 0x0200 },
887 { .name = "GND", .value = 0x2000 },
888 { .name = "VCC", .value = 0x4000 },
892 static const struct ocp_selector ptp_ocp_art_sma_in[] = {
893 { .name = "PPS1", .value = 0x0001 },
894 { .name = "10Mhz", .value = 0x0008 },
898 static const struct ocp_selector ptp_ocp_art_sma_out[] = {
899 { .name = "PHC", .value = 0x0002 },
900 { .name = "GNSS", .value = 0x0004 },
901 { .name = "10Mhz", .value = 0x0010 },
906 const struct ocp_selector *tbl[2];
907 void (*init)(struct ptp_ocp *bp);
908 u32 (*get)(struct ptp_ocp *bp, int sma_nr);
909 int (*set_inputs)(struct ptp_ocp *bp, int sma_nr, u32 val);
910 int (*set_output)(struct ptp_ocp *bp, int sma_nr, u32 val);
914 ptp_ocp_sma_init(struct ptp_ocp *bp)
916 return bp->sma_op->init(bp);
920 ptp_ocp_sma_get(struct ptp_ocp *bp, int sma_nr)
922 return bp->sma_op->get(bp, sma_nr);
926 ptp_ocp_sma_set_inputs(struct ptp_ocp *bp, int sma_nr, u32 val)
928 return bp->sma_op->set_inputs(bp, sma_nr, val);
932 ptp_ocp_sma_set_output(struct ptp_ocp *bp, int sma_nr, u32 val)
934 return bp->sma_op->set_output(bp, sma_nr, val);
938 ptp_ocp_select_name_from_val(const struct ocp_selector *tbl, int val)
942 for (i = 0; tbl[i].name; i++)
943 if (tbl[i].value == val)
949 ptp_ocp_select_val_from_name(const struct ocp_selector *tbl, const char *name)
954 for (i = 0; tbl[i].name; i++) {
955 select = tbl[i].name;
956 if (!strncasecmp(name, select, strlen(select)))
963 ptp_ocp_select_table_show(const struct ocp_selector *tbl, char *buf)
969 for (i = 0; tbl[i].name; i++)
970 count += sysfs_emit_at(buf, count, "%s ", tbl[i].name);
973 count += sysfs_emit_at(buf, count, "\n");
978 __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts,
979 struct ptp_system_timestamp *sts)
981 u32 ctrl, time_sec, time_ns;
984 ptp_read_system_prets(sts);
986 ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE;
987 iowrite32(ctrl, &bp->reg->ctrl);
989 for (i = 0; i < 100; i++) {
990 ctrl = ioread32(&bp->reg->ctrl);
991 if (ctrl & OCP_CTRL_READ_TIME_DONE)
994 ptp_read_system_postts(sts);
996 if (sts && bp->ts_window_adjust) {
997 s64 ns = timespec64_to_ns(&sts->post_ts);
999 sts->post_ts = ns_to_timespec64(ns - bp->ts_window_adjust);
1002 time_ns = ioread32(&bp->reg->time_ns);
1003 time_sec = ioread32(&bp->reg->time_sec);
1005 ts->tv_sec = time_sec;
1006 ts->tv_nsec = time_ns;
1008 return ctrl & OCP_CTRL_READ_TIME_DONE ? 0 : -ETIMEDOUT;
1012 ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts,
1013 struct ptp_system_timestamp *sts)
1015 struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
1016 unsigned long flags;
1019 spin_lock_irqsave(&bp->lock, flags);
1020 err = __ptp_ocp_gettime_locked(bp, ts, sts);
1021 spin_unlock_irqrestore(&bp->lock, flags);
1027 __ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts)
1029 u32 ctrl, time_sec, time_ns;
1032 time_ns = ts->tv_nsec;
1033 time_sec = ts->tv_sec;
1035 select = ioread32(&bp->reg->select);
1036 iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
1038 iowrite32(time_ns, &bp->reg->adjust_ns);
1039 iowrite32(time_sec, &bp->reg->adjust_sec);
1041 ctrl = OCP_CTRL_ADJUST_TIME | OCP_CTRL_ENABLE;
1042 iowrite32(ctrl, &bp->reg->ctrl);
1044 /* restore clock selection */
1045 iowrite32(select >> 16, &bp->reg->select);
1049 ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts)
1051 struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
1052 unsigned long flags;
1054 spin_lock_irqsave(&bp->lock, flags);
1055 __ptp_ocp_settime_locked(bp, ts);
1056 spin_unlock_irqrestore(&bp->lock, flags);
1062 __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val)
1066 select = ioread32(&bp->reg->select);
1067 iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
1069 iowrite32(adj_val, &bp->reg->offset_ns);
1070 iowrite32(NSEC_PER_SEC, &bp->reg->offset_window_ns);
1072 ctrl = OCP_CTRL_ADJUST_OFFSET | OCP_CTRL_ENABLE;
1073 iowrite32(ctrl, &bp->reg->ctrl);
1075 /* restore clock selection */
1076 iowrite32(select >> 16, &bp->reg->select);
1080 ptp_ocp_adjtime_coarse(struct ptp_ocp *bp, s64 delta_ns)
1082 struct timespec64 ts;
1083 unsigned long flags;
1086 spin_lock_irqsave(&bp->lock, flags);
1087 err = __ptp_ocp_gettime_locked(bp, &ts, NULL);
1089 set_normalized_timespec64(&ts, ts.tv_sec,
1090 ts.tv_nsec + delta_ns);
1091 __ptp_ocp_settime_locked(bp, &ts);
1093 spin_unlock_irqrestore(&bp->lock, flags);
1097 ptp_ocp_adjtime(struct ptp_clock_info *ptp_info, s64 delta_ns)
1099 struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
1100 unsigned long flags;
1103 if (delta_ns > NSEC_PER_SEC || -delta_ns > NSEC_PER_SEC) {
1104 ptp_ocp_adjtime_coarse(bp, delta_ns);
1108 sign = delta_ns < 0 ? BIT(31) : 0;
1109 adj_ns = sign ? -delta_ns : delta_ns;
1111 spin_lock_irqsave(&bp->lock, flags);
1112 __ptp_ocp_adjtime_locked(bp, sign | adj_ns);
1113 spin_unlock_irqrestore(&bp->lock, flags);
1119 ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm)
1121 if (scaled_ppm == 0)
1128 ptp_ocp_null_getmaxphase(struct ptp_clock_info *ptp_info)
1134 ptp_ocp_null_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns)
1140 ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq,
1143 struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
1144 struct ptp_ocp_ext_src *ext = NULL;
1149 case PTP_CLK_REQ_EXTTS:
1150 req = OCP_REQ_TIMESTAMP;
1151 switch (rq->extts.index) {
1172 case PTP_CLK_REQ_PPS:
1176 case PTP_CLK_REQ_PEROUT:
1177 switch (rq->perout.index) {
1179 /* This is a request for 1PPS on an output SMA.
1180 * Allow, but assume manual configuration.
1182 if (on && (rq->perout.period.sec != 1 ||
1183 rq->perout.period.nsec != 0))
1190 req = rq->perout.index - 1;
1191 ext = bp->signal_out[req];
1192 err = ptp_ocp_signal_from_perout(bp, req, &rq->perout);
1204 err = ext->info->enable(ext, req, on);
1210 ptp_ocp_verify(struct ptp_clock_info *ptp_info, unsigned pin,
1211 enum ptp_pin_function func, unsigned chan)
1213 struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
1218 snprintf(buf, sizeof(buf), "IN: None");
1221 /* Allow timestamps, but require sysfs configuration. */
1224 /* channel 0 is 1PPS from PHC.
1225 * channels 1..4 are the frequency generators.
1228 snprintf(buf, sizeof(buf), "OUT: GEN%d", chan);
1230 snprintf(buf, sizeof(buf), "OUT: PHC");
1236 return ptp_ocp_sma_store(bp, buf, pin + 1);
1239 static const struct ptp_clock_info ptp_ocp_clock_info = {
1240 .owner = THIS_MODULE,
1241 .name = KBUILD_MODNAME,
1242 .max_adj = 100000000,
1243 .gettimex64 = ptp_ocp_gettimex,
1244 .settime64 = ptp_ocp_settime,
1245 .adjtime = ptp_ocp_adjtime,
1246 .adjfine = ptp_ocp_null_adjfine,
1247 .adjphase = ptp_ocp_null_adjphase,
1248 .getmaxphase = ptp_ocp_null_getmaxphase,
1249 .enable = ptp_ocp_enable,
1250 .verify = ptp_ocp_verify,
1257 __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp)
1261 select = ioread32(&bp->reg->select);
1262 iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
1264 iowrite32(0, &bp->reg->drift_ns);
1266 ctrl = OCP_CTRL_ADJUST_DRIFT | OCP_CTRL_ENABLE;
1267 iowrite32(ctrl, &bp->reg->ctrl);
1269 /* restore clock selection */
1270 iowrite32(select >> 16, &bp->reg->select);
1274 ptp_ocp_utc_distribute(struct ptp_ocp *bp, u32 val)
1276 unsigned long flags;
1278 spin_lock_irqsave(&bp->lock, flags);
1280 bp->utc_tai_offset = val;
1283 iowrite32(val, &bp->irig_out->adj_sec);
1285 iowrite32(val, &bp->dcf_out->adj_sec);
1287 iowrite32(val, &bp->nmea_out->adj_sec);
1289 spin_unlock_irqrestore(&bp->lock, flags);
1293 ptp_ocp_watchdog(struct timer_list *t)
1295 struct ptp_ocp *bp = from_timer(bp, t, watchdog);
1296 unsigned long flags;
1297 u32 status, utc_offset;
1299 status = ioread32(&bp->pps_to_clk->status);
1301 if (status & PPS_STATUS_SUPERV_ERR) {
1302 iowrite32(status, &bp->pps_to_clk->status);
1303 if (!bp->gnss_lost) {
1304 spin_lock_irqsave(&bp->lock, flags);
1305 __ptp_ocp_clear_drift_locked(bp);
1306 spin_unlock_irqrestore(&bp->lock, flags);
1307 bp->gnss_lost = ktime_get_real_seconds();
1310 } else if (bp->gnss_lost) {
1314 /* if GNSS provides correct data we can rely on
1315 * it to get leap second information
1318 status = ioread32(&bp->tod->utc_status);
1319 utc_offset = status & TOD_STATUS_UTC_MASK;
1320 if (status & TOD_STATUS_UTC_VALID &&
1321 utc_offset != bp->utc_tai_offset)
1322 ptp_ocp_utc_distribute(bp, utc_offset);
1325 mod_timer(&bp->watchdog, jiffies + HZ);
1329 ptp_ocp_estimate_pci_timing(struct ptp_ocp *bp)
1335 ctrl = ioread32(&bp->reg->ctrl);
1336 ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE;
1338 iowrite32(ctrl, &bp->reg->ctrl);
1340 start = ktime_get_ns();
1342 ctrl = ioread32(&bp->reg->ctrl);
1344 end = ktime_get_ns();
1346 delay = end - start;
1347 bp->ts_window_adjust = (delay >> 5) * 3;
1351 ptp_ocp_init_clock(struct ptp_ocp *bp)
1353 struct timespec64 ts;
1357 ctrl = OCP_CTRL_ENABLE;
1358 iowrite32(ctrl, &bp->reg->ctrl);
1360 /* NO DRIFT Correction */
1361 /* offset_p:i 1/8, offset_i: 1/16, drift_p: 0, drift_i: 0 */
1362 iowrite32(0x2000, &bp->reg->servo_offset_p);
1363 iowrite32(0x1000, &bp->reg->servo_offset_i);
1364 iowrite32(0, &bp->reg->servo_drift_p);
1365 iowrite32(0, &bp->reg->servo_drift_i);
1367 /* latch servo values */
1368 ctrl |= OCP_CTRL_ADJUST_SERVO;
1369 iowrite32(ctrl, &bp->reg->ctrl);
1371 if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) {
1372 dev_err(&bp->pdev->dev, "clock not enabled\n");
1376 ptp_ocp_estimate_pci_timing(bp);
1378 sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC;
1380 ktime_get_clocktai_ts64(&ts);
1381 ptp_ocp_settime(&bp->ptp_info, &ts);
1384 /* If there is a clock supervisor, then enable the watchdog */
1385 if (bp->pps_to_clk) {
1386 timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0);
1387 mod_timer(&bp->watchdog, jiffies + HZ);
1394 ptp_ocp_tod_init(struct ptp_ocp *bp)
1398 ctrl = ioread32(&bp->tod->ctrl);
1399 ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE;
1400 ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B);
1401 iowrite32(ctrl, &bp->tod->ctrl);
1403 reg = ioread32(&bp->tod->utc_status);
1404 if (reg & TOD_STATUS_UTC_VALID)
1405 ptp_ocp_utc_distribute(bp, reg & TOD_STATUS_UTC_MASK);
1409 ptp_ocp_tod_proto_name(const int idx)
1411 static const char * const proto_name[] = {
1412 "NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none",
1413 "UBX", "UBX_UTC", "UBX_LS", "UBX_none"
1415 return proto_name[idx];
1419 ptp_ocp_tod_gnss_name(int idx)
1421 static const char * const gnss_name[] = {
1422 "ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU",
1425 if (idx >= ARRAY_SIZE(gnss_name))
1426 idx = ARRAY_SIZE(gnss_name) - 1;
1427 return gnss_name[idx];
1430 struct ptp_ocp_nvmem_match_info {
1432 const void * const tag;
1436 ptp_ocp_nvmem_match(struct device *dev, const void *data)
1438 const struct ptp_ocp_nvmem_match_info *info = data;
1441 if (!i2c_verify_client(dev) || info->tag != dev->platform_data)
1444 while ((dev = dev->parent))
1445 if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME))
1446 return info->bp == dev_get_drvdata(dev);
1450 static inline struct nvmem_device *
1451 ptp_ocp_nvmem_device_get(struct ptp_ocp *bp, const void * const tag)
1453 struct ptp_ocp_nvmem_match_info info = { .bp = bp, .tag = tag };
1455 return nvmem_device_find(&info, ptp_ocp_nvmem_match);
1459 ptp_ocp_nvmem_device_put(struct nvmem_device **nvmemp)
1461 if (!IS_ERR_OR_NULL(*nvmemp))
1462 nvmem_device_put(*nvmemp);
1467 ptp_ocp_read_eeprom(struct ptp_ocp *bp)
1469 const struct ptp_ocp_eeprom_map *map;
1470 struct nvmem_device *nvmem;
1480 for (map = bp->eeprom_map; map->len; map++) {
1481 if (map->tag != tag) {
1483 ptp_ocp_nvmem_device_put(&nvmem);
1486 nvmem = ptp_ocp_nvmem_device_get(bp, tag);
1487 if (IS_ERR(nvmem)) {
1488 ret = PTR_ERR(nvmem);
1492 ret = nvmem_device_read(nvmem, map->off, map->len,
1493 BP_MAP_ENTRY_ADDR(bp, map));
1494 if (ret != map->len)
1498 bp->has_eeprom_data = true;
1501 ptp_ocp_nvmem_device_put(&nvmem);
1505 dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", ret);
1509 static struct device *
1510 ptp_ocp_find_flash(struct ptp_ocp *bp)
1512 struct device *dev, *last;
1515 dev = &bp->spi_flash->dev;
1517 while ((dev = device_find_any_child(dev))) {
1518 if (!strcmp("mtd", dev_bus_name(dev)))
1529 ptp_ocp_devlink_fw_image(struct devlink *devlink, const struct firmware *fw,
1530 const u8 **data, size_t *size)
1532 struct ptp_ocp *bp = devlink_priv(devlink);
1533 const struct ptp_ocp_firmware_header *hdr;
1534 size_t offset, length;
1537 hdr = (const struct ptp_ocp_firmware_header *)fw->data;
1538 if (memcmp(hdr->magic, OCP_FIRMWARE_MAGIC_HEADER, 4)) {
1539 devlink_flash_update_status_notify(devlink,
1540 "No firmware header found, cancel firmware upgrade",
1545 if (be16_to_cpu(hdr->pci_vendor_id) != bp->pdev->vendor ||
1546 be16_to_cpu(hdr->pci_device_id) != bp->pdev->device) {
1547 devlink_flash_update_status_notify(devlink,
1548 "Firmware image compatibility check failed",
1553 offset = sizeof(*hdr);
1554 length = be32_to_cpu(hdr->image_size);
1555 if (length != (fw->size - offset)) {
1556 devlink_flash_update_status_notify(devlink,
1557 "Firmware image size check failed",
1562 crc = crc16(0xffff, &fw->data[offset], length);
1563 if (be16_to_cpu(hdr->crc) != crc) {
1564 devlink_flash_update_status_notify(devlink,
1565 "Firmware image CRC check failed",
1570 *data = &fw->data[offset];
1577 ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev,
1578 const struct firmware *fw)
1580 struct mtd_info *mtd = dev_get_drvdata(dev);
1581 struct ptp_ocp *bp = devlink_priv(devlink);
1582 size_t off, len, size, resid, wrote;
1583 struct erase_info erase;
1588 err = ptp_ocp_devlink_fw_image(devlink, fw, &data, &size);
1593 base = bp->flash_start;
1598 devlink_flash_update_status_notify(devlink, "Flashing",
1601 len = min_t(size_t, resid, blksz);
1602 erase.addr = base + off;
1605 err = mtd_erase(mtd, &erase);
1609 err = mtd_write(mtd, base + off, len, &wrote, data + off);
1621 ptp_ocp_devlink_flash_update(struct devlink *devlink,
1622 struct devlink_flash_update_params *params,
1623 struct netlink_ext_ack *extack)
1625 struct ptp_ocp *bp = devlink_priv(devlink);
1630 dev = ptp_ocp_find_flash(bp);
1632 dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n");
1636 devlink_flash_update_status_notify(devlink, "Preparing to flash",
1639 err = ptp_ocp_devlink_flash(devlink, dev, params->fw);
1641 msg = err ? "Flash error" : "Flash complete";
1642 devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0);
1649 ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req,
1650 struct netlink_ext_ack *extack)
1652 struct ptp_ocp *bp = devlink_priv(devlink);
1653 const char *fw_image;
1657 fw_image = bp->fw_loader ? "loader" : "fw";
1658 sprintf(buf, "%d.%d", bp->fw_tag, bp->fw_version);
1659 err = devlink_info_version_running_put(req, fw_image, buf);
1663 if (!bp->has_eeprom_data) {
1664 ptp_ocp_read_eeprom(bp);
1665 if (!bp->has_eeprom_data)
1669 sprintf(buf, "%pM", bp->serial);
1670 err = devlink_info_serial_number_put(req, buf);
1674 err = devlink_info_version_fixed_put(req,
1675 DEVLINK_INFO_VERSION_GENERIC_BOARD_ID,
1683 static const struct devlink_ops ptp_ocp_devlink_ops = {
1684 .flash_update = ptp_ocp_devlink_flash_update,
1685 .info_get = ptp_ocp_devlink_info_get,
1688 static void __iomem *
1689 __ptp_ocp_get_mem(struct ptp_ocp *bp, resource_size_t start, int size)
1691 struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp");
1693 return devm_ioremap_resource(&bp->pdev->dev, &res);
1696 static void __iomem *
1697 ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r)
1699 resource_size_t start;
1701 start = pci_resource_start(bp->pdev, 0) + r->offset;
1702 return __ptp_ocp_get_mem(bp, start, r->size);
1706 ptp_ocp_set_irq_resource(struct resource *res, int irq)
1708 struct resource r = DEFINE_RES_IRQ(irq);
1713 ptp_ocp_set_mem_resource(struct resource *res, resource_size_t start, int size)
1715 struct resource r = DEFINE_RES_MEM(start, size);
1720 ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r)
1722 struct ptp_ocp_flash_info *info;
1723 struct pci_dev *pdev = bp->pdev;
1724 struct platform_device *p;
1725 struct resource res[2];
1726 resource_size_t start;
1729 start = pci_resource_start(pdev, 0) + r->offset;
1730 ptp_ocp_set_mem_resource(&res[0], start, r->size);
1731 ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec));
1734 id = pci_dev_id(pdev) << 1;
1735 id += info->pci_offset;
1737 p = platform_device_register_resndata(&pdev->dev, info->name, id,
1743 bp_assign_entry(bp, r, p);
1748 static struct platform_device *
1749 ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id)
1751 struct ptp_ocp_i2c_info *info;
1752 struct resource res[2];
1753 resource_size_t start;
1756 start = pci_resource_start(pdev, 0) + r->offset;
1757 ptp_ocp_set_mem_resource(&res[0], start, r->size);
1758 ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec));
1760 return platform_device_register_resndata(&pdev->dev, info->name,
1762 info->data, info->data_size);
1766 ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r)
1768 struct pci_dev *pdev = bp->pdev;
1769 struct ptp_ocp_i2c_info *info;
1770 struct platform_device *p;
1776 id = pci_dev_id(bp->pdev);
1778 sprintf(buf, "AXI.%d", id);
1779 clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0,
1782 return PTR_ERR(clk);
1785 sprintf(buf, "%s.%d", info->name, id);
1786 devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf);
1787 p = ptp_ocp_i2c_bus(bp->pdev, r, id);
1791 bp_assign_entry(bp, r, p);
1796 /* The expectation is that this is triggered only on error. */
1798 ptp_ocp_signal_irq(int irq, void *priv)
1800 struct ptp_ocp_ext_src *ext = priv;
1801 struct signal_reg __iomem *reg = ext->mem;
1802 struct ptp_ocp *bp = ext->bp;
1806 gen = ext->info->index - 1;
1808 enable = ioread32(®->enable);
1809 status = ioread32(®->status);
1811 /* disable generator on error */
1812 if (status || !enable) {
1813 iowrite32(0, ®->intr_mask);
1814 iowrite32(0, ®->enable);
1815 bp->signal[gen].running = false;
1818 iowrite32(0, ®->intr); /* ack interrupt */
1824 ptp_ocp_signal_set(struct ptp_ocp *bp, int gen, struct ptp_ocp_signal *s)
1826 struct ptp_system_timestamp sts;
1827 struct timespec64 ts;
1835 s->pulse = ktime_divns(s->period * s->duty, 100);
1837 err = ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts);
1841 start_ns = ktime_set(ts.tv_sec, ts.tv_nsec) + NSEC_PER_MSEC;
1843 /* roundup() does not work on 32-bit systems */
1844 s->start = DIV64_U64_ROUND_UP(start_ns, s->period);
1845 s->start = ktime_add(s->start, s->phase);
1848 if (s->duty < 1 || s->duty > 99)
1851 if (s->pulse < 1 || s->pulse > s->period)
1854 if (s->start < start_ns)
1857 bp->signal[gen] = *s;
1863 ptp_ocp_signal_from_perout(struct ptp_ocp *bp, int gen,
1864 struct ptp_perout_request *req)
1866 struct ptp_ocp_signal s = { };
1868 s.polarity = bp->signal[gen].polarity;
1869 s.period = ktime_set(req->period.sec, req->period.nsec);
1873 if (req->flags & PTP_PEROUT_DUTY_CYCLE) {
1874 s.pulse = ktime_set(req->on.sec, req->on.nsec);
1875 s.duty = ktime_divns(s.pulse * 100, s.period);
1878 if (req->flags & PTP_PEROUT_PHASE)
1879 s.phase = ktime_set(req->phase.sec, req->phase.nsec);
1881 s.start = ktime_set(req->start.sec, req->start.nsec);
1883 return ptp_ocp_signal_set(bp, gen, &s);
1887 ptp_ocp_signal_enable(void *priv, u32 req, bool enable)
1889 struct ptp_ocp_ext_src *ext = priv;
1890 struct signal_reg __iomem *reg = ext->mem;
1891 struct ptp_ocp *bp = ext->bp;
1892 struct timespec64 ts;
1895 gen = ext->info->index - 1;
1897 iowrite32(0, ®->intr_mask);
1898 iowrite32(0, ®->enable);
1899 bp->signal[gen].running = false;
1903 ts = ktime_to_timespec64(bp->signal[gen].start);
1904 iowrite32(ts.tv_sec, ®->start_sec);
1905 iowrite32(ts.tv_nsec, ®->start_ns);
1907 ts = ktime_to_timespec64(bp->signal[gen].period);
1908 iowrite32(ts.tv_sec, ®->period_sec);
1909 iowrite32(ts.tv_nsec, ®->period_ns);
1911 ts = ktime_to_timespec64(bp->signal[gen].pulse);
1912 iowrite32(ts.tv_sec, ®->pulse_sec);
1913 iowrite32(ts.tv_nsec, ®->pulse_ns);
1915 iowrite32(bp->signal[gen].polarity, ®->polarity);
1916 iowrite32(0, ®->repeat_count);
1918 iowrite32(0, ®->intr); /* clear interrupt state */
1919 iowrite32(1, ®->intr_mask); /* enable interrupt */
1920 iowrite32(3, ®->enable); /* valid & enable */
1922 bp->signal[gen].running = true;
1928 ptp_ocp_ts_irq(int irq, void *priv)
1930 struct ptp_ocp_ext_src *ext = priv;
1931 struct ts_reg __iomem *reg = ext->mem;
1932 struct ptp_clock_event ev;
1935 if (ext == ext->bp->pps) {
1936 if (ext->bp->pps_req_map & OCP_REQ_PPS) {
1937 ev.type = PTP_CLOCK_PPS;
1938 ptp_clock_event(ext->bp->ptp, &ev);
1941 if ((ext->bp->pps_req_map & ~OCP_REQ_PPS) == 0)
1945 /* XXX should fix API - this converts s/ns -> ts -> s/ns */
1946 sec = ioread32(®->time_sec);
1947 nsec = ioread32(®->time_ns);
1949 ev.type = PTP_CLOCK_EXTTS;
1950 ev.index = ext->info->index;
1951 ev.timestamp = sec * NSEC_PER_SEC + nsec;
1953 ptp_clock_event(ext->bp->ptp, &ev);
1956 iowrite32(1, ®->intr); /* write 1 to ack */
1962 ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
1964 struct ptp_ocp_ext_src *ext = priv;
1965 struct ts_reg __iomem *reg = ext->mem;
1966 struct ptp_ocp *bp = ext->bp;
1968 if (ext == bp->pps) {
1969 u32 old_map = bp->pps_req_map;
1972 bp->pps_req_map |= req;
1974 bp->pps_req_map &= ~req;
1976 /* if no state change, just return */
1977 if ((!!old_map ^ !!bp->pps_req_map) == 0)
1982 iowrite32(1, ®->enable);
1983 iowrite32(1, ®->intr_mask);
1984 iowrite32(1, ®->intr);
1986 iowrite32(0, ®->intr_mask);
1987 iowrite32(0, ®->enable);
1994 ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext)
1996 ext->info->enable(ext, ~0, false);
1997 pci_free_irq(ext->bp->pdev, ext->irq_vec, ext);
2002 ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r)
2004 struct pci_dev *pdev = bp->pdev;
2005 struct ptp_ocp_ext_src *ext;
2008 ext = kzalloc(sizeof(*ext), GFP_KERNEL);
2012 ext->mem = ptp_ocp_get_mem(bp, r);
2013 if (IS_ERR(ext->mem)) {
2014 err = PTR_ERR(ext->mem);
2019 ext->info = r->extra;
2020 ext->irq_vec = r->irq_vec;
2022 err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL,
2023 ext, "ocp%d.%s", bp->id, r->name);
2025 dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec);
2029 bp_assign_entry(bp, r, ext);
2039 ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r)
2041 struct pci_dev *pdev = bp->pdev;
2042 struct uart_8250_port uart;
2044 /* Setting UPF_IOREMAP and leaving port.membase unspecified lets
2045 * the serial port device claim and release the pci resource.
2047 memset(&uart, 0, sizeof(uart));
2048 uart.port.dev = &pdev->dev;
2049 uart.port.iotype = UPIO_MEM;
2050 uart.port.regshift = 2;
2051 uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset;
2052 uart.port.irq = pci_irq_vector(pdev, r->irq_vec);
2053 uart.port.uartclk = 50000000;
2054 uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP | UPF_NO_THRE_TEST;
2055 uart.port.type = PORT_16550A;
2057 return serial8250_register_8250_port(&uart);
2061 ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r)
2063 struct ptp_ocp_serial_port *p = (struct ptp_ocp_serial_port *)r->extra;
2064 struct ptp_ocp_serial_port port = {};
2066 port.line = ptp_ocp_serial_line(bp, r);
2071 port.baud = p->baud;
2073 bp_assign_entry(bp, r, port);
2079 ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r)
2083 mem = ptp_ocp_get_mem(bp, r);
2085 return PTR_ERR(mem);
2087 bp_assign_entry(bp, r, mem);
2093 ptp_ocp_nmea_out_init(struct ptp_ocp *bp)
2098 iowrite32(0, &bp->nmea_out->ctrl); /* disable */
2099 iowrite32(7, &bp->nmea_out->uart_baud); /* 115200 */
2100 iowrite32(1, &bp->nmea_out->ctrl); /* enable */
2104 _ptp_ocp_signal_init(struct ptp_ocp_signal *s, struct signal_reg __iomem *reg)
2108 iowrite32(0, ®->enable); /* disable */
2110 val = ioread32(®->polarity);
2111 s->polarity = val ? true : false;
2116 ptp_ocp_signal_init(struct ptp_ocp *bp)
2120 for (i = 0; i < 4; i++)
2121 if (bp->signal_out[i])
2122 _ptp_ocp_signal_init(&bp->signal[i],
2123 bp->signal_out[i]->mem);
2127 ptp_ocp_attr_group_del(struct ptp_ocp *bp)
2129 sysfs_remove_groups(&bp->dev.kobj, bp->attr_group);
2130 kfree(bp->attr_group);
2134 ptp_ocp_attr_group_add(struct ptp_ocp *bp,
2135 const struct ocp_attr_group *attr_tbl)
2141 for (i = 0; attr_tbl[i].cap; i++)
2142 if (attr_tbl[i].cap & bp->fw_cap)
2145 bp->attr_group = kcalloc(count + 1, sizeof(struct attribute_group *),
2147 if (!bp->attr_group)
2151 for (i = 0; attr_tbl[i].cap; i++)
2152 if (attr_tbl[i].cap & bp->fw_cap)
2153 bp->attr_group[count++] = attr_tbl[i].group;
2155 err = sysfs_create_groups(&bp->dev.kobj, bp->attr_group);
2157 bp->attr_group[0] = NULL;
2163 ptp_ocp_enable_fpga(u32 __iomem *reg, u32 bit, bool enable)
2168 ctrl = ioread32(reg);
2172 ctrl |= enable ? bit : 0;
2173 iowrite32(ctrl, reg);
2178 ptp_ocp_irig_out(struct ptp_ocp *bp, bool enable)
2180 return ptp_ocp_enable_fpga(&bp->irig_out->ctrl,
2181 IRIG_M_CTRL_ENABLE, enable);
2185 ptp_ocp_irig_in(struct ptp_ocp *bp, bool enable)
2187 return ptp_ocp_enable_fpga(&bp->irig_in->ctrl,
2188 IRIG_S_CTRL_ENABLE, enable);
2192 ptp_ocp_dcf_out(struct ptp_ocp *bp, bool enable)
2194 return ptp_ocp_enable_fpga(&bp->dcf_out->ctrl,
2195 DCF_M_CTRL_ENABLE, enable);
2199 ptp_ocp_dcf_in(struct ptp_ocp *bp, bool enable)
2201 return ptp_ocp_enable_fpga(&bp->dcf_in->ctrl,
2202 DCF_S_CTRL_ENABLE, enable);
2206 __handle_signal_outputs(struct ptp_ocp *bp, u32 val)
2208 ptp_ocp_irig_out(bp, val & 0x00100010);
2209 ptp_ocp_dcf_out(bp, val & 0x00200020);
2213 __handle_signal_inputs(struct ptp_ocp *bp, u32 val)
2215 ptp_ocp_irig_in(bp, val & 0x00100010);
2216 ptp_ocp_dcf_in(bp, val & 0x00200020);
2220 ptp_ocp_sma_fb_get(struct ptp_ocp *bp, int sma_nr)
2225 if (bp->sma[sma_nr - 1].fixed_fcn)
2226 return (sma_nr - 1) & 1;
2228 if (bp->sma[sma_nr - 1].mode == SMA_MODE_IN)
2229 gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1;
2231 gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2;
2232 shift = sma_nr & 1 ? 0 : 16;
2234 return (ioread32(gpio) >> shift) & 0xffff;
2238 ptp_ocp_sma_fb_set_output(struct ptp_ocp *bp, int sma_nr, u32 val)
2240 u32 reg, mask, shift;
2241 unsigned long flags;
2244 gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2;
2245 shift = sma_nr & 1 ? 0 : 16;
2247 mask = 0xffff << (16 - shift);
2249 spin_lock_irqsave(&bp->lock, flags);
2251 reg = ioread32(gpio);
2252 reg = (reg & mask) | (val << shift);
2254 __handle_signal_outputs(bp, reg);
2256 iowrite32(reg, gpio);
2258 spin_unlock_irqrestore(&bp->lock, flags);
2264 ptp_ocp_sma_fb_set_inputs(struct ptp_ocp *bp, int sma_nr, u32 val)
2266 u32 reg, mask, shift;
2267 unsigned long flags;
2270 gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1;
2271 shift = sma_nr & 1 ? 0 : 16;
2273 mask = 0xffff << (16 - shift);
2275 spin_lock_irqsave(&bp->lock, flags);
2277 reg = ioread32(gpio);
2278 reg = (reg & mask) | (val << shift);
2280 __handle_signal_inputs(bp, reg);
2282 iowrite32(reg, gpio);
2284 spin_unlock_irqrestore(&bp->lock, flags);
2290 ptp_ocp_sma_fb_init(struct ptp_ocp *bp)
2296 bp->sma[0].mode = SMA_MODE_IN;
2297 bp->sma[1].mode = SMA_MODE_IN;
2298 bp->sma[2].mode = SMA_MODE_OUT;
2299 bp->sma[3].mode = SMA_MODE_OUT;
2300 for (i = 0; i < 4; i++)
2301 bp->sma[i].default_fcn = i & 1;
2303 /* If no SMA1 map, the pin functions and directions are fixed. */
2304 if (!bp->sma_map1) {
2305 for (i = 0; i < 4; i++) {
2306 bp->sma[i].fixed_fcn = true;
2307 bp->sma[i].fixed_dir = true;
2312 /* If SMA2 GPIO output map is all 1, it is not present.
2313 * This indicates the firmware has fixed direction SMA pins.
2315 reg = ioread32(&bp->sma_map2->gpio2);
2316 if (reg == 0xffffffff) {
2317 for (i = 0; i < 4; i++)
2318 bp->sma[i].fixed_dir = true;
2320 reg = ioread32(&bp->sma_map1->gpio1);
2321 bp->sma[0].mode = reg & BIT(15) ? SMA_MODE_IN : SMA_MODE_OUT;
2322 bp->sma[1].mode = reg & BIT(31) ? SMA_MODE_IN : SMA_MODE_OUT;
2324 reg = ioread32(&bp->sma_map1->gpio2);
2325 bp->sma[2].mode = reg & BIT(15) ? SMA_MODE_OUT : SMA_MODE_IN;
2326 bp->sma[3].mode = reg & BIT(31) ? SMA_MODE_OUT : SMA_MODE_IN;
2330 static const struct ocp_sma_op ocp_fb_sma_op = {
2331 .tbl = { ptp_ocp_sma_in, ptp_ocp_sma_out },
2332 .init = ptp_ocp_sma_fb_init,
2333 .get = ptp_ocp_sma_fb_get,
2334 .set_inputs = ptp_ocp_sma_fb_set_inputs,
2335 .set_output = ptp_ocp_sma_fb_set_output,
2339 ptp_ocp_fb_set_pins(struct ptp_ocp *bp)
2341 struct ptp_pin_desc *config;
2344 config = kcalloc(4, sizeof(*config), GFP_KERNEL);
2348 for (i = 0; i < 4; i++) {
2349 sprintf(config[i].name, "sma%d", i + 1);
2350 config[i].index = i;
2353 bp->ptp_info.n_pins = 4;
2354 bp->ptp_info.pin_config = config;
2360 ptp_ocp_fb_set_version(struct ptp_ocp *bp)
2362 u64 cap = OCP_CAP_BASIC;
2365 version = ioread32(&bp->image->version);
2367 /* if lower 16 bits are empty, this is the fw loader. */
2368 if ((version & 0xffff) == 0) {
2369 version = version >> 16;
2370 bp->fw_loader = true;
2373 bp->fw_tag = version >> 15;
2374 bp->fw_version = version & 0x7fff;
2379 cap |= OCP_CAP_SIGNAL | OCP_CAP_FREQ;
2383 cap |= OCP_CAP_SIGNAL;
2385 cap |= OCP_CAP_FREQ;
2391 /* FB specific board initializers; last "resource" registered. */
2393 ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r)
2397 bp->flash_start = 1024 * 4096;
2398 bp->eeprom_map = fb_eeprom_map;
2399 bp->fw_version = ioread32(&bp->image->version);
2400 bp->sma_op = &ocp_fb_sma_op;
2402 ptp_ocp_fb_set_version(bp);
2404 ptp_ocp_tod_init(bp);
2405 ptp_ocp_nmea_out_init(bp);
2406 ptp_ocp_sma_init(bp);
2407 ptp_ocp_signal_init(bp);
2409 err = ptp_ocp_attr_group_add(bp, fb_timecard_groups);
2413 err = ptp_ocp_fb_set_pins(bp);
2417 return ptp_ocp_init_clock(bp);
2421 ptp_ocp_allow_irq(struct ptp_ocp *bp, struct ocp_resource *r)
2423 bool allow = !r->irq_vec || r->irq_vec < bp->n_irqs;
2426 dev_err(&bp->pdev->dev, "irq %d out of range, skipping %s\n",
2427 r->irq_vec, r->name);
2432 ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data)
2434 struct ocp_resource *r, *table;
2437 table = (struct ocp_resource *)driver_data;
2438 for (r = table; r->setup; r++) {
2439 if (!ptp_ocp_allow_irq(bp, r))
2441 err = r->setup(bp, r);
2443 dev_err(&bp->pdev->dev,
2444 "Could not register %s: err %d\n",
2453 ptp_ocp_art_sma_init(struct ptp_ocp *bp)
2459 bp->sma[0].mode = SMA_MODE_IN;
2460 bp->sma[1].mode = SMA_MODE_IN;
2461 bp->sma[2].mode = SMA_MODE_OUT;
2462 bp->sma[3].mode = SMA_MODE_OUT;
2464 bp->sma[0].default_fcn = 0x08; /* IN: 10Mhz */
2465 bp->sma[1].default_fcn = 0x01; /* IN: PPS1 */
2466 bp->sma[2].default_fcn = 0x10; /* OUT: 10Mhz */
2467 bp->sma[3].default_fcn = 0x02; /* OUT: PHC */
2469 /* If no SMA map, the pin functions and directions are fixed. */
2471 for (i = 0; i < 4; i++) {
2472 bp->sma[i].fixed_fcn = true;
2473 bp->sma[i].fixed_dir = true;
2478 for (i = 0; i < 4; i++) {
2479 reg = ioread32(&bp->art_sma->map[i].gpio);
2481 switch (reg & 0xff) {
2483 bp->sma[i].fixed_fcn = true;
2484 bp->sma[i].fixed_dir = true;
2488 bp->sma[i].mode = SMA_MODE_IN;
2491 bp->sma[i].mode = SMA_MODE_OUT;
2498 ptp_ocp_art_sma_get(struct ptp_ocp *bp, int sma_nr)
2500 if (bp->sma[sma_nr - 1].fixed_fcn)
2501 return bp->sma[sma_nr - 1].default_fcn;
2503 return ioread32(&bp->art_sma->map[sma_nr - 1].gpio) & 0xff;
2506 /* note: store 0 is considered invalid. */
2508 ptp_ocp_art_sma_set(struct ptp_ocp *bp, int sma_nr, u32 val)
2510 unsigned long flags;
2515 val &= SMA_SELECT_MASK;
2516 if (hweight32(val) > 1)
2519 gpio = &bp->art_sma->map[sma_nr - 1].gpio;
2521 spin_lock_irqsave(&bp->lock, flags);
2522 reg = ioread32(gpio);
2523 if (((reg >> 16) & val) == 0) {
2526 reg = (reg & 0xff00) | (val & 0xff);
2527 iowrite32(reg, gpio);
2529 spin_unlock_irqrestore(&bp->lock, flags);
2534 static const struct ocp_sma_op ocp_art_sma_op = {
2535 .tbl = { ptp_ocp_art_sma_in, ptp_ocp_art_sma_out },
2536 .init = ptp_ocp_art_sma_init,
2537 .get = ptp_ocp_art_sma_get,
2538 .set_inputs = ptp_ocp_art_sma_set,
2539 .set_output = ptp_ocp_art_sma_set,
2542 /* ART specific board initializers; last "resource" registered. */
2544 ptp_ocp_art_board_init(struct ptp_ocp *bp, struct ocp_resource *r)
2548 bp->flash_start = 0x1000000;
2549 bp->eeprom_map = art_eeprom_map;
2550 bp->fw_cap = OCP_CAP_BASIC;
2551 bp->fw_version = ioread32(&bp->reg->version);
2553 bp->sma_op = &ocp_art_sma_op;
2555 /* Enable MAC serial port during initialisation */
2556 iowrite32(1, &bp->board_config->mro50_serial_activate);
2558 ptp_ocp_sma_init(bp);
2560 err = ptp_ocp_attr_group_add(bp, art_timecard_groups);
2564 return ptp_ocp_init_clock(bp);
2568 ptp_ocp_show_output(const struct ocp_selector *tbl, u32 val, char *buf,
2574 count = sysfs_emit(buf, "OUT: ");
2575 name = ptp_ocp_select_name_from_val(tbl, val);
2577 name = ptp_ocp_select_name_from_val(tbl, def_val);
2578 count += sysfs_emit_at(buf, count, "%s\n", name);
2583 ptp_ocp_show_inputs(const struct ocp_selector *tbl, u32 val, char *buf,
2590 count = sysfs_emit(buf, "IN: ");
2591 for (i = 0; tbl[i].name; i++) {
2592 if (val & tbl[i].value) {
2594 count += sysfs_emit_at(buf, count, "%s ", name);
2597 if (!val && def_val >= 0) {
2598 name = ptp_ocp_select_name_from_val(tbl, def_val);
2599 count += sysfs_emit_at(buf, count, "%s ", name);
2603 count += sysfs_emit_at(buf, count, "\n");
2608 sma_parse_inputs(const struct ocp_selector * const tbl[], const char *buf,
2609 enum ptp_ocp_sma_mode *mode)
2611 int idx, count, dir;
2615 argv = argv_split(GFP_KERNEL, buf, &count);
2624 dir = *mode == SMA_MODE_IN ? 0 : 1;
2625 if (!strcasecmp("IN:", argv[0])) {
2629 if (!strcasecmp("OUT:", argv[0])) {
2633 *mode = dir == 0 ? SMA_MODE_IN : SMA_MODE_OUT;
2636 for (; idx < count; idx++)
2637 ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]);
2647 ptp_ocp_sma_show(struct ptp_ocp *bp, int sma_nr, char *buf,
2648 int default_in_val, int default_out_val)
2650 struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1];
2651 const struct ocp_selector * const *tbl;
2654 tbl = bp->sma_op->tbl;
2655 val = ptp_ocp_sma_get(bp, sma_nr) & SMA_SELECT_MASK;
2657 if (sma->mode == SMA_MODE_IN) {
2660 return ptp_ocp_show_inputs(tbl[0], val, buf, default_in_val);
2663 return ptp_ocp_show_output(tbl[1], val, buf, default_out_val);
2667 sma1_show(struct device *dev, struct device_attribute *attr, char *buf)
2669 struct ptp_ocp *bp = dev_get_drvdata(dev);
2671 return ptp_ocp_sma_show(bp, 1, buf, 0, 1);
2675 sma2_show(struct device *dev, struct device_attribute *attr, char *buf)
2677 struct ptp_ocp *bp = dev_get_drvdata(dev);
2679 return ptp_ocp_sma_show(bp, 2, buf, -1, 1);
2683 sma3_show(struct device *dev, struct device_attribute *attr, char *buf)
2685 struct ptp_ocp *bp = dev_get_drvdata(dev);
2687 return ptp_ocp_sma_show(bp, 3, buf, -1, 0);
2691 sma4_show(struct device *dev, struct device_attribute *attr, char *buf)
2693 struct ptp_ocp *bp = dev_get_drvdata(dev);
2695 return ptp_ocp_sma_show(bp, 4, buf, -1, 1);
2699 ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr)
2701 struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1];
2702 enum ptp_ocp_sma_mode mode;
2706 val = sma_parse_inputs(bp->sma_op->tbl, buf, &mode);
2710 if (sma->fixed_dir && (mode != sma->mode || val & SMA_DISABLE))
2713 if (sma->fixed_fcn) {
2714 if (val != sma->default_fcn)
2719 sma->disabled = !!(val & SMA_DISABLE);
2721 if (mode != sma->mode) {
2722 if (mode == SMA_MODE_IN)
2723 ptp_ocp_sma_set_output(bp, sma_nr, 0);
2725 ptp_ocp_sma_set_inputs(bp, sma_nr, 0);
2729 if (!sma->fixed_dir)
2730 val |= SMA_ENABLE; /* add enable bit */
2735 if (mode == SMA_MODE_IN)
2736 val = ptp_ocp_sma_set_inputs(bp, sma_nr, val);
2738 val = ptp_ocp_sma_set_output(bp, sma_nr, val);
2744 sma1_store(struct device *dev, struct device_attribute *attr,
2745 const char *buf, size_t count)
2747 struct ptp_ocp *bp = dev_get_drvdata(dev);
2750 err = ptp_ocp_sma_store(bp, buf, 1);
2751 return err ? err : count;
2755 sma2_store(struct device *dev, struct device_attribute *attr,
2756 const char *buf, size_t count)
2758 struct ptp_ocp *bp = dev_get_drvdata(dev);
2761 err = ptp_ocp_sma_store(bp, buf, 2);
2762 return err ? err : count;
2766 sma3_store(struct device *dev, struct device_attribute *attr,
2767 const char *buf, size_t count)
2769 struct ptp_ocp *bp = dev_get_drvdata(dev);
2772 err = ptp_ocp_sma_store(bp, buf, 3);
2773 return err ? err : count;
2777 sma4_store(struct device *dev, struct device_attribute *attr,
2778 const char *buf, size_t count)
2780 struct ptp_ocp *bp = dev_get_drvdata(dev);
2783 err = ptp_ocp_sma_store(bp, buf, 4);
2784 return err ? err : count;
2786 static DEVICE_ATTR_RW(sma1);
2787 static DEVICE_ATTR_RW(sma2);
2788 static DEVICE_ATTR_RW(sma3);
2789 static DEVICE_ATTR_RW(sma4);
2792 available_sma_inputs_show(struct device *dev,
2793 struct device_attribute *attr, char *buf)
2795 struct ptp_ocp *bp = dev_get_drvdata(dev);
2797 return ptp_ocp_select_table_show(bp->sma_op->tbl[0], buf);
2799 static DEVICE_ATTR_RO(available_sma_inputs);
2802 available_sma_outputs_show(struct device *dev,
2803 struct device_attribute *attr, char *buf)
2805 struct ptp_ocp *bp = dev_get_drvdata(dev);
2807 return ptp_ocp_select_table_show(bp->sma_op->tbl[1], buf);
2809 static DEVICE_ATTR_RO(available_sma_outputs);
2811 #define EXT_ATTR_RO(_group, _name, _val) \
2812 struct dev_ext_attribute dev_attr_##_group##_val##_##_name = \
2813 { __ATTR_RO(_name), (void *)_val }
2814 #define EXT_ATTR_RW(_group, _name, _val) \
2815 struct dev_ext_attribute dev_attr_##_group##_val##_##_name = \
2816 { __ATTR_RW(_name), (void *)_val }
2817 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
2819 /* period [duty [phase [polarity]]] */
2821 signal_store(struct device *dev, struct device_attribute *attr,
2822 const char *buf, size_t count)
2824 struct dev_ext_attribute *ea = to_ext_attr(attr);
2825 struct ptp_ocp *bp = dev_get_drvdata(dev);
2826 struct ptp_ocp_signal s = { };
2827 int gen = (uintptr_t)ea->var;
2831 argv = argv_split(GFP_KERNEL, buf, &argc);
2836 s.duty = bp->signal[gen].duty;
2837 s.phase = bp->signal[gen].phase;
2838 s.period = bp->signal[gen].period;
2839 s.polarity = bp->signal[gen].polarity;
2844 err = kstrtobool(argv[argc], &s.polarity);
2850 err = kstrtou64(argv[argc], 0, &s.phase);
2856 err = kstrtoint(argv[argc], 0, &s.duty);
2862 err = kstrtou64(argv[argc], 0, &s.period);
2870 err = ptp_ocp_signal_set(bp, gen, &s);
2874 err = ptp_ocp_signal_enable(bp->signal_out[gen], gen, s.period != 0);
2878 return err ? err : count;
2882 signal_show(struct device *dev, struct device_attribute *attr, char *buf)
2884 struct dev_ext_attribute *ea = to_ext_attr(attr);
2885 struct ptp_ocp *bp = dev_get_drvdata(dev);
2886 struct ptp_ocp_signal *signal;
2887 struct timespec64 ts;
2891 i = (uintptr_t)ea->var;
2892 signal = &bp->signal[i];
2894 count = sysfs_emit(buf, "%llu %d %llu %d", signal->period,
2895 signal->duty, signal->phase, signal->polarity);
2897 ts = ktime_to_timespec64(signal->start);
2898 count += sysfs_emit_at(buf, count, " %ptT TAI\n", &ts);
2902 static EXT_ATTR_RW(signal, signal, 0);
2903 static EXT_ATTR_RW(signal, signal, 1);
2904 static EXT_ATTR_RW(signal, signal, 2);
2905 static EXT_ATTR_RW(signal, signal, 3);
2908 duty_show(struct device *dev, struct device_attribute *attr, char *buf)
2910 struct dev_ext_attribute *ea = to_ext_attr(attr);
2911 struct ptp_ocp *bp = dev_get_drvdata(dev);
2912 int i = (uintptr_t)ea->var;
2914 return sysfs_emit(buf, "%d\n", bp->signal[i].duty);
2916 static EXT_ATTR_RO(signal, duty, 0);
2917 static EXT_ATTR_RO(signal, duty, 1);
2918 static EXT_ATTR_RO(signal, duty, 2);
2919 static EXT_ATTR_RO(signal, duty, 3);
2922 period_show(struct device *dev, struct device_attribute *attr, char *buf)
2924 struct dev_ext_attribute *ea = to_ext_attr(attr);
2925 struct ptp_ocp *bp = dev_get_drvdata(dev);
2926 int i = (uintptr_t)ea->var;
2928 return sysfs_emit(buf, "%llu\n", bp->signal[i].period);
2930 static EXT_ATTR_RO(signal, period, 0);
2931 static EXT_ATTR_RO(signal, period, 1);
2932 static EXT_ATTR_RO(signal, period, 2);
2933 static EXT_ATTR_RO(signal, period, 3);
2936 phase_show(struct device *dev, struct device_attribute *attr, char *buf)
2938 struct dev_ext_attribute *ea = to_ext_attr(attr);
2939 struct ptp_ocp *bp = dev_get_drvdata(dev);
2940 int i = (uintptr_t)ea->var;
2942 return sysfs_emit(buf, "%llu\n", bp->signal[i].phase);
2944 static EXT_ATTR_RO(signal, phase, 0);
2945 static EXT_ATTR_RO(signal, phase, 1);
2946 static EXT_ATTR_RO(signal, phase, 2);
2947 static EXT_ATTR_RO(signal, phase, 3);
2950 polarity_show(struct device *dev, struct device_attribute *attr,
2953 struct dev_ext_attribute *ea = to_ext_attr(attr);
2954 struct ptp_ocp *bp = dev_get_drvdata(dev);
2955 int i = (uintptr_t)ea->var;
2957 return sysfs_emit(buf, "%d\n", bp->signal[i].polarity);
2959 static EXT_ATTR_RO(signal, polarity, 0);
2960 static EXT_ATTR_RO(signal, polarity, 1);
2961 static EXT_ATTR_RO(signal, polarity, 2);
2962 static EXT_ATTR_RO(signal, polarity, 3);
2965 running_show(struct device *dev, struct device_attribute *attr, char *buf)
2967 struct dev_ext_attribute *ea = to_ext_attr(attr);
2968 struct ptp_ocp *bp = dev_get_drvdata(dev);
2969 int i = (uintptr_t)ea->var;
2971 return sysfs_emit(buf, "%d\n", bp->signal[i].running);
2973 static EXT_ATTR_RO(signal, running, 0);
2974 static EXT_ATTR_RO(signal, running, 1);
2975 static EXT_ATTR_RO(signal, running, 2);
2976 static EXT_ATTR_RO(signal, running, 3);
2979 start_show(struct device *dev, struct device_attribute *attr, char *buf)
2981 struct dev_ext_attribute *ea = to_ext_attr(attr);
2982 struct ptp_ocp *bp = dev_get_drvdata(dev);
2983 int i = (uintptr_t)ea->var;
2984 struct timespec64 ts;
2986 ts = ktime_to_timespec64(bp->signal[i].start);
2987 return sysfs_emit(buf, "%llu.%lu\n", ts.tv_sec, ts.tv_nsec);
2989 static EXT_ATTR_RO(signal, start, 0);
2990 static EXT_ATTR_RO(signal, start, 1);
2991 static EXT_ATTR_RO(signal, start, 2);
2992 static EXT_ATTR_RO(signal, start, 3);
2995 seconds_store(struct device *dev, struct device_attribute *attr,
2996 const char *buf, size_t count)
2998 struct dev_ext_attribute *ea = to_ext_attr(attr);
2999 struct ptp_ocp *bp = dev_get_drvdata(dev);
3000 int idx = (uintptr_t)ea->var;
3004 err = kstrtou32(buf, 0, &val);
3011 val = (val << 8) | 0x1;
3013 iowrite32(val, &bp->freq_in[idx]->ctrl);
3019 seconds_show(struct device *dev, struct device_attribute *attr, char *buf)
3021 struct dev_ext_attribute *ea = to_ext_attr(attr);
3022 struct ptp_ocp *bp = dev_get_drvdata(dev);
3023 int idx = (uintptr_t)ea->var;
3026 val = ioread32(&bp->freq_in[idx]->ctrl);
3028 val = (val >> 8) & 0xff;
3032 return sysfs_emit(buf, "%u\n", val);
3034 static EXT_ATTR_RW(freq, seconds, 0);
3035 static EXT_ATTR_RW(freq, seconds, 1);
3036 static EXT_ATTR_RW(freq, seconds, 2);
3037 static EXT_ATTR_RW(freq, seconds, 3);
3040 frequency_show(struct device *dev, struct device_attribute *attr, char *buf)
3042 struct dev_ext_attribute *ea = to_ext_attr(attr);
3043 struct ptp_ocp *bp = dev_get_drvdata(dev);
3044 int idx = (uintptr_t)ea->var;
3047 val = ioread32(&bp->freq_in[idx]->status);
3048 if (val & FREQ_STATUS_ERROR)
3049 return sysfs_emit(buf, "error\n");
3050 if (val & FREQ_STATUS_OVERRUN)
3051 return sysfs_emit(buf, "overrun\n");
3052 if (val & FREQ_STATUS_VALID)
3053 return sysfs_emit(buf, "%lu\n", val & FREQ_STATUS_MASK);
3056 static EXT_ATTR_RO(freq, frequency, 0);
3057 static EXT_ATTR_RO(freq, frequency, 1);
3058 static EXT_ATTR_RO(freq, frequency, 2);
3059 static EXT_ATTR_RO(freq, frequency, 3);
3062 serialnum_show(struct device *dev, struct device_attribute *attr, char *buf)
3064 struct ptp_ocp *bp = dev_get_drvdata(dev);
3066 if (!bp->has_eeprom_data)
3067 ptp_ocp_read_eeprom(bp);
3069 return sysfs_emit(buf, "%pM\n", bp->serial);
3071 static DEVICE_ATTR_RO(serialnum);
3074 gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf)
3076 struct ptp_ocp *bp = dev_get_drvdata(dev);
3080 ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost);
3082 ret = sysfs_emit(buf, "SYNC\n");
3086 static DEVICE_ATTR_RO(gnss_sync);
3089 utc_tai_offset_show(struct device *dev,
3090 struct device_attribute *attr, char *buf)
3092 struct ptp_ocp *bp = dev_get_drvdata(dev);
3094 return sysfs_emit(buf, "%d\n", bp->utc_tai_offset);
3098 utc_tai_offset_store(struct device *dev,
3099 struct device_attribute *attr,
3100 const char *buf, size_t count)
3102 struct ptp_ocp *bp = dev_get_drvdata(dev);
3106 err = kstrtou32(buf, 0, &val);
3110 ptp_ocp_utc_distribute(bp, val);
3114 static DEVICE_ATTR_RW(utc_tai_offset);
3117 ts_window_adjust_show(struct device *dev,
3118 struct device_attribute *attr, char *buf)
3120 struct ptp_ocp *bp = dev_get_drvdata(dev);
3122 return sysfs_emit(buf, "%d\n", bp->ts_window_adjust);
3126 ts_window_adjust_store(struct device *dev,
3127 struct device_attribute *attr,
3128 const char *buf, size_t count)
3130 struct ptp_ocp *bp = dev_get_drvdata(dev);
3134 err = kstrtou32(buf, 0, &val);
3138 bp->ts_window_adjust = val;
3142 static DEVICE_ATTR_RW(ts_window_adjust);
3145 irig_b_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
3147 struct ptp_ocp *bp = dev_get_drvdata(dev);
3150 val = ioread32(&bp->irig_out->ctrl);
3151 val = (val >> 16) & 0x07;
3152 return sysfs_emit(buf, "%d\n", val);
3156 irig_b_mode_store(struct device *dev,
3157 struct device_attribute *attr,
3158 const char *buf, size_t count)
3160 struct ptp_ocp *bp = dev_get_drvdata(dev);
3161 unsigned long flags;
3166 err = kstrtou8(buf, 0, &val);
3172 reg = ((val & 0x7) << 16);
3174 spin_lock_irqsave(&bp->lock, flags);
3175 iowrite32(0, &bp->irig_out->ctrl); /* disable */
3176 iowrite32(reg, &bp->irig_out->ctrl); /* change mode */
3177 iowrite32(reg | IRIG_M_CTRL_ENABLE, &bp->irig_out->ctrl);
3178 spin_unlock_irqrestore(&bp->lock, flags);
3182 static DEVICE_ATTR_RW(irig_b_mode);
3185 clock_source_show(struct device *dev, struct device_attribute *attr, char *buf)
3187 struct ptp_ocp *bp = dev_get_drvdata(dev);
3191 select = ioread32(&bp->reg->select);
3192 p = ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16);
3194 return sysfs_emit(buf, "%s\n", p);
3198 clock_source_store(struct device *dev, struct device_attribute *attr,
3199 const char *buf, size_t count)
3201 struct ptp_ocp *bp = dev_get_drvdata(dev);
3202 unsigned long flags;
3205 val = ptp_ocp_select_val_from_name(ptp_ocp_clock, buf);
3209 spin_lock_irqsave(&bp->lock, flags);
3210 iowrite32(val, &bp->reg->select);
3211 spin_unlock_irqrestore(&bp->lock, flags);
3215 static DEVICE_ATTR_RW(clock_source);
3218 available_clock_sources_show(struct device *dev,
3219 struct device_attribute *attr, char *buf)
3221 return ptp_ocp_select_table_show(ptp_ocp_clock, buf);
3223 static DEVICE_ATTR_RO(available_clock_sources);
3226 clock_status_drift_show(struct device *dev,
3227 struct device_attribute *attr, char *buf)
3229 struct ptp_ocp *bp = dev_get_drvdata(dev);
3233 val = ioread32(&bp->reg->status_drift);
3234 res = (val & ~INT_MAX) ? -1 : 1;
3235 res *= (val & INT_MAX);
3236 return sysfs_emit(buf, "%d\n", res);
3238 static DEVICE_ATTR_RO(clock_status_drift);
3241 clock_status_offset_show(struct device *dev,
3242 struct device_attribute *attr, char *buf)
3244 struct ptp_ocp *bp = dev_get_drvdata(dev);
3248 val = ioread32(&bp->reg->status_offset);
3249 res = (val & ~INT_MAX) ? -1 : 1;
3250 res *= (val & INT_MAX);
3251 return sysfs_emit(buf, "%d\n", res);
3253 static DEVICE_ATTR_RO(clock_status_offset);
3256 tod_correction_show(struct device *dev,
3257 struct device_attribute *attr, char *buf)
3259 struct ptp_ocp *bp = dev_get_drvdata(dev);
3263 val = ioread32(&bp->tod->adj_sec);
3264 res = (val & ~INT_MAX) ? -1 : 1;
3265 res *= (val & INT_MAX);
3266 return sysfs_emit(buf, "%d\n", res);
3270 tod_correction_store(struct device *dev, struct device_attribute *attr,
3271 const char *buf, size_t count)
3273 struct ptp_ocp *bp = dev_get_drvdata(dev);
3274 unsigned long flags;
3278 err = kstrtos32(buf, 0, &res);
3287 spin_lock_irqsave(&bp->lock, flags);
3288 iowrite32(val, &bp->tod->adj_sec);
3289 spin_unlock_irqrestore(&bp->lock, flags);
3293 static DEVICE_ATTR_RW(tod_correction);
3295 #define _DEVICE_SIGNAL_GROUP_ATTRS(_nr) \
3296 static struct attribute *fb_timecard_signal##_nr##_attrs[] = { \
3297 &dev_attr_signal##_nr##_signal.attr.attr, \
3298 &dev_attr_signal##_nr##_duty.attr.attr, \
3299 &dev_attr_signal##_nr##_phase.attr.attr, \
3300 &dev_attr_signal##_nr##_period.attr.attr, \
3301 &dev_attr_signal##_nr##_polarity.attr.attr, \
3302 &dev_attr_signal##_nr##_running.attr.attr, \
3303 &dev_attr_signal##_nr##_start.attr.attr, \
3307 #define DEVICE_SIGNAL_GROUP(_name, _nr) \
3308 _DEVICE_SIGNAL_GROUP_ATTRS(_nr); \
3309 static const struct attribute_group \
3310 fb_timecard_signal##_nr##_group = { \
3312 .attrs = fb_timecard_signal##_nr##_attrs, \
3315 DEVICE_SIGNAL_GROUP(gen1, 0);
3316 DEVICE_SIGNAL_GROUP(gen2, 1);
3317 DEVICE_SIGNAL_GROUP(gen3, 2);
3318 DEVICE_SIGNAL_GROUP(gen4, 3);
3320 #define _DEVICE_FREQ_GROUP_ATTRS(_nr) \
3321 static struct attribute *fb_timecard_freq##_nr##_attrs[] = { \
3322 &dev_attr_freq##_nr##_seconds.attr.attr, \
3323 &dev_attr_freq##_nr##_frequency.attr.attr, \
3327 #define DEVICE_FREQ_GROUP(_name, _nr) \
3328 _DEVICE_FREQ_GROUP_ATTRS(_nr); \
3329 static const struct attribute_group \
3330 fb_timecard_freq##_nr##_group = { \
3332 .attrs = fb_timecard_freq##_nr##_attrs, \
3335 DEVICE_FREQ_GROUP(freq1, 0);
3336 DEVICE_FREQ_GROUP(freq2, 1);
3337 DEVICE_FREQ_GROUP(freq3, 2);
3338 DEVICE_FREQ_GROUP(freq4, 3);
3341 disciplining_config_read(struct file *filp, struct kobject *kobj,
3342 struct bin_attribute *bin_attr, char *buf,
3343 loff_t off, size_t count)
3345 struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj));
3346 size_t size = OCP_ART_CONFIG_SIZE;
3347 struct nvmem_device *nvmem;
3350 nvmem = ptp_ocp_nvmem_device_get(bp, NULL);
3352 return PTR_ERR(nvmem);
3359 if (off + count > size)
3362 // the configuration is in the very beginning of the EEPROM
3363 err = nvmem_device_read(nvmem, off, count, buf);
3370 ptp_ocp_nvmem_device_put(&nvmem);
3376 disciplining_config_write(struct file *filp, struct kobject *kobj,
3377 struct bin_attribute *bin_attr, char *buf,
3378 loff_t off, size_t count)
3380 struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj));
3381 struct nvmem_device *nvmem;
3384 /* Allow write of the whole area only */
3385 if (off || count != OCP_ART_CONFIG_SIZE)
3388 nvmem = ptp_ocp_nvmem_device_get(bp, NULL);
3390 return PTR_ERR(nvmem);
3392 err = nvmem_device_write(nvmem, 0x00, count, buf);
3396 ptp_ocp_nvmem_device_put(&nvmem);
3400 static BIN_ATTR_RW(disciplining_config, OCP_ART_CONFIG_SIZE);
3403 temperature_table_read(struct file *filp, struct kobject *kobj,
3404 struct bin_attribute *bin_attr, char *buf,
3405 loff_t off, size_t count)
3407 struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj));
3408 size_t size = OCP_ART_TEMP_TABLE_SIZE;
3409 struct nvmem_device *nvmem;
3412 nvmem = ptp_ocp_nvmem_device_get(bp, NULL);
3414 return PTR_ERR(nvmem);
3421 if (off + count > size)
3424 // the configuration is in the very beginning of the EEPROM
3425 err = nvmem_device_read(nvmem, 0x90 + off, count, buf);
3432 ptp_ocp_nvmem_device_put(&nvmem);
3438 temperature_table_write(struct file *filp, struct kobject *kobj,
3439 struct bin_attribute *bin_attr, char *buf,
3440 loff_t off, size_t count)
3442 struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj));
3443 struct nvmem_device *nvmem;
3446 /* Allow write of the whole area only */
3447 if (off || count != OCP_ART_TEMP_TABLE_SIZE)
3450 nvmem = ptp_ocp_nvmem_device_get(bp, NULL);
3452 return PTR_ERR(nvmem);
3454 err = nvmem_device_write(nvmem, 0x90, count, buf);
3458 ptp_ocp_nvmem_device_put(&nvmem);
3462 static BIN_ATTR_RW(temperature_table, OCP_ART_TEMP_TABLE_SIZE);
3464 static struct attribute *fb_timecard_attrs[] = {
3465 &dev_attr_serialnum.attr,
3466 &dev_attr_gnss_sync.attr,
3467 &dev_attr_clock_source.attr,
3468 &dev_attr_available_clock_sources.attr,
3469 &dev_attr_sma1.attr,
3470 &dev_attr_sma2.attr,
3471 &dev_attr_sma3.attr,
3472 &dev_attr_sma4.attr,
3473 &dev_attr_available_sma_inputs.attr,
3474 &dev_attr_available_sma_outputs.attr,
3475 &dev_attr_clock_status_drift.attr,
3476 &dev_attr_clock_status_offset.attr,
3477 &dev_attr_irig_b_mode.attr,
3478 &dev_attr_utc_tai_offset.attr,
3479 &dev_attr_ts_window_adjust.attr,
3480 &dev_attr_tod_correction.attr,
3484 static const struct attribute_group fb_timecard_group = {
3485 .attrs = fb_timecard_attrs,
3488 static const struct ocp_attr_group fb_timecard_groups[] = {
3489 { .cap = OCP_CAP_BASIC, .group = &fb_timecard_group },
3490 { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal0_group },
3491 { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal1_group },
3492 { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal2_group },
3493 { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal3_group },
3494 { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq0_group },
3495 { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq1_group },
3496 { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq2_group },
3497 { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq3_group },
3501 static struct attribute *art_timecard_attrs[] = {
3502 &dev_attr_serialnum.attr,
3503 &dev_attr_clock_source.attr,
3504 &dev_attr_available_clock_sources.attr,
3505 &dev_attr_utc_tai_offset.attr,
3506 &dev_attr_ts_window_adjust.attr,
3507 &dev_attr_sma1.attr,
3508 &dev_attr_sma2.attr,
3509 &dev_attr_sma3.attr,
3510 &dev_attr_sma4.attr,
3511 &dev_attr_available_sma_inputs.attr,
3512 &dev_attr_available_sma_outputs.attr,
3516 static struct bin_attribute *bin_art_timecard_attrs[] = {
3517 &bin_attr_disciplining_config,
3518 &bin_attr_temperature_table,
3522 static const struct attribute_group art_timecard_group = {
3523 .attrs = art_timecard_attrs,
3524 .bin_attrs = bin_art_timecard_attrs,
3527 static const struct ocp_attr_group art_timecard_groups[] = {
3528 { .cap = OCP_CAP_BASIC, .group = &art_timecard_group },
3533 gpio_input_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit,
3538 for (i = 0; i < 4; i++) {
3539 if (bp->sma[i].mode != SMA_MODE_IN)
3541 if (map[i][0] & (1 << bit)) {
3542 sprintf(buf, "sma%d", i + 1);
3552 gpio_output_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit)
3557 strcpy(ans, "----");
3558 for (i = 0; i < 4; i++) {
3559 if (bp->sma[i].mode != SMA_MODE_OUT)
3561 if (map[i][1] & (1 << bit))
3562 ans += sprintf(ans, "sma%d ", i + 1);
3567 _signal_summary_show(struct seq_file *s, struct ptp_ocp *bp, int nr)
3569 struct signal_reg __iomem *reg = bp->signal_out[nr]->mem;
3570 struct ptp_ocp_signal *signal = &bp->signal[nr];
3578 on = signal->running;
3579 sprintf(label, "GEN%d", nr + 1);
3580 seq_printf(s, "%7s: %s, period:%llu duty:%d%% phase:%llu pol:%d",
3581 label, on ? " ON" : "OFF",
3582 signal->period, signal->duty, signal->phase,
3585 val = ioread32(®->enable);
3586 seq_printf(s, " [%x", val);
3587 val = ioread32(®->status);
3588 seq_printf(s, " %x]", val);
3590 seq_printf(s, " start:%llu\n", signal->start);
3594 _frequency_summary_show(struct seq_file *s, int nr,
3595 struct frequency_reg __iomem *reg)
3604 sprintf(label, "FREQ%d", nr + 1);
3605 val = ioread32(®->ctrl);
3607 val = (val >> 8) & 0xff;
3608 seq_printf(s, "%7s: %s, sec:%u",
3613 val = ioread32(®->status);
3614 if (val & FREQ_STATUS_ERROR)
3615 seq_printf(s, ", error");
3616 if (val & FREQ_STATUS_OVERRUN)
3617 seq_printf(s, ", overrun");
3618 if (val & FREQ_STATUS_VALID)
3619 seq_printf(s, ", freq %lu Hz", val & FREQ_STATUS_MASK);
3620 seq_printf(s, " reg:%x\n", val);
3624 ptp_ocp_summary_show(struct seq_file *s, void *data)
3626 struct device *dev = s->private;
3627 struct ptp_system_timestamp sts;
3628 struct ts_reg __iomem *ts_reg;
3629 char *buf, *src, *mac_src;
3630 struct timespec64 ts;
3637 buf = (char *)__get_free_page(GFP_KERNEL);
3641 bp = dev_get_drvdata(dev);
3643 seq_printf(s, "%7s: /dev/ptp%d\n", "PTP", ptp_clock_index(bp->ptp));
3644 if (bp->gnss_port.line != -1)
3645 seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS1",
3646 bp->gnss_port.line);
3647 if (bp->gnss2_port.line != -1)
3648 seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS2",
3649 bp->gnss2_port.line);
3650 if (bp->mac_port.line != -1)
3651 seq_printf(s, "%7s: /dev/ttyS%d\n", "MAC", bp->mac_port.line);
3652 if (bp->nmea_port.line != -1)
3653 seq_printf(s, "%7s: /dev/ttyS%d\n", "NMEA", bp->nmea_port.line);
3655 memset(sma_val, 0xff, sizeof(sma_val));
3659 reg = ioread32(&bp->sma_map1->gpio1);
3660 sma_val[0][0] = reg & 0xffff;
3661 sma_val[1][0] = reg >> 16;
3663 reg = ioread32(&bp->sma_map1->gpio2);
3664 sma_val[2][1] = reg & 0xffff;
3665 sma_val[3][1] = reg >> 16;
3667 reg = ioread32(&bp->sma_map2->gpio1);
3668 sma_val[2][0] = reg & 0xffff;
3669 sma_val[3][0] = reg >> 16;
3671 reg = ioread32(&bp->sma_map2->gpio2);
3672 sma_val[0][1] = reg & 0xffff;
3673 sma_val[1][1] = reg >> 16;
3676 sma1_show(dev, NULL, buf);
3677 seq_printf(s, " sma1: %04x,%04x %s",
3678 sma_val[0][0], sma_val[0][1], buf);
3680 sma2_show(dev, NULL, buf);
3681 seq_printf(s, " sma2: %04x,%04x %s",
3682 sma_val[1][0], sma_val[1][1], buf);
3684 sma3_show(dev, NULL, buf);
3685 seq_printf(s, " sma3: %04x,%04x %s",
3686 sma_val[2][0], sma_val[2][1], buf);
3688 sma4_show(dev, NULL, buf);
3689 seq_printf(s, " sma4: %04x,%04x %s",
3690 sma_val[3][0], sma_val[3][1], buf);
3693 ts_reg = bp->ts0->mem;
3694 on = ioread32(&ts_reg->enable);
3696 seq_printf(s, "%7s: %s, src: %s\n", "TS0",
3697 on ? " ON" : "OFF", src);
3701 ts_reg = bp->ts1->mem;
3702 on = ioread32(&ts_reg->enable);
3703 gpio_input_map(buf, bp, sma_val, 2, NULL);
3704 seq_printf(s, "%7s: %s, src: %s\n", "TS1",
3705 on ? " ON" : "OFF", buf);
3709 ts_reg = bp->ts2->mem;
3710 on = ioread32(&ts_reg->enable);
3711 gpio_input_map(buf, bp, sma_val, 3, NULL);
3712 seq_printf(s, "%7s: %s, src: %s\n", "TS2",
3713 on ? " ON" : "OFF", buf);
3717 ts_reg = bp->ts3->mem;
3718 on = ioread32(&ts_reg->enable);
3719 gpio_input_map(buf, bp, sma_val, 6, NULL);
3720 seq_printf(s, "%7s: %s, src: %s\n", "TS3",
3721 on ? " ON" : "OFF", buf);
3725 ts_reg = bp->ts4->mem;
3726 on = ioread32(&ts_reg->enable);
3727 gpio_input_map(buf, bp, sma_val, 7, NULL);
3728 seq_printf(s, "%7s: %s, src: %s\n", "TS4",
3729 on ? " ON" : "OFF", buf);
3733 ts_reg = bp->pps->mem;
3735 on = ioread32(&ts_reg->enable);
3736 map = !!(bp->pps_req_map & OCP_REQ_TIMESTAMP);
3737 seq_printf(s, "%7s: %s, src: %s\n", "TS5",
3738 on && map ? " ON" : "OFF", src);
3740 map = !!(bp->pps_req_map & OCP_REQ_PPS);
3741 seq_printf(s, "%7s: %s, src: %s\n", "PPS",
3742 on && map ? " ON" : "OFF", src);
3745 if (bp->fw_cap & OCP_CAP_SIGNAL)
3746 for (i = 0; i < 4; i++)
3747 _signal_summary_show(s, bp, i);
3749 if (bp->fw_cap & OCP_CAP_FREQ)
3750 for (i = 0; i < 4; i++)
3751 _frequency_summary_show(s, i, bp->freq_in[i]);
3754 ctrl = ioread32(&bp->irig_out->ctrl);
3755 on = ctrl & IRIG_M_CTRL_ENABLE;
3756 val = ioread32(&bp->irig_out->status);
3757 gpio_output_map(buf, bp, sma_val, 4);
3758 seq_printf(s, "%7s: %s, error: %d, mode %d, out: %s\n", "IRIG",
3759 on ? " ON" : "OFF", val, (ctrl >> 16), buf);
3763 on = ioread32(&bp->irig_in->ctrl) & IRIG_S_CTRL_ENABLE;
3764 val = ioread32(&bp->irig_in->status);
3765 gpio_input_map(buf, bp, sma_val, 4, NULL);
3766 seq_printf(s, "%7s: %s, error: %d, src: %s\n", "IRIG in",
3767 on ? " ON" : "OFF", val, buf);
3771 on = ioread32(&bp->dcf_out->ctrl) & DCF_M_CTRL_ENABLE;
3772 val = ioread32(&bp->dcf_out->status);
3773 gpio_output_map(buf, bp, sma_val, 5);
3774 seq_printf(s, "%7s: %s, error: %d, out: %s\n", "DCF",
3775 on ? " ON" : "OFF", val, buf);
3779 on = ioread32(&bp->dcf_in->ctrl) & DCF_S_CTRL_ENABLE;
3780 val = ioread32(&bp->dcf_in->status);
3781 gpio_input_map(buf, bp, sma_val, 5, NULL);
3782 seq_printf(s, "%7s: %s, error: %d, src: %s\n", "DCF in",
3783 on ? " ON" : "OFF", val, buf);
3787 on = ioread32(&bp->nmea_out->ctrl) & 1;
3788 val = ioread32(&bp->nmea_out->status);
3789 seq_printf(s, "%7s: %s, error: %d\n", "NMEA",
3790 on ? " ON" : "OFF", val);
3793 /* compute src for PPS1, used below. */
3794 if (bp->pps_select) {
3795 val = ioread32(&bp->pps_select->gpio1);
3799 gpio_input_map(src, bp, sma_val, 0, NULL);
3801 } else if (val & 0x02) {
3803 } else if (val & 0x04) {
3813 seq_printf(s, "MAC PPS1 src: %s\n", mac_src);
3815 gpio_input_map(buf, bp, sma_val, 1, "GNSS2");
3816 seq_printf(s, "MAC PPS2 src: %s\n", buf);
3818 /* assumes automatic switchover/selection */
3819 val = ioread32(&bp->reg->select);
3820 switch (val >> 16) {
3822 sprintf(buf, "----");
3825 sprintf(buf, "IRIG");
3828 sprintf(buf, "%s via PPS1", src);
3831 sprintf(buf, "DCF");
3834 strcpy(buf, "unknown");
3837 val = ioread32(&bp->reg->status);
3838 seq_printf(s, "%7s: %s, state: %s\n", "PHC src", buf,
3839 val & OCP_STATUS_IN_SYNC ? "sync" : "unsynced");
3841 if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts)) {
3842 struct timespec64 sys_ts;
3843 s64 pre_ns, post_ns, ns;
3845 pre_ns = timespec64_to_ns(&sts.pre_ts);
3846 post_ns = timespec64_to_ns(&sts.post_ts);
3847 ns = (pre_ns + post_ns) / 2;
3848 ns += (s64)bp->utc_tai_offset * NSEC_PER_SEC;
3849 sys_ts = ns_to_timespec64(ns);
3851 seq_printf(s, "%7s: %lld.%ld == %ptT TAI\n", "PHC",
3852 ts.tv_sec, ts.tv_nsec, &ts);
3853 seq_printf(s, "%7s: %lld.%ld == %ptT UTC offset %d\n", "SYS",
3854 sys_ts.tv_sec, sys_ts.tv_nsec, &sys_ts,
3855 bp->utc_tai_offset);
3856 seq_printf(s, "%7s: PHC:SYS offset: %lld window: %lld\n", "",
3857 timespec64_to_ns(&ts) - ns,
3861 free_page((unsigned long)buf);
3864 DEFINE_SHOW_ATTRIBUTE(ptp_ocp_summary);
3867 ptp_ocp_tod_status_show(struct seq_file *s, void *data)
3869 struct device *dev = s->private;
3874 bp = dev_get_drvdata(dev);
3876 val = ioread32(&bp->tod->ctrl);
3877 if (!(val & TOD_CTRL_ENABLE)) {
3878 seq_printf(s, "TOD Slave disabled\n");
3881 seq_printf(s, "TOD Slave enabled, Control Register 0x%08X\n", val);
3883 idx = val & TOD_CTRL_PROTOCOL ? 4 : 0;
3884 idx += (val >> 16) & 3;
3885 seq_printf(s, "Protocol %s\n", ptp_ocp_tod_proto_name(idx));
3887 idx = (val >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK;
3888 seq_printf(s, "GNSS %s\n", ptp_ocp_tod_gnss_name(idx));
3890 val = ioread32(&bp->tod->version);
3891 seq_printf(s, "TOD Version %d.%d.%d\n",
3892 val >> 24, (val >> 16) & 0xff, val & 0xffff);
3894 val = ioread32(&bp->tod->status);
3895 seq_printf(s, "Status register: 0x%08X\n", val);
3897 val = ioread32(&bp->tod->adj_sec);
3898 idx = (val & ~INT_MAX) ? -1 : 1;
3899 idx *= (val & INT_MAX);
3900 seq_printf(s, "Correction seconds: %d\n", idx);
3902 val = ioread32(&bp->tod->utc_status);
3903 seq_printf(s, "UTC status register: 0x%08X\n", val);
3904 seq_printf(s, "UTC offset: %ld valid:%d\n",
3905 val & TOD_STATUS_UTC_MASK, val & TOD_STATUS_UTC_VALID ? 1 : 0);
3906 seq_printf(s, "Leap second info valid:%d, Leap second announce %d\n",
3907 val & TOD_STATUS_LEAP_VALID ? 1 : 0,
3908 val & TOD_STATUS_LEAP_ANNOUNCE ? 1 : 0);
3910 val = ioread32(&bp->tod->leap);
3911 seq_printf(s, "Time to next leap second (in sec): %d\n", (s32) val);
3915 DEFINE_SHOW_ATTRIBUTE(ptp_ocp_tod_status);
3917 static struct dentry *ptp_ocp_debugfs_root;
3920 ptp_ocp_debugfs_add_device(struct ptp_ocp *bp)
3924 d = debugfs_create_dir(dev_name(&bp->dev), ptp_ocp_debugfs_root);
3926 debugfs_create_file("summary", 0444, bp->debug_root,
3927 &bp->dev, &ptp_ocp_summary_fops);
3929 debugfs_create_file("tod_status", 0444, bp->debug_root,
3930 &bp->dev, &ptp_ocp_tod_status_fops);
3934 ptp_ocp_debugfs_remove_device(struct ptp_ocp *bp)
3936 debugfs_remove_recursive(bp->debug_root);
3940 ptp_ocp_debugfs_init(void)
3942 ptp_ocp_debugfs_root = debugfs_create_dir("timecard", NULL);
3946 ptp_ocp_debugfs_fini(void)
3948 debugfs_remove_recursive(ptp_ocp_debugfs_root);
3952 ptp_ocp_dev_release(struct device *dev)
3954 struct ptp_ocp *bp = dev_get_drvdata(dev);
3956 mutex_lock(&ptp_ocp_lock);
3957 idr_remove(&ptp_ocp_idr, bp->id);
3958 mutex_unlock(&ptp_ocp_lock);
3962 ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev)
3966 mutex_lock(&ptp_ocp_lock);
3967 err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL);
3968 mutex_unlock(&ptp_ocp_lock);
3970 dev_err(&pdev->dev, "idr_alloc failed: %d\n", err);
3975 bp->ptp_info = ptp_ocp_clock_info;
3976 spin_lock_init(&bp->lock);
3977 bp->gnss_port.line = -1;
3978 bp->gnss2_port.line = -1;
3979 bp->mac_port.line = -1;
3980 bp->nmea_port.line = -1;
3983 device_initialize(&bp->dev);
3984 dev_set_name(&bp->dev, "ocp%d", bp->id);
3985 bp->dev.class = &timecard_class;
3986 bp->dev.parent = &pdev->dev;
3987 bp->dev.release = ptp_ocp_dev_release;
3988 dev_set_drvdata(&bp->dev, bp);
3990 err = device_add(&bp->dev);
3992 dev_err(&bp->dev, "device add failed: %d\n", err);
3996 pci_set_drvdata(pdev, bp);
4001 ptp_ocp_dev_release(&bp->dev);
4002 put_device(&bp->dev);
4007 ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link)
4009 struct device *dev = &bp->dev;
4011 if (sysfs_create_link(&dev->kobj, &child->kobj, link))
4012 dev_err(dev, "%s symlink failed\n", link);
4016 ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link)
4018 struct device *dev, *child;
4020 dev = &bp->pdev->dev;
4022 child = device_find_child_by_name(dev, name);
4024 dev_err(dev, "Could not find device %s\n", name);
4028 ptp_ocp_symlink(bp, child, link);
4033 ptp_ocp_complete(struct ptp_ocp *bp)
4035 struct pps_device *pps;
4038 if (bp->gnss_port.line != -1) {
4039 sprintf(buf, "ttyS%d", bp->gnss_port.line);
4040 ptp_ocp_link_child(bp, buf, "ttyGNSS");
4042 if (bp->gnss2_port.line != -1) {
4043 sprintf(buf, "ttyS%d", bp->gnss2_port.line);
4044 ptp_ocp_link_child(bp, buf, "ttyGNSS2");
4046 if (bp->mac_port.line != -1) {
4047 sprintf(buf, "ttyS%d", bp->mac_port.line);
4048 ptp_ocp_link_child(bp, buf, "ttyMAC");
4050 if (bp->nmea_port.line != -1) {
4051 sprintf(buf, "ttyS%d", bp->nmea_port.line);
4052 ptp_ocp_link_child(bp, buf, "ttyNMEA");
4054 sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp));
4055 ptp_ocp_link_child(bp, buf, "ptp");
4057 pps = pps_lookup_dev(bp->ptp);
4059 ptp_ocp_symlink(bp, pps->dev, "pps");
4061 ptp_ocp_debugfs_add_device(bp);
4067 ptp_ocp_phc_info(struct ptp_ocp *bp)
4069 struct timespec64 ts;
4070 u32 version, select;
4073 version = ioread32(&bp->reg->version);
4074 select = ioread32(&bp->reg->select);
4075 dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n",
4076 version >> 24, (version >> 16) & 0xff, version & 0xffff,
4077 ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16),
4078 ptp_clock_index(bp->ptp));
4080 sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC;
4081 if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL))
4082 dev_info(&bp->pdev->dev, "Time: %lld.%ld, %s\n",
4083 ts.tv_sec, ts.tv_nsec,
4084 sync ? "in-sync" : "UNSYNCED");
4088 ptp_ocp_serial_info(struct device *dev, const char *name, int port, int baud)
4091 dev_info(dev, "%5s: /dev/ttyS%-2d @ %6d\n", name, port, baud);
4095 ptp_ocp_info(struct ptp_ocp *bp)
4097 static int nmea_baud[] = {
4098 1200, 2400, 4800, 9600, 19200, 38400,
4099 57600, 115200, 230400, 460800, 921600,
4102 struct device *dev = &bp->pdev->dev;
4105 ptp_ocp_phc_info(bp);
4107 ptp_ocp_serial_info(dev, "GNSS", bp->gnss_port.line,
4108 bp->gnss_port.baud);
4109 ptp_ocp_serial_info(dev, "GNSS2", bp->gnss2_port.line,
4110 bp->gnss2_port.baud);
4111 ptp_ocp_serial_info(dev, "MAC", bp->mac_port.line, bp->mac_port.baud);
4112 if (bp->nmea_out && bp->nmea_port.line != -1) {
4113 bp->nmea_port.baud = -1;
4115 reg = ioread32(&bp->nmea_out->uart_baud);
4116 if (reg < ARRAY_SIZE(nmea_baud))
4117 bp->nmea_port.baud = nmea_baud[reg];
4119 ptp_ocp_serial_info(dev, "NMEA", bp->nmea_port.line,
4120 bp->nmea_port.baud);
4125 ptp_ocp_detach_sysfs(struct ptp_ocp *bp)
4127 struct device *dev = &bp->dev;
4129 sysfs_remove_link(&dev->kobj, "ttyGNSS");
4130 sysfs_remove_link(&dev->kobj, "ttyGNSS2");
4131 sysfs_remove_link(&dev->kobj, "ttyMAC");
4132 sysfs_remove_link(&dev->kobj, "ptp");
4133 sysfs_remove_link(&dev->kobj, "pps");
4137 ptp_ocp_detach(struct ptp_ocp *bp)
4141 ptp_ocp_debugfs_remove_device(bp);
4142 ptp_ocp_detach_sysfs(bp);
4143 ptp_ocp_attr_group_del(bp);
4144 if (timer_pending(&bp->watchdog))
4145 del_timer_sync(&bp->watchdog);
4147 ptp_ocp_unregister_ext(bp->ts0);
4149 ptp_ocp_unregister_ext(bp->ts1);
4151 ptp_ocp_unregister_ext(bp->ts2);
4153 ptp_ocp_unregister_ext(bp->ts3);
4155 ptp_ocp_unregister_ext(bp->ts4);
4157 ptp_ocp_unregister_ext(bp->pps);
4158 for (i = 0; i < 4; i++)
4159 if (bp->signal_out[i])
4160 ptp_ocp_unregister_ext(bp->signal_out[i]);
4161 if (bp->gnss_port.line != -1)
4162 serial8250_unregister_port(bp->gnss_port.line);
4163 if (bp->gnss2_port.line != -1)
4164 serial8250_unregister_port(bp->gnss2_port.line);
4165 if (bp->mac_port.line != -1)
4166 serial8250_unregister_port(bp->mac_port.line);
4167 if (bp->nmea_port.line != -1)
4168 serial8250_unregister_port(bp->nmea_port.line);
4169 platform_device_unregister(bp->spi_flash);
4170 platform_device_unregister(bp->i2c_ctrl);
4172 clk_hw_unregister_fixed_rate(bp->i2c_clk);
4174 pci_free_irq_vectors(bp->pdev);
4176 ptp_clock_unregister(bp->ptp);
4177 kfree(bp->ptp_info.pin_config);
4178 device_unregister(&bp->dev);
4182 ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id)
4184 struct devlink *devlink;
4188 devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp), &pdev->dev);
4190 dev_err(&pdev->dev, "devlink_alloc failed\n");
4194 err = pci_enable_device(pdev);
4196 dev_err(&pdev->dev, "pci_enable_device\n");
4200 bp = devlink_priv(devlink);
4201 err = ptp_ocp_device_init(bp, pdev);
4206 * Older FPGA firmware only returns 2 irq's.
4207 * allow this - if not all of the IRQ's are returned, skip the
4208 * extra devices and just register the clock.
4210 err = pci_alloc_irq_vectors(pdev, 1, 17, PCI_IRQ_MSI | PCI_IRQ_MSIX);
4212 dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err);
4216 pci_set_master(pdev);
4218 err = ptp_ocp_register_resources(bp, id->driver_data);
4222 bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev);
4223 if (IS_ERR(bp->ptp)) {
4224 err = PTR_ERR(bp->ptp);
4225 dev_err(&pdev->dev, "ptp_clock_register: %d\n", err);
4230 err = ptp_ocp_complete(bp);
4235 devlink_register(devlink);
4241 pci_disable_device(pdev);
4243 devlink_free(devlink);
4248 ptp_ocp_remove(struct pci_dev *pdev)
4250 struct ptp_ocp *bp = pci_get_drvdata(pdev);
4251 struct devlink *devlink = priv_to_devlink(bp);
4253 devlink_unregister(devlink);
4255 pci_disable_device(pdev);
4257 devlink_free(devlink);
4260 static struct pci_driver ptp_ocp_driver = {
4261 .name = KBUILD_MODNAME,
4262 .id_table = ptp_ocp_pcidev_id,
4263 .probe = ptp_ocp_probe,
4264 .remove = ptp_ocp_remove,
4268 ptp_ocp_i2c_notifier_call(struct notifier_block *nb,
4269 unsigned long action, void *data)
4271 struct device *dev, *child = data;
4276 case BUS_NOTIFY_ADD_DEVICE:
4277 case BUS_NOTIFY_DEL_DEVICE:
4278 add = action == BUS_NOTIFY_ADD_DEVICE;
4284 if (!i2c_verify_adapter(child))
4288 while ((dev = dev->parent))
4289 if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME))
4294 bp = dev_get_drvdata(dev);
4296 ptp_ocp_symlink(bp, child, "i2c");
4298 sysfs_remove_link(&bp->dev.kobj, "i2c");
4303 static struct notifier_block ptp_ocp_i2c_notifier = {
4304 .notifier_call = ptp_ocp_i2c_notifier_call,
4313 ptp_ocp_debugfs_init();
4315 what = "timecard class";
4316 err = class_register(&timecard_class);
4320 what = "i2c notifier";
4321 err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
4325 what = "ptp_ocp driver";
4326 err = pci_register_driver(&ptp_ocp_driver);
4333 bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
4335 class_unregister(&timecard_class);
4337 ptp_ocp_debugfs_fini();
4338 pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err);
4345 bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
4346 pci_unregister_driver(&ptp_ocp_driver);
4347 class_unregister(&timecard_class);
4348 ptp_ocp_debugfs_fini();
4351 module_init(ptp_ocp_init);
4352 module_exit(ptp_ocp_fini);
4354 MODULE_DESCRIPTION("OpenCompute TimeCard driver");
4355 MODULE_LICENSE("GPL v2");