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_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns)
1134 ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq,
1137 struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
1138 struct ptp_ocp_ext_src *ext = NULL;
1143 case PTP_CLK_REQ_EXTTS:
1144 req = OCP_REQ_TIMESTAMP;
1145 switch (rq->extts.index) {
1166 case PTP_CLK_REQ_PPS:
1170 case PTP_CLK_REQ_PEROUT:
1171 switch (rq->perout.index) {
1173 /* This is a request for 1PPS on an output SMA.
1174 * Allow, but assume manual configuration.
1176 if (on && (rq->perout.period.sec != 1 ||
1177 rq->perout.period.nsec != 0))
1184 req = rq->perout.index - 1;
1185 ext = bp->signal_out[req];
1186 err = ptp_ocp_signal_from_perout(bp, req, &rq->perout);
1198 err = ext->info->enable(ext, req, on);
1204 ptp_ocp_verify(struct ptp_clock_info *ptp_info, unsigned pin,
1205 enum ptp_pin_function func, unsigned chan)
1207 struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
1212 snprintf(buf, sizeof(buf), "IN: None");
1215 /* Allow timestamps, but require sysfs configuration. */
1218 /* channel 0 is 1PPS from PHC.
1219 * channels 1..4 are the frequency generators.
1222 snprintf(buf, sizeof(buf), "OUT: GEN%d", chan);
1224 snprintf(buf, sizeof(buf), "OUT: PHC");
1230 return ptp_ocp_sma_store(bp, buf, pin + 1);
1233 static const struct ptp_clock_info ptp_ocp_clock_info = {
1234 .owner = THIS_MODULE,
1235 .name = KBUILD_MODNAME,
1236 .max_adj = 100000000,
1237 .gettimex64 = ptp_ocp_gettimex,
1238 .settime64 = ptp_ocp_settime,
1239 .adjtime = ptp_ocp_adjtime,
1240 .adjfine = ptp_ocp_null_adjfine,
1241 .adjphase = ptp_ocp_null_adjphase,
1242 .enable = ptp_ocp_enable,
1243 .verify = ptp_ocp_verify,
1250 __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp)
1254 select = ioread32(&bp->reg->select);
1255 iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
1257 iowrite32(0, &bp->reg->drift_ns);
1259 ctrl = OCP_CTRL_ADJUST_DRIFT | OCP_CTRL_ENABLE;
1260 iowrite32(ctrl, &bp->reg->ctrl);
1262 /* restore clock selection */
1263 iowrite32(select >> 16, &bp->reg->select);
1267 ptp_ocp_utc_distribute(struct ptp_ocp *bp, u32 val)
1269 unsigned long flags;
1271 spin_lock_irqsave(&bp->lock, flags);
1273 bp->utc_tai_offset = val;
1276 iowrite32(val, &bp->irig_out->adj_sec);
1278 iowrite32(val, &bp->dcf_out->adj_sec);
1280 iowrite32(val, &bp->nmea_out->adj_sec);
1282 spin_unlock_irqrestore(&bp->lock, flags);
1286 ptp_ocp_watchdog(struct timer_list *t)
1288 struct ptp_ocp *bp = from_timer(bp, t, watchdog);
1289 unsigned long flags;
1290 u32 status, utc_offset;
1292 status = ioread32(&bp->pps_to_clk->status);
1294 if (status & PPS_STATUS_SUPERV_ERR) {
1295 iowrite32(status, &bp->pps_to_clk->status);
1296 if (!bp->gnss_lost) {
1297 spin_lock_irqsave(&bp->lock, flags);
1298 __ptp_ocp_clear_drift_locked(bp);
1299 spin_unlock_irqrestore(&bp->lock, flags);
1300 bp->gnss_lost = ktime_get_real_seconds();
1303 } else if (bp->gnss_lost) {
1307 /* if GNSS provides correct data we can rely on
1308 * it to get leap second information
1311 status = ioread32(&bp->tod->utc_status);
1312 utc_offset = status & TOD_STATUS_UTC_MASK;
1313 if (status & TOD_STATUS_UTC_VALID &&
1314 utc_offset != bp->utc_tai_offset)
1315 ptp_ocp_utc_distribute(bp, utc_offset);
1318 mod_timer(&bp->watchdog, jiffies + HZ);
1322 ptp_ocp_estimate_pci_timing(struct ptp_ocp *bp)
1328 ctrl = ioread32(&bp->reg->ctrl);
1329 ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE;
1331 iowrite32(ctrl, &bp->reg->ctrl);
1333 start = ktime_get_ns();
1335 ctrl = ioread32(&bp->reg->ctrl);
1337 end = ktime_get_ns();
1339 delay = end - start;
1340 bp->ts_window_adjust = (delay >> 5) * 3;
1344 ptp_ocp_init_clock(struct ptp_ocp *bp)
1346 struct timespec64 ts;
1350 ctrl = OCP_CTRL_ENABLE;
1351 iowrite32(ctrl, &bp->reg->ctrl);
1353 /* NO DRIFT Correction */
1354 /* offset_p:i 1/8, offset_i: 1/16, drift_p: 0, drift_i: 0 */
1355 iowrite32(0x2000, &bp->reg->servo_offset_p);
1356 iowrite32(0x1000, &bp->reg->servo_offset_i);
1357 iowrite32(0, &bp->reg->servo_drift_p);
1358 iowrite32(0, &bp->reg->servo_drift_i);
1360 /* latch servo values */
1361 ctrl |= OCP_CTRL_ADJUST_SERVO;
1362 iowrite32(ctrl, &bp->reg->ctrl);
1364 if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) {
1365 dev_err(&bp->pdev->dev, "clock not enabled\n");
1369 ptp_ocp_estimate_pci_timing(bp);
1371 sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC;
1373 ktime_get_clocktai_ts64(&ts);
1374 ptp_ocp_settime(&bp->ptp_info, &ts);
1377 /* If there is a clock supervisor, then enable the watchdog */
1378 if (bp->pps_to_clk) {
1379 timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0);
1380 mod_timer(&bp->watchdog, jiffies + HZ);
1387 ptp_ocp_tod_init(struct ptp_ocp *bp)
1391 ctrl = ioread32(&bp->tod->ctrl);
1392 ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE;
1393 ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B);
1394 iowrite32(ctrl, &bp->tod->ctrl);
1396 reg = ioread32(&bp->tod->utc_status);
1397 if (reg & TOD_STATUS_UTC_VALID)
1398 ptp_ocp_utc_distribute(bp, reg & TOD_STATUS_UTC_MASK);
1402 ptp_ocp_tod_proto_name(const int idx)
1404 static const char * const proto_name[] = {
1405 "NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none",
1406 "UBX", "UBX_UTC", "UBX_LS", "UBX_none"
1408 return proto_name[idx];
1412 ptp_ocp_tod_gnss_name(int idx)
1414 static const char * const gnss_name[] = {
1415 "ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU",
1418 if (idx >= ARRAY_SIZE(gnss_name))
1419 idx = ARRAY_SIZE(gnss_name) - 1;
1420 return gnss_name[idx];
1423 struct ptp_ocp_nvmem_match_info {
1425 const void * const tag;
1429 ptp_ocp_nvmem_match(struct device *dev, const void *data)
1431 const struct ptp_ocp_nvmem_match_info *info = data;
1434 if (!i2c_verify_client(dev) || info->tag != dev->platform_data)
1437 while ((dev = dev->parent))
1438 if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME))
1439 return info->bp == dev_get_drvdata(dev);
1443 static inline struct nvmem_device *
1444 ptp_ocp_nvmem_device_get(struct ptp_ocp *bp, const void * const tag)
1446 struct ptp_ocp_nvmem_match_info info = { .bp = bp, .tag = tag };
1448 return nvmem_device_find(&info, ptp_ocp_nvmem_match);
1452 ptp_ocp_nvmem_device_put(struct nvmem_device **nvmemp)
1454 if (!IS_ERR_OR_NULL(*nvmemp))
1455 nvmem_device_put(*nvmemp);
1460 ptp_ocp_read_eeprom(struct ptp_ocp *bp)
1462 const struct ptp_ocp_eeprom_map *map;
1463 struct nvmem_device *nvmem;
1473 for (map = bp->eeprom_map; map->len; map++) {
1474 if (map->tag != tag) {
1476 ptp_ocp_nvmem_device_put(&nvmem);
1479 nvmem = ptp_ocp_nvmem_device_get(bp, tag);
1480 if (IS_ERR(nvmem)) {
1481 ret = PTR_ERR(nvmem);
1485 ret = nvmem_device_read(nvmem, map->off, map->len,
1486 BP_MAP_ENTRY_ADDR(bp, map));
1487 if (ret != map->len)
1491 bp->has_eeprom_data = true;
1494 ptp_ocp_nvmem_device_put(&nvmem);
1498 dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", ret);
1502 static struct device *
1503 ptp_ocp_find_flash(struct ptp_ocp *bp)
1505 struct device *dev, *last;
1508 dev = &bp->spi_flash->dev;
1510 while ((dev = device_find_any_child(dev))) {
1511 if (!strcmp("mtd", dev_bus_name(dev)))
1522 ptp_ocp_devlink_fw_image(struct devlink *devlink, const struct firmware *fw,
1523 const u8 **data, size_t *size)
1525 struct ptp_ocp *bp = devlink_priv(devlink);
1526 const struct ptp_ocp_firmware_header *hdr;
1527 size_t offset, length;
1530 hdr = (const struct ptp_ocp_firmware_header *)fw->data;
1531 if (memcmp(hdr->magic, OCP_FIRMWARE_MAGIC_HEADER, 4)) {
1532 devlink_flash_update_status_notify(devlink,
1533 "No firmware header found, cancel firmware upgrade",
1538 if (be16_to_cpu(hdr->pci_vendor_id) != bp->pdev->vendor ||
1539 be16_to_cpu(hdr->pci_device_id) != bp->pdev->device) {
1540 devlink_flash_update_status_notify(devlink,
1541 "Firmware image compatibility check failed",
1546 offset = sizeof(*hdr);
1547 length = be32_to_cpu(hdr->image_size);
1548 if (length != (fw->size - offset)) {
1549 devlink_flash_update_status_notify(devlink,
1550 "Firmware image size check failed",
1555 crc = crc16(0xffff, &fw->data[offset], length);
1556 if (be16_to_cpu(hdr->crc) != crc) {
1557 devlink_flash_update_status_notify(devlink,
1558 "Firmware image CRC check failed",
1563 *data = &fw->data[offset];
1570 ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev,
1571 const struct firmware *fw)
1573 struct mtd_info *mtd = dev_get_drvdata(dev);
1574 struct ptp_ocp *bp = devlink_priv(devlink);
1575 size_t off, len, size, resid, wrote;
1576 struct erase_info erase;
1581 err = ptp_ocp_devlink_fw_image(devlink, fw, &data, &size);
1586 base = bp->flash_start;
1591 devlink_flash_update_status_notify(devlink, "Flashing",
1594 len = min_t(size_t, resid, blksz);
1595 erase.addr = base + off;
1598 err = mtd_erase(mtd, &erase);
1602 err = mtd_write(mtd, base + off, len, &wrote, data + off);
1614 ptp_ocp_devlink_flash_update(struct devlink *devlink,
1615 struct devlink_flash_update_params *params,
1616 struct netlink_ext_ack *extack)
1618 struct ptp_ocp *bp = devlink_priv(devlink);
1623 dev = ptp_ocp_find_flash(bp);
1625 dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n");
1629 devlink_flash_update_status_notify(devlink, "Preparing to flash",
1632 err = ptp_ocp_devlink_flash(devlink, dev, params->fw);
1634 msg = err ? "Flash error" : "Flash complete";
1635 devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0);
1642 ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req,
1643 struct netlink_ext_ack *extack)
1645 struct ptp_ocp *bp = devlink_priv(devlink);
1646 const char *fw_image;
1650 fw_image = bp->fw_loader ? "loader" : "fw";
1651 sprintf(buf, "%d.%d", bp->fw_tag, bp->fw_version);
1652 err = devlink_info_version_running_put(req, fw_image, buf);
1656 if (!bp->has_eeprom_data) {
1657 ptp_ocp_read_eeprom(bp);
1658 if (!bp->has_eeprom_data)
1662 sprintf(buf, "%pM", bp->serial);
1663 err = devlink_info_serial_number_put(req, buf);
1667 err = devlink_info_version_fixed_put(req,
1668 DEVLINK_INFO_VERSION_GENERIC_BOARD_ID,
1676 static const struct devlink_ops ptp_ocp_devlink_ops = {
1677 .flash_update = ptp_ocp_devlink_flash_update,
1678 .info_get = ptp_ocp_devlink_info_get,
1681 static void __iomem *
1682 __ptp_ocp_get_mem(struct ptp_ocp *bp, resource_size_t start, int size)
1684 struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp");
1686 return devm_ioremap_resource(&bp->pdev->dev, &res);
1689 static void __iomem *
1690 ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r)
1692 resource_size_t start;
1694 start = pci_resource_start(bp->pdev, 0) + r->offset;
1695 return __ptp_ocp_get_mem(bp, start, r->size);
1699 ptp_ocp_set_irq_resource(struct resource *res, int irq)
1701 struct resource r = DEFINE_RES_IRQ(irq);
1706 ptp_ocp_set_mem_resource(struct resource *res, resource_size_t start, int size)
1708 struct resource r = DEFINE_RES_MEM(start, size);
1713 ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r)
1715 struct ptp_ocp_flash_info *info;
1716 struct pci_dev *pdev = bp->pdev;
1717 struct platform_device *p;
1718 struct resource res[2];
1719 resource_size_t start;
1722 start = pci_resource_start(pdev, 0) + r->offset;
1723 ptp_ocp_set_mem_resource(&res[0], start, r->size);
1724 ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec));
1727 id = pci_dev_id(pdev) << 1;
1728 id += info->pci_offset;
1730 p = platform_device_register_resndata(&pdev->dev, info->name, id,
1736 bp_assign_entry(bp, r, p);
1741 static struct platform_device *
1742 ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id)
1744 struct ptp_ocp_i2c_info *info;
1745 struct resource res[2];
1746 resource_size_t start;
1749 start = pci_resource_start(pdev, 0) + r->offset;
1750 ptp_ocp_set_mem_resource(&res[0], start, r->size);
1751 ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec));
1753 return platform_device_register_resndata(&pdev->dev, info->name,
1755 info->data, info->data_size);
1759 ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r)
1761 struct pci_dev *pdev = bp->pdev;
1762 struct ptp_ocp_i2c_info *info;
1763 struct platform_device *p;
1769 id = pci_dev_id(bp->pdev);
1771 sprintf(buf, "AXI.%d", id);
1772 clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0,
1775 return PTR_ERR(clk);
1778 sprintf(buf, "%s.%d", info->name, id);
1779 devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf);
1780 p = ptp_ocp_i2c_bus(bp->pdev, r, id);
1784 bp_assign_entry(bp, r, p);
1789 /* The expectation is that this is triggered only on error. */
1791 ptp_ocp_signal_irq(int irq, void *priv)
1793 struct ptp_ocp_ext_src *ext = priv;
1794 struct signal_reg __iomem *reg = ext->mem;
1795 struct ptp_ocp *bp = ext->bp;
1799 gen = ext->info->index - 1;
1801 enable = ioread32(®->enable);
1802 status = ioread32(®->status);
1804 /* disable generator on error */
1805 if (status || !enable) {
1806 iowrite32(0, ®->intr_mask);
1807 iowrite32(0, ®->enable);
1808 bp->signal[gen].running = false;
1811 iowrite32(0, ®->intr); /* ack interrupt */
1817 ptp_ocp_signal_set(struct ptp_ocp *bp, int gen, struct ptp_ocp_signal *s)
1819 struct ptp_system_timestamp sts;
1820 struct timespec64 ts;
1828 s->pulse = ktime_divns(s->period * s->duty, 100);
1830 err = ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts);
1834 start_ns = ktime_set(ts.tv_sec, ts.tv_nsec) + NSEC_PER_MSEC;
1836 /* roundup() does not work on 32-bit systems */
1837 s->start = DIV64_U64_ROUND_UP(start_ns, s->period);
1838 s->start = ktime_add(s->start, s->phase);
1841 if (s->duty < 1 || s->duty > 99)
1844 if (s->pulse < 1 || s->pulse > s->period)
1847 if (s->start < start_ns)
1850 bp->signal[gen] = *s;
1856 ptp_ocp_signal_from_perout(struct ptp_ocp *bp, int gen,
1857 struct ptp_perout_request *req)
1859 struct ptp_ocp_signal s = { };
1861 s.polarity = bp->signal[gen].polarity;
1862 s.period = ktime_set(req->period.sec, req->period.nsec);
1866 if (req->flags & PTP_PEROUT_DUTY_CYCLE) {
1867 s.pulse = ktime_set(req->on.sec, req->on.nsec);
1868 s.duty = ktime_divns(s.pulse * 100, s.period);
1871 if (req->flags & PTP_PEROUT_PHASE)
1872 s.phase = ktime_set(req->phase.sec, req->phase.nsec);
1874 s.start = ktime_set(req->start.sec, req->start.nsec);
1876 return ptp_ocp_signal_set(bp, gen, &s);
1880 ptp_ocp_signal_enable(void *priv, u32 req, bool enable)
1882 struct ptp_ocp_ext_src *ext = priv;
1883 struct signal_reg __iomem *reg = ext->mem;
1884 struct ptp_ocp *bp = ext->bp;
1885 struct timespec64 ts;
1888 gen = ext->info->index - 1;
1890 iowrite32(0, ®->intr_mask);
1891 iowrite32(0, ®->enable);
1892 bp->signal[gen].running = false;
1896 ts = ktime_to_timespec64(bp->signal[gen].start);
1897 iowrite32(ts.tv_sec, ®->start_sec);
1898 iowrite32(ts.tv_nsec, ®->start_ns);
1900 ts = ktime_to_timespec64(bp->signal[gen].period);
1901 iowrite32(ts.tv_sec, ®->period_sec);
1902 iowrite32(ts.tv_nsec, ®->period_ns);
1904 ts = ktime_to_timespec64(bp->signal[gen].pulse);
1905 iowrite32(ts.tv_sec, ®->pulse_sec);
1906 iowrite32(ts.tv_nsec, ®->pulse_ns);
1908 iowrite32(bp->signal[gen].polarity, ®->polarity);
1909 iowrite32(0, ®->repeat_count);
1911 iowrite32(0, ®->intr); /* clear interrupt state */
1912 iowrite32(1, ®->intr_mask); /* enable interrupt */
1913 iowrite32(3, ®->enable); /* valid & enable */
1915 bp->signal[gen].running = true;
1921 ptp_ocp_ts_irq(int irq, void *priv)
1923 struct ptp_ocp_ext_src *ext = priv;
1924 struct ts_reg __iomem *reg = ext->mem;
1925 struct ptp_clock_event ev;
1928 if (ext == ext->bp->pps) {
1929 if (ext->bp->pps_req_map & OCP_REQ_PPS) {
1930 ev.type = PTP_CLOCK_PPS;
1931 ptp_clock_event(ext->bp->ptp, &ev);
1934 if ((ext->bp->pps_req_map & ~OCP_REQ_PPS) == 0)
1938 /* XXX should fix API - this converts s/ns -> ts -> s/ns */
1939 sec = ioread32(®->time_sec);
1940 nsec = ioread32(®->time_ns);
1942 ev.type = PTP_CLOCK_EXTTS;
1943 ev.index = ext->info->index;
1944 ev.timestamp = sec * NSEC_PER_SEC + nsec;
1946 ptp_clock_event(ext->bp->ptp, &ev);
1949 iowrite32(1, ®->intr); /* write 1 to ack */
1955 ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
1957 struct ptp_ocp_ext_src *ext = priv;
1958 struct ts_reg __iomem *reg = ext->mem;
1959 struct ptp_ocp *bp = ext->bp;
1961 if (ext == bp->pps) {
1962 u32 old_map = bp->pps_req_map;
1965 bp->pps_req_map |= req;
1967 bp->pps_req_map &= ~req;
1969 /* if no state change, just return */
1970 if ((!!old_map ^ !!bp->pps_req_map) == 0)
1975 iowrite32(1, ®->enable);
1976 iowrite32(1, ®->intr_mask);
1977 iowrite32(1, ®->intr);
1979 iowrite32(0, ®->intr_mask);
1980 iowrite32(0, ®->enable);
1987 ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext)
1989 ext->info->enable(ext, ~0, false);
1990 pci_free_irq(ext->bp->pdev, ext->irq_vec, ext);
1995 ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r)
1997 struct pci_dev *pdev = bp->pdev;
1998 struct ptp_ocp_ext_src *ext;
2001 ext = kzalloc(sizeof(*ext), GFP_KERNEL);
2005 ext->mem = ptp_ocp_get_mem(bp, r);
2006 if (IS_ERR(ext->mem)) {
2007 err = PTR_ERR(ext->mem);
2012 ext->info = r->extra;
2013 ext->irq_vec = r->irq_vec;
2015 err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL,
2016 ext, "ocp%d.%s", bp->id, r->name);
2018 dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec);
2022 bp_assign_entry(bp, r, ext);
2032 ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r)
2034 struct pci_dev *pdev = bp->pdev;
2035 struct uart_8250_port uart;
2037 /* Setting UPF_IOREMAP and leaving port.membase unspecified lets
2038 * the serial port device claim and release the pci resource.
2040 memset(&uart, 0, sizeof(uart));
2041 uart.port.dev = &pdev->dev;
2042 uart.port.iotype = UPIO_MEM;
2043 uart.port.regshift = 2;
2044 uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset;
2045 uart.port.irq = pci_irq_vector(pdev, r->irq_vec);
2046 uart.port.uartclk = 50000000;
2047 uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP | UPF_NO_THRE_TEST;
2048 uart.port.type = PORT_16550A;
2050 return serial8250_register_8250_port(&uart);
2054 ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r)
2056 struct ptp_ocp_serial_port *p = (struct ptp_ocp_serial_port *)r->extra;
2057 struct ptp_ocp_serial_port port = {};
2059 port.line = ptp_ocp_serial_line(bp, r);
2064 port.baud = p->baud;
2066 bp_assign_entry(bp, r, port);
2072 ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r)
2076 mem = ptp_ocp_get_mem(bp, r);
2078 return PTR_ERR(mem);
2080 bp_assign_entry(bp, r, mem);
2086 ptp_ocp_nmea_out_init(struct ptp_ocp *bp)
2091 iowrite32(0, &bp->nmea_out->ctrl); /* disable */
2092 iowrite32(7, &bp->nmea_out->uart_baud); /* 115200 */
2093 iowrite32(1, &bp->nmea_out->ctrl); /* enable */
2097 _ptp_ocp_signal_init(struct ptp_ocp_signal *s, struct signal_reg __iomem *reg)
2101 iowrite32(0, ®->enable); /* disable */
2103 val = ioread32(®->polarity);
2104 s->polarity = val ? true : false;
2109 ptp_ocp_signal_init(struct ptp_ocp *bp)
2113 for (i = 0; i < 4; i++)
2114 if (bp->signal_out[i])
2115 _ptp_ocp_signal_init(&bp->signal[i],
2116 bp->signal_out[i]->mem);
2120 ptp_ocp_attr_group_del(struct ptp_ocp *bp)
2122 sysfs_remove_groups(&bp->dev.kobj, bp->attr_group);
2123 kfree(bp->attr_group);
2127 ptp_ocp_attr_group_add(struct ptp_ocp *bp,
2128 const struct ocp_attr_group *attr_tbl)
2134 for (i = 0; attr_tbl[i].cap; i++)
2135 if (attr_tbl[i].cap & bp->fw_cap)
2138 bp->attr_group = kcalloc(count + 1, sizeof(struct attribute_group *),
2140 if (!bp->attr_group)
2144 for (i = 0; attr_tbl[i].cap; i++)
2145 if (attr_tbl[i].cap & bp->fw_cap)
2146 bp->attr_group[count++] = attr_tbl[i].group;
2148 err = sysfs_create_groups(&bp->dev.kobj, bp->attr_group);
2150 bp->attr_group[0] = NULL;
2156 ptp_ocp_enable_fpga(u32 __iomem *reg, u32 bit, bool enable)
2161 ctrl = ioread32(reg);
2165 ctrl |= enable ? bit : 0;
2166 iowrite32(ctrl, reg);
2171 ptp_ocp_irig_out(struct ptp_ocp *bp, bool enable)
2173 return ptp_ocp_enable_fpga(&bp->irig_out->ctrl,
2174 IRIG_M_CTRL_ENABLE, enable);
2178 ptp_ocp_irig_in(struct ptp_ocp *bp, bool enable)
2180 return ptp_ocp_enable_fpga(&bp->irig_in->ctrl,
2181 IRIG_S_CTRL_ENABLE, enable);
2185 ptp_ocp_dcf_out(struct ptp_ocp *bp, bool enable)
2187 return ptp_ocp_enable_fpga(&bp->dcf_out->ctrl,
2188 DCF_M_CTRL_ENABLE, enable);
2192 ptp_ocp_dcf_in(struct ptp_ocp *bp, bool enable)
2194 return ptp_ocp_enable_fpga(&bp->dcf_in->ctrl,
2195 DCF_S_CTRL_ENABLE, enable);
2199 __handle_signal_outputs(struct ptp_ocp *bp, u32 val)
2201 ptp_ocp_irig_out(bp, val & 0x00100010);
2202 ptp_ocp_dcf_out(bp, val & 0x00200020);
2206 __handle_signal_inputs(struct ptp_ocp *bp, u32 val)
2208 ptp_ocp_irig_in(bp, val & 0x00100010);
2209 ptp_ocp_dcf_in(bp, val & 0x00200020);
2213 ptp_ocp_sma_fb_get(struct ptp_ocp *bp, int sma_nr)
2218 if (bp->sma[sma_nr - 1].fixed_fcn)
2219 return (sma_nr - 1) & 1;
2221 if (bp->sma[sma_nr - 1].mode == SMA_MODE_IN)
2222 gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1;
2224 gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2;
2225 shift = sma_nr & 1 ? 0 : 16;
2227 return (ioread32(gpio) >> shift) & 0xffff;
2231 ptp_ocp_sma_fb_set_output(struct ptp_ocp *bp, int sma_nr, u32 val)
2233 u32 reg, mask, shift;
2234 unsigned long flags;
2237 gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2;
2238 shift = sma_nr & 1 ? 0 : 16;
2240 mask = 0xffff << (16 - shift);
2242 spin_lock_irqsave(&bp->lock, flags);
2244 reg = ioread32(gpio);
2245 reg = (reg & mask) | (val << shift);
2247 __handle_signal_outputs(bp, reg);
2249 iowrite32(reg, gpio);
2251 spin_unlock_irqrestore(&bp->lock, flags);
2257 ptp_ocp_sma_fb_set_inputs(struct ptp_ocp *bp, int sma_nr, u32 val)
2259 u32 reg, mask, shift;
2260 unsigned long flags;
2263 gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1;
2264 shift = sma_nr & 1 ? 0 : 16;
2266 mask = 0xffff << (16 - shift);
2268 spin_lock_irqsave(&bp->lock, flags);
2270 reg = ioread32(gpio);
2271 reg = (reg & mask) | (val << shift);
2273 __handle_signal_inputs(bp, reg);
2275 iowrite32(reg, gpio);
2277 spin_unlock_irqrestore(&bp->lock, flags);
2283 ptp_ocp_sma_fb_init(struct ptp_ocp *bp)
2289 bp->sma[0].mode = SMA_MODE_IN;
2290 bp->sma[1].mode = SMA_MODE_IN;
2291 bp->sma[2].mode = SMA_MODE_OUT;
2292 bp->sma[3].mode = SMA_MODE_OUT;
2293 for (i = 0; i < 4; i++)
2294 bp->sma[i].default_fcn = i & 1;
2296 /* If no SMA1 map, the pin functions and directions are fixed. */
2297 if (!bp->sma_map1) {
2298 for (i = 0; i < 4; i++) {
2299 bp->sma[i].fixed_fcn = true;
2300 bp->sma[i].fixed_dir = true;
2305 /* If SMA2 GPIO output map is all 1, it is not present.
2306 * This indicates the firmware has fixed direction SMA pins.
2308 reg = ioread32(&bp->sma_map2->gpio2);
2309 if (reg == 0xffffffff) {
2310 for (i = 0; i < 4; i++)
2311 bp->sma[i].fixed_dir = true;
2313 reg = ioread32(&bp->sma_map1->gpio1);
2314 bp->sma[0].mode = reg & BIT(15) ? SMA_MODE_IN : SMA_MODE_OUT;
2315 bp->sma[1].mode = reg & BIT(31) ? SMA_MODE_IN : SMA_MODE_OUT;
2317 reg = ioread32(&bp->sma_map1->gpio2);
2318 bp->sma[2].mode = reg & BIT(15) ? SMA_MODE_OUT : SMA_MODE_IN;
2319 bp->sma[3].mode = reg & BIT(31) ? SMA_MODE_OUT : SMA_MODE_IN;
2323 static const struct ocp_sma_op ocp_fb_sma_op = {
2324 .tbl = { ptp_ocp_sma_in, ptp_ocp_sma_out },
2325 .init = ptp_ocp_sma_fb_init,
2326 .get = ptp_ocp_sma_fb_get,
2327 .set_inputs = ptp_ocp_sma_fb_set_inputs,
2328 .set_output = ptp_ocp_sma_fb_set_output,
2332 ptp_ocp_fb_set_pins(struct ptp_ocp *bp)
2334 struct ptp_pin_desc *config;
2337 config = kcalloc(4, sizeof(*config), GFP_KERNEL);
2341 for (i = 0; i < 4; i++) {
2342 sprintf(config[i].name, "sma%d", i + 1);
2343 config[i].index = i;
2346 bp->ptp_info.n_pins = 4;
2347 bp->ptp_info.pin_config = config;
2353 ptp_ocp_fb_set_version(struct ptp_ocp *bp)
2355 u64 cap = OCP_CAP_BASIC;
2358 version = ioread32(&bp->image->version);
2360 /* if lower 16 bits are empty, this is the fw loader. */
2361 if ((version & 0xffff) == 0) {
2362 version = version >> 16;
2363 bp->fw_loader = true;
2366 bp->fw_tag = version >> 15;
2367 bp->fw_version = version & 0x7fff;
2372 cap |= OCP_CAP_SIGNAL | OCP_CAP_FREQ;
2376 cap |= OCP_CAP_SIGNAL;
2378 cap |= OCP_CAP_FREQ;
2384 /* FB specific board initializers; last "resource" registered. */
2386 ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r)
2390 bp->flash_start = 1024 * 4096;
2391 bp->eeprom_map = fb_eeprom_map;
2392 bp->fw_version = ioread32(&bp->image->version);
2393 bp->sma_op = &ocp_fb_sma_op;
2395 ptp_ocp_fb_set_version(bp);
2397 ptp_ocp_tod_init(bp);
2398 ptp_ocp_nmea_out_init(bp);
2399 ptp_ocp_sma_init(bp);
2400 ptp_ocp_signal_init(bp);
2402 err = ptp_ocp_attr_group_add(bp, fb_timecard_groups);
2406 err = ptp_ocp_fb_set_pins(bp);
2410 return ptp_ocp_init_clock(bp);
2414 ptp_ocp_allow_irq(struct ptp_ocp *bp, struct ocp_resource *r)
2416 bool allow = !r->irq_vec || r->irq_vec < bp->n_irqs;
2419 dev_err(&bp->pdev->dev, "irq %d out of range, skipping %s\n",
2420 r->irq_vec, r->name);
2425 ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data)
2427 struct ocp_resource *r, *table;
2430 table = (struct ocp_resource *)driver_data;
2431 for (r = table; r->setup; r++) {
2432 if (!ptp_ocp_allow_irq(bp, r))
2434 err = r->setup(bp, r);
2436 dev_err(&bp->pdev->dev,
2437 "Could not register %s: err %d\n",
2446 ptp_ocp_art_sma_init(struct ptp_ocp *bp)
2452 bp->sma[0].mode = SMA_MODE_IN;
2453 bp->sma[1].mode = SMA_MODE_IN;
2454 bp->sma[2].mode = SMA_MODE_OUT;
2455 bp->sma[3].mode = SMA_MODE_OUT;
2457 bp->sma[0].default_fcn = 0x08; /* IN: 10Mhz */
2458 bp->sma[1].default_fcn = 0x01; /* IN: PPS1 */
2459 bp->sma[2].default_fcn = 0x10; /* OUT: 10Mhz */
2460 bp->sma[3].default_fcn = 0x02; /* OUT: PHC */
2462 /* If no SMA map, the pin functions and directions are fixed. */
2464 for (i = 0; i < 4; i++) {
2465 bp->sma[i].fixed_fcn = true;
2466 bp->sma[i].fixed_dir = true;
2471 for (i = 0; i < 4; i++) {
2472 reg = ioread32(&bp->art_sma->map[i].gpio);
2474 switch (reg & 0xff) {
2476 bp->sma[i].fixed_fcn = true;
2477 bp->sma[i].fixed_dir = true;
2481 bp->sma[i].mode = SMA_MODE_IN;
2484 bp->sma[i].mode = SMA_MODE_OUT;
2491 ptp_ocp_art_sma_get(struct ptp_ocp *bp, int sma_nr)
2493 if (bp->sma[sma_nr - 1].fixed_fcn)
2494 return bp->sma[sma_nr - 1].default_fcn;
2496 return ioread32(&bp->art_sma->map[sma_nr - 1].gpio) & 0xff;
2499 /* note: store 0 is considered invalid. */
2501 ptp_ocp_art_sma_set(struct ptp_ocp *bp, int sma_nr, u32 val)
2503 unsigned long flags;
2508 val &= SMA_SELECT_MASK;
2509 if (hweight32(val) > 1)
2512 gpio = &bp->art_sma->map[sma_nr - 1].gpio;
2514 spin_lock_irqsave(&bp->lock, flags);
2515 reg = ioread32(gpio);
2516 if (((reg >> 16) & val) == 0) {
2519 reg = (reg & 0xff00) | (val & 0xff);
2520 iowrite32(reg, gpio);
2522 spin_unlock_irqrestore(&bp->lock, flags);
2527 static const struct ocp_sma_op ocp_art_sma_op = {
2528 .tbl = { ptp_ocp_art_sma_in, ptp_ocp_art_sma_out },
2529 .init = ptp_ocp_art_sma_init,
2530 .get = ptp_ocp_art_sma_get,
2531 .set_inputs = ptp_ocp_art_sma_set,
2532 .set_output = ptp_ocp_art_sma_set,
2535 /* ART specific board initializers; last "resource" registered. */
2537 ptp_ocp_art_board_init(struct ptp_ocp *bp, struct ocp_resource *r)
2541 bp->flash_start = 0x1000000;
2542 bp->eeprom_map = art_eeprom_map;
2543 bp->fw_cap = OCP_CAP_BASIC;
2544 bp->fw_version = ioread32(&bp->reg->version);
2546 bp->sma_op = &ocp_art_sma_op;
2548 /* Enable MAC serial port during initialisation */
2549 iowrite32(1, &bp->board_config->mro50_serial_activate);
2551 ptp_ocp_sma_init(bp);
2553 err = ptp_ocp_attr_group_add(bp, art_timecard_groups);
2557 return ptp_ocp_init_clock(bp);
2561 ptp_ocp_show_output(const struct ocp_selector *tbl, u32 val, char *buf,
2567 count = sysfs_emit(buf, "OUT: ");
2568 name = ptp_ocp_select_name_from_val(tbl, val);
2570 name = ptp_ocp_select_name_from_val(tbl, def_val);
2571 count += sysfs_emit_at(buf, count, "%s\n", name);
2576 ptp_ocp_show_inputs(const struct ocp_selector *tbl, u32 val, char *buf,
2583 count = sysfs_emit(buf, "IN: ");
2584 for (i = 0; tbl[i].name; i++) {
2585 if (val & tbl[i].value) {
2587 count += sysfs_emit_at(buf, count, "%s ", name);
2590 if (!val && def_val >= 0) {
2591 name = ptp_ocp_select_name_from_val(tbl, def_val);
2592 count += sysfs_emit_at(buf, count, "%s ", name);
2596 count += sysfs_emit_at(buf, count, "\n");
2601 sma_parse_inputs(const struct ocp_selector * const tbl[], const char *buf,
2602 enum ptp_ocp_sma_mode *mode)
2604 int idx, count, dir;
2608 argv = argv_split(GFP_KERNEL, buf, &count);
2617 dir = *mode == SMA_MODE_IN ? 0 : 1;
2618 if (!strcasecmp("IN:", argv[0])) {
2622 if (!strcasecmp("OUT:", argv[0])) {
2626 *mode = dir == 0 ? SMA_MODE_IN : SMA_MODE_OUT;
2629 for (; idx < count; idx++)
2630 ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]);
2640 ptp_ocp_sma_show(struct ptp_ocp *bp, int sma_nr, char *buf,
2641 int default_in_val, int default_out_val)
2643 struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1];
2644 const struct ocp_selector * const *tbl;
2647 tbl = bp->sma_op->tbl;
2648 val = ptp_ocp_sma_get(bp, sma_nr) & SMA_SELECT_MASK;
2650 if (sma->mode == SMA_MODE_IN) {
2653 return ptp_ocp_show_inputs(tbl[0], val, buf, default_in_val);
2656 return ptp_ocp_show_output(tbl[1], val, buf, default_out_val);
2660 sma1_show(struct device *dev, struct device_attribute *attr, char *buf)
2662 struct ptp_ocp *bp = dev_get_drvdata(dev);
2664 return ptp_ocp_sma_show(bp, 1, buf, 0, 1);
2668 sma2_show(struct device *dev, struct device_attribute *attr, char *buf)
2670 struct ptp_ocp *bp = dev_get_drvdata(dev);
2672 return ptp_ocp_sma_show(bp, 2, buf, -1, 1);
2676 sma3_show(struct device *dev, struct device_attribute *attr, char *buf)
2678 struct ptp_ocp *bp = dev_get_drvdata(dev);
2680 return ptp_ocp_sma_show(bp, 3, buf, -1, 0);
2684 sma4_show(struct device *dev, struct device_attribute *attr, char *buf)
2686 struct ptp_ocp *bp = dev_get_drvdata(dev);
2688 return ptp_ocp_sma_show(bp, 4, buf, -1, 1);
2692 ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr)
2694 struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1];
2695 enum ptp_ocp_sma_mode mode;
2699 val = sma_parse_inputs(bp->sma_op->tbl, buf, &mode);
2703 if (sma->fixed_dir && (mode != sma->mode || val & SMA_DISABLE))
2706 if (sma->fixed_fcn) {
2707 if (val != sma->default_fcn)
2712 sma->disabled = !!(val & SMA_DISABLE);
2714 if (mode != sma->mode) {
2715 if (mode == SMA_MODE_IN)
2716 ptp_ocp_sma_set_output(bp, sma_nr, 0);
2718 ptp_ocp_sma_set_inputs(bp, sma_nr, 0);
2722 if (!sma->fixed_dir)
2723 val |= SMA_ENABLE; /* add enable bit */
2728 if (mode == SMA_MODE_IN)
2729 val = ptp_ocp_sma_set_inputs(bp, sma_nr, val);
2731 val = ptp_ocp_sma_set_output(bp, sma_nr, val);
2737 sma1_store(struct device *dev, struct device_attribute *attr,
2738 const char *buf, size_t count)
2740 struct ptp_ocp *bp = dev_get_drvdata(dev);
2743 err = ptp_ocp_sma_store(bp, buf, 1);
2744 return err ? err : count;
2748 sma2_store(struct device *dev, struct device_attribute *attr,
2749 const char *buf, size_t count)
2751 struct ptp_ocp *bp = dev_get_drvdata(dev);
2754 err = ptp_ocp_sma_store(bp, buf, 2);
2755 return err ? err : count;
2759 sma3_store(struct device *dev, struct device_attribute *attr,
2760 const char *buf, size_t count)
2762 struct ptp_ocp *bp = dev_get_drvdata(dev);
2765 err = ptp_ocp_sma_store(bp, buf, 3);
2766 return err ? err : count;
2770 sma4_store(struct device *dev, struct device_attribute *attr,
2771 const char *buf, size_t count)
2773 struct ptp_ocp *bp = dev_get_drvdata(dev);
2776 err = ptp_ocp_sma_store(bp, buf, 4);
2777 return err ? err : count;
2779 static DEVICE_ATTR_RW(sma1);
2780 static DEVICE_ATTR_RW(sma2);
2781 static DEVICE_ATTR_RW(sma3);
2782 static DEVICE_ATTR_RW(sma4);
2785 available_sma_inputs_show(struct device *dev,
2786 struct device_attribute *attr, char *buf)
2788 struct ptp_ocp *bp = dev_get_drvdata(dev);
2790 return ptp_ocp_select_table_show(bp->sma_op->tbl[0], buf);
2792 static DEVICE_ATTR_RO(available_sma_inputs);
2795 available_sma_outputs_show(struct device *dev,
2796 struct device_attribute *attr, char *buf)
2798 struct ptp_ocp *bp = dev_get_drvdata(dev);
2800 return ptp_ocp_select_table_show(bp->sma_op->tbl[1], buf);
2802 static DEVICE_ATTR_RO(available_sma_outputs);
2804 #define EXT_ATTR_RO(_group, _name, _val) \
2805 struct dev_ext_attribute dev_attr_##_group##_val##_##_name = \
2806 { __ATTR_RO(_name), (void *)_val }
2807 #define EXT_ATTR_RW(_group, _name, _val) \
2808 struct dev_ext_attribute dev_attr_##_group##_val##_##_name = \
2809 { __ATTR_RW(_name), (void *)_val }
2810 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
2812 /* period [duty [phase [polarity]]] */
2814 signal_store(struct device *dev, struct device_attribute *attr,
2815 const char *buf, size_t count)
2817 struct dev_ext_attribute *ea = to_ext_attr(attr);
2818 struct ptp_ocp *bp = dev_get_drvdata(dev);
2819 struct ptp_ocp_signal s = { };
2820 int gen = (uintptr_t)ea->var;
2824 argv = argv_split(GFP_KERNEL, buf, &argc);
2829 s.duty = bp->signal[gen].duty;
2830 s.phase = bp->signal[gen].phase;
2831 s.period = bp->signal[gen].period;
2832 s.polarity = bp->signal[gen].polarity;
2837 err = kstrtobool(argv[argc], &s.polarity);
2843 err = kstrtou64(argv[argc], 0, &s.phase);
2849 err = kstrtoint(argv[argc], 0, &s.duty);
2855 err = kstrtou64(argv[argc], 0, &s.period);
2863 err = ptp_ocp_signal_set(bp, gen, &s);
2867 err = ptp_ocp_signal_enable(bp->signal_out[gen], gen, s.period != 0);
2871 return err ? err : count;
2875 signal_show(struct device *dev, struct device_attribute *attr, char *buf)
2877 struct dev_ext_attribute *ea = to_ext_attr(attr);
2878 struct ptp_ocp *bp = dev_get_drvdata(dev);
2879 struct ptp_ocp_signal *signal;
2880 struct timespec64 ts;
2884 i = (uintptr_t)ea->var;
2885 signal = &bp->signal[i];
2887 count = sysfs_emit(buf, "%llu %d %llu %d", signal->period,
2888 signal->duty, signal->phase, signal->polarity);
2890 ts = ktime_to_timespec64(signal->start);
2891 count += sysfs_emit_at(buf, count, " %ptT TAI\n", &ts);
2895 static EXT_ATTR_RW(signal, signal, 0);
2896 static EXT_ATTR_RW(signal, signal, 1);
2897 static EXT_ATTR_RW(signal, signal, 2);
2898 static EXT_ATTR_RW(signal, signal, 3);
2901 duty_show(struct device *dev, struct device_attribute *attr, char *buf)
2903 struct dev_ext_attribute *ea = to_ext_attr(attr);
2904 struct ptp_ocp *bp = dev_get_drvdata(dev);
2905 int i = (uintptr_t)ea->var;
2907 return sysfs_emit(buf, "%d\n", bp->signal[i].duty);
2909 static EXT_ATTR_RO(signal, duty, 0);
2910 static EXT_ATTR_RO(signal, duty, 1);
2911 static EXT_ATTR_RO(signal, duty, 2);
2912 static EXT_ATTR_RO(signal, duty, 3);
2915 period_show(struct device *dev, struct device_attribute *attr, char *buf)
2917 struct dev_ext_attribute *ea = to_ext_attr(attr);
2918 struct ptp_ocp *bp = dev_get_drvdata(dev);
2919 int i = (uintptr_t)ea->var;
2921 return sysfs_emit(buf, "%llu\n", bp->signal[i].period);
2923 static EXT_ATTR_RO(signal, period, 0);
2924 static EXT_ATTR_RO(signal, period, 1);
2925 static EXT_ATTR_RO(signal, period, 2);
2926 static EXT_ATTR_RO(signal, period, 3);
2929 phase_show(struct device *dev, struct device_attribute *attr, char *buf)
2931 struct dev_ext_attribute *ea = to_ext_attr(attr);
2932 struct ptp_ocp *bp = dev_get_drvdata(dev);
2933 int i = (uintptr_t)ea->var;
2935 return sysfs_emit(buf, "%llu\n", bp->signal[i].phase);
2937 static EXT_ATTR_RO(signal, phase, 0);
2938 static EXT_ATTR_RO(signal, phase, 1);
2939 static EXT_ATTR_RO(signal, phase, 2);
2940 static EXT_ATTR_RO(signal, phase, 3);
2943 polarity_show(struct device *dev, struct device_attribute *attr,
2946 struct dev_ext_attribute *ea = to_ext_attr(attr);
2947 struct ptp_ocp *bp = dev_get_drvdata(dev);
2948 int i = (uintptr_t)ea->var;
2950 return sysfs_emit(buf, "%d\n", bp->signal[i].polarity);
2952 static EXT_ATTR_RO(signal, polarity, 0);
2953 static EXT_ATTR_RO(signal, polarity, 1);
2954 static EXT_ATTR_RO(signal, polarity, 2);
2955 static EXT_ATTR_RO(signal, polarity, 3);
2958 running_show(struct device *dev, struct device_attribute *attr, char *buf)
2960 struct dev_ext_attribute *ea = to_ext_attr(attr);
2961 struct ptp_ocp *bp = dev_get_drvdata(dev);
2962 int i = (uintptr_t)ea->var;
2964 return sysfs_emit(buf, "%d\n", bp->signal[i].running);
2966 static EXT_ATTR_RO(signal, running, 0);
2967 static EXT_ATTR_RO(signal, running, 1);
2968 static EXT_ATTR_RO(signal, running, 2);
2969 static EXT_ATTR_RO(signal, running, 3);
2972 start_show(struct device *dev, struct device_attribute *attr, char *buf)
2974 struct dev_ext_attribute *ea = to_ext_attr(attr);
2975 struct ptp_ocp *bp = dev_get_drvdata(dev);
2976 int i = (uintptr_t)ea->var;
2977 struct timespec64 ts;
2979 ts = ktime_to_timespec64(bp->signal[i].start);
2980 return sysfs_emit(buf, "%llu.%lu\n", ts.tv_sec, ts.tv_nsec);
2982 static EXT_ATTR_RO(signal, start, 0);
2983 static EXT_ATTR_RO(signal, start, 1);
2984 static EXT_ATTR_RO(signal, start, 2);
2985 static EXT_ATTR_RO(signal, start, 3);
2988 seconds_store(struct device *dev, struct device_attribute *attr,
2989 const char *buf, size_t count)
2991 struct dev_ext_attribute *ea = to_ext_attr(attr);
2992 struct ptp_ocp *bp = dev_get_drvdata(dev);
2993 int idx = (uintptr_t)ea->var;
2997 err = kstrtou32(buf, 0, &val);
3004 val = (val << 8) | 0x1;
3006 iowrite32(val, &bp->freq_in[idx]->ctrl);
3012 seconds_show(struct device *dev, struct device_attribute *attr, char *buf)
3014 struct dev_ext_attribute *ea = to_ext_attr(attr);
3015 struct ptp_ocp *bp = dev_get_drvdata(dev);
3016 int idx = (uintptr_t)ea->var;
3019 val = ioread32(&bp->freq_in[idx]->ctrl);
3021 val = (val >> 8) & 0xff;
3025 return sysfs_emit(buf, "%u\n", val);
3027 static EXT_ATTR_RW(freq, seconds, 0);
3028 static EXT_ATTR_RW(freq, seconds, 1);
3029 static EXT_ATTR_RW(freq, seconds, 2);
3030 static EXT_ATTR_RW(freq, seconds, 3);
3033 frequency_show(struct device *dev, struct device_attribute *attr, char *buf)
3035 struct dev_ext_attribute *ea = to_ext_attr(attr);
3036 struct ptp_ocp *bp = dev_get_drvdata(dev);
3037 int idx = (uintptr_t)ea->var;
3040 val = ioread32(&bp->freq_in[idx]->status);
3041 if (val & FREQ_STATUS_ERROR)
3042 return sysfs_emit(buf, "error\n");
3043 if (val & FREQ_STATUS_OVERRUN)
3044 return sysfs_emit(buf, "overrun\n");
3045 if (val & FREQ_STATUS_VALID)
3046 return sysfs_emit(buf, "%lu\n", val & FREQ_STATUS_MASK);
3049 static EXT_ATTR_RO(freq, frequency, 0);
3050 static EXT_ATTR_RO(freq, frequency, 1);
3051 static EXT_ATTR_RO(freq, frequency, 2);
3052 static EXT_ATTR_RO(freq, frequency, 3);
3055 serialnum_show(struct device *dev, struct device_attribute *attr, char *buf)
3057 struct ptp_ocp *bp = dev_get_drvdata(dev);
3059 if (!bp->has_eeprom_data)
3060 ptp_ocp_read_eeprom(bp);
3062 return sysfs_emit(buf, "%pM\n", bp->serial);
3064 static DEVICE_ATTR_RO(serialnum);
3067 gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf)
3069 struct ptp_ocp *bp = dev_get_drvdata(dev);
3073 ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost);
3075 ret = sysfs_emit(buf, "SYNC\n");
3079 static DEVICE_ATTR_RO(gnss_sync);
3082 utc_tai_offset_show(struct device *dev,
3083 struct device_attribute *attr, char *buf)
3085 struct ptp_ocp *bp = dev_get_drvdata(dev);
3087 return sysfs_emit(buf, "%d\n", bp->utc_tai_offset);
3091 utc_tai_offset_store(struct device *dev,
3092 struct device_attribute *attr,
3093 const char *buf, size_t count)
3095 struct ptp_ocp *bp = dev_get_drvdata(dev);
3099 err = kstrtou32(buf, 0, &val);
3103 ptp_ocp_utc_distribute(bp, val);
3107 static DEVICE_ATTR_RW(utc_tai_offset);
3110 ts_window_adjust_show(struct device *dev,
3111 struct device_attribute *attr, char *buf)
3113 struct ptp_ocp *bp = dev_get_drvdata(dev);
3115 return sysfs_emit(buf, "%d\n", bp->ts_window_adjust);
3119 ts_window_adjust_store(struct device *dev,
3120 struct device_attribute *attr,
3121 const char *buf, size_t count)
3123 struct ptp_ocp *bp = dev_get_drvdata(dev);
3127 err = kstrtou32(buf, 0, &val);
3131 bp->ts_window_adjust = val;
3135 static DEVICE_ATTR_RW(ts_window_adjust);
3138 irig_b_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
3140 struct ptp_ocp *bp = dev_get_drvdata(dev);
3143 val = ioread32(&bp->irig_out->ctrl);
3144 val = (val >> 16) & 0x07;
3145 return sysfs_emit(buf, "%d\n", val);
3149 irig_b_mode_store(struct device *dev,
3150 struct device_attribute *attr,
3151 const char *buf, size_t count)
3153 struct ptp_ocp *bp = dev_get_drvdata(dev);
3154 unsigned long flags;
3159 err = kstrtou8(buf, 0, &val);
3165 reg = ((val & 0x7) << 16);
3167 spin_lock_irqsave(&bp->lock, flags);
3168 iowrite32(0, &bp->irig_out->ctrl); /* disable */
3169 iowrite32(reg, &bp->irig_out->ctrl); /* change mode */
3170 iowrite32(reg | IRIG_M_CTRL_ENABLE, &bp->irig_out->ctrl);
3171 spin_unlock_irqrestore(&bp->lock, flags);
3175 static DEVICE_ATTR_RW(irig_b_mode);
3178 clock_source_show(struct device *dev, struct device_attribute *attr, char *buf)
3180 struct ptp_ocp *bp = dev_get_drvdata(dev);
3184 select = ioread32(&bp->reg->select);
3185 p = ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16);
3187 return sysfs_emit(buf, "%s\n", p);
3191 clock_source_store(struct device *dev, struct device_attribute *attr,
3192 const char *buf, size_t count)
3194 struct ptp_ocp *bp = dev_get_drvdata(dev);
3195 unsigned long flags;
3198 val = ptp_ocp_select_val_from_name(ptp_ocp_clock, buf);
3202 spin_lock_irqsave(&bp->lock, flags);
3203 iowrite32(val, &bp->reg->select);
3204 spin_unlock_irqrestore(&bp->lock, flags);
3208 static DEVICE_ATTR_RW(clock_source);
3211 available_clock_sources_show(struct device *dev,
3212 struct device_attribute *attr, char *buf)
3214 return ptp_ocp_select_table_show(ptp_ocp_clock, buf);
3216 static DEVICE_ATTR_RO(available_clock_sources);
3219 clock_status_drift_show(struct device *dev,
3220 struct device_attribute *attr, char *buf)
3222 struct ptp_ocp *bp = dev_get_drvdata(dev);
3226 val = ioread32(&bp->reg->status_drift);
3227 res = (val & ~INT_MAX) ? -1 : 1;
3228 res *= (val & INT_MAX);
3229 return sysfs_emit(buf, "%d\n", res);
3231 static DEVICE_ATTR_RO(clock_status_drift);
3234 clock_status_offset_show(struct device *dev,
3235 struct device_attribute *attr, char *buf)
3237 struct ptp_ocp *bp = dev_get_drvdata(dev);
3241 val = ioread32(&bp->reg->status_offset);
3242 res = (val & ~INT_MAX) ? -1 : 1;
3243 res *= (val & INT_MAX);
3244 return sysfs_emit(buf, "%d\n", res);
3246 static DEVICE_ATTR_RO(clock_status_offset);
3249 tod_correction_show(struct device *dev,
3250 struct device_attribute *attr, char *buf)
3252 struct ptp_ocp *bp = dev_get_drvdata(dev);
3256 val = ioread32(&bp->tod->adj_sec);
3257 res = (val & ~INT_MAX) ? -1 : 1;
3258 res *= (val & INT_MAX);
3259 return sysfs_emit(buf, "%d\n", res);
3263 tod_correction_store(struct device *dev, struct device_attribute *attr,
3264 const char *buf, size_t count)
3266 struct ptp_ocp *bp = dev_get_drvdata(dev);
3267 unsigned long flags;
3271 err = kstrtos32(buf, 0, &res);
3280 spin_lock_irqsave(&bp->lock, flags);
3281 iowrite32(val, &bp->tod->adj_sec);
3282 spin_unlock_irqrestore(&bp->lock, flags);
3286 static DEVICE_ATTR_RW(tod_correction);
3288 #define _DEVICE_SIGNAL_GROUP_ATTRS(_nr) \
3289 static struct attribute *fb_timecard_signal##_nr##_attrs[] = { \
3290 &dev_attr_signal##_nr##_signal.attr.attr, \
3291 &dev_attr_signal##_nr##_duty.attr.attr, \
3292 &dev_attr_signal##_nr##_phase.attr.attr, \
3293 &dev_attr_signal##_nr##_period.attr.attr, \
3294 &dev_attr_signal##_nr##_polarity.attr.attr, \
3295 &dev_attr_signal##_nr##_running.attr.attr, \
3296 &dev_attr_signal##_nr##_start.attr.attr, \
3300 #define DEVICE_SIGNAL_GROUP(_name, _nr) \
3301 _DEVICE_SIGNAL_GROUP_ATTRS(_nr); \
3302 static const struct attribute_group \
3303 fb_timecard_signal##_nr##_group = { \
3305 .attrs = fb_timecard_signal##_nr##_attrs, \
3308 DEVICE_SIGNAL_GROUP(gen1, 0);
3309 DEVICE_SIGNAL_GROUP(gen2, 1);
3310 DEVICE_SIGNAL_GROUP(gen3, 2);
3311 DEVICE_SIGNAL_GROUP(gen4, 3);
3313 #define _DEVICE_FREQ_GROUP_ATTRS(_nr) \
3314 static struct attribute *fb_timecard_freq##_nr##_attrs[] = { \
3315 &dev_attr_freq##_nr##_seconds.attr.attr, \
3316 &dev_attr_freq##_nr##_frequency.attr.attr, \
3320 #define DEVICE_FREQ_GROUP(_name, _nr) \
3321 _DEVICE_FREQ_GROUP_ATTRS(_nr); \
3322 static const struct attribute_group \
3323 fb_timecard_freq##_nr##_group = { \
3325 .attrs = fb_timecard_freq##_nr##_attrs, \
3328 DEVICE_FREQ_GROUP(freq1, 0);
3329 DEVICE_FREQ_GROUP(freq2, 1);
3330 DEVICE_FREQ_GROUP(freq3, 2);
3331 DEVICE_FREQ_GROUP(freq4, 3);
3334 disciplining_config_read(struct file *filp, struct kobject *kobj,
3335 struct bin_attribute *bin_attr, char *buf,
3336 loff_t off, size_t count)
3338 struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj));
3339 size_t size = OCP_ART_CONFIG_SIZE;
3340 struct nvmem_device *nvmem;
3343 nvmem = ptp_ocp_nvmem_device_get(bp, NULL);
3345 return PTR_ERR(nvmem);
3352 if (off + count > size)
3355 // the configuration is in the very beginning of the EEPROM
3356 err = nvmem_device_read(nvmem, off, count, buf);
3363 ptp_ocp_nvmem_device_put(&nvmem);
3369 disciplining_config_write(struct file *filp, struct kobject *kobj,
3370 struct bin_attribute *bin_attr, char *buf,
3371 loff_t off, size_t count)
3373 struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj));
3374 struct nvmem_device *nvmem;
3377 /* Allow write of the whole area only */
3378 if (off || count != OCP_ART_CONFIG_SIZE)
3381 nvmem = ptp_ocp_nvmem_device_get(bp, NULL);
3383 return PTR_ERR(nvmem);
3385 err = nvmem_device_write(nvmem, 0x00, count, buf);
3389 ptp_ocp_nvmem_device_put(&nvmem);
3393 static BIN_ATTR_RW(disciplining_config, OCP_ART_CONFIG_SIZE);
3396 temperature_table_read(struct file *filp, struct kobject *kobj,
3397 struct bin_attribute *bin_attr, char *buf,
3398 loff_t off, size_t count)
3400 struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj));
3401 size_t size = OCP_ART_TEMP_TABLE_SIZE;
3402 struct nvmem_device *nvmem;
3405 nvmem = ptp_ocp_nvmem_device_get(bp, NULL);
3407 return PTR_ERR(nvmem);
3414 if (off + count > size)
3417 // the configuration is in the very beginning of the EEPROM
3418 err = nvmem_device_read(nvmem, 0x90 + off, count, buf);
3425 ptp_ocp_nvmem_device_put(&nvmem);
3431 temperature_table_write(struct file *filp, struct kobject *kobj,
3432 struct bin_attribute *bin_attr, char *buf,
3433 loff_t off, size_t count)
3435 struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj));
3436 struct nvmem_device *nvmem;
3439 /* Allow write of the whole area only */
3440 if (off || count != OCP_ART_TEMP_TABLE_SIZE)
3443 nvmem = ptp_ocp_nvmem_device_get(bp, NULL);
3445 return PTR_ERR(nvmem);
3447 err = nvmem_device_write(nvmem, 0x90, count, buf);
3451 ptp_ocp_nvmem_device_put(&nvmem);
3455 static BIN_ATTR_RW(temperature_table, OCP_ART_TEMP_TABLE_SIZE);
3457 static struct attribute *fb_timecard_attrs[] = {
3458 &dev_attr_serialnum.attr,
3459 &dev_attr_gnss_sync.attr,
3460 &dev_attr_clock_source.attr,
3461 &dev_attr_available_clock_sources.attr,
3462 &dev_attr_sma1.attr,
3463 &dev_attr_sma2.attr,
3464 &dev_attr_sma3.attr,
3465 &dev_attr_sma4.attr,
3466 &dev_attr_available_sma_inputs.attr,
3467 &dev_attr_available_sma_outputs.attr,
3468 &dev_attr_clock_status_drift.attr,
3469 &dev_attr_clock_status_offset.attr,
3470 &dev_attr_irig_b_mode.attr,
3471 &dev_attr_utc_tai_offset.attr,
3472 &dev_attr_ts_window_adjust.attr,
3473 &dev_attr_tod_correction.attr,
3477 static const struct attribute_group fb_timecard_group = {
3478 .attrs = fb_timecard_attrs,
3481 static const struct ocp_attr_group fb_timecard_groups[] = {
3482 { .cap = OCP_CAP_BASIC, .group = &fb_timecard_group },
3483 { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal0_group },
3484 { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal1_group },
3485 { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal2_group },
3486 { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal3_group },
3487 { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq0_group },
3488 { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq1_group },
3489 { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq2_group },
3490 { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq3_group },
3494 static struct attribute *art_timecard_attrs[] = {
3495 &dev_attr_serialnum.attr,
3496 &dev_attr_clock_source.attr,
3497 &dev_attr_available_clock_sources.attr,
3498 &dev_attr_utc_tai_offset.attr,
3499 &dev_attr_ts_window_adjust.attr,
3500 &dev_attr_sma1.attr,
3501 &dev_attr_sma2.attr,
3502 &dev_attr_sma3.attr,
3503 &dev_attr_sma4.attr,
3504 &dev_attr_available_sma_inputs.attr,
3505 &dev_attr_available_sma_outputs.attr,
3509 static struct bin_attribute *bin_art_timecard_attrs[] = {
3510 &bin_attr_disciplining_config,
3511 &bin_attr_temperature_table,
3515 static const struct attribute_group art_timecard_group = {
3516 .attrs = art_timecard_attrs,
3517 .bin_attrs = bin_art_timecard_attrs,
3520 static const struct ocp_attr_group art_timecard_groups[] = {
3521 { .cap = OCP_CAP_BASIC, .group = &art_timecard_group },
3526 gpio_input_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit,
3531 for (i = 0; i < 4; i++) {
3532 if (bp->sma[i].mode != SMA_MODE_IN)
3534 if (map[i][0] & (1 << bit)) {
3535 sprintf(buf, "sma%d", i + 1);
3545 gpio_output_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit)
3550 strcpy(ans, "----");
3551 for (i = 0; i < 4; i++) {
3552 if (bp->sma[i].mode != SMA_MODE_OUT)
3554 if (map[i][1] & (1 << bit))
3555 ans += sprintf(ans, "sma%d ", i + 1);
3560 _signal_summary_show(struct seq_file *s, struct ptp_ocp *bp, int nr)
3562 struct signal_reg __iomem *reg = bp->signal_out[nr]->mem;
3563 struct ptp_ocp_signal *signal = &bp->signal[nr];
3571 on = signal->running;
3572 sprintf(label, "GEN%d", nr + 1);
3573 seq_printf(s, "%7s: %s, period:%llu duty:%d%% phase:%llu pol:%d",
3574 label, on ? " ON" : "OFF",
3575 signal->period, signal->duty, signal->phase,
3578 val = ioread32(®->enable);
3579 seq_printf(s, " [%x", val);
3580 val = ioread32(®->status);
3581 seq_printf(s, " %x]", val);
3583 seq_printf(s, " start:%llu\n", signal->start);
3587 _frequency_summary_show(struct seq_file *s, int nr,
3588 struct frequency_reg __iomem *reg)
3597 sprintf(label, "FREQ%d", nr + 1);
3598 val = ioread32(®->ctrl);
3600 val = (val >> 8) & 0xff;
3601 seq_printf(s, "%7s: %s, sec:%u",
3606 val = ioread32(®->status);
3607 if (val & FREQ_STATUS_ERROR)
3608 seq_printf(s, ", error");
3609 if (val & FREQ_STATUS_OVERRUN)
3610 seq_printf(s, ", overrun");
3611 if (val & FREQ_STATUS_VALID)
3612 seq_printf(s, ", freq %lu Hz", val & FREQ_STATUS_MASK);
3613 seq_printf(s, " reg:%x\n", val);
3617 ptp_ocp_summary_show(struct seq_file *s, void *data)
3619 struct device *dev = s->private;
3620 struct ptp_system_timestamp sts;
3621 struct ts_reg __iomem *ts_reg;
3622 char *buf, *src, *mac_src;
3623 struct timespec64 ts;
3630 buf = (char *)__get_free_page(GFP_KERNEL);
3634 bp = dev_get_drvdata(dev);
3636 seq_printf(s, "%7s: /dev/ptp%d\n", "PTP", ptp_clock_index(bp->ptp));
3637 if (bp->gnss_port.line != -1)
3638 seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS1",
3639 bp->gnss_port.line);
3640 if (bp->gnss2_port.line != -1)
3641 seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS2",
3642 bp->gnss2_port.line);
3643 if (bp->mac_port.line != -1)
3644 seq_printf(s, "%7s: /dev/ttyS%d\n", "MAC", bp->mac_port.line);
3645 if (bp->nmea_port.line != -1)
3646 seq_printf(s, "%7s: /dev/ttyS%d\n", "NMEA", bp->nmea_port.line);
3648 memset(sma_val, 0xff, sizeof(sma_val));
3652 reg = ioread32(&bp->sma_map1->gpio1);
3653 sma_val[0][0] = reg & 0xffff;
3654 sma_val[1][0] = reg >> 16;
3656 reg = ioread32(&bp->sma_map1->gpio2);
3657 sma_val[2][1] = reg & 0xffff;
3658 sma_val[3][1] = reg >> 16;
3660 reg = ioread32(&bp->sma_map2->gpio1);
3661 sma_val[2][0] = reg & 0xffff;
3662 sma_val[3][0] = reg >> 16;
3664 reg = ioread32(&bp->sma_map2->gpio2);
3665 sma_val[0][1] = reg & 0xffff;
3666 sma_val[1][1] = reg >> 16;
3669 sma1_show(dev, NULL, buf);
3670 seq_printf(s, " sma1: %04x,%04x %s",
3671 sma_val[0][0], sma_val[0][1], buf);
3673 sma2_show(dev, NULL, buf);
3674 seq_printf(s, " sma2: %04x,%04x %s",
3675 sma_val[1][0], sma_val[1][1], buf);
3677 sma3_show(dev, NULL, buf);
3678 seq_printf(s, " sma3: %04x,%04x %s",
3679 sma_val[2][0], sma_val[2][1], buf);
3681 sma4_show(dev, NULL, buf);
3682 seq_printf(s, " sma4: %04x,%04x %s",
3683 sma_val[3][0], sma_val[3][1], buf);
3686 ts_reg = bp->ts0->mem;
3687 on = ioread32(&ts_reg->enable);
3689 seq_printf(s, "%7s: %s, src: %s\n", "TS0",
3690 on ? " ON" : "OFF", src);
3694 ts_reg = bp->ts1->mem;
3695 on = ioread32(&ts_reg->enable);
3696 gpio_input_map(buf, bp, sma_val, 2, NULL);
3697 seq_printf(s, "%7s: %s, src: %s\n", "TS1",
3698 on ? " ON" : "OFF", buf);
3702 ts_reg = bp->ts2->mem;
3703 on = ioread32(&ts_reg->enable);
3704 gpio_input_map(buf, bp, sma_val, 3, NULL);
3705 seq_printf(s, "%7s: %s, src: %s\n", "TS2",
3706 on ? " ON" : "OFF", buf);
3710 ts_reg = bp->ts3->mem;
3711 on = ioread32(&ts_reg->enable);
3712 gpio_input_map(buf, bp, sma_val, 6, NULL);
3713 seq_printf(s, "%7s: %s, src: %s\n", "TS3",
3714 on ? " ON" : "OFF", buf);
3718 ts_reg = bp->ts4->mem;
3719 on = ioread32(&ts_reg->enable);
3720 gpio_input_map(buf, bp, sma_val, 7, NULL);
3721 seq_printf(s, "%7s: %s, src: %s\n", "TS4",
3722 on ? " ON" : "OFF", buf);
3726 ts_reg = bp->pps->mem;
3728 on = ioread32(&ts_reg->enable);
3729 map = !!(bp->pps_req_map & OCP_REQ_TIMESTAMP);
3730 seq_printf(s, "%7s: %s, src: %s\n", "TS5",
3731 on && map ? " ON" : "OFF", src);
3733 map = !!(bp->pps_req_map & OCP_REQ_PPS);
3734 seq_printf(s, "%7s: %s, src: %s\n", "PPS",
3735 on && map ? " ON" : "OFF", src);
3738 if (bp->fw_cap & OCP_CAP_SIGNAL)
3739 for (i = 0; i < 4; i++)
3740 _signal_summary_show(s, bp, i);
3742 if (bp->fw_cap & OCP_CAP_FREQ)
3743 for (i = 0; i < 4; i++)
3744 _frequency_summary_show(s, i, bp->freq_in[i]);
3747 ctrl = ioread32(&bp->irig_out->ctrl);
3748 on = ctrl & IRIG_M_CTRL_ENABLE;
3749 val = ioread32(&bp->irig_out->status);
3750 gpio_output_map(buf, bp, sma_val, 4);
3751 seq_printf(s, "%7s: %s, error: %d, mode %d, out: %s\n", "IRIG",
3752 on ? " ON" : "OFF", val, (ctrl >> 16), buf);
3756 on = ioread32(&bp->irig_in->ctrl) & IRIG_S_CTRL_ENABLE;
3757 val = ioread32(&bp->irig_in->status);
3758 gpio_input_map(buf, bp, sma_val, 4, NULL);
3759 seq_printf(s, "%7s: %s, error: %d, src: %s\n", "IRIG in",
3760 on ? " ON" : "OFF", val, buf);
3764 on = ioread32(&bp->dcf_out->ctrl) & DCF_M_CTRL_ENABLE;
3765 val = ioread32(&bp->dcf_out->status);
3766 gpio_output_map(buf, bp, sma_val, 5);
3767 seq_printf(s, "%7s: %s, error: %d, out: %s\n", "DCF",
3768 on ? " ON" : "OFF", val, buf);
3772 on = ioread32(&bp->dcf_in->ctrl) & DCF_S_CTRL_ENABLE;
3773 val = ioread32(&bp->dcf_in->status);
3774 gpio_input_map(buf, bp, sma_val, 5, NULL);
3775 seq_printf(s, "%7s: %s, error: %d, src: %s\n", "DCF in",
3776 on ? " ON" : "OFF", val, buf);
3780 on = ioread32(&bp->nmea_out->ctrl) & 1;
3781 val = ioread32(&bp->nmea_out->status);
3782 seq_printf(s, "%7s: %s, error: %d\n", "NMEA",
3783 on ? " ON" : "OFF", val);
3786 /* compute src for PPS1, used below. */
3787 if (bp->pps_select) {
3788 val = ioread32(&bp->pps_select->gpio1);
3792 gpio_input_map(src, bp, sma_val, 0, NULL);
3794 } else if (val & 0x02) {
3796 } else if (val & 0x04) {
3806 seq_printf(s, "MAC PPS1 src: %s\n", mac_src);
3808 gpio_input_map(buf, bp, sma_val, 1, "GNSS2");
3809 seq_printf(s, "MAC PPS2 src: %s\n", buf);
3811 /* assumes automatic switchover/selection */
3812 val = ioread32(&bp->reg->select);
3813 switch (val >> 16) {
3815 sprintf(buf, "----");
3818 sprintf(buf, "IRIG");
3821 sprintf(buf, "%s via PPS1", src);
3824 sprintf(buf, "DCF");
3827 strcpy(buf, "unknown");
3830 val = ioread32(&bp->reg->status);
3831 seq_printf(s, "%7s: %s, state: %s\n", "PHC src", buf,
3832 val & OCP_STATUS_IN_SYNC ? "sync" : "unsynced");
3834 if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts)) {
3835 struct timespec64 sys_ts;
3836 s64 pre_ns, post_ns, ns;
3838 pre_ns = timespec64_to_ns(&sts.pre_ts);
3839 post_ns = timespec64_to_ns(&sts.post_ts);
3840 ns = (pre_ns + post_ns) / 2;
3841 ns += (s64)bp->utc_tai_offset * NSEC_PER_SEC;
3842 sys_ts = ns_to_timespec64(ns);
3844 seq_printf(s, "%7s: %lld.%ld == %ptT TAI\n", "PHC",
3845 ts.tv_sec, ts.tv_nsec, &ts);
3846 seq_printf(s, "%7s: %lld.%ld == %ptT UTC offset %d\n", "SYS",
3847 sys_ts.tv_sec, sys_ts.tv_nsec, &sys_ts,
3848 bp->utc_tai_offset);
3849 seq_printf(s, "%7s: PHC:SYS offset: %lld window: %lld\n", "",
3850 timespec64_to_ns(&ts) - ns,
3854 free_page((unsigned long)buf);
3857 DEFINE_SHOW_ATTRIBUTE(ptp_ocp_summary);
3860 ptp_ocp_tod_status_show(struct seq_file *s, void *data)
3862 struct device *dev = s->private;
3867 bp = dev_get_drvdata(dev);
3869 val = ioread32(&bp->tod->ctrl);
3870 if (!(val & TOD_CTRL_ENABLE)) {
3871 seq_printf(s, "TOD Slave disabled\n");
3874 seq_printf(s, "TOD Slave enabled, Control Register 0x%08X\n", val);
3876 idx = val & TOD_CTRL_PROTOCOL ? 4 : 0;
3877 idx += (val >> 16) & 3;
3878 seq_printf(s, "Protocol %s\n", ptp_ocp_tod_proto_name(idx));
3880 idx = (val >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK;
3881 seq_printf(s, "GNSS %s\n", ptp_ocp_tod_gnss_name(idx));
3883 val = ioread32(&bp->tod->version);
3884 seq_printf(s, "TOD Version %d.%d.%d\n",
3885 val >> 24, (val >> 16) & 0xff, val & 0xffff);
3887 val = ioread32(&bp->tod->status);
3888 seq_printf(s, "Status register: 0x%08X\n", val);
3890 val = ioread32(&bp->tod->adj_sec);
3891 idx = (val & ~INT_MAX) ? -1 : 1;
3892 idx *= (val & INT_MAX);
3893 seq_printf(s, "Correction seconds: %d\n", idx);
3895 val = ioread32(&bp->tod->utc_status);
3896 seq_printf(s, "UTC status register: 0x%08X\n", val);
3897 seq_printf(s, "UTC offset: %ld valid:%d\n",
3898 val & TOD_STATUS_UTC_MASK, val & TOD_STATUS_UTC_VALID ? 1 : 0);
3899 seq_printf(s, "Leap second info valid:%d, Leap second announce %d\n",
3900 val & TOD_STATUS_LEAP_VALID ? 1 : 0,
3901 val & TOD_STATUS_LEAP_ANNOUNCE ? 1 : 0);
3903 val = ioread32(&bp->tod->leap);
3904 seq_printf(s, "Time to next leap second (in sec): %d\n", (s32) val);
3908 DEFINE_SHOW_ATTRIBUTE(ptp_ocp_tod_status);
3910 static struct dentry *ptp_ocp_debugfs_root;
3913 ptp_ocp_debugfs_add_device(struct ptp_ocp *bp)
3917 d = debugfs_create_dir(dev_name(&bp->dev), ptp_ocp_debugfs_root);
3919 debugfs_create_file("summary", 0444, bp->debug_root,
3920 &bp->dev, &ptp_ocp_summary_fops);
3922 debugfs_create_file("tod_status", 0444, bp->debug_root,
3923 &bp->dev, &ptp_ocp_tod_status_fops);
3927 ptp_ocp_debugfs_remove_device(struct ptp_ocp *bp)
3929 debugfs_remove_recursive(bp->debug_root);
3933 ptp_ocp_debugfs_init(void)
3935 ptp_ocp_debugfs_root = debugfs_create_dir("timecard", NULL);
3939 ptp_ocp_debugfs_fini(void)
3941 debugfs_remove_recursive(ptp_ocp_debugfs_root);
3945 ptp_ocp_dev_release(struct device *dev)
3947 struct ptp_ocp *bp = dev_get_drvdata(dev);
3949 mutex_lock(&ptp_ocp_lock);
3950 idr_remove(&ptp_ocp_idr, bp->id);
3951 mutex_unlock(&ptp_ocp_lock);
3955 ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev)
3959 mutex_lock(&ptp_ocp_lock);
3960 err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL);
3961 mutex_unlock(&ptp_ocp_lock);
3963 dev_err(&pdev->dev, "idr_alloc failed: %d\n", err);
3968 bp->ptp_info = ptp_ocp_clock_info;
3969 spin_lock_init(&bp->lock);
3970 bp->gnss_port.line = -1;
3971 bp->gnss2_port.line = -1;
3972 bp->mac_port.line = -1;
3973 bp->nmea_port.line = -1;
3976 device_initialize(&bp->dev);
3977 dev_set_name(&bp->dev, "ocp%d", bp->id);
3978 bp->dev.class = &timecard_class;
3979 bp->dev.parent = &pdev->dev;
3980 bp->dev.release = ptp_ocp_dev_release;
3981 dev_set_drvdata(&bp->dev, bp);
3983 err = device_add(&bp->dev);
3985 dev_err(&bp->dev, "device add failed: %d\n", err);
3989 pci_set_drvdata(pdev, bp);
3994 ptp_ocp_dev_release(&bp->dev);
3995 put_device(&bp->dev);
4000 ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link)
4002 struct device *dev = &bp->dev;
4004 if (sysfs_create_link(&dev->kobj, &child->kobj, link))
4005 dev_err(dev, "%s symlink failed\n", link);
4009 ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link)
4011 struct device *dev, *child;
4013 dev = &bp->pdev->dev;
4015 child = device_find_child_by_name(dev, name);
4017 dev_err(dev, "Could not find device %s\n", name);
4021 ptp_ocp_symlink(bp, child, link);
4026 ptp_ocp_complete(struct ptp_ocp *bp)
4028 struct pps_device *pps;
4031 if (bp->gnss_port.line != -1) {
4032 sprintf(buf, "ttyS%d", bp->gnss_port.line);
4033 ptp_ocp_link_child(bp, buf, "ttyGNSS");
4035 if (bp->gnss2_port.line != -1) {
4036 sprintf(buf, "ttyS%d", bp->gnss2_port.line);
4037 ptp_ocp_link_child(bp, buf, "ttyGNSS2");
4039 if (bp->mac_port.line != -1) {
4040 sprintf(buf, "ttyS%d", bp->mac_port.line);
4041 ptp_ocp_link_child(bp, buf, "ttyMAC");
4043 if (bp->nmea_port.line != -1) {
4044 sprintf(buf, "ttyS%d", bp->nmea_port.line);
4045 ptp_ocp_link_child(bp, buf, "ttyNMEA");
4047 sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp));
4048 ptp_ocp_link_child(bp, buf, "ptp");
4050 pps = pps_lookup_dev(bp->ptp);
4052 ptp_ocp_symlink(bp, pps->dev, "pps");
4054 ptp_ocp_debugfs_add_device(bp);
4060 ptp_ocp_phc_info(struct ptp_ocp *bp)
4062 struct timespec64 ts;
4063 u32 version, select;
4066 version = ioread32(&bp->reg->version);
4067 select = ioread32(&bp->reg->select);
4068 dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n",
4069 version >> 24, (version >> 16) & 0xff, version & 0xffff,
4070 ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16),
4071 ptp_clock_index(bp->ptp));
4073 sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC;
4074 if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL))
4075 dev_info(&bp->pdev->dev, "Time: %lld.%ld, %s\n",
4076 ts.tv_sec, ts.tv_nsec,
4077 sync ? "in-sync" : "UNSYNCED");
4081 ptp_ocp_serial_info(struct device *dev, const char *name, int port, int baud)
4084 dev_info(dev, "%5s: /dev/ttyS%-2d @ %6d\n", name, port, baud);
4088 ptp_ocp_info(struct ptp_ocp *bp)
4090 static int nmea_baud[] = {
4091 1200, 2400, 4800, 9600, 19200, 38400,
4092 57600, 115200, 230400, 460800, 921600,
4095 struct device *dev = &bp->pdev->dev;
4098 ptp_ocp_phc_info(bp);
4100 ptp_ocp_serial_info(dev, "GNSS", bp->gnss_port.line,
4101 bp->gnss_port.baud);
4102 ptp_ocp_serial_info(dev, "GNSS2", bp->gnss2_port.line,
4103 bp->gnss2_port.baud);
4104 ptp_ocp_serial_info(dev, "MAC", bp->mac_port.line, bp->mac_port.baud);
4105 if (bp->nmea_out && bp->nmea_port.line != -1) {
4106 bp->nmea_port.baud = -1;
4108 reg = ioread32(&bp->nmea_out->uart_baud);
4109 if (reg < ARRAY_SIZE(nmea_baud))
4110 bp->nmea_port.baud = nmea_baud[reg];
4112 ptp_ocp_serial_info(dev, "NMEA", bp->nmea_port.line,
4113 bp->nmea_port.baud);
4118 ptp_ocp_detach_sysfs(struct ptp_ocp *bp)
4120 struct device *dev = &bp->dev;
4122 sysfs_remove_link(&dev->kobj, "ttyGNSS");
4123 sysfs_remove_link(&dev->kobj, "ttyGNSS2");
4124 sysfs_remove_link(&dev->kobj, "ttyMAC");
4125 sysfs_remove_link(&dev->kobj, "ptp");
4126 sysfs_remove_link(&dev->kobj, "pps");
4130 ptp_ocp_detach(struct ptp_ocp *bp)
4134 ptp_ocp_debugfs_remove_device(bp);
4135 ptp_ocp_detach_sysfs(bp);
4136 ptp_ocp_attr_group_del(bp);
4137 if (timer_pending(&bp->watchdog))
4138 del_timer_sync(&bp->watchdog);
4140 ptp_ocp_unregister_ext(bp->ts0);
4142 ptp_ocp_unregister_ext(bp->ts1);
4144 ptp_ocp_unregister_ext(bp->ts2);
4146 ptp_ocp_unregister_ext(bp->ts3);
4148 ptp_ocp_unregister_ext(bp->ts4);
4150 ptp_ocp_unregister_ext(bp->pps);
4151 for (i = 0; i < 4; i++)
4152 if (bp->signal_out[i])
4153 ptp_ocp_unregister_ext(bp->signal_out[i]);
4154 if (bp->gnss_port.line != -1)
4155 serial8250_unregister_port(bp->gnss_port.line);
4156 if (bp->gnss2_port.line != -1)
4157 serial8250_unregister_port(bp->gnss2_port.line);
4158 if (bp->mac_port.line != -1)
4159 serial8250_unregister_port(bp->mac_port.line);
4160 if (bp->nmea_port.line != -1)
4161 serial8250_unregister_port(bp->nmea_port.line);
4162 platform_device_unregister(bp->spi_flash);
4163 platform_device_unregister(bp->i2c_ctrl);
4165 clk_hw_unregister_fixed_rate(bp->i2c_clk);
4167 pci_free_irq_vectors(bp->pdev);
4169 ptp_clock_unregister(bp->ptp);
4170 kfree(bp->ptp_info.pin_config);
4171 device_unregister(&bp->dev);
4175 ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id)
4177 struct devlink *devlink;
4181 devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp), &pdev->dev);
4183 dev_err(&pdev->dev, "devlink_alloc failed\n");
4187 err = pci_enable_device(pdev);
4189 dev_err(&pdev->dev, "pci_enable_device\n");
4193 bp = devlink_priv(devlink);
4194 err = ptp_ocp_device_init(bp, pdev);
4199 * Older FPGA firmware only returns 2 irq's.
4200 * allow this - if not all of the IRQ's are returned, skip the
4201 * extra devices and just register the clock.
4203 err = pci_alloc_irq_vectors(pdev, 1, 17, PCI_IRQ_MSI | PCI_IRQ_MSIX);
4205 dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err);
4209 pci_set_master(pdev);
4211 err = ptp_ocp_register_resources(bp, id->driver_data);
4215 bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev);
4216 if (IS_ERR(bp->ptp)) {
4217 err = PTR_ERR(bp->ptp);
4218 dev_err(&pdev->dev, "ptp_clock_register: %d\n", err);
4223 err = ptp_ocp_complete(bp);
4228 devlink_register(devlink);
4234 pci_disable_device(pdev);
4236 devlink_free(devlink);
4241 ptp_ocp_remove(struct pci_dev *pdev)
4243 struct ptp_ocp *bp = pci_get_drvdata(pdev);
4244 struct devlink *devlink = priv_to_devlink(bp);
4246 devlink_unregister(devlink);
4248 pci_disable_device(pdev);
4250 devlink_free(devlink);
4253 static struct pci_driver ptp_ocp_driver = {
4254 .name = KBUILD_MODNAME,
4255 .id_table = ptp_ocp_pcidev_id,
4256 .probe = ptp_ocp_probe,
4257 .remove = ptp_ocp_remove,
4261 ptp_ocp_i2c_notifier_call(struct notifier_block *nb,
4262 unsigned long action, void *data)
4264 struct device *dev, *child = data;
4269 case BUS_NOTIFY_ADD_DEVICE:
4270 case BUS_NOTIFY_DEL_DEVICE:
4271 add = action == BUS_NOTIFY_ADD_DEVICE;
4277 if (!i2c_verify_adapter(child))
4281 while ((dev = dev->parent))
4282 if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME))
4287 bp = dev_get_drvdata(dev);
4289 ptp_ocp_symlink(bp, child, "i2c");
4291 sysfs_remove_link(&bp->dev.kobj, "i2c");
4296 static struct notifier_block ptp_ocp_i2c_notifier = {
4297 .notifier_call = ptp_ocp_i2c_notifier_call,
4306 ptp_ocp_debugfs_init();
4308 what = "timecard class";
4309 err = class_register(&timecard_class);
4313 what = "i2c notifier";
4314 err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
4318 what = "ptp_ocp driver";
4319 err = pci_register_driver(&ptp_ocp_driver);
4326 bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
4328 class_unregister(&timecard_class);
4330 ptp_ocp_debugfs_fini();
4331 pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err);
4338 bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
4339 pci_unregister_driver(&ptp_ocp_driver);
4340 class_unregister(&timecard_class);
4341 ptp_ocp_debugfs_fini();
4344 module_init(ptp_ocp_init);
4345 module_exit(ptp_ocp_fini);
4347 MODULE_DESCRIPTION("OpenCompute TimeCard driver");
4348 MODULE_LICENSE("GPL v2");