1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2020 Facebook */
5 #include <linux/kernel.h>
6 #include <linux/module.h>
7 #include <linux/debugfs.h>
8 #include <linux/init.h>
10 #include <linux/serial_8250.h>
11 #include <linux/clkdev.h>
12 #include <linux/clk-provider.h>
13 #include <linux/platform_device.h>
14 #include <linux/platform_data/i2c-xiic.h>
15 #include <linux/ptp_clock_kernel.h>
16 #include <linux/spi/spi.h>
17 #include <linux/spi/xilinx_spi.h>
18 #include <net/devlink.h>
19 #include <linux/i2c.h>
20 #include <linux/mtd/mtd.h>
21 #include <linux/nvmem-consumer.h>
22 #include <linux/crc16.h>
24 #define PCI_VENDOR_ID_FACEBOOK 0x1d9b
25 #define PCI_DEVICE_ID_FACEBOOK_TIMECARD 0x0400
27 #define PCI_VENDOR_ID_CELESTICA 0x18d4
28 #define PCI_DEVICE_ID_CELESTICA_TIMECARD 0x1008
30 static struct class timecard_class = {
60 #define OCP_CTRL_ENABLE BIT(0)
61 #define OCP_CTRL_ADJUST_TIME BIT(1)
62 #define OCP_CTRL_ADJUST_OFFSET BIT(2)
63 #define OCP_CTRL_ADJUST_DRIFT BIT(3)
64 #define OCP_CTRL_ADJUST_SERVO BIT(8)
65 #define OCP_CTRL_READ_TIME_REQ BIT(30)
66 #define OCP_CTRL_READ_TIME_DONE BIT(31)
68 #define OCP_STATUS_IN_SYNC BIT(0)
69 #define OCP_STATUS_IN_HOLDOVER BIT(1)
71 #define OCP_SELECT_CLK_NONE 0
72 #define OCP_SELECT_CLK_REG 0xfe
87 #define TOD_CTRL_PROTOCOL BIT(28)
88 #define TOD_CTRL_DISABLE_FMT_A BIT(17)
89 #define TOD_CTRL_DISABLE_FMT_B BIT(16)
90 #define TOD_CTRL_ENABLE BIT(0)
91 #define TOD_CTRL_GNSS_MASK ((1U << 4) - 1)
92 #define TOD_CTRL_GNSS_SHIFT 24
94 #define TOD_STATUS_UTC_MASK 0xff
95 #define TOD_STATUS_UTC_VALID BIT(8)
96 #define TOD_STATUS_LEAP_ANNOUNCE BIT(12)
97 #define TOD_STATUS_LEAP_VALID BIT(16)
125 #define PPS_STATUS_FILTER_ERR BIT(0)
126 #define PPS_STATUS_SUPERV_ERR BIT(1)
139 struct irig_master_reg {
148 #define IRIG_M_CTRL_ENABLE BIT(0)
150 struct irig_slave_reg {
159 #define IRIG_S_CTRL_ENABLE BIT(0)
161 struct dcf_master_reg {
169 #define DCF_M_CTRL_ENABLE BIT(0)
171 struct dcf_slave_reg {
179 #define DCF_S_CTRL_ENABLE BIT(0)
201 struct frequency_reg {
205 #define FREQ_STATUS_VALID BIT(31)
206 #define FREQ_STATUS_ERROR BIT(30)
207 #define FREQ_STATUS_OVERRUN BIT(29)
208 #define FREQ_STATUS_MASK (BIT(24) - 1)
210 struct ptp_ocp_flash_info {
217 struct ptp_ocp_firmware_header {
219 __be16 pci_vendor_id;
220 __be16 pci_device_id;
226 #define OCP_FIRMWARE_MAGIC_HEADER "OCPC"
228 struct ptp_ocp_i2c_info {
230 unsigned long fixed_rate;
235 struct ptp_ocp_ext_info {
237 irqreturn_t (*irq_fcn)(int irq, void *priv);
238 int (*enable)(void *priv, u32 req, bool enable);
241 struct ptp_ocp_ext_src {
244 struct ptp_ocp_ext_info *info;
248 enum ptp_ocp_sma_mode {
253 struct ptp_ocp_sma_connector {
254 enum ptp_ocp_sma_mode mode;
261 struct ocp_attr_group {
263 const struct attribute_group *group;
266 #define OCP_CAP_BASIC BIT(0)
267 #define OCP_CAP_SIGNAL BIT(1)
268 #define OCP_CAP_FREQ BIT(2)
270 struct ptp_ocp_signal {
280 #define OCP_BOARD_ID_LEN 13
281 #define OCP_SERIAL_LEN 6
284 struct pci_dev *pdev;
287 struct ocp_reg __iomem *reg;
288 struct tod_reg __iomem *tod;
289 struct pps_reg __iomem *pps_to_ext;
290 struct pps_reg __iomem *pps_to_clk;
291 struct gpio_reg __iomem *pps_select;
292 struct gpio_reg __iomem *sma_map1;
293 struct gpio_reg __iomem *sma_map2;
294 struct irig_master_reg __iomem *irig_out;
295 struct irig_slave_reg __iomem *irig_in;
296 struct dcf_master_reg __iomem *dcf_out;
297 struct dcf_slave_reg __iomem *dcf_in;
298 struct tod_reg __iomem *nmea_out;
299 struct frequency_reg __iomem *freq_in[4];
300 struct ptp_ocp_ext_src *signal_out[4];
301 struct ptp_ocp_ext_src *pps;
302 struct ptp_ocp_ext_src *ts0;
303 struct ptp_ocp_ext_src *ts1;
304 struct ptp_ocp_ext_src *ts2;
305 struct ptp_ocp_ext_src *ts3;
306 struct ptp_ocp_ext_src *ts4;
307 struct img_reg __iomem *image;
308 struct ptp_clock *ptp;
309 struct ptp_clock_info ptp_info;
310 struct platform_device *i2c_ctrl;
311 struct platform_device *spi_flash;
312 struct clk_hw *i2c_clk;
313 struct timer_list watchdog;
314 const struct attribute_group **attr_group;
315 const struct ptp_ocp_eeprom_map *eeprom_map;
316 struct dentry *debug_root;
322 int mac_port; /* miniature atomic clock */
327 u8 board_id[OCP_BOARD_ID_LEN];
328 u8 serial[OCP_SERIAL_LEN];
329 bool has_eeprom_data;
333 u32 ts_window_adjust;
335 struct ptp_ocp_signal signal[4];
336 struct ptp_ocp_sma_connector sma[4];
337 const struct ocp_sma_op *sma_op;
340 #define OCP_REQ_TIMESTAMP BIT(0)
341 #define OCP_REQ_PPS BIT(1)
343 struct ocp_resource {
344 unsigned long offset;
347 int (*setup)(struct ptp_ocp *bp, struct ocp_resource *r);
349 unsigned long bp_offset;
350 const char * const name;
353 static int ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r);
354 static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r);
355 static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r);
356 static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r);
357 static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r);
358 static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r);
359 static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv);
360 static irqreturn_t ptp_ocp_signal_irq(int irq, void *priv);
361 static int ptp_ocp_ts_enable(void *priv, u32 req, bool enable);
362 static int ptp_ocp_signal_from_perout(struct ptp_ocp *bp, int gen,
363 struct ptp_perout_request *req);
364 static int ptp_ocp_signal_enable(void *priv, u32 req, bool enable);
365 static int ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr);
367 static const struct ocp_attr_group fb_timecard_groups[];
369 struct ptp_ocp_eeprom_map {
373 const void * const tag;
376 #define EEPROM_ENTRY(addr, member) \
378 .len = sizeof_field(struct ptp_ocp, member), \
379 .bp_offset = offsetof(struct ptp_ocp, member)
381 #define BP_MAP_ENTRY_ADDR(bp, map) ({ \
382 (void *)((uintptr_t)(bp) + (map)->bp_offset); \
385 static struct ptp_ocp_eeprom_map fb_eeprom_map[] = {
386 { EEPROM_ENTRY(0x43, board_id) },
387 { EEPROM_ENTRY(0x00, serial), .tag = "mac" },
391 #define bp_assign_entry(bp, res, val) ({ \
392 uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset; \
393 *(typeof(val) *)addr = val; \
396 #define OCP_RES_LOCATION(member) \
397 .name = #member, .bp_offset = offsetof(struct ptp_ocp, member)
399 #define OCP_MEM_RESOURCE(member) \
400 OCP_RES_LOCATION(member), .setup = ptp_ocp_register_mem
402 #define OCP_SERIAL_RESOURCE(member) \
403 OCP_RES_LOCATION(member), .setup = ptp_ocp_register_serial
405 #define OCP_I2C_RESOURCE(member) \
406 OCP_RES_LOCATION(member), .setup = ptp_ocp_register_i2c
408 #define OCP_SPI_RESOURCE(member) \
409 OCP_RES_LOCATION(member), .setup = ptp_ocp_register_spi
411 #define OCP_EXT_RESOURCE(member) \
412 OCP_RES_LOCATION(member), .setup = ptp_ocp_register_ext
414 /* This is the MSI vector mapping used.
423 * 8: HWICAP (notused)
426 * 11: Signal Generator 1
427 * 12: Signal Generator 2
428 * 13: Signal Generator 3
429 * 14: Signal Generator 4
434 static struct ocp_resource ocp_fb_resource[] = {
436 OCP_MEM_RESOURCE(reg),
437 .offset = 0x01000000, .size = 0x10000,
440 OCP_EXT_RESOURCE(ts0),
441 .offset = 0x01010000, .size = 0x10000, .irq_vec = 1,
442 .extra = &(struct ptp_ocp_ext_info) {
444 .irq_fcn = ptp_ocp_ts_irq,
445 .enable = ptp_ocp_ts_enable,
449 OCP_EXT_RESOURCE(ts1),
450 .offset = 0x01020000, .size = 0x10000, .irq_vec = 2,
451 .extra = &(struct ptp_ocp_ext_info) {
453 .irq_fcn = ptp_ocp_ts_irq,
454 .enable = ptp_ocp_ts_enable,
458 OCP_EXT_RESOURCE(ts2),
459 .offset = 0x01060000, .size = 0x10000, .irq_vec = 6,
460 .extra = &(struct ptp_ocp_ext_info) {
462 .irq_fcn = ptp_ocp_ts_irq,
463 .enable = ptp_ocp_ts_enable,
467 OCP_EXT_RESOURCE(ts3),
468 .offset = 0x01110000, .size = 0x10000, .irq_vec = 15,
469 .extra = &(struct ptp_ocp_ext_info) {
471 .irq_fcn = ptp_ocp_ts_irq,
472 .enable = ptp_ocp_ts_enable,
476 OCP_EXT_RESOURCE(ts4),
477 .offset = 0x01120000, .size = 0x10000, .irq_vec = 16,
478 .extra = &(struct ptp_ocp_ext_info) {
480 .irq_fcn = ptp_ocp_ts_irq,
481 .enable = ptp_ocp_ts_enable,
484 /* Timestamp for PHC and/or PPS generator */
486 OCP_EXT_RESOURCE(pps),
487 .offset = 0x010C0000, .size = 0x10000, .irq_vec = 0,
488 .extra = &(struct ptp_ocp_ext_info) {
490 .irq_fcn = ptp_ocp_ts_irq,
491 .enable = ptp_ocp_ts_enable,
495 OCP_EXT_RESOURCE(signal_out[0]),
496 .offset = 0x010D0000, .size = 0x10000, .irq_vec = 11,
497 .extra = &(struct ptp_ocp_ext_info) {
499 .irq_fcn = ptp_ocp_signal_irq,
500 .enable = ptp_ocp_signal_enable,
504 OCP_EXT_RESOURCE(signal_out[1]),
505 .offset = 0x010E0000, .size = 0x10000, .irq_vec = 12,
506 .extra = &(struct ptp_ocp_ext_info) {
508 .irq_fcn = ptp_ocp_signal_irq,
509 .enable = ptp_ocp_signal_enable,
513 OCP_EXT_RESOURCE(signal_out[2]),
514 .offset = 0x010F0000, .size = 0x10000, .irq_vec = 13,
515 .extra = &(struct ptp_ocp_ext_info) {
517 .irq_fcn = ptp_ocp_signal_irq,
518 .enable = ptp_ocp_signal_enable,
522 OCP_EXT_RESOURCE(signal_out[3]),
523 .offset = 0x01100000, .size = 0x10000, .irq_vec = 14,
524 .extra = &(struct ptp_ocp_ext_info) {
526 .irq_fcn = ptp_ocp_signal_irq,
527 .enable = ptp_ocp_signal_enable,
531 OCP_MEM_RESOURCE(pps_to_ext),
532 .offset = 0x01030000, .size = 0x10000,
535 OCP_MEM_RESOURCE(pps_to_clk),
536 .offset = 0x01040000, .size = 0x10000,
539 OCP_MEM_RESOURCE(tod),
540 .offset = 0x01050000, .size = 0x10000,
543 OCP_MEM_RESOURCE(irig_in),
544 .offset = 0x01070000, .size = 0x10000,
547 OCP_MEM_RESOURCE(irig_out),
548 .offset = 0x01080000, .size = 0x10000,
551 OCP_MEM_RESOURCE(dcf_in),
552 .offset = 0x01090000, .size = 0x10000,
555 OCP_MEM_RESOURCE(dcf_out),
556 .offset = 0x010A0000, .size = 0x10000,
559 OCP_MEM_RESOURCE(nmea_out),
560 .offset = 0x010B0000, .size = 0x10000,
563 OCP_MEM_RESOURCE(image),
564 .offset = 0x00020000, .size = 0x1000,
567 OCP_MEM_RESOURCE(pps_select),
568 .offset = 0x00130000, .size = 0x1000,
571 OCP_MEM_RESOURCE(sma_map1),
572 .offset = 0x00140000, .size = 0x1000,
575 OCP_MEM_RESOURCE(sma_map2),
576 .offset = 0x00220000, .size = 0x1000,
579 OCP_I2C_RESOURCE(i2c_ctrl),
580 .offset = 0x00150000, .size = 0x10000, .irq_vec = 7,
581 .extra = &(struct ptp_ocp_i2c_info) {
583 .fixed_rate = 50000000,
584 .data_size = sizeof(struct xiic_i2c_platform_data),
585 .data = &(struct xiic_i2c_platform_data) {
587 .devices = (struct i2c_board_info[]) {
588 { I2C_BOARD_INFO("24c02", 0x50) },
589 { I2C_BOARD_INFO("24mac402", 0x58),
590 .platform_data = "mac" },
596 OCP_SERIAL_RESOURCE(gnss_port),
597 .offset = 0x00160000 + 0x1000, .irq_vec = 3,
600 OCP_SERIAL_RESOURCE(gnss2_port),
601 .offset = 0x00170000 + 0x1000, .irq_vec = 4,
604 OCP_SERIAL_RESOURCE(mac_port),
605 .offset = 0x00180000 + 0x1000, .irq_vec = 5,
608 OCP_SERIAL_RESOURCE(nmea_port),
609 .offset = 0x00190000 + 0x1000, .irq_vec = 10,
612 OCP_SPI_RESOURCE(spi_flash),
613 .offset = 0x00310000, .size = 0x10000, .irq_vec = 9,
614 .extra = &(struct ptp_ocp_flash_info) {
615 .name = "xilinx_spi", .pci_offset = 0,
616 .data_size = sizeof(struct xspi_platform_data),
617 .data = &(struct xspi_platform_data) {
621 .devices = &(struct spi_board_info) {
622 .modalias = "spi-nor",
628 OCP_MEM_RESOURCE(freq_in[0]),
629 .offset = 0x01200000, .size = 0x10000,
632 OCP_MEM_RESOURCE(freq_in[1]),
633 .offset = 0x01210000, .size = 0x10000,
636 OCP_MEM_RESOURCE(freq_in[2]),
637 .offset = 0x01220000, .size = 0x10000,
640 OCP_MEM_RESOURCE(freq_in[3]),
641 .offset = 0x01230000, .size = 0x10000,
644 .setup = ptp_ocp_fb_board_init,
649 static const struct pci_device_id ptp_ocp_pcidev_id[] = {
650 { PCI_DEVICE_DATA(FACEBOOK, TIMECARD, &ocp_fb_resource) },
651 { PCI_DEVICE_DATA(CELESTICA, TIMECARD, &ocp_fb_resource) },
654 MODULE_DEVICE_TABLE(pci, ptp_ocp_pcidev_id);
656 static DEFINE_MUTEX(ptp_ocp_lock);
657 static DEFINE_IDR(ptp_ocp_idr);
659 struct ocp_selector {
664 static const struct ocp_selector ptp_ocp_clock[] = {
665 { .name = "NONE", .value = 0 },
666 { .name = "TOD", .value = 1 },
667 { .name = "IRIG", .value = 2 },
668 { .name = "PPS", .value = 3 },
669 { .name = "PTP", .value = 4 },
670 { .name = "RTC", .value = 5 },
671 { .name = "DCF", .value = 6 },
672 { .name = "REGS", .value = 0xfe },
673 { .name = "EXT", .value = 0xff },
677 #define SMA_ENABLE BIT(15)
678 #define SMA_SELECT_MASK ((1U << 15) - 1)
679 #define SMA_DISABLE 0x10000
681 static const struct ocp_selector ptp_ocp_sma_in[] = {
682 { .name = "10Mhz", .value = 0x0000 },
683 { .name = "PPS1", .value = 0x0001 },
684 { .name = "PPS2", .value = 0x0002 },
685 { .name = "TS1", .value = 0x0004 },
686 { .name = "TS2", .value = 0x0008 },
687 { .name = "IRIG", .value = 0x0010 },
688 { .name = "DCF", .value = 0x0020 },
689 { .name = "TS3", .value = 0x0040 },
690 { .name = "TS4", .value = 0x0080 },
691 { .name = "FREQ1", .value = 0x0100 },
692 { .name = "FREQ2", .value = 0x0200 },
693 { .name = "FREQ3", .value = 0x0400 },
694 { .name = "FREQ4", .value = 0x0800 },
695 { .name = "None", .value = SMA_DISABLE },
699 static const struct ocp_selector ptp_ocp_sma_out[] = {
700 { .name = "10Mhz", .value = 0x0000 },
701 { .name = "PHC", .value = 0x0001 },
702 { .name = "MAC", .value = 0x0002 },
703 { .name = "GNSS1", .value = 0x0004 },
704 { .name = "GNSS2", .value = 0x0008 },
705 { .name = "IRIG", .value = 0x0010 },
706 { .name = "DCF", .value = 0x0020 },
707 { .name = "GEN1", .value = 0x0040 },
708 { .name = "GEN2", .value = 0x0080 },
709 { .name = "GEN3", .value = 0x0100 },
710 { .name = "GEN4", .value = 0x0200 },
711 { .name = "GND", .value = 0x2000 },
712 { .name = "VCC", .value = 0x4000 },
717 const struct ocp_selector *tbl[2];
718 void (*init)(struct ptp_ocp *bp);
719 u32 (*get)(struct ptp_ocp *bp, int sma_nr);
720 int (*set_inputs)(struct ptp_ocp *bp, int sma_nr, u32 val);
721 int (*set_output)(struct ptp_ocp *bp, int sma_nr, u32 val);
725 ptp_ocp_sma_init(struct ptp_ocp *bp)
727 return bp->sma_op->init(bp);
731 ptp_ocp_sma_get(struct ptp_ocp *bp, int sma_nr)
733 return bp->sma_op->get(bp, sma_nr);
737 ptp_ocp_sma_set_inputs(struct ptp_ocp *bp, int sma_nr, u32 val)
739 return bp->sma_op->set_inputs(bp, sma_nr, val);
743 ptp_ocp_sma_set_output(struct ptp_ocp *bp, int sma_nr, u32 val)
745 return bp->sma_op->set_output(bp, sma_nr, val);
749 ptp_ocp_select_name_from_val(const struct ocp_selector *tbl, int val)
753 for (i = 0; tbl[i].name; i++)
754 if (tbl[i].value == val)
760 ptp_ocp_select_val_from_name(const struct ocp_selector *tbl, const char *name)
765 for (i = 0; tbl[i].name; i++) {
766 select = tbl[i].name;
767 if (!strncasecmp(name, select, strlen(select)))
774 ptp_ocp_select_table_show(const struct ocp_selector *tbl, char *buf)
780 for (i = 0; tbl[i].name; i++)
781 count += sysfs_emit_at(buf, count, "%s ", tbl[i].name);
784 count += sysfs_emit_at(buf, count, "\n");
789 __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts,
790 struct ptp_system_timestamp *sts)
792 u32 ctrl, time_sec, time_ns;
795 ptp_read_system_prets(sts);
797 ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE;
798 iowrite32(ctrl, &bp->reg->ctrl);
800 for (i = 0; i < 100; i++) {
801 ctrl = ioread32(&bp->reg->ctrl);
802 if (ctrl & OCP_CTRL_READ_TIME_DONE)
805 ptp_read_system_postts(sts);
807 if (sts && bp->ts_window_adjust) {
808 s64 ns = timespec64_to_ns(&sts->post_ts);
810 sts->post_ts = ns_to_timespec64(ns - bp->ts_window_adjust);
813 time_ns = ioread32(&bp->reg->time_ns);
814 time_sec = ioread32(&bp->reg->time_sec);
816 ts->tv_sec = time_sec;
817 ts->tv_nsec = time_ns;
819 return ctrl & OCP_CTRL_READ_TIME_DONE ? 0 : -ETIMEDOUT;
823 ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts,
824 struct ptp_system_timestamp *sts)
826 struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
830 spin_lock_irqsave(&bp->lock, flags);
831 err = __ptp_ocp_gettime_locked(bp, ts, sts);
832 spin_unlock_irqrestore(&bp->lock, flags);
838 __ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts)
840 u32 ctrl, time_sec, time_ns;
843 time_ns = ts->tv_nsec;
844 time_sec = ts->tv_sec;
846 select = ioread32(&bp->reg->select);
847 iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
849 iowrite32(time_ns, &bp->reg->adjust_ns);
850 iowrite32(time_sec, &bp->reg->adjust_sec);
852 ctrl = OCP_CTRL_ADJUST_TIME | OCP_CTRL_ENABLE;
853 iowrite32(ctrl, &bp->reg->ctrl);
855 /* restore clock selection */
856 iowrite32(select >> 16, &bp->reg->select);
860 ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts)
862 struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
865 spin_lock_irqsave(&bp->lock, flags);
866 __ptp_ocp_settime_locked(bp, ts);
867 spin_unlock_irqrestore(&bp->lock, flags);
873 __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val)
877 select = ioread32(&bp->reg->select);
878 iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
880 iowrite32(adj_val, &bp->reg->offset_ns);
881 iowrite32(NSEC_PER_SEC, &bp->reg->offset_window_ns);
883 ctrl = OCP_CTRL_ADJUST_OFFSET | OCP_CTRL_ENABLE;
884 iowrite32(ctrl, &bp->reg->ctrl);
886 /* restore clock selection */
887 iowrite32(select >> 16, &bp->reg->select);
891 ptp_ocp_adjtime_coarse(struct ptp_ocp *bp, s64 delta_ns)
893 struct timespec64 ts;
897 spin_lock_irqsave(&bp->lock, flags);
898 err = __ptp_ocp_gettime_locked(bp, &ts, NULL);
900 set_normalized_timespec64(&ts, ts.tv_sec,
901 ts.tv_nsec + delta_ns);
902 __ptp_ocp_settime_locked(bp, &ts);
904 spin_unlock_irqrestore(&bp->lock, flags);
908 ptp_ocp_adjtime(struct ptp_clock_info *ptp_info, s64 delta_ns)
910 struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
914 if (delta_ns > NSEC_PER_SEC || -delta_ns > NSEC_PER_SEC) {
915 ptp_ocp_adjtime_coarse(bp, delta_ns);
919 sign = delta_ns < 0 ? BIT(31) : 0;
920 adj_ns = sign ? -delta_ns : delta_ns;
922 spin_lock_irqsave(&bp->lock, flags);
923 __ptp_ocp_adjtime_locked(bp, sign | adj_ns);
924 spin_unlock_irqrestore(&bp->lock, flags);
930 ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm)
939 ptp_ocp_null_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns)
945 ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq,
948 struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
949 struct ptp_ocp_ext_src *ext = NULL;
954 case PTP_CLK_REQ_EXTTS:
955 req = OCP_REQ_TIMESTAMP;
956 switch (rq->extts.index) {
977 case PTP_CLK_REQ_PPS:
981 case PTP_CLK_REQ_PEROUT:
982 switch (rq->perout.index) {
984 /* This is a request for 1PPS on an output SMA.
985 * Allow, but assume manual configuration.
987 if (on && (rq->perout.period.sec != 1 ||
988 rq->perout.period.nsec != 0))
995 req = rq->perout.index - 1;
996 ext = bp->signal_out[req];
997 err = ptp_ocp_signal_from_perout(bp, req, &rq->perout);
1009 err = ext->info->enable(ext, req, on);
1015 ptp_ocp_verify(struct ptp_clock_info *ptp_info, unsigned pin,
1016 enum ptp_pin_function func, unsigned chan)
1018 struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
1023 snprintf(buf, sizeof(buf), "IN: None");
1026 /* Allow timestamps, but require sysfs configuration. */
1029 /* channel 0 is 1PPS from PHC.
1030 * channels 1..4 are the frequency generators.
1033 snprintf(buf, sizeof(buf), "OUT: GEN%d", chan);
1035 snprintf(buf, sizeof(buf), "OUT: PHC");
1041 return ptp_ocp_sma_store(bp, buf, pin + 1);
1044 static const struct ptp_clock_info ptp_ocp_clock_info = {
1045 .owner = THIS_MODULE,
1046 .name = KBUILD_MODNAME,
1047 .max_adj = 100000000,
1048 .gettimex64 = ptp_ocp_gettimex,
1049 .settime64 = ptp_ocp_settime,
1050 .adjtime = ptp_ocp_adjtime,
1051 .adjfine = ptp_ocp_null_adjfine,
1052 .adjphase = ptp_ocp_null_adjphase,
1053 .enable = ptp_ocp_enable,
1054 .verify = ptp_ocp_verify,
1061 __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp)
1065 select = ioread32(&bp->reg->select);
1066 iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
1068 iowrite32(0, &bp->reg->drift_ns);
1070 ctrl = OCP_CTRL_ADJUST_DRIFT | OCP_CTRL_ENABLE;
1071 iowrite32(ctrl, &bp->reg->ctrl);
1073 /* restore clock selection */
1074 iowrite32(select >> 16, &bp->reg->select);
1078 ptp_ocp_utc_distribute(struct ptp_ocp *bp, u32 val)
1080 unsigned long flags;
1082 spin_lock_irqsave(&bp->lock, flags);
1084 bp->utc_tai_offset = val;
1087 iowrite32(val, &bp->irig_out->adj_sec);
1089 iowrite32(val, &bp->dcf_out->adj_sec);
1091 iowrite32(val, &bp->nmea_out->adj_sec);
1093 spin_unlock_irqrestore(&bp->lock, flags);
1097 ptp_ocp_watchdog(struct timer_list *t)
1099 struct ptp_ocp *bp = from_timer(bp, t, watchdog);
1100 unsigned long flags;
1101 u32 status, utc_offset;
1103 status = ioread32(&bp->pps_to_clk->status);
1105 if (status & PPS_STATUS_SUPERV_ERR) {
1106 iowrite32(status, &bp->pps_to_clk->status);
1107 if (!bp->gnss_lost) {
1108 spin_lock_irqsave(&bp->lock, flags);
1109 __ptp_ocp_clear_drift_locked(bp);
1110 spin_unlock_irqrestore(&bp->lock, flags);
1111 bp->gnss_lost = ktime_get_real_seconds();
1114 } else if (bp->gnss_lost) {
1118 /* if GNSS provides correct data we can rely on
1119 * it to get leap second information
1122 status = ioread32(&bp->tod->utc_status);
1123 utc_offset = status & TOD_STATUS_UTC_MASK;
1124 if (status & TOD_STATUS_UTC_VALID &&
1125 utc_offset != bp->utc_tai_offset)
1126 ptp_ocp_utc_distribute(bp, utc_offset);
1129 mod_timer(&bp->watchdog, jiffies + HZ);
1133 ptp_ocp_estimate_pci_timing(struct ptp_ocp *bp)
1139 ctrl = ioread32(&bp->reg->ctrl);
1140 ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE;
1142 iowrite32(ctrl, &bp->reg->ctrl);
1144 start = ktime_get_ns();
1146 ctrl = ioread32(&bp->reg->ctrl);
1148 end = ktime_get_ns();
1150 delay = end - start;
1151 bp->ts_window_adjust = (delay >> 5) * 3;
1155 ptp_ocp_init_clock(struct ptp_ocp *bp)
1157 struct timespec64 ts;
1161 ctrl = OCP_CTRL_ENABLE;
1162 iowrite32(ctrl, &bp->reg->ctrl);
1164 /* NO DRIFT Correction */
1165 /* offset_p:i 1/8, offset_i: 1/16, drift_p: 0, drift_i: 0 */
1166 iowrite32(0x2000, &bp->reg->servo_offset_p);
1167 iowrite32(0x1000, &bp->reg->servo_offset_i);
1168 iowrite32(0, &bp->reg->servo_drift_p);
1169 iowrite32(0, &bp->reg->servo_drift_i);
1171 /* latch servo values */
1172 ctrl |= OCP_CTRL_ADJUST_SERVO;
1173 iowrite32(ctrl, &bp->reg->ctrl);
1175 if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) {
1176 dev_err(&bp->pdev->dev, "clock not enabled\n");
1180 ptp_ocp_estimate_pci_timing(bp);
1182 sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC;
1184 ktime_get_clocktai_ts64(&ts);
1185 ptp_ocp_settime(&bp->ptp_info, &ts);
1188 /* If there is a clock supervisor, then enable the watchdog */
1189 if (bp->pps_to_clk) {
1190 timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0);
1191 mod_timer(&bp->watchdog, jiffies + HZ);
1198 ptp_ocp_tod_init(struct ptp_ocp *bp)
1202 ctrl = ioread32(&bp->tod->ctrl);
1203 ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE;
1204 ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B);
1205 iowrite32(ctrl, &bp->tod->ctrl);
1207 reg = ioread32(&bp->tod->utc_status);
1208 if (reg & TOD_STATUS_UTC_VALID)
1209 ptp_ocp_utc_distribute(bp, reg & TOD_STATUS_UTC_MASK);
1213 ptp_ocp_tod_proto_name(const int idx)
1215 static const char * const proto_name[] = {
1216 "NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none",
1217 "UBX", "UBX_UTC", "UBX_LS", "UBX_none"
1219 return proto_name[idx];
1223 ptp_ocp_tod_gnss_name(int idx)
1225 static const char * const gnss_name[] = {
1226 "ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU",
1229 if (idx >= ARRAY_SIZE(gnss_name))
1230 idx = ARRAY_SIZE(gnss_name) - 1;
1231 return gnss_name[idx];
1234 struct ptp_ocp_nvmem_match_info {
1236 const void * const tag;
1240 ptp_ocp_nvmem_match(struct device *dev, const void *data)
1242 const struct ptp_ocp_nvmem_match_info *info = data;
1245 if (!i2c_verify_client(dev) || info->tag != dev->platform_data)
1248 while ((dev = dev->parent))
1249 if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME))
1250 return info->bp == dev_get_drvdata(dev);
1254 static inline struct nvmem_device *
1255 ptp_ocp_nvmem_device_get(struct ptp_ocp *bp, const void * const tag)
1257 struct ptp_ocp_nvmem_match_info info = { .bp = bp, .tag = tag };
1259 return nvmem_device_find(&info, ptp_ocp_nvmem_match);
1263 ptp_ocp_nvmem_device_put(struct nvmem_device **nvmemp)
1265 if (!IS_ERR_OR_NULL(*nvmemp))
1266 nvmem_device_put(*nvmemp);
1271 ptp_ocp_read_eeprom(struct ptp_ocp *bp)
1273 const struct ptp_ocp_eeprom_map *map;
1274 struct nvmem_device *nvmem;
1284 for (map = bp->eeprom_map; map->len; map++) {
1285 if (map->tag != tag) {
1287 ptp_ocp_nvmem_device_put(&nvmem);
1290 nvmem = ptp_ocp_nvmem_device_get(bp, tag);
1291 if (IS_ERR(nvmem)) {
1292 ret = PTR_ERR(nvmem);
1296 ret = nvmem_device_read(nvmem, map->off, map->len,
1297 BP_MAP_ENTRY_ADDR(bp, map));
1298 if (ret != map->len)
1302 bp->has_eeprom_data = true;
1305 ptp_ocp_nvmem_device_put(&nvmem);
1309 dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", ret);
1314 ptp_ocp_firstchild(struct device *dev, void *data)
1319 static struct device *
1320 ptp_ocp_find_flash(struct ptp_ocp *bp)
1322 struct device *dev, *last;
1325 dev = &bp->spi_flash->dev;
1327 while ((dev = device_find_child(dev, NULL, ptp_ocp_firstchild))) {
1328 if (!strcmp("mtd", dev_bus_name(dev)))
1339 ptp_ocp_devlink_fw_image(struct devlink *devlink, const struct firmware *fw,
1340 const u8 **data, size_t *size)
1342 struct ptp_ocp *bp = devlink_priv(devlink);
1343 const struct ptp_ocp_firmware_header *hdr;
1344 size_t offset, length;
1347 hdr = (const struct ptp_ocp_firmware_header *)fw->data;
1348 if (memcmp(hdr->magic, OCP_FIRMWARE_MAGIC_HEADER, 4)) {
1349 devlink_flash_update_status_notify(devlink,
1350 "No firmware header found, flashing raw image",
1357 if (be16_to_cpu(hdr->pci_vendor_id) != bp->pdev->vendor ||
1358 be16_to_cpu(hdr->pci_device_id) != bp->pdev->device) {
1359 devlink_flash_update_status_notify(devlink,
1360 "Firmware image compatibility check failed",
1365 offset = sizeof(*hdr);
1366 length = be32_to_cpu(hdr->image_size);
1367 if (length != (fw->size - offset)) {
1368 devlink_flash_update_status_notify(devlink,
1369 "Firmware image size check failed",
1374 crc = crc16(0xffff, &fw->data[offset], length);
1375 if (be16_to_cpu(hdr->crc) != crc) {
1376 devlink_flash_update_status_notify(devlink,
1377 "Firmware image CRC check failed",
1383 *data = &fw->data[offset];
1390 ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev,
1391 const struct firmware *fw)
1393 struct mtd_info *mtd = dev_get_drvdata(dev);
1394 struct ptp_ocp *bp = devlink_priv(devlink);
1395 size_t off, len, size, resid, wrote;
1396 struct erase_info erase;
1401 err = ptp_ocp_devlink_fw_image(devlink, fw, &data, &size);
1406 base = bp->flash_start;
1411 devlink_flash_update_status_notify(devlink, "Flashing",
1414 len = min_t(size_t, resid, blksz);
1415 erase.addr = base + off;
1418 err = mtd_erase(mtd, &erase);
1422 err = mtd_write(mtd, base + off, len, &wrote, data + off);
1434 ptp_ocp_devlink_flash_update(struct devlink *devlink,
1435 struct devlink_flash_update_params *params,
1436 struct netlink_ext_ack *extack)
1438 struct ptp_ocp *bp = devlink_priv(devlink);
1443 dev = ptp_ocp_find_flash(bp);
1445 dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n");
1449 devlink_flash_update_status_notify(devlink, "Preparing to flash",
1452 err = ptp_ocp_devlink_flash(devlink, dev, params->fw);
1454 msg = err ? "Flash error" : "Flash complete";
1455 devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0);
1462 ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req,
1463 struct netlink_ext_ack *extack)
1465 struct ptp_ocp *bp = devlink_priv(devlink);
1466 const char *fw_image;
1470 err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
1474 fw_image = bp->fw_loader ? "loader" : "fw";
1475 sprintf(buf, "%d.%d", bp->fw_tag, bp->fw_version);
1476 err = devlink_info_version_running_put(req, fw_image, buf);
1480 if (!bp->has_eeprom_data) {
1481 ptp_ocp_read_eeprom(bp);
1482 if (!bp->has_eeprom_data)
1486 sprintf(buf, "%pM", bp->serial);
1487 err = devlink_info_serial_number_put(req, buf);
1491 err = devlink_info_version_fixed_put(req,
1492 DEVLINK_INFO_VERSION_GENERIC_BOARD_ID,
1500 static const struct devlink_ops ptp_ocp_devlink_ops = {
1501 .flash_update = ptp_ocp_devlink_flash_update,
1502 .info_get = ptp_ocp_devlink_info_get,
1505 static void __iomem *
1506 __ptp_ocp_get_mem(struct ptp_ocp *bp, resource_size_t start, int size)
1508 struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp");
1510 return devm_ioremap_resource(&bp->pdev->dev, &res);
1513 static void __iomem *
1514 ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r)
1516 resource_size_t start;
1518 start = pci_resource_start(bp->pdev, 0) + r->offset;
1519 return __ptp_ocp_get_mem(bp, start, r->size);
1523 ptp_ocp_set_irq_resource(struct resource *res, int irq)
1525 struct resource r = DEFINE_RES_IRQ(irq);
1530 ptp_ocp_set_mem_resource(struct resource *res, resource_size_t start, int size)
1532 struct resource r = DEFINE_RES_MEM(start, size);
1537 ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r)
1539 struct ptp_ocp_flash_info *info;
1540 struct pci_dev *pdev = bp->pdev;
1541 struct platform_device *p;
1542 struct resource res[2];
1543 resource_size_t start;
1546 start = pci_resource_start(pdev, 0) + r->offset;
1547 ptp_ocp_set_mem_resource(&res[0], start, r->size);
1548 ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec));
1551 id = pci_dev_id(pdev) << 1;
1552 id += info->pci_offset;
1554 p = platform_device_register_resndata(&pdev->dev, info->name, id,
1560 bp_assign_entry(bp, r, p);
1565 static struct platform_device *
1566 ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id)
1568 struct ptp_ocp_i2c_info *info;
1569 struct resource res[2];
1570 resource_size_t start;
1573 start = pci_resource_start(pdev, 0) + r->offset;
1574 ptp_ocp_set_mem_resource(&res[0], start, r->size);
1575 ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec));
1577 return platform_device_register_resndata(&pdev->dev, info->name,
1579 info->data, info->data_size);
1583 ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r)
1585 struct pci_dev *pdev = bp->pdev;
1586 struct ptp_ocp_i2c_info *info;
1587 struct platform_device *p;
1593 id = pci_dev_id(bp->pdev);
1595 sprintf(buf, "AXI.%d", id);
1596 clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0,
1599 return PTR_ERR(clk);
1602 sprintf(buf, "%s.%d", info->name, id);
1603 devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf);
1604 p = ptp_ocp_i2c_bus(bp->pdev, r, id);
1608 bp_assign_entry(bp, r, p);
1613 /* The expectation is that this is triggered only on error. */
1615 ptp_ocp_signal_irq(int irq, void *priv)
1617 struct ptp_ocp_ext_src *ext = priv;
1618 struct signal_reg __iomem *reg = ext->mem;
1619 struct ptp_ocp *bp = ext->bp;
1623 gen = ext->info->index - 1;
1625 enable = ioread32(®->enable);
1626 status = ioread32(®->status);
1628 /* disable generator on error */
1629 if (status || !enable) {
1630 iowrite32(0, ®->intr_mask);
1631 iowrite32(0, ®->enable);
1632 bp->signal[gen].running = false;
1635 iowrite32(0, ®->intr); /* ack interrupt */
1641 ptp_ocp_signal_set(struct ptp_ocp *bp, int gen, struct ptp_ocp_signal *s)
1643 struct ptp_system_timestamp sts;
1644 struct timespec64 ts;
1652 s->pulse = ktime_divns(s->period * s->duty, 100);
1654 err = ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts);
1658 start_ns = ktime_set(ts.tv_sec, ts.tv_nsec) + NSEC_PER_MSEC;
1660 /* roundup() does not work on 32-bit systems */
1661 s->start = DIV64_U64_ROUND_UP(start_ns, s->period);
1662 s->start = ktime_add(s->start, s->phase);
1665 if (s->duty < 1 || s->duty > 99)
1668 if (s->pulse < 1 || s->pulse > s->period)
1671 if (s->start < start_ns)
1674 bp->signal[gen] = *s;
1680 ptp_ocp_signal_from_perout(struct ptp_ocp *bp, int gen,
1681 struct ptp_perout_request *req)
1683 struct ptp_ocp_signal s = { };
1685 s.polarity = bp->signal[gen].polarity;
1686 s.period = ktime_set(req->period.sec, req->period.nsec);
1690 if (req->flags & PTP_PEROUT_DUTY_CYCLE) {
1691 s.pulse = ktime_set(req->on.sec, req->on.nsec);
1692 s.duty = ktime_divns(s.pulse * 100, s.period);
1695 if (req->flags & PTP_PEROUT_PHASE)
1696 s.phase = ktime_set(req->phase.sec, req->phase.nsec);
1698 s.start = ktime_set(req->start.sec, req->start.nsec);
1700 return ptp_ocp_signal_set(bp, gen, &s);
1704 ptp_ocp_signal_enable(void *priv, u32 req, bool enable)
1706 struct ptp_ocp_ext_src *ext = priv;
1707 struct signal_reg __iomem *reg = ext->mem;
1708 struct ptp_ocp *bp = ext->bp;
1709 struct timespec64 ts;
1712 gen = ext->info->index - 1;
1714 iowrite32(0, ®->intr_mask);
1715 iowrite32(0, ®->enable);
1716 bp->signal[gen].running = false;
1720 ts = ktime_to_timespec64(bp->signal[gen].start);
1721 iowrite32(ts.tv_sec, ®->start_sec);
1722 iowrite32(ts.tv_nsec, ®->start_ns);
1724 ts = ktime_to_timespec64(bp->signal[gen].period);
1725 iowrite32(ts.tv_sec, ®->period_sec);
1726 iowrite32(ts.tv_nsec, ®->period_ns);
1728 ts = ktime_to_timespec64(bp->signal[gen].pulse);
1729 iowrite32(ts.tv_sec, ®->pulse_sec);
1730 iowrite32(ts.tv_nsec, ®->pulse_ns);
1732 iowrite32(bp->signal[gen].polarity, ®->polarity);
1733 iowrite32(0, ®->repeat_count);
1735 iowrite32(0, ®->intr); /* clear interrupt state */
1736 iowrite32(1, ®->intr_mask); /* enable interrupt */
1737 iowrite32(3, ®->enable); /* valid & enable */
1739 bp->signal[gen].running = true;
1745 ptp_ocp_ts_irq(int irq, void *priv)
1747 struct ptp_ocp_ext_src *ext = priv;
1748 struct ts_reg __iomem *reg = ext->mem;
1749 struct ptp_clock_event ev;
1752 if (ext == ext->bp->pps) {
1753 if (ext->bp->pps_req_map & OCP_REQ_PPS) {
1754 ev.type = PTP_CLOCK_PPS;
1755 ptp_clock_event(ext->bp->ptp, &ev);
1758 if ((ext->bp->pps_req_map & ~OCP_REQ_PPS) == 0)
1762 /* XXX should fix API - this converts s/ns -> ts -> s/ns */
1763 sec = ioread32(®->time_sec);
1764 nsec = ioread32(®->time_ns);
1766 ev.type = PTP_CLOCK_EXTTS;
1767 ev.index = ext->info->index;
1768 ev.timestamp = sec * NSEC_PER_SEC + nsec;
1770 ptp_clock_event(ext->bp->ptp, &ev);
1773 iowrite32(1, ®->intr); /* write 1 to ack */
1779 ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
1781 struct ptp_ocp_ext_src *ext = priv;
1782 struct ts_reg __iomem *reg = ext->mem;
1783 struct ptp_ocp *bp = ext->bp;
1785 if (ext == bp->pps) {
1786 u32 old_map = bp->pps_req_map;
1789 bp->pps_req_map |= req;
1791 bp->pps_req_map &= ~req;
1793 /* if no state change, just return */
1794 if ((!!old_map ^ !!bp->pps_req_map) == 0)
1799 iowrite32(1, ®->enable);
1800 iowrite32(1, ®->intr_mask);
1801 iowrite32(1, ®->intr);
1803 iowrite32(0, ®->intr_mask);
1804 iowrite32(0, ®->enable);
1811 ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext)
1813 ext->info->enable(ext, ~0, false);
1814 pci_free_irq(ext->bp->pdev, ext->irq_vec, ext);
1819 ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r)
1821 struct pci_dev *pdev = bp->pdev;
1822 struct ptp_ocp_ext_src *ext;
1825 ext = kzalloc(sizeof(*ext), GFP_KERNEL);
1829 ext->mem = ptp_ocp_get_mem(bp, r);
1830 if (IS_ERR(ext->mem)) {
1831 err = PTR_ERR(ext->mem);
1836 ext->info = r->extra;
1837 ext->irq_vec = r->irq_vec;
1839 err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL,
1840 ext, "ocp%d.%s", bp->id, r->name);
1842 dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec);
1846 bp_assign_entry(bp, r, ext);
1856 ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r)
1858 struct pci_dev *pdev = bp->pdev;
1859 struct uart_8250_port uart;
1861 /* Setting UPF_IOREMAP and leaving port.membase unspecified lets
1862 * the serial port device claim and release the pci resource.
1864 memset(&uart, 0, sizeof(uart));
1865 uart.port.dev = &pdev->dev;
1866 uart.port.iotype = UPIO_MEM;
1867 uart.port.regshift = 2;
1868 uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset;
1869 uart.port.irq = pci_irq_vector(pdev, r->irq_vec);
1870 uart.port.uartclk = 50000000;
1871 uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP | UPF_NO_THRE_TEST;
1872 uart.port.type = PORT_16550A;
1874 return serial8250_register_8250_port(&uart);
1878 ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r)
1882 port = ptp_ocp_serial_line(bp, r);
1886 bp_assign_entry(bp, r, port);
1892 ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r)
1896 mem = ptp_ocp_get_mem(bp, r);
1898 return PTR_ERR(mem);
1900 bp_assign_entry(bp, r, mem);
1906 ptp_ocp_nmea_out_init(struct ptp_ocp *bp)
1911 iowrite32(0, &bp->nmea_out->ctrl); /* disable */
1912 iowrite32(7, &bp->nmea_out->uart_baud); /* 115200 */
1913 iowrite32(1, &bp->nmea_out->ctrl); /* enable */
1917 _ptp_ocp_signal_init(struct ptp_ocp_signal *s, struct signal_reg __iomem *reg)
1921 iowrite32(0, ®->enable); /* disable */
1923 val = ioread32(®->polarity);
1924 s->polarity = val ? true : false;
1929 ptp_ocp_signal_init(struct ptp_ocp *bp)
1933 for (i = 0; i < 4; i++)
1934 if (bp->signal_out[i])
1935 _ptp_ocp_signal_init(&bp->signal[i],
1936 bp->signal_out[i]->mem);
1940 ptp_ocp_attr_group_del(struct ptp_ocp *bp)
1942 sysfs_remove_groups(&bp->dev.kobj, bp->attr_group);
1943 kfree(bp->attr_group);
1947 ptp_ocp_attr_group_add(struct ptp_ocp *bp,
1948 const struct ocp_attr_group *attr_tbl)
1954 for (i = 0; attr_tbl[i].cap; i++)
1955 if (attr_tbl[i].cap & bp->fw_cap)
1958 bp->attr_group = kcalloc(count + 1, sizeof(struct attribute_group *),
1960 if (!bp->attr_group)
1964 for (i = 0; attr_tbl[i].cap; i++)
1965 if (attr_tbl[i].cap & bp->fw_cap)
1966 bp->attr_group[count++] = attr_tbl[i].group;
1968 err = sysfs_create_groups(&bp->dev.kobj, bp->attr_group);
1970 bp->attr_group[0] = NULL;
1976 ptp_ocp_enable_fpga(u32 __iomem *reg, u32 bit, bool enable)
1981 ctrl = ioread32(reg);
1985 ctrl |= enable ? bit : 0;
1986 iowrite32(ctrl, reg);
1991 ptp_ocp_irig_out(struct ptp_ocp *bp, bool enable)
1993 return ptp_ocp_enable_fpga(&bp->irig_out->ctrl,
1994 IRIG_M_CTRL_ENABLE, enable);
1998 ptp_ocp_irig_in(struct ptp_ocp *bp, bool enable)
2000 return ptp_ocp_enable_fpga(&bp->irig_in->ctrl,
2001 IRIG_S_CTRL_ENABLE, enable);
2005 ptp_ocp_dcf_out(struct ptp_ocp *bp, bool enable)
2007 return ptp_ocp_enable_fpga(&bp->dcf_out->ctrl,
2008 DCF_M_CTRL_ENABLE, enable);
2012 ptp_ocp_dcf_in(struct ptp_ocp *bp, bool enable)
2014 return ptp_ocp_enable_fpga(&bp->dcf_in->ctrl,
2015 DCF_S_CTRL_ENABLE, enable);
2019 __handle_signal_outputs(struct ptp_ocp *bp, u32 val)
2021 ptp_ocp_irig_out(bp, val & 0x00100010);
2022 ptp_ocp_dcf_out(bp, val & 0x00200020);
2026 __handle_signal_inputs(struct ptp_ocp *bp, u32 val)
2028 ptp_ocp_irig_in(bp, val & 0x00100010);
2029 ptp_ocp_dcf_in(bp, val & 0x00200020);
2033 ptp_ocp_sma_fb_get(struct ptp_ocp *bp, int sma_nr)
2038 if (bp->sma[sma_nr - 1].fixed_fcn)
2039 return (sma_nr - 1) & 1;
2041 if (bp->sma[sma_nr - 1].mode == SMA_MODE_IN)
2042 gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1;
2044 gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2;
2045 shift = sma_nr & 1 ? 0 : 16;
2047 return (ioread32(gpio) >> shift) & 0xffff;
2051 ptp_ocp_sma_fb_set_output(struct ptp_ocp *bp, int sma_nr, u32 val)
2053 u32 reg, mask, shift;
2054 unsigned long flags;
2057 gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2;
2058 shift = sma_nr & 1 ? 0 : 16;
2060 mask = 0xffff << (16 - shift);
2062 spin_lock_irqsave(&bp->lock, flags);
2064 reg = ioread32(gpio);
2065 reg = (reg & mask) | (val << shift);
2067 __handle_signal_outputs(bp, reg);
2069 iowrite32(reg, gpio);
2071 spin_unlock_irqrestore(&bp->lock, flags);
2077 ptp_ocp_sma_fb_set_inputs(struct ptp_ocp *bp, int sma_nr, u32 val)
2079 u32 reg, mask, shift;
2080 unsigned long flags;
2083 gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1;
2084 shift = sma_nr & 1 ? 0 : 16;
2086 mask = 0xffff << (16 - shift);
2088 spin_lock_irqsave(&bp->lock, flags);
2090 reg = ioread32(gpio);
2091 reg = (reg & mask) | (val << shift);
2093 __handle_signal_inputs(bp, reg);
2095 iowrite32(reg, gpio);
2097 spin_unlock_irqrestore(&bp->lock, flags);
2103 ptp_ocp_sma_fb_init(struct ptp_ocp *bp)
2109 bp->sma[0].mode = SMA_MODE_IN;
2110 bp->sma[1].mode = SMA_MODE_IN;
2111 bp->sma[2].mode = SMA_MODE_OUT;
2112 bp->sma[3].mode = SMA_MODE_OUT;
2113 for (i = 0; i < 4; i++)
2114 bp->sma[i].default_fcn = i & 1;
2116 /* If no SMA1 map, the pin functions and directions are fixed. */
2117 if (!bp->sma_map1) {
2118 for (i = 0; i < 4; i++) {
2119 bp->sma[i].fixed_fcn = true;
2120 bp->sma[i].fixed_dir = true;
2125 /* If SMA2 GPIO output map is all 1, it is not present.
2126 * This indicates the firmware has fixed direction SMA pins.
2128 reg = ioread32(&bp->sma_map2->gpio2);
2129 if (reg == 0xffffffff) {
2130 for (i = 0; i < 4; i++)
2131 bp->sma[i].fixed_dir = true;
2133 reg = ioread32(&bp->sma_map1->gpio1);
2134 bp->sma[0].mode = reg & BIT(15) ? SMA_MODE_IN : SMA_MODE_OUT;
2135 bp->sma[1].mode = reg & BIT(31) ? SMA_MODE_IN : SMA_MODE_OUT;
2137 reg = ioread32(&bp->sma_map1->gpio2);
2138 bp->sma[2].mode = reg & BIT(15) ? SMA_MODE_OUT : SMA_MODE_IN;
2139 bp->sma[3].mode = reg & BIT(31) ? SMA_MODE_OUT : SMA_MODE_IN;
2143 static const struct ocp_sma_op ocp_fb_sma_op = {
2144 .tbl = { ptp_ocp_sma_in, ptp_ocp_sma_out },
2145 .init = ptp_ocp_sma_fb_init,
2146 .get = ptp_ocp_sma_fb_get,
2147 .set_inputs = ptp_ocp_sma_fb_set_inputs,
2148 .set_output = ptp_ocp_sma_fb_set_output,
2152 ptp_ocp_fb_set_pins(struct ptp_ocp *bp)
2154 struct ptp_pin_desc *config;
2157 config = kzalloc(sizeof(*config) * 4, GFP_KERNEL);
2161 for (i = 0; i < 4; i++) {
2162 sprintf(config[i].name, "sma%d", i + 1);
2163 config[i].index = i;
2166 bp->ptp_info.n_pins = 4;
2167 bp->ptp_info.pin_config = config;
2173 ptp_ocp_fb_set_version(struct ptp_ocp *bp)
2175 u64 cap = OCP_CAP_BASIC;
2178 version = ioread32(&bp->image->version);
2180 /* if lower 16 bits are empty, this is the fw loader. */
2181 if ((version & 0xffff) == 0) {
2182 version = version >> 16;
2183 bp->fw_loader = true;
2186 bp->fw_tag = version >> 15;
2187 bp->fw_version = version & 0x7fff;
2192 cap |= OCP_CAP_SIGNAL | OCP_CAP_FREQ;
2196 cap |= OCP_CAP_SIGNAL;
2198 cap |= OCP_CAP_FREQ;
2204 /* FB specific board initializers; last "resource" registered. */
2206 ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r)
2210 bp->flash_start = 1024 * 4096;
2211 bp->eeprom_map = fb_eeprom_map;
2212 bp->fw_version = ioread32(&bp->image->version);
2213 bp->sma_op = &ocp_fb_sma_op;
2215 ptp_ocp_fb_set_version(bp);
2217 ptp_ocp_tod_init(bp);
2218 ptp_ocp_nmea_out_init(bp);
2219 ptp_ocp_sma_init(bp);
2220 ptp_ocp_signal_init(bp);
2222 err = ptp_ocp_attr_group_add(bp, fb_timecard_groups);
2226 err = ptp_ocp_fb_set_pins(bp);
2230 return ptp_ocp_init_clock(bp);
2234 ptp_ocp_allow_irq(struct ptp_ocp *bp, struct ocp_resource *r)
2236 bool allow = !r->irq_vec || r->irq_vec < bp->n_irqs;
2239 dev_err(&bp->pdev->dev, "irq %d out of range, skipping %s\n",
2240 r->irq_vec, r->name);
2245 ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data)
2247 struct ocp_resource *r, *table;
2250 table = (struct ocp_resource *)driver_data;
2251 for (r = table; r->setup; r++) {
2252 if (!ptp_ocp_allow_irq(bp, r))
2254 err = r->setup(bp, r);
2256 dev_err(&bp->pdev->dev,
2257 "Could not register %s: err %d\n",
2266 ptp_ocp_show_output(const struct ocp_selector *tbl, u32 val, char *buf,
2272 count = sysfs_emit(buf, "OUT: ");
2273 name = ptp_ocp_select_name_from_val(tbl, val);
2275 name = ptp_ocp_select_name_from_val(tbl, def_val);
2276 count += sysfs_emit_at(buf, count, "%s\n", name);
2281 ptp_ocp_show_inputs(const struct ocp_selector *tbl, u32 val, char *buf,
2288 count = sysfs_emit(buf, "IN: ");
2289 for (i = 0; tbl[i].name; i++) {
2290 if (val & tbl[i].value) {
2292 count += sysfs_emit_at(buf, count, "%s ", name);
2295 if (!val && def_val >= 0) {
2296 name = ptp_ocp_select_name_from_val(tbl, def_val);
2297 count += sysfs_emit_at(buf, count, "%s ", name);
2301 count += sysfs_emit_at(buf, count, "\n");
2306 sma_parse_inputs(const struct ocp_selector * const tbl[], const char *buf,
2307 enum ptp_ocp_sma_mode *mode)
2309 int idx, count, dir;
2313 argv = argv_split(GFP_KERNEL, buf, &count);
2322 dir = *mode == SMA_MODE_IN ? 0 : 1;
2323 if (!strcasecmp("IN:", argv[0])) {
2327 if (!strcasecmp("OUT:", argv[0])) {
2331 *mode = dir == 0 ? SMA_MODE_IN : SMA_MODE_OUT;
2334 for (; idx < count; idx++)
2335 ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]);
2345 ptp_ocp_sma_show(struct ptp_ocp *bp, int sma_nr, char *buf,
2346 int default_in_val, int default_out_val)
2348 struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1];
2349 const struct ocp_selector * const *tbl;
2352 tbl = bp->sma_op->tbl;
2353 val = ptp_ocp_sma_get(bp, sma_nr) & SMA_SELECT_MASK;
2355 if (sma->mode == SMA_MODE_IN) {
2358 return ptp_ocp_show_inputs(tbl[0], val, buf, default_in_val);
2361 return ptp_ocp_show_output(tbl[1], val, buf, default_out_val);
2365 sma1_show(struct device *dev, struct device_attribute *attr, char *buf)
2367 struct ptp_ocp *bp = dev_get_drvdata(dev);
2369 return ptp_ocp_sma_show(bp, 1, buf, 0, 1);
2373 sma2_show(struct device *dev, struct device_attribute *attr, char *buf)
2375 struct ptp_ocp *bp = dev_get_drvdata(dev);
2377 return ptp_ocp_sma_show(bp, 2, buf, -1, 1);
2381 sma3_show(struct device *dev, struct device_attribute *attr, char *buf)
2383 struct ptp_ocp *bp = dev_get_drvdata(dev);
2385 return ptp_ocp_sma_show(bp, 3, buf, -1, 0);
2389 sma4_show(struct device *dev, struct device_attribute *attr, char *buf)
2391 struct ptp_ocp *bp = dev_get_drvdata(dev);
2393 return ptp_ocp_sma_show(bp, 4, buf, -1, 1);
2397 ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr)
2399 struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1];
2400 enum ptp_ocp_sma_mode mode;
2404 val = sma_parse_inputs(bp->sma_op->tbl, buf, &mode);
2408 if (sma->fixed_dir && (mode != sma->mode || val & SMA_DISABLE))
2411 if (sma->fixed_fcn) {
2412 if (val != sma->default_fcn)
2417 sma->disabled = !!(val & SMA_DISABLE);
2419 if (mode != sma->mode) {
2420 if (mode == SMA_MODE_IN)
2421 ptp_ocp_sma_set_output(bp, sma_nr, 0);
2423 ptp_ocp_sma_set_inputs(bp, sma_nr, 0);
2427 if (!sma->fixed_dir)
2428 val |= SMA_ENABLE; /* add enable bit */
2433 if (mode == SMA_MODE_IN)
2434 val = ptp_ocp_sma_set_inputs(bp, sma_nr, val);
2436 val = ptp_ocp_sma_set_output(bp, sma_nr, val);
2442 sma1_store(struct device *dev, struct device_attribute *attr,
2443 const char *buf, size_t count)
2445 struct ptp_ocp *bp = dev_get_drvdata(dev);
2448 err = ptp_ocp_sma_store(bp, buf, 1);
2449 return err ? err : count;
2453 sma2_store(struct device *dev, struct device_attribute *attr,
2454 const char *buf, size_t count)
2456 struct ptp_ocp *bp = dev_get_drvdata(dev);
2459 err = ptp_ocp_sma_store(bp, buf, 2);
2460 return err ? err : count;
2464 sma3_store(struct device *dev, struct device_attribute *attr,
2465 const char *buf, size_t count)
2467 struct ptp_ocp *bp = dev_get_drvdata(dev);
2470 err = ptp_ocp_sma_store(bp, buf, 3);
2471 return err ? err : count;
2475 sma4_store(struct device *dev, struct device_attribute *attr,
2476 const char *buf, size_t count)
2478 struct ptp_ocp *bp = dev_get_drvdata(dev);
2481 err = ptp_ocp_sma_store(bp, buf, 4);
2482 return err ? err : count;
2484 static DEVICE_ATTR_RW(sma1);
2485 static DEVICE_ATTR_RW(sma2);
2486 static DEVICE_ATTR_RW(sma3);
2487 static DEVICE_ATTR_RW(sma4);
2490 available_sma_inputs_show(struct device *dev,
2491 struct device_attribute *attr, char *buf)
2493 struct ptp_ocp *bp = dev_get_drvdata(dev);
2495 return ptp_ocp_select_table_show(bp->sma_op->tbl[0], buf);
2497 static DEVICE_ATTR_RO(available_sma_inputs);
2500 available_sma_outputs_show(struct device *dev,
2501 struct device_attribute *attr, char *buf)
2503 struct ptp_ocp *bp = dev_get_drvdata(dev);
2505 return ptp_ocp_select_table_show(bp->sma_op->tbl[1], buf);
2507 static DEVICE_ATTR_RO(available_sma_outputs);
2509 #define EXT_ATTR_RO(_group, _name, _val) \
2510 struct dev_ext_attribute dev_attr_##_group##_val##_##_name = \
2511 { __ATTR_RO(_name), (void *)_val }
2512 #define EXT_ATTR_RW(_group, _name, _val) \
2513 struct dev_ext_attribute dev_attr_##_group##_val##_##_name = \
2514 { __ATTR_RW(_name), (void *)_val }
2515 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
2517 /* period [duty [phase [polarity]]] */
2519 signal_store(struct device *dev, struct device_attribute *attr,
2520 const char *buf, size_t count)
2522 struct dev_ext_attribute *ea = to_ext_attr(attr);
2523 struct ptp_ocp *bp = dev_get_drvdata(dev);
2524 struct ptp_ocp_signal s = { };
2525 int gen = (uintptr_t)ea->var;
2529 argv = argv_split(GFP_KERNEL, buf, &argc);
2534 s.duty = bp->signal[gen].duty;
2535 s.phase = bp->signal[gen].phase;
2536 s.period = bp->signal[gen].period;
2537 s.polarity = bp->signal[gen].polarity;
2542 err = kstrtobool(argv[argc], &s.polarity);
2548 err = kstrtou64(argv[argc], 0, &s.phase);
2554 err = kstrtoint(argv[argc], 0, &s.duty);
2560 err = kstrtou64(argv[argc], 0, &s.period);
2568 err = ptp_ocp_signal_set(bp, gen, &s);
2572 err = ptp_ocp_signal_enable(bp->signal_out[gen], gen, s.period != 0);
2576 return err ? err : count;
2580 signal_show(struct device *dev, struct device_attribute *attr, char *buf)
2582 struct dev_ext_attribute *ea = to_ext_attr(attr);
2583 struct ptp_ocp *bp = dev_get_drvdata(dev);
2584 struct ptp_ocp_signal *signal;
2585 struct timespec64 ts;
2589 i = (uintptr_t)ea->var;
2590 signal = &bp->signal[i];
2592 count = sysfs_emit(buf, "%llu %d %llu %d", signal->period,
2593 signal->duty, signal->phase, signal->polarity);
2595 ts = ktime_to_timespec64(signal->start);
2596 count += sysfs_emit_at(buf, count, " %ptT TAI\n", &ts);
2600 static EXT_ATTR_RW(signal, signal, 0);
2601 static EXT_ATTR_RW(signal, signal, 1);
2602 static EXT_ATTR_RW(signal, signal, 2);
2603 static EXT_ATTR_RW(signal, signal, 3);
2606 duty_show(struct device *dev, struct device_attribute *attr, char *buf)
2608 struct dev_ext_attribute *ea = to_ext_attr(attr);
2609 struct ptp_ocp *bp = dev_get_drvdata(dev);
2610 int i = (uintptr_t)ea->var;
2612 return sysfs_emit(buf, "%d\n", bp->signal[i].duty);
2614 static EXT_ATTR_RO(signal, duty, 0);
2615 static EXT_ATTR_RO(signal, duty, 1);
2616 static EXT_ATTR_RO(signal, duty, 2);
2617 static EXT_ATTR_RO(signal, duty, 3);
2620 period_show(struct device *dev, struct device_attribute *attr, char *buf)
2622 struct dev_ext_attribute *ea = to_ext_attr(attr);
2623 struct ptp_ocp *bp = dev_get_drvdata(dev);
2624 int i = (uintptr_t)ea->var;
2626 return sysfs_emit(buf, "%llu\n", bp->signal[i].period);
2628 static EXT_ATTR_RO(signal, period, 0);
2629 static EXT_ATTR_RO(signal, period, 1);
2630 static EXT_ATTR_RO(signal, period, 2);
2631 static EXT_ATTR_RO(signal, period, 3);
2634 phase_show(struct device *dev, struct device_attribute *attr, char *buf)
2636 struct dev_ext_attribute *ea = to_ext_attr(attr);
2637 struct ptp_ocp *bp = dev_get_drvdata(dev);
2638 int i = (uintptr_t)ea->var;
2640 return sysfs_emit(buf, "%llu\n", bp->signal[i].phase);
2642 static EXT_ATTR_RO(signal, phase, 0);
2643 static EXT_ATTR_RO(signal, phase, 1);
2644 static EXT_ATTR_RO(signal, phase, 2);
2645 static EXT_ATTR_RO(signal, phase, 3);
2648 polarity_show(struct device *dev, struct device_attribute *attr,
2651 struct dev_ext_attribute *ea = to_ext_attr(attr);
2652 struct ptp_ocp *bp = dev_get_drvdata(dev);
2653 int i = (uintptr_t)ea->var;
2655 return sysfs_emit(buf, "%d\n", bp->signal[i].polarity);
2657 static EXT_ATTR_RO(signal, polarity, 0);
2658 static EXT_ATTR_RO(signal, polarity, 1);
2659 static EXT_ATTR_RO(signal, polarity, 2);
2660 static EXT_ATTR_RO(signal, polarity, 3);
2663 running_show(struct device *dev, struct device_attribute *attr, char *buf)
2665 struct dev_ext_attribute *ea = to_ext_attr(attr);
2666 struct ptp_ocp *bp = dev_get_drvdata(dev);
2667 int i = (uintptr_t)ea->var;
2669 return sysfs_emit(buf, "%d\n", bp->signal[i].running);
2671 static EXT_ATTR_RO(signal, running, 0);
2672 static EXT_ATTR_RO(signal, running, 1);
2673 static EXT_ATTR_RO(signal, running, 2);
2674 static EXT_ATTR_RO(signal, running, 3);
2677 start_show(struct device *dev, struct device_attribute *attr, char *buf)
2679 struct dev_ext_attribute *ea = to_ext_attr(attr);
2680 struct ptp_ocp *bp = dev_get_drvdata(dev);
2681 int i = (uintptr_t)ea->var;
2682 struct timespec64 ts;
2684 ts = ktime_to_timespec64(bp->signal[i].start);
2685 return sysfs_emit(buf, "%llu.%lu\n", ts.tv_sec, ts.tv_nsec);
2687 static EXT_ATTR_RO(signal, start, 0);
2688 static EXT_ATTR_RO(signal, start, 1);
2689 static EXT_ATTR_RO(signal, start, 2);
2690 static EXT_ATTR_RO(signal, start, 3);
2693 seconds_store(struct device *dev, struct device_attribute *attr,
2694 const char *buf, size_t count)
2696 struct dev_ext_attribute *ea = to_ext_attr(attr);
2697 struct ptp_ocp *bp = dev_get_drvdata(dev);
2698 int idx = (uintptr_t)ea->var;
2702 err = kstrtou32(buf, 0, &val);
2709 val = (val << 8) | 0x1;
2711 iowrite32(val, &bp->freq_in[idx]->ctrl);
2717 seconds_show(struct device *dev, struct device_attribute *attr, char *buf)
2719 struct dev_ext_attribute *ea = to_ext_attr(attr);
2720 struct ptp_ocp *bp = dev_get_drvdata(dev);
2721 int idx = (uintptr_t)ea->var;
2724 val = ioread32(&bp->freq_in[idx]->ctrl);
2726 val = (val >> 8) & 0xff;
2730 return sysfs_emit(buf, "%u\n", val);
2732 static EXT_ATTR_RW(freq, seconds, 0);
2733 static EXT_ATTR_RW(freq, seconds, 1);
2734 static EXT_ATTR_RW(freq, seconds, 2);
2735 static EXT_ATTR_RW(freq, seconds, 3);
2738 frequency_show(struct device *dev, struct device_attribute *attr, char *buf)
2740 struct dev_ext_attribute *ea = to_ext_attr(attr);
2741 struct ptp_ocp *bp = dev_get_drvdata(dev);
2742 int idx = (uintptr_t)ea->var;
2745 val = ioread32(&bp->freq_in[idx]->status);
2746 if (val & FREQ_STATUS_ERROR)
2747 return sysfs_emit(buf, "error\n");
2748 if (val & FREQ_STATUS_OVERRUN)
2749 return sysfs_emit(buf, "overrun\n");
2750 if (val & FREQ_STATUS_VALID)
2751 return sysfs_emit(buf, "%lu\n", val & FREQ_STATUS_MASK);
2754 static EXT_ATTR_RO(freq, frequency, 0);
2755 static EXT_ATTR_RO(freq, frequency, 1);
2756 static EXT_ATTR_RO(freq, frequency, 2);
2757 static EXT_ATTR_RO(freq, frequency, 3);
2760 serialnum_show(struct device *dev, struct device_attribute *attr, char *buf)
2762 struct ptp_ocp *bp = dev_get_drvdata(dev);
2764 if (!bp->has_eeprom_data)
2765 ptp_ocp_read_eeprom(bp);
2767 return sysfs_emit(buf, "%pM\n", bp->serial);
2769 static DEVICE_ATTR_RO(serialnum);
2772 gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf)
2774 struct ptp_ocp *bp = dev_get_drvdata(dev);
2778 ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost);
2780 ret = sysfs_emit(buf, "SYNC\n");
2784 static DEVICE_ATTR_RO(gnss_sync);
2787 utc_tai_offset_show(struct device *dev,
2788 struct device_attribute *attr, char *buf)
2790 struct ptp_ocp *bp = dev_get_drvdata(dev);
2792 return sysfs_emit(buf, "%d\n", bp->utc_tai_offset);
2796 utc_tai_offset_store(struct device *dev,
2797 struct device_attribute *attr,
2798 const char *buf, size_t count)
2800 struct ptp_ocp *bp = dev_get_drvdata(dev);
2804 err = kstrtou32(buf, 0, &val);
2808 ptp_ocp_utc_distribute(bp, val);
2812 static DEVICE_ATTR_RW(utc_tai_offset);
2815 ts_window_adjust_show(struct device *dev,
2816 struct device_attribute *attr, char *buf)
2818 struct ptp_ocp *bp = dev_get_drvdata(dev);
2820 return sysfs_emit(buf, "%d\n", bp->ts_window_adjust);
2824 ts_window_adjust_store(struct device *dev,
2825 struct device_attribute *attr,
2826 const char *buf, size_t count)
2828 struct ptp_ocp *bp = dev_get_drvdata(dev);
2832 err = kstrtou32(buf, 0, &val);
2836 bp->ts_window_adjust = val;
2840 static DEVICE_ATTR_RW(ts_window_adjust);
2843 irig_b_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
2845 struct ptp_ocp *bp = dev_get_drvdata(dev);
2848 val = ioread32(&bp->irig_out->ctrl);
2849 val = (val >> 16) & 0x07;
2850 return sysfs_emit(buf, "%d\n", val);
2854 irig_b_mode_store(struct device *dev,
2855 struct device_attribute *attr,
2856 const char *buf, size_t count)
2858 struct ptp_ocp *bp = dev_get_drvdata(dev);
2859 unsigned long flags;
2864 err = kstrtou8(buf, 0, &val);
2870 reg = ((val & 0x7) << 16);
2872 spin_lock_irqsave(&bp->lock, flags);
2873 iowrite32(0, &bp->irig_out->ctrl); /* disable */
2874 iowrite32(reg, &bp->irig_out->ctrl); /* change mode */
2875 iowrite32(reg | IRIG_M_CTRL_ENABLE, &bp->irig_out->ctrl);
2876 spin_unlock_irqrestore(&bp->lock, flags);
2880 static DEVICE_ATTR_RW(irig_b_mode);
2883 clock_source_show(struct device *dev, struct device_attribute *attr, char *buf)
2885 struct ptp_ocp *bp = dev_get_drvdata(dev);
2889 select = ioread32(&bp->reg->select);
2890 p = ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16);
2892 return sysfs_emit(buf, "%s\n", p);
2896 clock_source_store(struct device *dev, struct device_attribute *attr,
2897 const char *buf, size_t count)
2899 struct ptp_ocp *bp = dev_get_drvdata(dev);
2900 unsigned long flags;
2903 val = ptp_ocp_select_val_from_name(ptp_ocp_clock, buf);
2907 spin_lock_irqsave(&bp->lock, flags);
2908 iowrite32(val, &bp->reg->select);
2909 spin_unlock_irqrestore(&bp->lock, flags);
2913 static DEVICE_ATTR_RW(clock_source);
2916 available_clock_sources_show(struct device *dev,
2917 struct device_attribute *attr, char *buf)
2919 return ptp_ocp_select_table_show(ptp_ocp_clock, buf);
2921 static DEVICE_ATTR_RO(available_clock_sources);
2924 clock_status_drift_show(struct device *dev,
2925 struct device_attribute *attr, char *buf)
2927 struct ptp_ocp *bp = dev_get_drvdata(dev);
2931 val = ioread32(&bp->reg->status_drift);
2932 res = (val & ~INT_MAX) ? -1 : 1;
2933 res *= (val & INT_MAX);
2934 return sysfs_emit(buf, "%d\n", res);
2936 static DEVICE_ATTR_RO(clock_status_drift);
2939 clock_status_offset_show(struct device *dev,
2940 struct device_attribute *attr, char *buf)
2942 struct ptp_ocp *bp = dev_get_drvdata(dev);
2946 val = ioread32(&bp->reg->status_offset);
2947 res = (val & ~INT_MAX) ? -1 : 1;
2948 res *= (val & INT_MAX);
2949 return sysfs_emit(buf, "%d\n", res);
2951 static DEVICE_ATTR_RO(clock_status_offset);
2954 tod_correction_show(struct device *dev,
2955 struct device_attribute *attr, char *buf)
2957 struct ptp_ocp *bp = dev_get_drvdata(dev);
2961 val = ioread32(&bp->tod->adj_sec);
2962 res = (val & ~INT_MAX) ? -1 : 1;
2963 res *= (val & INT_MAX);
2964 return sysfs_emit(buf, "%d\n", res);
2968 tod_correction_store(struct device *dev, struct device_attribute *attr,
2969 const char *buf, size_t count)
2971 struct ptp_ocp *bp = dev_get_drvdata(dev);
2972 unsigned long flags;
2976 err = kstrtos32(buf, 0, &res);
2985 spin_lock_irqsave(&bp->lock, flags);
2986 iowrite32(val, &bp->tod->adj_sec);
2987 spin_unlock_irqrestore(&bp->lock, flags);
2991 static DEVICE_ATTR_RW(tod_correction);
2993 #define _DEVICE_SIGNAL_GROUP_ATTRS(_nr) \
2994 static struct attribute *fb_timecard_signal##_nr##_attrs[] = { \
2995 &dev_attr_signal##_nr##_signal.attr.attr, \
2996 &dev_attr_signal##_nr##_duty.attr.attr, \
2997 &dev_attr_signal##_nr##_phase.attr.attr, \
2998 &dev_attr_signal##_nr##_period.attr.attr, \
2999 &dev_attr_signal##_nr##_polarity.attr.attr, \
3000 &dev_attr_signal##_nr##_running.attr.attr, \
3001 &dev_attr_signal##_nr##_start.attr.attr, \
3005 #define DEVICE_SIGNAL_GROUP(_name, _nr) \
3006 _DEVICE_SIGNAL_GROUP_ATTRS(_nr); \
3007 static const struct attribute_group \
3008 fb_timecard_signal##_nr##_group = { \
3010 .attrs = fb_timecard_signal##_nr##_attrs, \
3013 DEVICE_SIGNAL_GROUP(gen1, 0);
3014 DEVICE_SIGNAL_GROUP(gen2, 1);
3015 DEVICE_SIGNAL_GROUP(gen3, 2);
3016 DEVICE_SIGNAL_GROUP(gen4, 3);
3018 #define _DEVICE_FREQ_GROUP_ATTRS(_nr) \
3019 static struct attribute *fb_timecard_freq##_nr##_attrs[] = { \
3020 &dev_attr_freq##_nr##_seconds.attr.attr, \
3021 &dev_attr_freq##_nr##_frequency.attr.attr, \
3025 #define DEVICE_FREQ_GROUP(_name, _nr) \
3026 _DEVICE_FREQ_GROUP_ATTRS(_nr); \
3027 static const struct attribute_group \
3028 fb_timecard_freq##_nr##_group = { \
3030 .attrs = fb_timecard_freq##_nr##_attrs, \
3033 DEVICE_FREQ_GROUP(freq1, 0);
3034 DEVICE_FREQ_GROUP(freq2, 1);
3035 DEVICE_FREQ_GROUP(freq3, 2);
3036 DEVICE_FREQ_GROUP(freq4, 3);
3038 static struct attribute *fb_timecard_attrs[] = {
3039 &dev_attr_serialnum.attr,
3040 &dev_attr_gnss_sync.attr,
3041 &dev_attr_clock_source.attr,
3042 &dev_attr_available_clock_sources.attr,
3043 &dev_attr_sma1.attr,
3044 &dev_attr_sma2.attr,
3045 &dev_attr_sma3.attr,
3046 &dev_attr_sma4.attr,
3047 &dev_attr_available_sma_inputs.attr,
3048 &dev_attr_available_sma_outputs.attr,
3049 &dev_attr_clock_status_drift.attr,
3050 &dev_attr_clock_status_offset.attr,
3051 &dev_attr_irig_b_mode.attr,
3052 &dev_attr_utc_tai_offset.attr,
3053 &dev_attr_ts_window_adjust.attr,
3054 &dev_attr_tod_correction.attr,
3057 static const struct attribute_group fb_timecard_group = {
3058 .attrs = fb_timecard_attrs,
3060 static const struct ocp_attr_group fb_timecard_groups[] = {
3061 { .cap = OCP_CAP_BASIC, .group = &fb_timecard_group },
3062 { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal0_group },
3063 { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal1_group },
3064 { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal2_group },
3065 { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal3_group },
3066 { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq0_group },
3067 { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq1_group },
3068 { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq2_group },
3069 { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq3_group },
3074 gpio_input_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit,
3079 for (i = 0; i < 4; i++) {
3080 if (bp->sma[i].mode != SMA_MODE_IN)
3082 if (map[i][0] & (1 << bit)) {
3083 sprintf(buf, "sma%d", i + 1);
3093 gpio_output_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit)
3098 strcpy(ans, "----");
3099 for (i = 0; i < 4; i++) {
3100 if (bp->sma[i].mode != SMA_MODE_OUT)
3102 if (map[i][1] & (1 << bit))
3103 ans += sprintf(ans, "sma%d ", i + 1);
3108 _signal_summary_show(struct seq_file *s, struct ptp_ocp *bp, int nr)
3110 struct signal_reg __iomem *reg = bp->signal_out[nr]->mem;
3111 struct ptp_ocp_signal *signal = &bp->signal[nr];
3119 on = signal->running;
3120 sprintf(label, "GEN%d", nr + 1);
3121 seq_printf(s, "%7s: %s, period:%llu duty:%d%% phase:%llu pol:%d",
3122 label, on ? " ON" : "OFF",
3123 signal->period, signal->duty, signal->phase,
3126 val = ioread32(®->enable);
3127 seq_printf(s, " [%x", val);
3128 val = ioread32(®->status);
3129 seq_printf(s, " %x]", val);
3131 seq_printf(s, " start:%llu\n", signal->start);
3135 _frequency_summary_show(struct seq_file *s, int nr,
3136 struct frequency_reg __iomem *reg)
3145 sprintf(label, "FREQ%d", nr + 1);
3146 val = ioread32(®->ctrl);
3148 val = (val >> 8) & 0xff;
3149 seq_printf(s, "%7s: %s, sec:%u",
3154 val = ioread32(®->status);
3155 if (val & FREQ_STATUS_ERROR)
3156 seq_printf(s, ", error");
3157 if (val & FREQ_STATUS_OVERRUN)
3158 seq_printf(s, ", overrun");
3159 if (val & FREQ_STATUS_VALID)
3160 seq_printf(s, ", freq %lu Hz", val & FREQ_STATUS_MASK);
3161 seq_printf(s, " reg:%x\n", val);
3165 ptp_ocp_summary_show(struct seq_file *s, void *data)
3167 struct device *dev = s->private;
3168 struct ptp_system_timestamp sts;
3169 struct ts_reg __iomem *ts_reg;
3170 char *buf, *src, *mac_src;
3171 struct timespec64 ts;
3178 buf = (char *)__get_free_page(GFP_KERNEL);
3182 bp = dev_get_drvdata(dev);
3184 seq_printf(s, "%7s: /dev/ptp%d\n", "PTP", ptp_clock_index(bp->ptp));
3185 if (bp->gnss_port != -1)
3186 seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS1", bp->gnss_port);
3187 if (bp->gnss2_port != -1)
3188 seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS2", bp->gnss2_port);
3189 if (bp->mac_port != -1)
3190 seq_printf(s, "%7s: /dev/ttyS%d\n", "MAC", bp->mac_port);
3191 if (bp->nmea_port != -1)
3192 seq_printf(s, "%7s: /dev/ttyS%d\n", "NMEA", bp->nmea_port);
3194 memset(sma_val, 0xff, sizeof(sma_val));
3198 reg = ioread32(&bp->sma_map1->gpio1);
3199 sma_val[0][0] = reg & 0xffff;
3200 sma_val[1][0] = reg >> 16;
3202 reg = ioread32(&bp->sma_map1->gpio2);
3203 sma_val[2][1] = reg & 0xffff;
3204 sma_val[3][1] = reg >> 16;
3206 reg = ioread32(&bp->sma_map2->gpio1);
3207 sma_val[2][0] = reg & 0xffff;
3208 sma_val[3][0] = reg >> 16;
3210 reg = ioread32(&bp->sma_map2->gpio2);
3211 sma_val[0][1] = reg & 0xffff;
3212 sma_val[1][1] = reg >> 16;
3215 sma1_show(dev, NULL, buf);
3216 seq_printf(s, " sma1: %04x,%04x %s",
3217 sma_val[0][0], sma_val[0][1], buf);
3219 sma2_show(dev, NULL, buf);
3220 seq_printf(s, " sma2: %04x,%04x %s",
3221 sma_val[1][0], sma_val[1][1], buf);
3223 sma3_show(dev, NULL, buf);
3224 seq_printf(s, " sma3: %04x,%04x %s",
3225 sma_val[2][0], sma_val[2][1], buf);
3227 sma4_show(dev, NULL, buf);
3228 seq_printf(s, " sma4: %04x,%04x %s",
3229 sma_val[3][0], sma_val[3][1], buf);
3232 ts_reg = bp->ts0->mem;
3233 on = ioread32(&ts_reg->enable);
3235 seq_printf(s, "%7s: %s, src: %s\n", "TS0",
3236 on ? " ON" : "OFF", src);
3240 ts_reg = bp->ts1->mem;
3241 on = ioread32(&ts_reg->enable);
3242 gpio_input_map(buf, bp, sma_val, 2, NULL);
3243 seq_printf(s, "%7s: %s, src: %s\n", "TS1",
3244 on ? " ON" : "OFF", buf);
3248 ts_reg = bp->ts2->mem;
3249 on = ioread32(&ts_reg->enable);
3250 gpio_input_map(buf, bp, sma_val, 3, NULL);
3251 seq_printf(s, "%7s: %s, src: %s\n", "TS2",
3252 on ? " ON" : "OFF", buf);
3256 ts_reg = bp->ts3->mem;
3257 on = ioread32(&ts_reg->enable);
3258 gpio_input_map(buf, bp, sma_val, 6, NULL);
3259 seq_printf(s, "%7s: %s, src: %s\n", "TS3",
3260 on ? " ON" : "OFF", buf);
3264 ts_reg = bp->ts4->mem;
3265 on = ioread32(&ts_reg->enable);
3266 gpio_input_map(buf, bp, sma_val, 7, NULL);
3267 seq_printf(s, "%7s: %s, src: %s\n", "TS4",
3268 on ? " ON" : "OFF", buf);
3272 ts_reg = bp->pps->mem;
3274 on = ioread32(&ts_reg->enable);
3275 map = !!(bp->pps_req_map & OCP_REQ_TIMESTAMP);
3276 seq_printf(s, "%7s: %s, src: %s\n", "TS5",
3277 on && map ? " ON" : "OFF", src);
3279 map = !!(bp->pps_req_map & OCP_REQ_PPS);
3280 seq_printf(s, "%7s: %s, src: %s\n", "PPS",
3281 on && map ? " ON" : "OFF", src);
3284 if (bp->fw_cap & OCP_CAP_SIGNAL)
3285 for (i = 0; i < 4; i++)
3286 _signal_summary_show(s, bp, i);
3288 if (bp->fw_cap & OCP_CAP_FREQ)
3289 for (i = 0; i < 4; i++)
3290 _frequency_summary_show(s, i, bp->freq_in[i]);
3293 ctrl = ioread32(&bp->irig_out->ctrl);
3294 on = ctrl & IRIG_M_CTRL_ENABLE;
3295 val = ioread32(&bp->irig_out->status);
3296 gpio_output_map(buf, bp, sma_val, 4);
3297 seq_printf(s, "%7s: %s, error: %d, mode %d, out: %s\n", "IRIG",
3298 on ? " ON" : "OFF", val, (ctrl >> 16), buf);
3302 on = ioread32(&bp->irig_in->ctrl) & IRIG_S_CTRL_ENABLE;
3303 val = ioread32(&bp->irig_in->status);
3304 gpio_input_map(buf, bp, sma_val, 4, NULL);
3305 seq_printf(s, "%7s: %s, error: %d, src: %s\n", "IRIG in",
3306 on ? " ON" : "OFF", val, buf);
3310 on = ioread32(&bp->dcf_out->ctrl) & DCF_M_CTRL_ENABLE;
3311 val = ioread32(&bp->dcf_out->status);
3312 gpio_output_map(buf, bp, sma_val, 5);
3313 seq_printf(s, "%7s: %s, error: %d, out: %s\n", "DCF",
3314 on ? " ON" : "OFF", val, buf);
3318 on = ioread32(&bp->dcf_in->ctrl) & DCF_S_CTRL_ENABLE;
3319 val = ioread32(&bp->dcf_in->status);
3320 gpio_input_map(buf, bp, sma_val, 5, NULL);
3321 seq_printf(s, "%7s: %s, error: %d, src: %s\n", "DCF in",
3322 on ? " ON" : "OFF", val, buf);
3326 on = ioread32(&bp->nmea_out->ctrl) & 1;
3327 val = ioread32(&bp->nmea_out->status);
3328 seq_printf(s, "%7s: %s, error: %d\n", "NMEA",
3329 on ? " ON" : "OFF", val);
3332 /* compute src for PPS1, used below. */
3333 if (bp->pps_select) {
3334 val = ioread32(&bp->pps_select->gpio1);
3338 gpio_input_map(src, bp, sma_val, 0, NULL);
3340 } else if (val & 0x02) {
3342 } else if (val & 0x04) {
3352 seq_printf(s, "MAC PPS1 src: %s\n", mac_src);
3354 gpio_input_map(buf, bp, sma_val, 1, "GNSS2");
3355 seq_printf(s, "MAC PPS2 src: %s\n", buf);
3357 /* assumes automatic switchover/selection */
3358 val = ioread32(&bp->reg->select);
3359 switch (val >> 16) {
3361 sprintf(buf, "----");
3364 sprintf(buf, "IRIG");
3367 sprintf(buf, "%s via PPS1", src);
3370 sprintf(buf, "DCF");
3373 strcpy(buf, "unknown");
3376 val = ioread32(&bp->reg->status);
3377 seq_printf(s, "%7s: %s, state: %s\n", "PHC src", buf,
3378 val & OCP_STATUS_IN_SYNC ? "sync" : "unsynced");
3380 if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts)) {
3381 struct timespec64 sys_ts;
3382 s64 pre_ns, post_ns, ns;
3384 pre_ns = timespec64_to_ns(&sts.pre_ts);
3385 post_ns = timespec64_to_ns(&sts.post_ts);
3386 ns = (pre_ns + post_ns) / 2;
3387 ns += (s64)bp->utc_tai_offset * NSEC_PER_SEC;
3388 sys_ts = ns_to_timespec64(ns);
3390 seq_printf(s, "%7s: %lld.%ld == %ptT TAI\n", "PHC",
3391 ts.tv_sec, ts.tv_nsec, &ts);
3392 seq_printf(s, "%7s: %lld.%ld == %ptT UTC offset %d\n", "SYS",
3393 sys_ts.tv_sec, sys_ts.tv_nsec, &sys_ts,
3394 bp->utc_tai_offset);
3395 seq_printf(s, "%7s: PHC:SYS offset: %lld window: %lld\n", "",
3396 timespec64_to_ns(&ts) - ns,
3400 free_page((unsigned long)buf);
3403 DEFINE_SHOW_ATTRIBUTE(ptp_ocp_summary);
3406 ptp_ocp_tod_status_show(struct seq_file *s, void *data)
3408 struct device *dev = s->private;
3413 bp = dev_get_drvdata(dev);
3415 val = ioread32(&bp->tod->ctrl);
3416 if (!(val & TOD_CTRL_ENABLE)) {
3417 seq_printf(s, "TOD Slave disabled\n");
3420 seq_printf(s, "TOD Slave enabled, Control Register 0x%08X\n", val);
3422 idx = val & TOD_CTRL_PROTOCOL ? 4 : 0;
3423 idx += (val >> 16) & 3;
3424 seq_printf(s, "Protocol %s\n", ptp_ocp_tod_proto_name(idx));
3426 idx = (val >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK;
3427 seq_printf(s, "GNSS %s\n", ptp_ocp_tod_gnss_name(idx));
3429 val = ioread32(&bp->tod->version);
3430 seq_printf(s, "TOD Version %d.%d.%d\n",
3431 val >> 24, (val >> 16) & 0xff, val & 0xffff);
3433 val = ioread32(&bp->tod->status);
3434 seq_printf(s, "Status register: 0x%08X\n", val);
3436 val = ioread32(&bp->tod->adj_sec);
3437 idx = (val & ~INT_MAX) ? -1 : 1;
3438 idx *= (val & INT_MAX);
3439 seq_printf(s, "Correction seconds: %d\n", idx);
3441 val = ioread32(&bp->tod->utc_status);
3442 seq_printf(s, "UTC status register: 0x%08X\n", val);
3443 seq_printf(s, "UTC offset: %d valid:%d\n",
3444 val & TOD_STATUS_UTC_MASK, val & TOD_STATUS_UTC_VALID ? 1 : 0);
3445 seq_printf(s, "Leap second info valid:%d, Leap second announce %d\n",
3446 val & TOD_STATUS_LEAP_VALID ? 1 : 0,
3447 val & TOD_STATUS_LEAP_ANNOUNCE ? 1 : 0);
3449 val = ioread32(&bp->tod->leap);
3450 seq_printf(s, "Time to next leap second (in sec): %d\n", (s32) val);
3454 DEFINE_SHOW_ATTRIBUTE(ptp_ocp_tod_status);
3456 static struct dentry *ptp_ocp_debugfs_root;
3459 ptp_ocp_debugfs_add_device(struct ptp_ocp *bp)
3463 d = debugfs_create_dir(dev_name(&bp->dev), ptp_ocp_debugfs_root);
3465 debugfs_create_file("summary", 0444, bp->debug_root,
3466 &bp->dev, &ptp_ocp_summary_fops);
3468 debugfs_create_file("tod_status", 0444, bp->debug_root,
3469 &bp->dev, &ptp_ocp_tod_status_fops);
3473 ptp_ocp_debugfs_remove_device(struct ptp_ocp *bp)
3475 debugfs_remove_recursive(bp->debug_root);
3479 ptp_ocp_debugfs_init(void)
3481 ptp_ocp_debugfs_root = debugfs_create_dir("timecard", NULL);
3485 ptp_ocp_debugfs_fini(void)
3487 debugfs_remove_recursive(ptp_ocp_debugfs_root);
3491 ptp_ocp_dev_release(struct device *dev)
3493 struct ptp_ocp *bp = dev_get_drvdata(dev);
3495 mutex_lock(&ptp_ocp_lock);
3496 idr_remove(&ptp_ocp_idr, bp->id);
3497 mutex_unlock(&ptp_ocp_lock);
3501 ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev)
3505 mutex_lock(&ptp_ocp_lock);
3506 err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL);
3507 mutex_unlock(&ptp_ocp_lock);
3509 dev_err(&pdev->dev, "idr_alloc failed: %d\n", err);
3514 bp->ptp_info = ptp_ocp_clock_info;
3515 spin_lock_init(&bp->lock);
3517 bp->gnss2_port = -1;
3522 device_initialize(&bp->dev);
3523 dev_set_name(&bp->dev, "ocp%d", bp->id);
3524 bp->dev.class = &timecard_class;
3525 bp->dev.parent = &pdev->dev;
3526 bp->dev.release = ptp_ocp_dev_release;
3527 dev_set_drvdata(&bp->dev, bp);
3529 err = device_add(&bp->dev);
3531 dev_err(&bp->dev, "device add failed: %d\n", err);
3535 pci_set_drvdata(pdev, bp);
3540 ptp_ocp_dev_release(&bp->dev);
3541 put_device(&bp->dev);
3546 ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link)
3548 struct device *dev = &bp->dev;
3550 if (sysfs_create_link(&dev->kobj, &child->kobj, link))
3551 dev_err(dev, "%s symlink failed\n", link);
3555 ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link)
3557 struct device *dev, *child;
3559 dev = &bp->pdev->dev;
3561 child = device_find_child_by_name(dev, name);
3563 dev_err(dev, "Could not find device %s\n", name);
3567 ptp_ocp_symlink(bp, child, link);
3572 ptp_ocp_complete(struct ptp_ocp *bp)
3574 struct pps_device *pps;
3577 if (bp->gnss_port != -1) {
3578 sprintf(buf, "ttyS%d", bp->gnss_port);
3579 ptp_ocp_link_child(bp, buf, "ttyGNSS");
3581 if (bp->gnss2_port != -1) {
3582 sprintf(buf, "ttyS%d", bp->gnss2_port);
3583 ptp_ocp_link_child(bp, buf, "ttyGNSS2");
3585 if (bp->mac_port != -1) {
3586 sprintf(buf, "ttyS%d", bp->mac_port);
3587 ptp_ocp_link_child(bp, buf, "ttyMAC");
3589 if (bp->nmea_port != -1) {
3590 sprintf(buf, "ttyS%d", bp->nmea_port);
3591 ptp_ocp_link_child(bp, buf, "ttyNMEA");
3593 sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp));
3594 ptp_ocp_link_child(bp, buf, "ptp");
3596 pps = pps_lookup_dev(bp->ptp);
3598 ptp_ocp_symlink(bp, pps->dev, "pps");
3600 ptp_ocp_debugfs_add_device(bp);
3606 ptp_ocp_phc_info(struct ptp_ocp *bp)
3608 struct timespec64 ts;
3609 u32 version, select;
3612 version = ioread32(&bp->reg->version);
3613 select = ioread32(&bp->reg->select);
3614 dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n",
3615 version >> 24, (version >> 16) & 0xff, version & 0xffff,
3616 ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16),
3617 ptp_clock_index(bp->ptp));
3619 sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC;
3620 if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL))
3621 dev_info(&bp->pdev->dev, "Time: %lld.%ld, %s\n",
3622 ts.tv_sec, ts.tv_nsec,
3623 sync ? "in-sync" : "UNSYNCED");
3627 ptp_ocp_serial_info(struct device *dev, const char *name, int port, int baud)
3630 dev_info(dev, "%5s: /dev/ttyS%-2d @ %6d\n", name, port, baud);
3634 ptp_ocp_info(struct ptp_ocp *bp)
3636 static int nmea_baud[] = {
3637 1200, 2400, 4800, 9600, 19200, 38400,
3638 57600, 115200, 230400, 460800, 921600,
3641 struct device *dev = &bp->pdev->dev;
3644 ptp_ocp_phc_info(bp);
3646 ptp_ocp_serial_info(dev, "GNSS", bp->gnss_port, 115200);
3647 ptp_ocp_serial_info(dev, "GNSS2", bp->gnss2_port, 115200);
3648 ptp_ocp_serial_info(dev, "MAC", bp->mac_port, 57600);
3649 if (bp->nmea_out && bp->nmea_port != -1) {
3652 reg = ioread32(&bp->nmea_out->uart_baud);
3653 if (reg < ARRAY_SIZE(nmea_baud))
3654 baud = nmea_baud[reg];
3655 ptp_ocp_serial_info(dev, "NMEA", bp->nmea_port, baud);
3660 ptp_ocp_detach_sysfs(struct ptp_ocp *bp)
3662 struct device *dev = &bp->dev;
3664 sysfs_remove_link(&dev->kobj, "ttyGNSS");
3665 sysfs_remove_link(&dev->kobj, "ttyMAC");
3666 sysfs_remove_link(&dev->kobj, "ptp");
3667 sysfs_remove_link(&dev->kobj, "pps");
3671 ptp_ocp_detach(struct ptp_ocp *bp)
3675 ptp_ocp_debugfs_remove_device(bp);
3676 ptp_ocp_detach_sysfs(bp);
3677 ptp_ocp_attr_group_del(bp);
3678 if (timer_pending(&bp->watchdog))
3679 del_timer_sync(&bp->watchdog);
3681 ptp_ocp_unregister_ext(bp->ts0);
3683 ptp_ocp_unregister_ext(bp->ts1);
3685 ptp_ocp_unregister_ext(bp->ts2);
3687 ptp_ocp_unregister_ext(bp->ts3);
3689 ptp_ocp_unregister_ext(bp->ts4);
3691 ptp_ocp_unregister_ext(bp->pps);
3692 for (i = 0; i < 4; i++)
3693 if (bp->signal_out[i])
3694 ptp_ocp_unregister_ext(bp->signal_out[i]);
3695 if (bp->gnss_port != -1)
3696 serial8250_unregister_port(bp->gnss_port);
3697 if (bp->gnss2_port != -1)
3698 serial8250_unregister_port(bp->gnss2_port);
3699 if (bp->mac_port != -1)
3700 serial8250_unregister_port(bp->mac_port);
3701 if (bp->nmea_port != -1)
3702 serial8250_unregister_port(bp->nmea_port);
3704 platform_device_unregister(bp->spi_flash);
3706 platform_device_unregister(bp->i2c_ctrl);
3708 clk_hw_unregister_fixed_rate(bp->i2c_clk);
3710 pci_free_irq_vectors(bp->pdev);
3712 ptp_clock_unregister(bp->ptp);
3713 kfree(bp->ptp_info.pin_config);
3714 device_unregister(&bp->dev);
3718 ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3720 struct devlink *devlink;
3724 devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp), &pdev->dev);
3726 dev_err(&pdev->dev, "devlink_alloc failed\n");
3730 err = pci_enable_device(pdev);
3732 dev_err(&pdev->dev, "pci_enable_device\n");
3736 bp = devlink_priv(devlink);
3737 err = ptp_ocp_device_init(bp, pdev);
3742 * Older FPGA firmware only returns 2 irq's.
3743 * allow this - if not all of the IRQ's are returned, skip the
3744 * extra devices and just register the clock.
3746 err = pci_alloc_irq_vectors(pdev, 1, 17, PCI_IRQ_MSI | PCI_IRQ_MSIX);
3748 dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err);
3752 pci_set_master(pdev);
3754 err = ptp_ocp_register_resources(bp, id->driver_data);
3758 bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev);
3759 if (IS_ERR(bp->ptp)) {
3760 err = PTR_ERR(bp->ptp);
3761 dev_err(&pdev->dev, "ptp_clock_register: %d\n", err);
3766 err = ptp_ocp_complete(bp);
3771 devlink_register(devlink);
3776 pci_set_drvdata(pdev, NULL);
3778 pci_disable_device(pdev);
3780 devlink_free(devlink);
3785 ptp_ocp_remove(struct pci_dev *pdev)
3787 struct ptp_ocp *bp = pci_get_drvdata(pdev);
3788 struct devlink *devlink = priv_to_devlink(bp);
3790 devlink_unregister(devlink);
3792 pci_set_drvdata(pdev, NULL);
3793 pci_disable_device(pdev);
3795 devlink_free(devlink);
3798 static struct pci_driver ptp_ocp_driver = {
3799 .name = KBUILD_MODNAME,
3800 .id_table = ptp_ocp_pcidev_id,
3801 .probe = ptp_ocp_probe,
3802 .remove = ptp_ocp_remove,
3806 ptp_ocp_i2c_notifier_call(struct notifier_block *nb,
3807 unsigned long action, void *data)
3809 struct device *dev, *child = data;
3814 case BUS_NOTIFY_ADD_DEVICE:
3815 case BUS_NOTIFY_DEL_DEVICE:
3816 add = action == BUS_NOTIFY_ADD_DEVICE;
3822 if (!i2c_verify_adapter(child))
3826 while ((dev = dev->parent))
3827 if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME))
3832 bp = dev_get_drvdata(dev);
3834 ptp_ocp_symlink(bp, child, "i2c");
3836 sysfs_remove_link(&bp->dev.kobj, "i2c");
3841 static struct notifier_block ptp_ocp_i2c_notifier = {
3842 .notifier_call = ptp_ocp_i2c_notifier_call,
3851 ptp_ocp_debugfs_init();
3853 what = "timecard class";
3854 err = class_register(&timecard_class);
3858 what = "i2c notifier";
3859 err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
3863 what = "ptp_ocp driver";
3864 err = pci_register_driver(&ptp_ocp_driver);
3871 bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
3873 class_unregister(&timecard_class);
3875 ptp_ocp_debugfs_fini();
3876 pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err);
3883 bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
3884 pci_unregister_driver(&ptp_ocp_driver);
3885 class_unregister(&timecard_class);
3886 ptp_ocp_debugfs_fini();
3889 module_init(ptp_ocp_init);
3890 module_exit(ptp_ocp_fini);
3892 MODULE_DESCRIPTION("OpenCompute TimeCard driver");
3893 MODULE_LICENSE("GPL v2");