1a7e1abadSJonathan Lemon // SPDX-License-Identifier: GPL-2.0-only 2a7e1abadSJonathan Lemon /* Copyright (c) 2020 Facebook */ 3a7e1abadSJonathan Lemon 4a7e1abadSJonathan Lemon #include <linux/err.h> 5a7e1abadSJonathan Lemon #include <linux/kernel.h> 6a7e1abadSJonathan Lemon #include <linux/module.h> 7f67bf662SJonathan Lemon #include <linux/debugfs.h> 8a7e1abadSJonathan Lemon #include <linux/init.h> 9a7e1abadSJonathan Lemon #include <linux/pci.h> 10773bda96SJonathan Lemon #include <linux/serial_8250.h> 11773bda96SJonathan Lemon #include <linux/clkdev.h> 12773bda96SJonathan Lemon #include <linux/clk-provider.h> 13773bda96SJonathan Lemon #include <linux/platform_device.h> 140cfcdd1eSJonathan Lemon #include <linux/platform_data/i2c-xiic.h> 15a7e1abadSJonathan Lemon #include <linux/ptp_clock_kernel.h> 16773bda96SJonathan Lemon #include <linux/spi/spi.h> 17773bda96SJonathan Lemon #include <linux/spi/xilinx_spi.h> 18773bda96SJonathan Lemon #include <net/devlink.h> 19773bda96SJonathan Lemon #include <linux/i2c.h> 20773bda96SJonathan Lemon #include <linux/mtd/mtd.h> 210cfcdd1eSJonathan Lemon #include <linux/nvmem-consumer.h> 22a7e1abadSJonathan Lemon 23773bda96SJonathan Lemon #ifndef PCI_VENDOR_ID_FACEBOOK 24773bda96SJonathan Lemon #define PCI_VENDOR_ID_FACEBOOK 0x1d9b 25773bda96SJonathan Lemon #endif 26773bda96SJonathan Lemon 27773bda96SJonathan Lemon #ifndef PCI_DEVICE_ID_FACEBOOK_TIMECARD 28773bda96SJonathan Lemon #define PCI_DEVICE_ID_FACEBOOK_TIMECARD 0x0400 29773bda96SJonathan Lemon #endif 30773bda96SJonathan Lemon 31773bda96SJonathan Lemon static struct class timecard_class = { 32773bda96SJonathan Lemon .owner = THIS_MODULE, 33773bda96SJonathan Lemon .name = "timecard", 34a7e1abadSJonathan Lemon }; 35a7e1abadSJonathan Lemon 36a7e1abadSJonathan Lemon struct ocp_reg { 37a7e1abadSJonathan Lemon u32 ctrl; 38a7e1abadSJonathan Lemon u32 status; 39a7e1abadSJonathan Lemon u32 select; 40a7e1abadSJonathan Lemon u32 version; 41a7e1abadSJonathan Lemon u32 time_ns; 42a7e1abadSJonathan Lemon u32 time_sec; 43a7e1abadSJonathan Lemon u32 __pad0[2]; 44a7e1abadSJonathan Lemon u32 adjust_ns; 45a7e1abadSJonathan Lemon u32 adjust_sec; 46a7e1abadSJonathan Lemon u32 __pad1[2]; 47a7e1abadSJonathan Lemon u32 offset_ns; 48a7e1abadSJonathan Lemon u32 offset_window_ns; 49773bda96SJonathan Lemon u32 __pad2[2]; 50773bda96SJonathan Lemon u32 drift_ns; 51773bda96SJonathan Lemon u32 drift_window_ns; 52773bda96SJonathan Lemon u32 __pad3[6]; 53773bda96SJonathan Lemon u32 servo_offset_p; 54773bda96SJonathan Lemon u32 servo_offset_i; 55773bda96SJonathan Lemon u32 servo_drift_p; 56773bda96SJonathan Lemon u32 servo_drift_i; 572f23f486SVadim Fedorenko u32 status_offset; 582f23f486SVadim Fedorenko u32 status_drift; 59a7e1abadSJonathan Lemon }; 60a7e1abadSJonathan Lemon 61a7e1abadSJonathan Lemon #define OCP_CTRL_ENABLE BIT(0) 62a7e1abadSJonathan Lemon #define OCP_CTRL_ADJUST_TIME BIT(1) 63a7e1abadSJonathan Lemon #define OCP_CTRL_ADJUST_OFFSET BIT(2) 64773bda96SJonathan Lemon #define OCP_CTRL_ADJUST_DRIFT BIT(3) 65773bda96SJonathan Lemon #define OCP_CTRL_ADJUST_SERVO BIT(8) 66a7e1abadSJonathan Lemon #define OCP_CTRL_READ_TIME_REQ BIT(30) 67a7e1abadSJonathan Lemon #define OCP_CTRL_READ_TIME_DONE BIT(31) 68a7e1abadSJonathan Lemon 69a7e1abadSJonathan Lemon #define OCP_STATUS_IN_SYNC BIT(0) 70773bda96SJonathan Lemon #define OCP_STATUS_IN_HOLDOVER BIT(1) 71a7e1abadSJonathan Lemon 72a7e1abadSJonathan Lemon #define OCP_SELECT_CLK_NONE 0 73773bda96SJonathan Lemon #define OCP_SELECT_CLK_REG 0xfe 74a7e1abadSJonathan Lemon 75a7e1abadSJonathan Lemon struct tod_reg { 76a7e1abadSJonathan Lemon u32 ctrl; 77a7e1abadSJonathan Lemon u32 status; 78a7e1abadSJonathan Lemon u32 uart_polarity; 79a7e1abadSJonathan Lemon u32 version; 80065efcc5SJonathan Lemon u32 adj_sec; 81a7e1abadSJonathan Lemon u32 __pad0[3]; 82a7e1abadSJonathan Lemon u32 uart_baud; 83a7e1abadSJonathan Lemon u32 __pad1[3]; 84a7e1abadSJonathan Lemon u32 utc_status; 85a7e1abadSJonathan Lemon u32 leap; 86a7e1abadSJonathan Lemon }; 87a7e1abadSJonathan Lemon 88a7e1abadSJonathan Lemon #define TOD_CTRL_PROTOCOL BIT(28) 89a7e1abadSJonathan Lemon #define TOD_CTRL_DISABLE_FMT_A BIT(17) 90a7e1abadSJonathan Lemon #define TOD_CTRL_DISABLE_FMT_B BIT(16) 91a7e1abadSJonathan Lemon #define TOD_CTRL_ENABLE BIT(0) 92a7e1abadSJonathan Lemon #define TOD_CTRL_GNSS_MASK ((1U << 4) - 1) 93a7e1abadSJonathan Lemon #define TOD_CTRL_GNSS_SHIFT 24 94a7e1abadSJonathan Lemon 95a7e1abadSJonathan Lemon #define TOD_STATUS_UTC_MASK 0xff 96a7e1abadSJonathan Lemon #define TOD_STATUS_UTC_VALID BIT(8) 979f492c4cSVadim Fedorenko #define TOD_STATUS_LEAP_ANNOUNCE BIT(12) 98a7e1abadSJonathan Lemon #define TOD_STATUS_LEAP_VALID BIT(16) 99a7e1abadSJonathan Lemon 100773bda96SJonathan Lemon struct ts_reg { 101773bda96SJonathan Lemon u32 enable; 102773bda96SJonathan Lemon u32 error; 103773bda96SJonathan Lemon u32 polarity; 104773bda96SJonathan Lemon u32 version; 105773bda96SJonathan Lemon u32 __pad0[4]; 106773bda96SJonathan Lemon u32 cable_delay; 107773bda96SJonathan Lemon u32 __pad1[3]; 108773bda96SJonathan Lemon u32 intr; 109773bda96SJonathan Lemon u32 intr_mask; 110773bda96SJonathan Lemon u32 event_count; 111773bda96SJonathan Lemon u32 __pad2[1]; 112773bda96SJonathan Lemon u32 ts_count; 113773bda96SJonathan Lemon u32 time_ns; 114773bda96SJonathan Lemon u32 time_sec; 115773bda96SJonathan Lemon u32 data_width; 116773bda96SJonathan Lemon u32 data; 117773bda96SJonathan Lemon }; 118773bda96SJonathan Lemon 119773bda96SJonathan Lemon struct pps_reg { 120773bda96SJonathan Lemon u32 ctrl; 121773bda96SJonathan Lemon u32 status; 1220d43d4f2SJonathan Lemon u32 __pad0[6]; 1230d43d4f2SJonathan Lemon u32 cable_delay; 124773bda96SJonathan Lemon }; 125773bda96SJonathan Lemon 126773bda96SJonathan Lemon #define PPS_STATUS_FILTER_ERR BIT(0) 127773bda96SJonathan Lemon #define PPS_STATUS_SUPERV_ERR BIT(1) 128773bda96SJonathan Lemon 129773bda96SJonathan Lemon struct img_reg { 130773bda96SJonathan Lemon u32 version; 131773bda96SJonathan Lemon }; 132773bda96SJonathan Lemon 133e1daf0ecSJonathan Lemon struct gpio_reg { 134e1daf0ecSJonathan Lemon u32 gpio1; 135e1daf0ecSJonathan Lemon u32 __pad0; 136e1daf0ecSJonathan Lemon u32 gpio2; 137e1daf0ecSJonathan Lemon u32 __pad1; 138e1daf0ecSJonathan Lemon }; 139e1daf0ecSJonathan Lemon 1406baf2925SJonathan Lemon struct irig_master_reg { 1416baf2925SJonathan Lemon u32 ctrl; 1426baf2925SJonathan Lemon u32 status; 1436baf2925SJonathan Lemon u32 __pad0; 1446baf2925SJonathan Lemon u32 version; 1456baf2925SJonathan Lemon u32 adj_sec; 1466baf2925SJonathan Lemon u32 mode_ctrl; 1476baf2925SJonathan Lemon }; 1486baf2925SJonathan Lemon 1496baf2925SJonathan Lemon #define IRIG_M_CTRL_ENABLE BIT(0) 1506baf2925SJonathan Lemon 1516baf2925SJonathan Lemon struct irig_slave_reg { 1526baf2925SJonathan Lemon u32 ctrl; 1536baf2925SJonathan Lemon u32 status; 1546baf2925SJonathan Lemon u32 __pad0; 1556baf2925SJonathan Lemon u32 version; 1566baf2925SJonathan Lemon u32 adj_sec; 1576baf2925SJonathan Lemon u32 mode_ctrl; 1586baf2925SJonathan Lemon }; 1596baf2925SJonathan Lemon 1606baf2925SJonathan Lemon #define IRIG_S_CTRL_ENABLE BIT(0) 1616baf2925SJonathan Lemon 1626baf2925SJonathan Lemon struct dcf_master_reg { 1636baf2925SJonathan Lemon u32 ctrl; 1646baf2925SJonathan Lemon u32 status; 1656baf2925SJonathan Lemon u32 __pad0; 1666baf2925SJonathan Lemon u32 version; 1676baf2925SJonathan Lemon u32 adj_sec; 1686baf2925SJonathan Lemon }; 1696baf2925SJonathan Lemon 1706baf2925SJonathan Lemon #define DCF_M_CTRL_ENABLE BIT(0) 1716baf2925SJonathan Lemon 1726baf2925SJonathan Lemon struct dcf_slave_reg { 1736baf2925SJonathan Lemon u32 ctrl; 1746baf2925SJonathan Lemon u32 status; 1756baf2925SJonathan Lemon u32 __pad0; 1766baf2925SJonathan Lemon u32 version; 1776baf2925SJonathan Lemon u32 adj_sec; 1786baf2925SJonathan Lemon }; 1796baf2925SJonathan Lemon 1806baf2925SJonathan Lemon #define DCF_S_CTRL_ENABLE BIT(0) 1816baf2925SJonathan Lemon 182b325af3cSJonathan Lemon struct signal_reg { 183b325af3cSJonathan Lemon u32 enable; 184b325af3cSJonathan Lemon u32 status; 185b325af3cSJonathan Lemon u32 polarity; 186b325af3cSJonathan Lemon u32 version; 187b325af3cSJonathan Lemon u32 __pad0[4]; 188b325af3cSJonathan Lemon u32 cable_delay; 189b325af3cSJonathan Lemon u32 __pad1[3]; 190b325af3cSJonathan Lemon u32 intr; 191b325af3cSJonathan Lemon u32 intr_mask; 192b325af3cSJonathan Lemon u32 __pad2[2]; 193b325af3cSJonathan Lemon u32 start_ns; 194b325af3cSJonathan Lemon u32 start_sec; 195b325af3cSJonathan Lemon u32 pulse_ns; 196b325af3cSJonathan Lemon u32 pulse_sec; 197b325af3cSJonathan Lemon u32 period_ns; 198b325af3cSJonathan Lemon u32 period_sec; 199b325af3cSJonathan Lemon u32 repeat_count; 200b325af3cSJonathan Lemon }; 201b325af3cSJonathan Lemon 202773bda96SJonathan Lemon struct ptp_ocp_flash_info { 203773bda96SJonathan Lemon const char *name; 204773bda96SJonathan Lemon int pci_offset; 205773bda96SJonathan Lemon int data_size; 206773bda96SJonathan Lemon void *data; 207773bda96SJonathan Lemon }; 208773bda96SJonathan Lemon 2091618df6aSJonathan Lemon struct ptp_ocp_i2c_info { 2101618df6aSJonathan Lemon const char *name; 2111618df6aSJonathan Lemon unsigned long fixed_rate; 2121618df6aSJonathan Lemon size_t data_size; 2131618df6aSJonathan Lemon void *data; 2141618df6aSJonathan Lemon }; 2151618df6aSJonathan Lemon 216773bda96SJonathan Lemon struct ptp_ocp_ext_info { 217773bda96SJonathan Lemon int index; 218773bda96SJonathan Lemon irqreturn_t (*irq_fcn)(int irq, void *priv); 219a62a56d0SJonathan Lemon int (*enable)(void *priv, u32 req, bool enable); 220773bda96SJonathan Lemon }; 221773bda96SJonathan Lemon 222773bda96SJonathan Lemon struct ptp_ocp_ext_src { 223773bda96SJonathan Lemon void __iomem *mem; 224773bda96SJonathan Lemon struct ptp_ocp *bp; 225773bda96SJonathan Lemon struct ptp_ocp_ext_info *info; 226773bda96SJonathan Lemon int irq_vec; 227773bda96SJonathan Lemon }; 228773bda96SJonathan Lemon 229a509a7c6SJonathan Lemon enum ptp_ocp_sma_mode { 230a509a7c6SJonathan Lemon SMA_MODE_IN, 231a509a7c6SJonathan Lemon SMA_MODE_OUT, 232a509a7c6SJonathan Lemon }; 233a509a7c6SJonathan Lemon 234a509a7c6SJonathan Lemon struct ptp_ocp_sma_connector { 235a509a7c6SJonathan Lemon enum ptp_ocp_sma_mode mode; 236a509a7c6SJonathan Lemon bool fixed_fcn; 237a509a7c6SJonathan Lemon bool fixed_dir; 238b2c4f0acSJonathan Lemon bool disabled; 239a509a7c6SJonathan Lemon }; 240a509a7c6SJonathan Lemon 241c205d53cSJonathan Lemon struct ocp_attr_group { 242c205d53cSJonathan Lemon u64 cap; 243c205d53cSJonathan Lemon const struct attribute_group *group; 244c205d53cSJonathan Lemon }; 245c205d53cSJonathan Lemon 246c205d53cSJonathan Lemon #define OCP_CAP_BASIC BIT(0) 247b325af3cSJonathan Lemon #define OCP_CAP_SIGNAL BIT(1) 248b325af3cSJonathan Lemon 249b325af3cSJonathan Lemon struct ptp_ocp_signal { 250b325af3cSJonathan Lemon ktime_t period; 251b325af3cSJonathan Lemon ktime_t pulse; 252b325af3cSJonathan Lemon ktime_t phase; 253b325af3cSJonathan Lemon ktime_t start; 254b325af3cSJonathan Lemon int duty; 255b325af3cSJonathan Lemon bool polarity; 256b325af3cSJonathan Lemon bool running; 257b325af3cSJonathan Lemon }; 258c205d53cSJonathan Lemon 2590cfcdd1eSJonathan Lemon #define OCP_BOARD_ID_LEN 13 2600cfcdd1eSJonathan Lemon #define OCP_SERIAL_LEN 6 2610cfcdd1eSJonathan Lemon 262a7e1abadSJonathan Lemon struct ptp_ocp { 263a7e1abadSJonathan Lemon struct pci_dev *pdev; 264773bda96SJonathan Lemon struct device dev; 265a7e1abadSJonathan Lemon spinlock_t lock; 266a7e1abadSJonathan Lemon struct ocp_reg __iomem *reg; 267a7e1abadSJonathan Lemon struct tod_reg __iomem *tod; 2680d43d4f2SJonathan Lemon struct pps_reg __iomem *pps_to_ext; 2690d43d4f2SJonathan Lemon struct pps_reg __iomem *pps_to_clk; 270f67bf662SJonathan Lemon struct gpio_reg __iomem *pps_select; 271a509a7c6SJonathan Lemon struct gpio_reg __iomem *sma_map1; 272a509a7c6SJonathan Lemon struct gpio_reg __iomem *sma_map2; 2736baf2925SJonathan Lemon struct irig_master_reg __iomem *irig_out; 2746baf2925SJonathan Lemon struct irig_slave_reg __iomem *irig_in; 2756baf2925SJonathan Lemon struct dcf_master_reg __iomem *dcf_out; 2766baf2925SJonathan Lemon struct dcf_slave_reg __iomem *dcf_in; 277e3516bb4SJonathan Lemon struct tod_reg __iomem *nmea_out; 278b325af3cSJonathan Lemon struct ptp_ocp_ext_src *signal_out[4]; 279773bda96SJonathan Lemon struct ptp_ocp_ext_src *pps; 280773bda96SJonathan Lemon struct ptp_ocp_ext_src *ts0; 281773bda96SJonathan Lemon struct ptp_ocp_ext_src *ts1; 282dcf61469SJonathan Lemon struct ptp_ocp_ext_src *ts2; 283773bda96SJonathan Lemon struct img_reg __iomem *image; 284a7e1abadSJonathan Lemon struct ptp_clock *ptp; 285a7e1abadSJonathan Lemon struct ptp_clock_info ptp_info; 286773bda96SJonathan Lemon struct platform_device *i2c_ctrl; 287773bda96SJonathan Lemon struct platform_device *spi_flash; 288773bda96SJonathan Lemon struct clk_hw *i2c_clk; 289773bda96SJonathan Lemon struct timer_list watchdog; 290c205d53cSJonathan Lemon const struct ocp_attr_group *attr_tbl; 2910cfcdd1eSJonathan Lemon const struct ptp_ocp_eeprom_map *eeprom_map; 292f67bf662SJonathan Lemon struct dentry *debug_root; 293ef0cfb34SJonathan Lemon time64_t gnss_lost; 294773bda96SJonathan Lemon int id; 295773bda96SJonathan Lemon int n_irqs; 296ef0cfb34SJonathan Lemon int gnss_port; 29771d7e085SJonathan Lemon int gnss2_port; 298773bda96SJonathan Lemon int mac_port; /* miniature atomic clock */ 299e3516bb4SJonathan Lemon int nmea_port; 300b0ca789aSJonathan Lemon u32 fw_version; 3010cfcdd1eSJonathan Lemon u8 board_id[OCP_BOARD_ID_LEN]; 3020cfcdd1eSJonathan Lemon u8 serial[OCP_SERIAL_LEN]; 3030cfcdd1eSJonathan Lemon bool has_eeprom_data; 304a62a56d0SJonathan Lemon u32 pps_req_map; 30589260d87SJonathan Lemon int flash_start; 30689260d87SJonathan Lemon u32 utc_tai_offset; 3071acffc6eSJonathan Lemon u32 ts_window_adjust; 308c205d53cSJonathan Lemon u64 fw_cap; 309b325af3cSJonathan Lemon struct ptp_ocp_signal signal[4]; 310a509a7c6SJonathan Lemon struct ptp_ocp_sma_connector sma[4]; 311a7e1abadSJonathan Lemon }; 312a7e1abadSJonathan Lemon 313a62a56d0SJonathan Lemon #define OCP_REQ_TIMESTAMP BIT(0) 314a62a56d0SJonathan Lemon #define OCP_REQ_PPS BIT(1) 315a62a56d0SJonathan Lemon 316773bda96SJonathan Lemon struct ocp_resource { 317773bda96SJonathan Lemon unsigned long offset; 318773bda96SJonathan Lemon int size; 319773bda96SJonathan Lemon int irq_vec; 320773bda96SJonathan Lemon int (*setup)(struct ptp_ocp *bp, struct ocp_resource *r); 321773bda96SJonathan Lemon void *extra; 322773bda96SJonathan Lemon unsigned long bp_offset; 32356ec4403SJonathan Lemon const char * const name; 324773bda96SJonathan Lemon }; 325773bda96SJonathan Lemon 326773bda96SJonathan Lemon static int ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r); 327773bda96SJonathan Lemon static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r); 328773bda96SJonathan Lemon static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r); 329773bda96SJonathan Lemon static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r); 330773bda96SJonathan Lemon static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r); 331773bda96SJonathan Lemon static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r); 332773bda96SJonathan Lemon static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv); 333b325af3cSJonathan Lemon static irqreturn_t ptp_ocp_signal_irq(int irq, void *priv); 334a62a56d0SJonathan Lemon static int ptp_ocp_ts_enable(void *priv, u32 req, bool enable); 335*1aa66a3aSJonathan Lemon static int ptp_ocp_signal_from_perout(struct ptp_ocp *bp, int gen, 336*1aa66a3aSJonathan Lemon struct ptp_perout_request *req); 337b325af3cSJonathan Lemon static int ptp_ocp_signal_enable(void *priv, u32 req, bool enable); 338b325af3cSJonathan Lemon static int ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr); 339773bda96SJonathan Lemon 340c205d53cSJonathan Lemon static const struct ocp_attr_group fb_timecard_groups[]; 341c205d53cSJonathan Lemon 3420cfcdd1eSJonathan Lemon struct ptp_ocp_eeprom_map { 3430cfcdd1eSJonathan Lemon u16 off; 3440cfcdd1eSJonathan Lemon u16 len; 3450cfcdd1eSJonathan Lemon u32 bp_offset; 3460cfcdd1eSJonathan Lemon const void * const tag; 3470cfcdd1eSJonathan Lemon }; 3480cfcdd1eSJonathan Lemon 3490cfcdd1eSJonathan Lemon #define EEPROM_ENTRY(addr, member) \ 3500cfcdd1eSJonathan Lemon .off = addr, \ 3510cfcdd1eSJonathan Lemon .len = sizeof_field(struct ptp_ocp, member), \ 3520cfcdd1eSJonathan Lemon .bp_offset = offsetof(struct ptp_ocp, member) 3530cfcdd1eSJonathan Lemon 3540cfcdd1eSJonathan Lemon #define BP_MAP_ENTRY_ADDR(bp, map) ({ \ 3550cfcdd1eSJonathan Lemon (void *)((uintptr_t)(bp) + (map)->bp_offset); \ 3560cfcdd1eSJonathan Lemon }) 3570cfcdd1eSJonathan Lemon 3580cfcdd1eSJonathan Lemon static struct ptp_ocp_eeprom_map fb_eeprom_map[] = { 3590cfcdd1eSJonathan Lemon { EEPROM_ENTRY(0x43, board_id) }, 3600cfcdd1eSJonathan Lemon { EEPROM_ENTRY(0x00, serial), .tag = "mac" }, 3610cfcdd1eSJonathan Lemon { } 3620cfcdd1eSJonathan Lemon }; 3630cfcdd1eSJonathan Lemon 364773bda96SJonathan Lemon #define bp_assign_entry(bp, res, val) ({ \ 365773bda96SJonathan Lemon uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset; \ 366773bda96SJonathan Lemon *(typeof(val) *)addr = val; \ 367773bda96SJonathan Lemon }) 368773bda96SJonathan Lemon 369773bda96SJonathan Lemon #define OCP_RES_LOCATION(member) \ 37056ec4403SJonathan Lemon .name = #member, .bp_offset = offsetof(struct ptp_ocp, member) 371773bda96SJonathan Lemon 372773bda96SJonathan Lemon #define OCP_MEM_RESOURCE(member) \ 373773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_mem 374773bda96SJonathan Lemon 375773bda96SJonathan Lemon #define OCP_SERIAL_RESOURCE(member) \ 376773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_serial 377773bda96SJonathan Lemon 378773bda96SJonathan Lemon #define OCP_I2C_RESOURCE(member) \ 379773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_i2c 380773bda96SJonathan Lemon 381773bda96SJonathan Lemon #define OCP_SPI_RESOURCE(member) \ 382773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_spi 383773bda96SJonathan Lemon 384773bda96SJonathan Lemon #define OCP_EXT_RESOURCE(member) \ 385773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_ext 386773bda96SJonathan Lemon 387773bda96SJonathan Lemon /* This is the MSI vector mapping used. 388a62a56d0SJonathan Lemon * 0: TS3 (and PPS) 389773bda96SJonathan Lemon * 1: TS0 390773bda96SJonathan Lemon * 2: TS1 391be69087cSJonathan Lemon * 3: GNSS1 39271d7e085SJonathan Lemon * 4: GNSS2 393773bda96SJonathan Lemon * 5: MAC 394dcf61469SJonathan Lemon * 6: TS2 3951447149dSJonathan Lemon * 7: I2C controller 396e3516bb4SJonathan Lemon * 8: HWICAP (notused) 397773bda96SJonathan Lemon * 9: SPI Flash 398e3516bb4SJonathan Lemon * 10: NMEA 399b325af3cSJonathan Lemon * 11: Signal Generator 1 400b325af3cSJonathan Lemon * 12: Signal Generator 2 401b325af3cSJonathan Lemon * 13: Signal Generator 3 402b325af3cSJonathan Lemon * 14: Signal Generator 4 403773bda96SJonathan Lemon */ 404773bda96SJonathan Lemon 405773bda96SJonathan Lemon static struct ocp_resource ocp_fb_resource[] = { 406773bda96SJonathan Lemon { 407773bda96SJonathan Lemon OCP_MEM_RESOURCE(reg), 408773bda96SJonathan Lemon .offset = 0x01000000, .size = 0x10000, 409773bda96SJonathan Lemon }, 410773bda96SJonathan Lemon { 411773bda96SJonathan Lemon OCP_EXT_RESOURCE(ts0), 412773bda96SJonathan Lemon .offset = 0x01010000, .size = 0x10000, .irq_vec = 1, 413773bda96SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 41456ec4403SJonathan Lemon .index = 0, 415773bda96SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 416773bda96SJonathan Lemon .enable = ptp_ocp_ts_enable, 417773bda96SJonathan Lemon }, 418773bda96SJonathan Lemon }, 419773bda96SJonathan Lemon { 420773bda96SJonathan Lemon OCP_EXT_RESOURCE(ts1), 421773bda96SJonathan Lemon .offset = 0x01020000, .size = 0x10000, .irq_vec = 2, 422773bda96SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 42356ec4403SJonathan Lemon .index = 1, 424773bda96SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 425773bda96SJonathan Lemon .enable = ptp_ocp_ts_enable, 426773bda96SJonathan Lemon }, 427773bda96SJonathan Lemon }, 428773bda96SJonathan Lemon { 429dcf61469SJonathan Lemon OCP_EXT_RESOURCE(ts2), 430dcf61469SJonathan Lemon .offset = 0x01060000, .size = 0x10000, .irq_vec = 6, 431dcf61469SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 432dcf61469SJonathan Lemon .index = 2, 433dcf61469SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 434dcf61469SJonathan Lemon .enable = ptp_ocp_ts_enable, 435dcf61469SJonathan Lemon }, 436dcf61469SJonathan Lemon }, 437dcf61469SJonathan Lemon { 438a62a56d0SJonathan Lemon OCP_EXT_RESOURCE(pps), 439a62a56d0SJonathan Lemon .offset = 0x010C0000, .size = 0x10000, .irq_vec = 0, 440a62a56d0SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 441a62a56d0SJonathan Lemon .index = 3, 442a62a56d0SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 443a62a56d0SJonathan Lemon .enable = ptp_ocp_ts_enable, 444a62a56d0SJonathan Lemon }, 445a62a56d0SJonathan Lemon }, 446a62a56d0SJonathan Lemon { 447b325af3cSJonathan Lemon OCP_EXT_RESOURCE(signal_out[0]), 448b325af3cSJonathan Lemon .offset = 0x010D0000, .size = 0x10000, .irq_vec = 11, 449b325af3cSJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 450b325af3cSJonathan Lemon .index = 1, 451b325af3cSJonathan Lemon .irq_fcn = ptp_ocp_signal_irq, 452b325af3cSJonathan Lemon .enable = ptp_ocp_signal_enable, 453b325af3cSJonathan Lemon }, 454b325af3cSJonathan Lemon }, 455b325af3cSJonathan Lemon { 456b325af3cSJonathan Lemon OCP_EXT_RESOURCE(signal_out[1]), 457b325af3cSJonathan Lemon .offset = 0x010E0000, .size = 0x10000, .irq_vec = 12, 458b325af3cSJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 459b325af3cSJonathan Lemon .index = 2, 460b325af3cSJonathan Lemon .irq_fcn = ptp_ocp_signal_irq, 461b325af3cSJonathan Lemon .enable = ptp_ocp_signal_enable, 462b325af3cSJonathan Lemon }, 463b325af3cSJonathan Lemon }, 464b325af3cSJonathan Lemon { 465b325af3cSJonathan Lemon OCP_EXT_RESOURCE(signal_out[2]), 466b325af3cSJonathan Lemon .offset = 0x010F0000, .size = 0x10000, .irq_vec = 13, 467b325af3cSJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 468b325af3cSJonathan Lemon .index = 3, 469b325af3cSJonathan Lemon .irq_fcn = ptp_ocp_signal_irq, 470b325af3cSJonathan Lemon .enable = ptp_ocp_signal_enable, 471b325af3cSJonathan Lemon }, 472b325af3cSJonathan Lemon }, 473b325af3cSJonathan Lemon { 474b325af3cSJonathan Lemon OCP_EXT_RESOURCE(signal_out[3]), 475b325af3cSJonathan Lemon .offset = 0x01100000, .size = 0x10000, .irq_vec = 14, 476b325af3cSJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 477b325af3cSJonathan Lemon .index = 4, 478b325af3cSJonathan Lemon .irq_fcn = ptp_ocp_signal_irq, 479b325af3cSJonathan Lemon .enable = ptp_ocp_signal_enable, 480b325af3cSJonathan Lemon }, 481b325af3cSJonathan Lemon }, 482b325af3cSJonathan Lemon { 4830d43d4f2SJonathan Lemon OCP_MEM_RESOURCE(pps_to_ext), 4840d43d4f2SJonathan Lemon .offset = 0x01030000, .size = 0x10000, 4850d43d4f2SJonathan Lemon }, 4860d43d4f2SJonathan Lemon { 4870d43d4f2SJonathan Lemon OCP_MEM_RESOURCE(pps_to_clk), 488773bda96SJonathan Lemon .offset = 0x01040000, .size = 0x10000, 489773bda96SJonathan Lemon }, 490773bda96SJonathan Lemon { 491773bda96SJonathan Lemon OCP_MEM_RESOURCE(tod), 492773bda96SJonathan Lemon .offset = 0x01050000, .size = 0x10000, 493773bda96SJonathan Lemon }, 494773bda96SJonathan Lemon { 4956baf2925SJonathan Lemon OCP_MEM_RESOURCE(irig_in), 4966baf2925SJonathan Lemon .offset = 0x01070000, .size = 0x10000, 4976baf2925SJonathan Lemon }, 4986baf2925SJonathan Lemon { 4996baf2925SJonathan Lemon OCP_MEM_RESOURCE(irig_out), 5006baf2925SJonathan Lemon .offset = 0x01080000, .size = 0x10000, 5016baf2925SJonathan Lemon }, 5026baf2925SJonathan Lemon { 5036baf2925SJonathan Lemon OCP_MEM_RESOURCE(dcf_in), 5046baf2925SJonathan Lemon .offset = 0x01090000, .size = 0x10000, 5056baf2925SJonathan Lemon }, 5066baf2925SJonathan Lemon { 5076baf2925SJonathan Lemon OCP_MEM_RESOURCE(dcf_out), 5086baf2925SJonathan Lemon .offset = 0x010A0000, .size = 0x10000, 5096baf2925SJonathan Lemon }, 5106baf2925SJonathan Lemon { 511e3516bb4SJonathan Lemon OCP_MEM_RESOURCE(nmea_out), 512e3516bb4SJonathan Lemon .offset = 0x010B0000, .size = 0x10000, 513e3516bb4SJonathan Lemon }, 514e3516bb4SJonathan Lemon { 515773bda96SJonathan Lemon OCP_MEM_RESOURCE(image), 516773bda96SJonathan Lemon .offset = 0x00020000, .size = 0x1000, 517773bda96SJonathan Lemon }, 518773bda96SJonathan Lemon { 519f67bf662SJonathan Lemon OCP_MEM_RESOURCE(pps_select), 520f67bf662SJonathan Lemon .offset = 0x00130000, .size = 0x1000, 521f67bf662SJonathan Lemon }, 522f67bf662SJonathan Lemon { 523a509a7c6SJonathan Lemon OCP_MEM_RESOURCE(sma_map1), 524e1daf0ecSJonathan Lemon .offset = 0x00140000, .size = 0x1000, 525e1daf0ecSJonathan Lemon }, 526e1daf0ecSJonathan Lemon { 527a509a7c6SJonathan Lemon OCP_MEM_RESOURCE(sma_map2), 528a509a7c6SJonathan Lemon .offset = 0x00220000, .size = 0x1000, 529a509a7c6SJonathan Lemon }, 530a509a7c6SJonathan Lemon { 531773bda96SJonathan Lemon OCP_I2C_RESOURCE(i2c_ctrl), 532773bda96SJonathan Lemon .offset = 0x00150000, .size = 0x10000, .irq_vec = 7, 5331618df6aSJonathan Lemon .extra = &(struct ptp_ocp_i2c_info) { 5341618df6aSJonathan Lemon .name = "xiic-i2c", 5351618df6aSJonathan Lemon .fixed_rate = 50000000, 5360cfcdd1eSJonathan Lemon .data_size = sizeof(struct xiic_i2c_platform_data), 5370cfcdd1eSJonathan Lemon .data = &(struct xiic_i2c_platform_data) { 5380cfcdd1eSJonathan Lemon .num_devices = 2, 5390cfcdd1eSJonathan Lemon .devices = (struct i2c_board_info[]) { 5400cfcdd1eSJonathan Lemon { I2C_BOARD_INFO("24c02", 0x50) }, 5410cfcdd1eSJonathan Lemon { I2C_BOARD_INFO("24mac402", 0x58), 5420cfcdd1eSJonathan Lemon .platform_data = "mac" }, 5430cfcdd1eSJonathan Lemon }, 5440cfcdd1eSJonathan Lemon }, 5451618df6aSJonathan Lemon }, 546773bda96SJonathan Lemon }, 547773bda96SJonathan Lemon { 548ef0cfb34SJonathan Lemon OCP_SERIAL_RESOURCE(gnss_port), 549773bda96SJonathan Lemon .offset = 0x00160000 + 0x1000, .irq_vec = 3, 550773bda96SJonathan Lemon }, 551773bda96SJonathan Lemon { 55271d7e085SJonathan Lemon OCP_SERIAL_RESOURCE(gnss2_port), 55371d7e085SJonathan Lemon .offset = 0x00170000 + 0x1000, .irq_vec = 4, 55471d7e085SJonathan Lemon }, 55571d7e085SJonathan Lemon { 556773bda96SJonathan Lemon OCP_SERIAL_RESOURCE(mac_port), 557773bda96SJonathan Lemon .offset = 0x00180000 + 0x1000, .irq_vec = 5, 558773bda96SJonathan Lemon }, 559773bda96SJonathan Lemon { 560e3516bb4SJonathan Lemon OCP_SERIAL_RESOURCE(nmea_port), 561e3516bb4SJonathan Lemon .offset = 0x00190000 + 0x1000, .irq_vec = 10, 562e3516bb4SJonathan Lemon }, 563e3516bb4SJonathan Lemon { 564773bda96SJonathan Lemon OCP_SPI_RESOURCE(spi_flash), 565773bda96SJonathan Lemon .offset = 0x00310000, .size = 0x10000, .irq_vec = 9, 566773bda96SJonathan Lemon .extra = &(struct ptp_ocp_flash_info) { 567773bda96SJonathan Lemon .name = "xilinx_spi", .pci_offset = 0, 568773bda96SJonathan Lemon .data_size = sizeof(struct xspi_platform_data), 569773bda96SJonathan Lemon .data = &(struct xspi_platform_data) { 570773bda96SJonathan Lemon .num_chipselect = 1, 571773bda96SJonathan Lemon .bits_per_word = 8, 572773bda96SJonathan Lemon .num_devices = 1, 573773bda96SJonathan Lemon .devices = &(struct spi_board_info) { 574773bda96SJonathan Lemon .modalias = "spi-nor", 575773bda96SJonathan Lemon }, 576773bda96SJonathan Lemon }, 577773bda96SJonathan Lemon }, 578773bda96SJonathan Lemon }, 579773bda96SJonathan Lemon { 580773bda96SJonathan Lemon .setup = ptp_ocp_fb_board_init, 581773bda96SJonathan Lemon }, 582773bda96SJonathan Lemon { } 583773bda96SJonathan Lemon }; 584773bda96SJonathan Lemon 585773bda96SJonathan Lemon static const struct pci_device_id ptp_ocp_pcidev_id[] = { 586773bda96SJonathan Lemon { PCI_DEVICE_DATA(FACEBOOK, TIMECARD, &ocp_fb_resource) }, 587773bda96SJonathan Lemon { 0 } 588773bda96SJonathan Lemon }; 589773bda96SJonathan Lemon MODULE_DEVICE_TABLE(pci, ptp_ocp_pcidev_id); 590773bda96SJonathan Lemon 591773bda96SJonathan Lemon static DEFINE_MUTEX(ptp_ocp_lock); 592773bda96SJonathan Lemon static DEFINE_IDR(ptp_ocp_idr); 593773bda96SJonathan Lemon 594e1daf0ecSJonathan Lemon struct ocp_selector { 595773bda96SJonathan Lemon const char *name; 596773bda96SJonathan Lemon int value; 597e1daf0ecSJonathan Lemon }; 598e1daf0ecSJonathan Lemon 599e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_clock[] = { 600773bda96SJonathan Lemon { .name = "NONE", .value = 0 }, 601773bda96SJonathan Lemon { .name = "TOD", .value = 1 }, 602773bda96SJonathan Lemon { .name = "IRIG", .value = 2 }, 603773bda96SJonathan Lemon { .name = "PPS", .value = 3 }, 604773bda96SJonathan Lemon { .name = "PTP", .value = 4 }, 605773bda96SJonathan Lemon { .name = "RTC", .value = 5 }, 606773bda96SJonathan Lemon { .name = "DCF", .value = 6 }, 607773bda96SJonathan Lemon { .name = "REGS", .value = 0xfe }, 608773bda96SJonathan Lemon { .name = "EXT", .value = 0xff }, 609e1daf0ecSJonathan Lemon { } 610e1daf0ecSJonathan Lemon }; 611e1daf0ecSJonathan Lemon 612a509a7c6SJonathan Lemon #define SMA_ENABLE BIT(15) 613a509a7c6SJonathan Lemon #define SMA_SELECT_MASK ((1U << 15) - 1) 614b2c4f0acSJonathan Lemon #define SMA_DISABLE 0x10000 615a509a7c6SJonathan Lemon 616e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_sma_in[] = { 617e1daf0ecSJonathan Lemon { .name = "10Mhz", .value = 0x00 }, 618e1daf0ecSJonathan Lemon { .name = "PPS1", .value = 0x01 }, 619e1daf0ecSJonathan Lemon { .name = "PPS2", .value = 0x02 }, 620e1daf0ecSJonathan Lemon { .name = "TS1", .value = 0x04 }, 621e1daf0ecSJonathan Lemon { .name = "TS2", .value = 0x08 }, 6226baf2925SJonathan Lemon { .name = "IRIG", .value = 0x10 }, 6236baf2925SJonathan Lemon { .name = "DCF", .value = 0x20 }, 624b2c4f0acSJonathan Lemon { .name = "None", .value = SMA_DISABLE }, 625e1daf0ecSJonathan Lemon { } 626e1daf0ecSJonathan Lemon }; 627e1daf0ecSJonathan Lemon 628e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_sma_out[] = { 629b325af3cSJonathan Lemon { .name = "10Mhz", .value = 0x0000 }, 630b325af3cSJonathan Lemon { .name = "PHC", .value = 0x0001 }, 631b325af3cSJonathan Lemon { .name = "MAC", .value = 0x0002 }, 632b325af3cSJonathan Lemon { .name = "GNSS1", .value = 0x0004 }, 633b325af3cSJonathan Lemon { .name = "GNSS2", .value = 0x0008 }, 634b325af3cSJonathan Lemon { .name = "IRIG", .value = 0x0010 }, 635b325af3cSJonathan Lemon { .name = "DCF", .value = 0x0020 }, 636b325af3cSJonathan Lemon { .name = "GEN1", .value = 0x0040 }, 637b325af3cSJonathan Lemon { .name = "GEN2", .value = 0x0080 }, 638b325af3cSJonathan Lemon { .name = "GEN3", .value = 0x0100 }, 639b325af3cSJonathan Lemon { .name = "GEN4", .value = 0x0200 }, 640cd09193fSJonathan Lemon { .name = "GND", .value = 0x2000 }, 641cd09193fSJonathan Lemon { .name = "VCC", .value = 0x4000 }, 642e1daf0ecSJonathan Lemon { } 643773bda96SJonathan Lemon }; 644773bda96SJonathan Lemon 645773bda96SJonathan Lemon static const char * 646e1daf0ecSJonathan Lemon ptp_ocp_select_name_from_val(struct ocp_selector *tbl, int val) 647773bda96SJonathan Lemon { 648773bda96SJonathan Lemon int i; 649773bda96SJonathan Lemon 650e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) 651e1daf0ecSJonathan Lemon if (tbl[i].value == val) 652e1daf0ecSJonathan Lemon return tbl[i].name; 653773bda96SJonathan Lemon return NULL; 654773bda96SJonathan Lemon } 655773bda96SJonathan Lemon 656773bda96SJonathan Lemon static int 657e1daf0ecSJonathan Lemon ptp_ocp_select_val_from_name(struct ocp_selector *tbl, const char *name) 658773bda96SJonathan Lemon { 659e1daf0ecSJonathan Lemon const char *select; 660773bda96SJonathan Lemon int i; 661773bda96SJonathan Lemon 662e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) { 663e1daf0ecSJonathan Lemon select = tbl[i].name; 664e1daf0ecSJonathan Lemon if (!strncasecmp(name, select, strlen(select))) 665e1daf0ecSJonathan Lemon return tbl[i].value; 666773bda96SJonathan Lemon } 667773bda96SJonathan Lemon return -EINVAL; 668773bda96SJonathan Lemon } 669773bda96SJonathan Lemon 670e1daf0ecSJonathan Lemon static ssize_t 671e1daf0ecSJonathan Lemon ptp_ocp_select_table_show(struct ocp_selector *tbl, char *buf) 672e1daf0ecSJonathan Lemon { 673e1daf0ecSJonathan Lemon ssize_t count; 674e1daf0ecSJonathan Lemon int i; 675e1daf0ecSJonathan Lemon 676e1daf0ecSJonathan Lemon count = 0; 677e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) 678e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", tbl[i].name); 679e1daf0ecSJonathan Lemon if (count) 680e1daf0ecSJonathan Lemon count--; 681e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "\n"); 682e1daf0ecSJonathan Lemon return count; 683e1daf0ecSJonathan Lemon } 684e1daf0ecSJonathan Lemon 685a7e1abadSJonathan Lemon static int 686a7e1abadSJonathan Lemon __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts, 687a7e1abadSJonathan Lemon struct ptp_system_timestamp *sts) 688a7e1abadSJonathan Lemon { 689a7e1abadSJonathan Lemon u32 ctrl, time_sec, time_ns; 690a7e1abadSJonathan Lemon int i; 691a7e1abadSJonathan Lemon 692a7e1abadSJonathan Lemon ptp_read_system_prets(sts); 6931acffc6eSJonathan Lemon 6941acffc6eSJonathan Lemon ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE; 695a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 696a7e1abadSJonathan Lemon 697a7e1abadSJonathan Lemon for (i = 0; i < 100; i++) { 698a7e1abadSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 699a7e1abadSJonathan Lemon if (ctrl & OCP_CTRL_READ_TIME_DONE) 700a7e1abadSJonathan Lemon break; 701a7e1abadSJonathan Lemon } 702a7e1abadSJonathan Lemon ptp_read_system_postts(sts); 703a7e1abadSJonathan Lemon 7041acffc6eSJonathan Lemon if (sts && bp->ts_window_adjust) { 7051acffc6eSJonathan Lemon s64 ns = timespec64_to_ns(&sts->post_ts); 7061acffc6eSJonathan Lemon 7071acffc6eSJonathan Lemon sts->post_ts = ns_to_timespec64(ns - bp->ts_window_adjust); 7081acffc6eSJonathan Lemon } 7091acffc6eSJonathan Lemon 710a7e1abadSJonathan Lemon time_ns = ioread32(&bp->reg->time_ns); 711a7e1abadSJonathan Lemon time_sec = ioread32(&bp->reg->time_sec); 712a7e1abadSJonathan Lemon 713a7e1abadSJonathan Lemon ts->tv_sec = time_sec; 714a7e1abadSJonathan Lemon ts->tv_nsec = time_ns; 715a7e1abadSJonathan Lemon 716a7e1abadSJonathan Lemon return ctrl & OCP_CTRL_READ_TIME_DONE ? 0 : -ETIMEDOUT; 717a7e1abadSJonathan Lemon } 718a7e1abadSJonathan Lemon 719a7e1abadSJonathan Lemon static int 720a7e1abadSJonathan Lemon ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts, 721a7e1abadSJonathan Lemon struct ptp_system_timestamp *sts) 722a7e1abadSJonathan Lemon { 723a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 724a7e1abadSJonathan Lemon unsigned long flags; 725a7e1abadSJonathan Lemon int err; 726a7e1abadSJonathan Lemon 727a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 728a7e1abadSJonathan Lemon err = __ptp_ocp_gettime_locked(bp, ts, sts); 729a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 730a7e1abadSJonathan Lemon 731a7e1abadSJonathan Lemon return err; 732a7e1abadSJonathan Lemon } 733a7e1abadSJonathan Lemon 734a7e1abadSJonathan Lemon static void 735a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts) 736a7e1abadSJonathan Lemon { 737a7e1abadSJonathan Lemon u32 ctrl, time_sec, time_ns; 738a7e1abadSJonathan Lemon u32 select; 739a7e1abadSJonathan Lemon 740a7e1abadSJonathan Lemon time_ns = ts->tv_nsec; 741a7e1abadSJonathan Lemon time_sec = ts->tv_sec; 742a7e1abadSJonathan Lemon 743a7e1abadSJonathan Lemon select = ioread32(&bp->reg->select); 744a7e1abadSJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 745a7e1abadSJonathan Lemon 746a7e1abadSJonathan Lemon iowrite32(time_ns, &bp->reg->adjust_ns); 747a7e1abadSJonathan Lemon iowrite32(time_sec, &bp->reg->adjust_sec); 748a7e1abadSJonathan Lemon 7491acffc6eSJonathan Lemon ctrl = OCP_CTRL_ADJUST_TIME | OCP_CTRL_ENABLE; 750a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 751a7e1abadSJonathan Lemon 752a7e1abadSJonathan Lemon /* restore clock selection */ 753a7e1abadSJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 754a7e1abadSJonathan Lemon } 755a7e1abadSJonathan Lemon 756a7e1abadSJonathan Lemon static int 757a7e1abadSJonathan Lemon ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts) 758a7e1abadSJonathan Lemon { 759a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 760a7e1abadSJonathan Lemon unsigned long flags; 761a7e1abadSJonathan Lemon 762a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 763a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(bp, ts); 764a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 765a7e1abadSJonathan Lemon 766a7e1abadSJonathan Lemon return 0; 767a7e1abadSJonathan Lemon } 768a7e1abadSJonathan Lemon 7696d59d4faSJonathan Lemon static void 77090f8f4c0SJonathan Lemon __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val) 7716d59d4faSJonathan Lemon { 7726d59d4faSJonathan Lemon u32 select, ctrl; 7736d59d4faSJonathan Lemon 7746d59d4faSJonathan Lemon select = ioread32(&bp->reg->select); 7756d59d4faSJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 7766d59d4faSJonathan Lemon 7776d59d4faSJonathan Lemon iowrite32(adj_val, &bp->reg->offset_ns); 77890f8f4c0SJonathan Lemon iowrite32(NSEC_PER_SEC, &bp->reg->offset_window_ns); 7796d59d4faSJonathan Lemon 7806d59d4faSJonathan Lemon ctrl = OCP_CTRL_ADJUST_OFFSET | OCP_CTRL_ENABLE; 7816d59d4faSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 7826d59d4faSJonathan Lemon 7836d59d4faSJonathan Lemon /* restore clock selection */ 7846d59d4faSJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 7856d59d4faSJonathan Lemon } 7866d59d4faSJonathan Lemon 78790f8f4c0SJonathan Lemon static void 78890f8f4c0SJonathan Lemon ptp_ocp_adjtime_coarse(struct ptp_ocp *bp, u64 delta_ns) 78990f8f4c0SJonathan Lemon { 79090f8f4c0SJonathan Lemon struct timespec64 ts; 79190f8f4c0SJonathan Lemon unsigned long flags; 79290f8f4c0SJonathan Lemon int err; 79390f8f4c0SJonathan Lemon 79490f8f4c0SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 79590f8f4c0SJonathan Lemon err = __ptp_ocp_gettime_locked(bp, &ts, NULL); 79690f8f4c0SJonathan Lemon if (likely(!err)) { 79790f8f4c0SJonathan Lemon timespec64_add_ns(&ts, delta_ns); 79890f8f4c0SJonathan Lemon __ptp_ocp_settime_locked(bp, &ts); 79990f8f4c0SJonathan Lemon } 80090f8f4c0SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 80190f8f4c0SJonathan Lemon } 80290f8f4c0SJonathan Lemon 803a7e1abadSJonathan Lemon static int 804a7e1abadSJonathan Lemon ptp_ocp_adjtime(struct ptp_clock_info *ptp_info, s64 delta_ns) 805a7e1abadSJonathan Lemon { 806a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 807a7e1abadSJonathan Lemon unsigned long flags; 8086d59d4faSJonathan Lemon u32 adj_ns, sign; 809a7e1abadSJonathan Lemon 81090f8f4c0SJonathan Lemon if (delta_ns > NSEC_PER_SEC || -delta_ns > NSEC_PER_SEC) { 81190f8f4c0SJonathan Lemon ptp_ocp_adjtime_coarse(bp, delta_ns); 81290f8f4c0SJonathan Lemon return 0; 81390f8f4c0SJonathan Lemon } 81490f8f4c0SJonathan Lemon 8156d59d4faSJonathan Lemon sign = delta_ns < 0 ? BIT(31) : 0; 8166d59d4faSJonathan Lemon adj_ns = sign ? -delta_ns : delta_ns; 817a7e1abadSJonathan Lemon 818a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 8196d59d4faSJonathan Lemon __ptp_ocp_adjtime_locked(bp, sign | adj_ns); 820a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 821a7e1abadSJonathan Lemon 8226d59d4faSJonathan Lemon return 0; 823a7e1abadSJonathan Lemon } 824a7e1abadSJonathan Lemon 825a7e1abadSJonathan Lemon static int 826a7e1abadSJonathan Lemon ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm) 827a7e1abadSJonathan Lemon { 828a7e1abadSJonathan Lemon if (scaled_ppm == 0) 829a7e1abadSJonathan Lemon return 0; 830a7e1abadSJonathan Lemon 831a7e1abadSJonathan Lemon return -EOPNOTSUPP; 832a7e1abadSJonathan Lemon } 833a7e1abadSJonathan Lemon 834773bda96SJonathan Lemon static int 8356d59d4faSJonathan Lemon ptp_ocp_null_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns) 836773bda96SJonathan Lemon { 837773bda96SJonathan Lemon return -EOPNOTSUPP; 838773bda96SJonathan Lemon } 839773bda96SJonathan Lemon 840773bda96SJonathan Lemon static int 841773bda96SJonathan Lemon ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq, 842773bda96SJonathan Lemon int on) 843773bda96SJonathan Lemon { 844773bda96SJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 845773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = NULL; 846a62a56d0SJonathan Lemon u32 req; 847773bda96SJonathan Lemon int err; 848773bda96SJonathan Lemon 849773bda96SJonathan Lemon switch (rq->type) { 850773bda96SJonathan Lemon case PTP_CLK_REQ_EXTTS: 851a62a56d0SJonathan Lemon req = OCP_REQ_TIMESTAMP; 852773bda96SJonathan Lemon switch (rq->extts.index) { 853773bda96SJonathan Lemon case 0: 854773bda96SJonathan Lemon ext = bp->ts0; 855773bda96SJonathan Lemon break; 856773bda96SJonathan Lemon case 1: 857773bda96SJonathan Lemon ext = bp->ts1; 858773bda96SJonathan Lemon break; 859dcf61469SJonathan Lemon case 2: 860dcf61469SJonathan Lemon ext = bp->ts2; 861dcf61469SJonathan Lemon break; 862a62a56d0SJonathan Lemon case 3: 863a62a56d0SJonathan Lemon ext = bp->pps; 864a62a56d0SJonathan Lemon break; 865773bda96SJonathan Lemon } 866773bda96SJonathan Lemon break; 867773bda96SJonathan Lemon case PTP_CLK_REQ_PPS: 868a62a56d0SJonathan Lemon req = OCP_REQ_PPS; 869773bda96SJonathan Lemon ext = bp->pps; 870773bda96SJonathan Lemon break; 871a62a56d0SJonathan Lemon case PTP_CLK_REQ_PEROUT: 872*1aa66a3aSJonathan Lemon switch (rq->perout.index) { 873*1aa66a3aSJonathan Lemon case 0: 874a62a56d0SJonathan Lemon /* This is a request for 1PPS on an output SMA. 875a62a56d0SJonathan Lemon * Allow, but assume manual configuration. 876a62a56d0SJonathan Lemon */ 877*1aa66a3aSJonathan Lemon if (on && (rq->perout.period.sec != 1 || 878*1aa66a3aSJonathan Lemon rq->perout.period.nsec != 0)) 879*1aa66a3aSJonathan Lemon return -EINVAL; 880a62a56d0SJonathan Lemon return 0; 881*1aa66a3aSJonathan Lemon case 1: 882*1aa66a3aSJonathan Lemon case 2: 883*1aa66a3aSJonathan Lemon case 3: 884*1aa66a3aSJonathan Lemon case 4: 885*1aa66a3aSJonathan Lemon req = rq->perout.index - 1; 886*1aa66a3aSJonathan Lemon ext = bp->signal_out[req]; 887*1aa66a3aSJonathan Lemon err = ptp_ocp_signal_from_perout(bp, req, &rq->perout); 888*1aa66a3aSJonathan Lemon if (err) 889*1aa66a3aSJonathan Lemon return err; 890*1aa66a3aSJonathan Lemon break; 891*1aa66a3aSJonathan Lemon } 892*1aa66a3aSJonathan Lemon break; 893773bda96SJonathan Lemon default: 894773bda96SJonathan Lemon return -EOPNOTSUPP; 895773bda96SJonathan Lemon } 896773bda96SJonathan Lemon 897773bda96SJonathan Lemon err = -ENXIO; 898773bda96SJonathan Lemon if (ext) 899a62a56d0SJonathan Lemon err = ext->info->enable(ext, req, on); 900773bda96SJonathan Lemon 901773bda96SJonathan Lemon return err; 902773bda96SJonathan Lemon } 903773bda96SJonathan Lemon 904*1aa66a3aSJonathan Lemon static int 905*1aa66a3aSJonathan Lemon ptp_ocp_verify(struct ptp_clock_info *ptp_info, unsigned pin, 906*1aa66a3aSJonathan Lemon enum ptp_pin_function func, unsigned chan) 907*1aa66a3aSJonathan Lemon { 908*1aa66a3aSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 909*1aa66a3aSJonathan Lemon char buf[16]; 910*1aa66a3aSJonathan Lemon 911*1aa66a3aSJonathan Lemon if (func != PTP_PF_PEROUT) 912*1aa66a3aSJonathan Lemon return -EOPNOTSUPP; 913*1aa66a3aSJonathan Lemon 914*1aa66a3aSJonathan Lemon if (chan) 915*1aa66a3aSJonathan Lemon sprintf(buf, "OUT: GEN%d", chan); 916*1aa66a3aSJonathan Lemon else 917*1aa66a3aSJonathan Lemon sprintf(buf, "OUT: PHC"); 918*1aa66a3aSJonathan Lemon 919*1aa66a3aSJonathan Lemon return ptp_ocp_sma_store(bp, buf, pin + 1); 920*1aa66a3aSJonathan Lemon } 921*1aa66a3aSJonathan Lemon 922a7e1abadSJonathan Lemon static const struct ptp_clock_info ptp_ocp_clock_info = { 923a7e1abadSJonathan Lemon .owner = THIS_MODULE, 924a7e1abadSJonathan Lemon .name = KBUILD_MODNAME, 925a7e1abadSJonathan Lemon .max_adj = 100000000, 926a7e1abadSJonathan Lemon .gettimex64 = ptp_ocp_gettimex, 927a7e1abadSJonathan Lemon .settime64 = ptp_ocp_settime, 928a7e1abadSJonathan Lemon .adjtime = ptp_ocp_adjtime, 929a7e1abadSJonathan Lemon .adjfine = ptp_ocp_null_adjfine, 9306d59d4faSJonathan Lemon .adjphase = ptp_ocp_null_adjphase, 931773bda96SJonathan Lemon .enable = ptp_ocp_enable, 932*1aa66a3aSJonathan Lemon .verify = ptp_ocp_verify, 933773bda96SJonathan Lemon .pps = true, 934a62a56d0SJonathan Lemon .n_ext_ts = 4, 935*1aa66a3aSJonathan Lemon .n_per_out = 5, 936a7e1abadSJonathan Lemon }; 937a7e1abadSJonathan Lemon 938773bda96SJonathan Lemon static void 939773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp) 940773bda96SJonathan Lemon { 941773bda96SJonathan Lemon u32 ctrl, select; 942773bda96SJonathan Lemon 943773bda96SJonathan Lemon select = ioread32(&bp->reg->select); 944773bda96SJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 945773bda96SJonathan Lemon 946773bda96SJonathan Lemon iowrite32(0, &bp->reg->drift_ns); 947773bda96SJonathan Lemon 9481acffc6eSJonathan Lemon ctrl = OCP_CTRL_ADJUST_DRIFT | OCP_CTRL_ENABLE; 949773bda96SJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 950773bda96SJonathan Lemon 951773bda96SJonathan Lemon /* restore clock selection */ 952773bda96SJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 953773bda96SJonathan Lemon } 954773bda96SJonathan Lemon 955773bda96SJonathan Lemon static void 956e68462a0SVadim Fedorenko ptp_ocp_utc_distribute(struct ptp_ocp *bp, u32 val) 957e68462a0SVadim Fedorenko { 958e68462a0SVadim Fedorenko unsigned long flags; 959e68462a0SVadim Fedorenko 960e68462a0SVadim Fedorenko spin_lock_irqsave(&bp->lock, flags); 961e68462a0SVadim Fedorenko 962e68462a0SVadim Fedorenko bp->utc_tai_offset = val; 963e68462a0SVadim Fedorenko 964e68462a0SVadim Fedorenko if (bp->irig_out) 965e68462a0SVadim Fedorenko iowrite32(val, &bp->irig_out->adj_sec); 966e68462a0SVadim Fedorenko if (bp->dcf_out) 967e68462a0SVadim Fedorenko iowrite32(val, &bp->dcf_out->adj_sec); 968e68462a0SVadim Fedorenko if (bp->nmea_out) 969e68462a0SVadim Fedorenko iowrite32(val, &bp->nmea_out->adj_sec); 970e68462a0SVadim Fedorenko 971e68462a0SVadim Fedorenko spin_unlock_irqrestore(&bp->lock, flags); 972e68462a0SVadim Fedorenko } 973e68462a0SVadim Fedorenko 974e68462a0SVadim Fedorenko static void 975773bda96SJonathan Lemon ptp_ocp_watchdog(struct timer_list *t) 976773bda96SJonathan Lemon { 977773bda96SJonathan Lemon struct ptp_ocp *bp = from_timer(bp, t, watchdog); 978773bda96SJonathan Lemon unsigned long flags; 979e68462a0SVadim Fedorenko u32 status, utc_offset; 980773bda96SJonathan Lemon 9810d43d4f2SJonathan Lemon status = ioread32(&bp->pps_to_clk->status); 982773bda96SJonathan Lemon 983773bda96SJonathan Lemon if (status & PPS_STATUS_SUPERV_ERR) { 9840d43d4f2SJonathan Lemon iowrite32(status, &bp->pps_to_clk->status); 985ef0cfb34SJonathan Lemon if (!bp->gnss_lost) { 986773bda96SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 987773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(bp); 988773bda96SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 989ef0cfb34SJonathan Lemon bp->gnss_lost = ktime_get_real_seconds(); 990773bda96SJonathan Lemon } 991773bda96SJonathan Lemon 992ef0cfb34SJonathan Lemon } else if (bp->gnss_lost) { 993ef0cfb34SJonathan Lemon bp->gnss_lost = 0; 994773bda96SJonathan Lemon } 995773bda96SJonathan Lemon 996e68462a0SVadim Fedorenko /* if GNSS provides correct data we can rely on 997e68462a0SVadim Fedorenko * it to get leap second information 998e68462a0SVadim Fedorenko */ 999e68462a0SVadim Fedorenko if (bp->tod) { 1000e68462a0SVadim Fedorenko status = ioread32(&bp->tod->utc_status); 1001e68462a0SVadim Fedorenko utc_offset = status & TOD_STATUS_UTC_MASK; 1002e68462a0SVadim Fedorenko if (status & TOD_STATUS_UTC_VALID && 1003e68462a0SVadim Fedorenko utc_offset != bp->utc_tai_offset) 1004e68462a0SVadim Fedorenko ptp_ocp_utc_distribute(bp, utc_offset); 1005e68462a0SVadim Fedorenko } 1006e68462a0SVadim Fedorenko 1007773bda96SJonathan Lemon mod_timer(&bp->watchdog, jiffies + HZ); 1008773bda96SJonathan Lemon } 1009773bda96SJonathan Lemon 10101acffc6eSJonathan Lemon static void 10111acffc6eSJonathan Lemon ptp_ocp_estimate_pci_timing(struct ptp_ocp *bp) 10121acffc6eSJonathan Lemon { 10131acffc6eSJonathan Lemon ktime_t start, end; 10141acffc6eSJonathan Lemon ktime_t delay; 10151acffc6eSJonathan Lemon u32 ctrl; 10161acffc6eSJonathan Lemon 10171acffc6eSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 10181acffc6eSJonathan Lemon ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE; 10191acffc6eSJonathan Lemon 10201acffc6eSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 10211acffc6eSJonathan Lemon 10221acffc6eSJonathan Lemon start = ktime_get_ns(); 10231acffc6eSJonathan Lemon 10241acffc6eSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 10251acffc6eSJonathan Lemon 10261acffc6eSJonathan Lemon end = ktime_get_ns(); 10271acffc6eSJonathan Lemon 10281acffc6eSJonathan Lemon delay = end - start; 10291acffc6eSJonathan Lemon bp->ts_window_adjust = (delay >> 5) * 3; 10301acffc6eSJonathan Lemon } 10311acffc6eSJonathan Lemon 1032a7e1abadSJonathan Lemon static int 1033773bda96SJonathan Lemon ptp_ocp_init_clock(struct ptp_ocp *bp) 1034a7e1abadSJonathan Lemon { 1035a7e1abadSJonathan Lemon struct timespec64 ts; 1036a7e1abadSJonathan Lemon bool sync; 1037a7e1abadSJonathan Lemon u32 ctrl; 1038a7e1abadSJonathan Lemon 10391acffc6eSJonathan Lemon ctrl = OCP_CTRL_ENABLE; 1040a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 1041a7e1abadSJonathan Lemon 1042773bda96SJonathan Lemon /* NO DRIFT Correction */ 1043773bda96SJonathan Lemon /* offset_p:i 1/8, offset_i: 1/16, drift_p: 0, drift_i: 0 */ 1044773bda96SJonathan Lemon iowrite32(0x2000, &bp->reg->servo_offset_p); 1045773bda96SJonathan Lemon iowrite32(0x1000, &bp->reg->servo_offset_i); 1046773bda96SJonathan Lemon iowrite32(0, &bp->reg->servo_drift_p); 1047773bda96SJonathan Lemon iowrite32(0, &bp->reg->servo_drift_i); 1048773bda96SJonathan Lemon 1049773bda96SJonathan Lemon /* latch servo values */ 1050773bda96SJonathan Lemon ctrl |= OCP_CTRL_ADJUST_SERVO; 1051773bda96SJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 1052773bda96SJonathan Lemon 1053a7e1abadSJonathan Lemon if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) { 1054a7e1abadSJonathan Lemon dev_err(&bp->pdev->dev, "clock not enabled\n"); 1055a7e1abadSJonathan Lemon return -ENODEV; 1056a7e1abadSJonathan Lemon } 1057a7e1abadSJonathan Lemon 10581acffc6eSJonathan Lemon ptp_ocp_estimate_pci_timing(bp); 10591acffc6eSJonathan Lemon 1060a7e1abadSJonathan Lemon sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC; 1061a7e1abadSJonathan Lemon if (!sync) { 1062065efcc5SJonathan Lemon ktime_get_clocktai_ts64(&ts); 1063a7e1abadSJonathan Lemon ptp_ocp_settime(&bp->ptp_info, &ts); 1064a7e1abadSJonathan Lemon } 1065a7e1abadSJonathan Lemon 1066065efcc5SJonathan Lemon /* If there is a clock supervisor, then enable the watchdog */ 1067065efcc5SJonathan Lemon if (bp->pps_to_clk) { 1068773bda96SJonathan Lemon timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0); 1069773bda96SJonathan Lemon mod_timer(&bp->watchdog, jiffies + HZ); 1070065efcc5SJonathan Lemon } 1071773bda96SJonathan Lemon 1072a7e1abadSJonathan Lemon return 0; 1073a7e1abadSJonathan Lemon } 1074a7e1abadSJonathan Lemon 1075a7e1abadSJonathan Lemon static void 1076065efcc5SJonathan Lemon ptp_ocp_tod_init(struct ptp_ocp *bp) 1077065efcc5SJonathan Lemon { 1078065efcc5SJonathan Lemon u32 ctrl, reg; 1079065efcc5SJonathan Lemon 1080065efcc5SJonathan Lemon ctrl = ioread32(&bp->tod->ctrl); 1081065efcc5SJonathan Lemon ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE; 1082065efcc5SJonathan Lemon ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B); 1083065efcc5SJonathan Lemon iowrite32(ctrl, &bp->tod->ctrl); 1084065efcc5SJonathan Lemon 1085065efcc5SJonathan Lemon reg = ioread32(&bp->tod->utc_status); 1086065efcc5SJonathan Lemon if (reg & TOD_STATUS_UTC_VALID) 1087065efcc5SJonathan Lemon ptp_ocp_utc_distribute(bp, reg & TOD_STATUS_UTC_MASK); 1088065efcc5SJonathan Lemon } 1089065efcc5SJonathan Lemon 10909f492c4cSVadim Fedorenko static const char * 10919f492c4cSVadim Fedorenko ptp_ocp_tod_proto_name(const int idx) 1092a7e1abadSJonathan Lemon { 1093a7e1abadSJonathan Lemon static const char * const proto_name[] = { 1094a7e1abadSJonathan Lemon "NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none", 1095a7e1abadSJonathan Lemon "UBX", "UBX_UTC", "UBX_LS", "UBX_none" 1096a7e1abadSJonathan Lemon }; 10979f492c4cSVadim Fedorenko return proto_name[idx]; 10989f492c4cSVadim Fedorenko } 10999f492c4cSVadim Fedorenko 11009f492c4cSVadim Fedorenko static const char * 11019f492c4cSVadim Fedorenko ptp_ocp_tod_gnss_name(int idx) 11029f492c4cSVadim Fedorenko { 1103a7e1abadSJonathan Lemon static const char * const gnss_name[] = { 1104a7e1abadSJonathan Lemon "ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU", 11059f492c4cSVadim Fedorenko "Unknown" 1106a7e1abadSJonathan Lemon }; 110772f00505SDan Carpenter if (idx >= ARRAY_SIZE(gnss_name)) 11089f492c4cSVadim Fedorenko idx = ARRAY_SIZE(gnss_name) - 1; 11099f492c4cSVadim Fedorenko return gnss_name[idx]; 1110a7e1abadSJonathan Lemon } 1111a7e1abadSJonathan Lemon 11120cfcdd1eSJonathan Lemon struct ptp_ocp_nvmem_match_info { 11130cfcdd1eSJonathan Lemon struct ptp_ocp *bp; 11140cfcdd1eSJonathan Lemon const void * const tag; 1115773bda96SJonathan Lemon }; 1116773bda96SJonathan Lemon 11170cfcdd1eSJonathan Lemon static int 11180cfcdd1eSJonathan Lemon ptp_ocp_nvmem_match(struct device *dev, const void *data) 11190cfcdd1eSJonathan Lemon { 11200cfcdd1eSJonathan Lemon const struct ptp_ocp_nvmem_match_info *info = data; 11210cfcdd1eSJonathan Lemon 11220cfcdd1eSJonathan Lemon dev = dev->parent; 11230cfcdd1eSJonathan Lemon if (!i2c_verify_client(dev) || info->tag != dev->platform_data) 11240cfcdd1eSJonathan Lemon return 0; 11250cfcdd1eSJonathan Lemon 11260cfcdd1eSJonathan Lemon while ((dev = dev->parent)) 11270cfcdd1eSJonathan Lemon if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME)) 11280cfcdd1eSJonathan Lemon return info->bp == dev_get_drvdata(dev); 1129773bda96SJonathan Lemon return 0; 1130773bda96SJonathan Lemon } 1131773bda96SJonathan Lemon 11320cfcdd1eSJonathan Lemon static inline struct nvmem_device * 11330cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_get(struct ptp_ocp *bp, const void * const tag) 1134773bda96SJonathan Lemon { 11350cfcdd1eSJonathan Lemon struct ptp_ocp_nvmem_match_info info = { .bp = bp, .tag = tag }; 11360cfcdd1eSJonathan Lemon 11370cfcdd1eSJonathan Lemon return nvmem_device_find(&info, ptp_ocp_nvmem_match); 11380cfcdd1eSJonathan Lemon } 11390cfcdd1eSJonathan Lemon 11400cfcdd1eSJonathan Lemon static inline void 11410cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_put(struct nvmem_device **nvmemp) 11420cfcdd1eSJonathan Lemon { 11430cfcdd1eSJonathan Lemon if (*nvmemp != NULL) { 11440cfcdd1eSJonathan Lemon nvmem_device_put(*nvmemp); 11450cfcdd1eSJonathan Lemon *nvmemp = NULL; 11460cfcdd1eSJonathan Lemon } 11470cfcdd1eSJonathan Lemon } 11480cfcdd1eSJonathan Lemon 11490cfcdd1eSJonathan Lemon static void 11500cfcdd1eSJonathan Lemon ptp_ocp_read_eeprom(struct ptp_ocp *bp) 11510cfcdd1eSJonathan Lemon { 11520cfcdd1eSJonathan Lemon const struct ptp_ocp_eeprom_map *map; 11530cfcdd1eSJonathan Lemon struct nvmem_device *nvmem; 11540cfcdd1eSJonathan Lemon const void *tag; 11550cfcdd1eSJonathan Lemon int ret; 1156773bda96SJonathan Lemon 11571447149dSJonathan Lemon if (!bp->i2c_ctrl) 11581447149dSJonathan Lemon return; 11591447149dSJonathan Lemon 11600cfcdd1eSJonathan Lemon tag = NULL; 11610cfcdd1eSJonathan Lemon nvmem = NULL; 1162773bda96SJonathan Lemon 11630cfcdd1eSJonathan Lemon for (map = bp->eeprom_map; map->len; map++) { 11640cfcdd1eSJonathan Lemon if (map->tag != tag) { 11650cfcdd1eSJonathan Lemon tag = map->tag; 11660cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_put(&nvmem); 11670cfcdd1eSJonathan Lemon } 11680cfcdd1eSJonathan Lemon if (!nvmem) { 11690cfcdd1eSJonathan Lemon nvmem = ptp_ocp_nvmem_device_get(bp, tag); 11700cfcdd1eSJonathan Lemon if (!nvmem) 1171773bda96SJonathan Lemon goto out; 1172773bda96SJonathan Lemon } 11730cfcdd1eSJonathan Lemon ret = nvmem_device_read(nvmem, map->off, map->len, 11740cfcdd1eSJonathan Lemon BP_MAP_ENTRY_ADDR(bp, map)); 11750cfcdd1eSJonathan Lemon if (ret != map->len) 11760cfcdd1eSJonathan Lemon goto read_fail; 1177773bda96SJonathan Lemon } 1178773bda96SJonathan Lemon 11790cfcdd1eSJonathan Lemon bp->has_eeprom_data = true; 1180773bda96SJonathan Lemon 1181773bda96SJonathan Lemon out: 11820cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_put(&nvmem); 11830cfcdd1eSJonathan Lemon return; 11840cfcdd1eSJonathan Lemon 11850cfcdd1eSJonathan Lemon read_fail: 11860cfcdd1eSJonathan Lemon dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", ret); 11870cfcdd1eSJonathan Lemon goto out; 11880cfcdd1eSJonathan Lemon } 11890cfcdd1eSJonathan Lemon 11900cfcdd1eSJonathan Lemon static int 11910cfcdd1eSJonathan Lemon ptp_ocp_firstchild(struct device *dev, void *data) 11920cfcdd1eSJonathan Lemon { 11930cfcdd1eSJonathan Lemon return 1; 1194773bda96SJonathan Lemon } 1195773bda96SJonathan Lemon 1196773bda96SJonathan Lemon static struct device * 1197773bda96SJonathan Lemon ptp_ocp_find_flash(struct ptp_ocp *bp) 1198773bda96SJonathan Lemon { 1199773bda96SJonathan Lemon struct device *dev, *last; 1200773bda96SJonathan Lemon 1201773bda96SJonathan Lemon last = NULL; 1202773bda96SJonathan Lemon dev = &bp->spi_flash->dev; 1203773bda96SJonathan Lemon 1204773bda96SJonathan Lemon while ((dev = device_find_child(dev, NULL, ptp_ocp_firstchild))) { 1205773bda96SJonathan Lemon if (!strcmp("mtd", dev_bus_name(dev))) 1206773bda96SJonathan Lemon break; 1207773bda96SJonathan Lemon put_device(last); 1208773bda96SJonathan Lemon last = dev; 1209773bda96SJonathan Lemon } 1210773bda96SJonathan Lemon put_device(last); 1211773bda96SJonathan Lemon 1212773bda96SJonathan Lemon return dev; 1213773bda96SJonathan Lemon } 1214773bda96SJonathan Lemon 1215773bda96SJonathan Lemon static int 1216773bda96SJonathan Lemon ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev, 1217773bda96SJonathan Lemon const struct firmware *fw) 1218773bda96SJonathan Lemon { 1219773bda96SJonathan Lemon struct mtd_info *mtd = dev_get_drvdata(dev); 1220773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 1221773bda96SJonathan Lemon size_t off, len, resid, wrote; 1222773bda96SJonathan Lemon struct erase_info erase; 1223773bda96SJonathan Lemon size_t base, blksz; 12247c807572SJonathan Lemon int err = 0; 1225773bda96SJonathan Lemon 1226773bda96SJonathan Lemon off = 0; 1227773bda96SJonathan Lemon base = bp->flash_start; 1228773bda96SJonathan Lemon blksz = 4096; 1229773bda96SJonathan Lemon resid = fw->size; 1230773bda96SJonathan Lemon 1231773bda96SJonathan Lemon while (resid) { 1232773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, "Flashing", 1233773bda96SJonathan Lemon NULL, off, fw->size); 1234773bda96SJonathan Lemon 1235773bda96SJonathan Lemon len = min_t(size_t, resid, blksz); 1236773bda96SJonathan Lemon erase.addr = base + off; 1237773bda96SJonathan Lemon erase.len = blksz; 1238773bda96SJonathan Lemon 1239773bda96SJonathan Lemon err = mtd_erase(mtd, &erase); 1240773bda96SJonathan Lemon if (err) 1241773bda96SJonathan Lemon goto out; 1242773bda96SJonathan Lemon 1243773bda96SJonathan Lemon err = mtd_write(mtd, base + off, len, &wrote, &fw->data[off]); 1244773bda96SJonathan Lemon if (err) 1245773bda96SJonathan Lemon goto out; 1246773bda96SJonathan Lemon 1247773bda96SJonathan Lemon off += blksz; 1248773bda96SJonathan Lemon resid -= len; 1249773bda96SJonathan Lemon } 1250773bda96SJonathan Lemon out: 1251773bda96SJonathan Lemon return err; 1252773bda96SJonathan Lemon } 1253773bda96SJonathan Lemon 1254773bda96SJonathan Lemon static int 1255773bda96SJonathan Lemon ptp_ocp_devlink_flash_update(struct devlink *devlink, 1256773bda96SJonathan Lemon struct devlink_flash_update_params *params, 1257773bda96SJonathan Lemon struct netlink_ext_ack *extack) 1258773bda96SJonathan Lemon { 1259773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 1260773bda96SJonathan Lemon struct device *dev; 1261773bda96SJonathan Lemon const char *msg; 1262773bda96SJonathan Lemon int err; 1263773bda96SJonathan Lemon 1264773bda96SJonathan Lemon dev = ptp_ocp_find_flash(bp); 1265773bda96SJonathan Lemon if (!dev) { 1266773bda96SJonathan Lemon dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n"); 1267773bda96SJonathan Lemon return -ENODEV; 1268773bda96SJonathan Lemon } 1269773bda96SJonathan Lemon 1270773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, "Preparing to flash", 1271773bda96SJonathan Lemon NULL, 0, 0); 1272773bda96SJonathan Lemon 1273773bda96SJonathan Lemon err = ptp_ocp_devlink_flash(devlink, dev, params->fw); 1274773bda96SJonathan Lemon 1275773bda96SJonathan Lemon msg = err ? "Flash error" : "Flash complete"; 1276773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0); 1277773bda96SJonathan Lemon 1278773bda96SJonathan Lemon put_device(dev); 1279773bda96SJonathan Lemon return err; 1280773bda96SJonathan Lemon } 1281773bda96SJonathan Lemon 1282773bda96SJonathan Lemon static int 1283773bda96SJonathan Lemon ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req, 1284773bda96SJonathan Lemon struct netlink_ext_ack *extack) 1285773bda96SJonathan Lemon { 1286773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 1287773bda96SJonathan Lemon char buf[32]; 1288773bda96SJonathan Lemon int err; 1289773bda96SJonathan Lemon 1290773bda96SJonathan Lemon err = devlink_info_driver_name_put(req, KBUILD_MODNAME); 1291773bda96SJonathan Lemon if (err) 1292773bda96SJonathan Lemon return err; 1293773bda96SJonathan Lemon 1294b0ca789aSJonathan Lemon if (bp->fw_version & 0xffff) { 1295b0ca789aSJonathan Lemon sprintf(buf, "%d", bp->fw_version); 1296b0ca789aSJonathan Lemon err = devlink_info_version_running_put(req, "fw", buf); 1297773bda96SJonathan Lemon } else { 1298b0ca789aSJonathan Lemon sprintf(buf, "%d", bp->fw_version >> 16); 1299b0ca789aSJonathan Lemon err = devlink_info_version_running_put(req, "loader", buf); 1300773bda96SJonathan Lemon } 1301773bda96SJonathan Lemon if (err) 1302773bda96SJonathan Lemon return err; 1303773bda96SJonathan Lemon 13040cfcdd1eSJonathan Lemon if (!bp->has_eeprom_data) { 13050cfcdd1eSJonathan Lemon ptp_ocp_read_eeprom(bp); 13060cfcdd1eSJonathan Lemon if (!bp->has_eeprom_data) 13070cfcdd1eSJonathan Lemon return 0; 13080cfcdd1eSJonathan Lemon } 1309773bda96SJonathan Lemon 1310773bda96SJonathan Lemon sprintf(buf, "%pM", bp->serial); 1311773bda96SJonathan Lemon err = devlink_info_serial_number_put(req, buf); 1312773bda96SJonathan Lemon if (err) 1313773bda96SJonathan Lemon return err; 13140cfcdd1eSJonathan Lemon 13150cfcdd1eSJonathan Lemon err = devlink_info_version_fixed_put(req, 13160cfcdd1eSJonathan Lemon DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, 13170cfcdd1eSJonathan Lemon bp->board_id); 13180cfcdd1eSJonathan Lemon if (err) 13190cfcdd1eSJonathan Lemon return err; 1320773bda96SJonathan Lemon 1321773bda96SJonathan Lemon return 0; 1322773bda96SJonathan Lemon } 1323773bda96SJonathan Lemon 1324773bda96SJonathan Lemon static const struct devlink_ops ptp_ocp_devlink_ops = { 1325773bda96SJonathan Lemon .flash_update = ptp_ocp_devlink_flash_update, 1326773bda96SJonathan Lemon .info_get = ptp_ocp_devlink_info_get, 1327773bda96SJonathan Lemon }; 1328773bda96SJonathan Lemon 1329773bda96SJonathan Lemon static void __iomem * 1330773bda96SJonathan Lemon __ptp_ocp_get_mem(struct ptp_ocp *bp, unsigned long start, int size) 1331773bda96SJonathan Lemon { 1332773bda96SJonathan Lemon struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp"); 1333773bda96SJonathan Lemon 1334773bda96SJonathan Lemon return devm_ioremap_resource(&bp->pdev->dev, &res); 1335773bda96SJonathan Lemon } 1336773bda96SJonathan Lemon 1337773bda96SJonathan Lemon static void __iomem * 1338773bda96SJonathan Lemon ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r) 1339773bda96SJonathan Lemon { 1340773bda96SJonathan Lemon unsigned long start; 1341773bda96SJonathan Lemon 1342773bda96SJonathan Lemon start = pci_resource_start(bp->pdev, 0) + r->offset; 1343773bda96SJonathan Lemon return __ptp_ocp_get_mem(bp, start, r->size); 1344773bda96SJonathan Lemon } 1345773bda96SJonathan Lemon 1346773bda96SJonathan Lemon static void 1347773bda96SJonathan Lemon ptp_ocp_set_irq_resource(struct resource *res, int irq) 1348773bda96SJonathan Lemon { 1349773bda96SJonathan Lemon struct resource r = DEFINE_RES_IRQ(irq); 1350773bda96SJonathan Lemon *res = r; 1351773bda96SJonathan Lemon } 1352773bda96SJonathan Lemon 1353773bda96SJonathan Lemon static void 1354773bda96SJonathan Lemon ptp_ocp_set_mem_resource(struct resource *res, unsigned long start, int size) 1355773bda96SJonathan Lemon { 1356773bda96SJonathan Lemon struct resource r = DEFINE_RES_MEM(start, size); 1357773bda96SJonathan Lemon *res = r; 1358773bda96SJonathan Lemon } 1359773bda96SJonathan Lemon 1360773bda96SJonathan Lemon static int 1361773bda96SJonathan Lemon ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r) 1362773bda96SJonathan Lemon { 1363773bda96SJonathan Lemon struct ptp_ocp_flash_info *info; 1364773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1365773bda96SJonathan Lemon struct platform_device *p; 1366773bda96SJonathan Lemon struct resource res[2]; 1367773bda96SJonathan Lemon unsigned long start; 1368773bda96SJonathan Lemon int id; 1369773bda96SJonathan Lemon 1370773bda96SJonathan Lemon start = pci_resource_start(pdev, 0) + r->offset; 1371773bda96SJonathan Lemon ptp_ocp_set_mem_resource(&res[0], start, r->size); 1372773bda96SJonathan Lemon ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1373773bda96SJonathan Lemon 1374773bda96SJonathan Lemon info = r->extra; 1375773bda96SJonathan Lemon id = pci_dev_id(pdev) << 1; 1376773bda96SJonathan Lemon id += info->pci_offset; 1377773bda96SJonathan Lemon 1378773bda96SJonathan Lemon p = platform_device_register_resndata(&pdev->dev, info->name, id, 1379773bda96SJonathan Lemon res, 2, info->data, 1380773bda96SJonathan Lemon info->data_size); 1381773bda96SJonathan Lemon if (IS_ERR(p)) 1382773bda96SJonathan Lemon return PTR_ERR(p); 1383773bda96SJonathan Lemon 1384773bda96SJonathan Lemon bp_assign_entry(bp, r, p); 1385773bda96SJonathan Lemon 1386773bda96SJonathan Lemon return 0; 1387773bda96SJonathan Lemon } 1388773bda96SJonathan Lemon 1389773bda96SJonathan Lemon static struct platform_device * 1390773bda96SJonathan Lemon ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id) 1391773bda96SJonathan Lemon { 13921618df6aSJonathan Lemon struct ptp_ocp_i2c_info *info; 1393773bda96SJonathan Lemon struct resource res[2]; 1394773bda96SJonathan Lemon unsigned long start; 1395773bda96SJonathan Lemon 13961618df6aSJonathan Lemon info = r->extra; 1397773bda96SJonathan Lemon start = pci_resource_start(pdev, 0) + r->offset; 1398773bda96SJonathan Lemon ptp_ocp_set_mem_resource(&res[0], start, r->size); 1399773bda96SJonathan Lemon ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1400773bda96SJonathan Lemon 14011618df6aSJonathan Lemon return platform_device_register_resndata(&pdev->dev, info->name, 14021618df6aSJonathan Lemon id, res, 2, 14031618df6aSJonathan Lemon info->data, info->data_size); 1404773bda96SJonathan Lemon } 1405773bda96SJonathan Lemon 1406773bda96SJonathan Lemon static int 1407773bda96SJonathan Lemon ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r) 1408773bda96SJonathan Lemon { 1409773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 14101618df6aSJonathan Lemon struct ptp_ocp_i2c_info *info; 1411773bda96SJonathan Lemon struct platform_device *p; 1412773bda96SJonathan Lemon struct clk_hw *clk; 1413773bda96SJonathan Lemon char buf[32]; 1414773bda96SJonathan Lemon int id; 1415773bda96SJonathan Lemon 14161618df6aSJonathan Lemon info = r->extra; 1417773bda96SJonathan Lemon id = pci_dev_id(bp->pdev); 1418773bda96SJonathan Lemon 1419773bda96SJonathan Lemon sprintf(buf, "AXI.%d", id); 14201618df6aSJonathan Lemon clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0, 14211618df6aSJonathan Lemon info->fixed_rate); 1422773bda96SJonathan Lemon if (IS_ERR(clk)) 1423773bda96SJonathan Lemon return PTR_ERR(clk); 1424773bda96SJonathan Lemon bp->i2c_clk = clk; 1425773bda96SJonathan Lemon 14261618df6aSJonathan Lemon sprintf(buf, "%s.%d", info->name, id); 1427773bda96SJonathan Lemon devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf); 1428773bda96SJonathan Lemon p = ptp_ocp_i2c_bus(bp->pdev, r, id); 1429773bda96SJonathan Lemon if (IS_ERR(p)) 1430773bda96SJonathan Lemon return PTR_ERR(p); 1431773bda96SJonathan Lemon 1432773bda96SJonathan Lemon bp_assign_entry(bp, r, p); 1433773bda96SJonathan Lemon 1434773bda96SJonathan Lemon return 0; 1435773bda96SJonathan Lemon } 1436773bda96SJonathan Lemon 1437b325af3cSJonathan Lemon /* The expectation is that this is triggered only on error. */ 1438b325af3cSJonathan Lemon static irqreturn_t 1439b325af3cSJonathan Lemon ptp_ocp_signal_irq(int irq, void *priv) 1440b325af3cSJonathan Lemon { 1441b325af3cSJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1442b325af3cSJonathan Lemon struct signal_reg __iomem *reg = ext->mem; 1443b325af3cSJonathan Lemon struct ptp_ocp *bp = ext->bp; 1444b325af3cSJonathan Lemon u32 enable, status; 1445b325af3cSJonathan Lemon int gen; 1446b325af3cSJonathan Lemon 1447b325af3cSJonathan Lemon gen = ext->info->index - 1; 1448b325af3cSJonathan Lemon 1449b325af3cSJonathan Lemon enable = ioread32(®->enable); 1450b325af3cSJonathan Lemon status = ioread32(®->status); 1451b325af3cSJonathan Lemon 1452b325af3cSJonathan Lemon /* disable generator on error */ 1453b325af3cSJonathan Lemon if (status || !enable) { 1454b325af3cSJonathan Lemon iowrite32(0, ®->intr_mask); 1455b325af3cSJonathan Lemon iowrite32(0, ®->enable); 1456b325af3cSJonathan Lemon bp->signal[gen].running = false; 1457b325af3cSJonathan Lemon } 1458b325af3cSJonathan Lemon 1459b325af3cSJonathan Lemon iowrite32(0, ®->intr); /* ack interrupt */ 1460b325af3cSJonathan Lemon 1461b325af3cSJonathan Lemon return IRQ_HANDLED; 1462b325af3cSJonathan Lemon } 1463b325af3cSJonathan Lemon 1464b325af3cSJonathan Lemon static int 1465b325af3cSJonathan Lemon ptp_ocp_signal_set(struct ptp_ocp *bp, int gen, struct ptp_ocp_signal *s) 1466b325af3cSJonathan Lemon { 1467b325af3cSJonathan Lemon struct ptp_system_timestamp sts; 1468b325af3cSJonathan Lemon struct timespec64 ts; 1469b325af3cSJonathan Lemon ktime_t start_ns; 1470b325af3cSJonathan Lemon int err; 1471b325af3cSJonathan Lemon 1472b325af3cSJonathan Lemon if (!s->period) 1473b325af3cSJonathan Lemon return 0; 1474b325af3cSJonathan Lemon 1475b325af3cSJonathan Lemon if (!s->pulse) 1476b325af3cSJonathan Lemon s->pulse = ktime_divns(s->period * s->duty, 100); 1477b325af3cSJonathan Lemon 1478b325af3cSJonathan Lemon err = ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts); 1479b325af3cSJonathan Lemon if (err) 1480b325af3cSJonathan Lemon return err; 1481b325af3cSJonathan Lemon 1482b325af3cSJonathan Lemon start_ns = ktime_set(ts.tv_sec, ts.tv_nsec) + NSEC_PER_MSEC; 1483b325af3cSJonathan Lemon if (!s->start) { 1484b325af3cSJonathan Lemon /* roundup() does not work on 32-bit systems */ 1485b325af3cSJonathan Lemon s->start = DIV_ROUND_UP_ULL(start_ns, s->period); 1486b325af3cSJonathan Lemon s->start = ktime_add(s->start, s->phase); 1487b325af3cSJonathan Lemon } 1488b325af3cSJonathan Lemon 1489b325af3cSJonathan Lemon if (s->duty < 1 || s->duty > 99) 1490b325af3cSJonathan Lemon return -EINVAL; 1491b325af3cSJonathan Lemon 1492b325af3cSJonathan Lemon if (s->pulse < 1 || s->pulse > s->period) 1493b325af3cSJonathan Lemon return -EINVAL; 1494b325af3cSJonathan Lemon 1495b325af3cSJonathan Lemon if (s->start < start_ns) 1496b325af3cSJonathan Lemon return -EINVAL; 1497b325af3cSJonathan Lemon 1498b325af3cSJonathan Lemon bp->signal[gen] = *s; 1499b325af3cSJonathan Lemon 1500b325af3cSJonathan Lemon return 0; 1501b325af3cSJonathan Lemon } 1502b325af3cSJonathan Lemon 1503b325af3cSJonathan Lemon static int 1504*1aa66a3aSJonathan Lemon ptp_ocp_signal_from_perout(struct ptp_ocp *bp, int gen, 1505*1aa66a3aSJonathan Lemon struct ptp_perout_request *req) 1506*1aa66a3aSJonathan Lemon { 1507*1aa66a3aSJonathan Lemon struct ptp_ocp_signal s = { }; 1508*1aa66a3aSJonathan Lemon 1509*1aa66a3aSJonathan Lemon s.polarity = bp->signal[gen].polarity; 1510*1aa66a3aSJonathan Lemon s.period = ktime_set(req->period.sec, req->period.nsec); 1511*1aa66a3aSJonathan Lemon if (!s.period) 1512*1aa66a3aSJonathan Lemon return 0; 1513*1aa66a3aSJonathan Lemon 1514*1aa66a3aSJonathan Lemon if (req->flags & PTP_PEROUT_DUTY_CYCLE) { 1515*1aa66a3aSJonathan Lemon s.pulse = ktime_set(req->on.sec, req->on.nsec); 1516*1aa66a3aSJonathan Lemon s.duty = ktime_divns(s.pulse * 100, s.period); 1517*1aa66a3aSJonathan Lemon } 1518*1aa66a3aSJonathan Lemon 1519*1aa66a3aSJonathan Lemon if (req->flags & PTP_PEROUT_PHASE) 1520*1aa66a3aSJonathan Lemon s.phase = ktime_set(req->phase.sec, req->phase.nsec); 1521*1aa66a3aSJonathan Lemon else 1522*1aa66a3aSJonathan Lemon s.start = ktime_set(req->start.sec, req->start.nsec); 1523*1aa66a3aSJonathan Lemon 1524*1aa66a3aSJonathan Lemon return ptp_ocp_signal_set(bp, gen, &s); 1525*1aa66a3aSJonathan Lemon } 1526*1aa66a3aSJonathan Lemon 1527*1aa66a3aSJonathan Lemon static int 1528b325af3cSJonathan Lemon ptp_ocp_signal_enable(void *priv, u32 req, bool enable) 1529b325af3cSJonathan Lemon { 1530b325af3cSJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1531b325af3cSJonathan Lemon struct signal_reg __iomem *reg = ext->mem; 1532b325af3cSJonathan Lemon struct ptp_ocp *bp = ext->bp; 1533b325af3cSJonathan Lemon struct timespec64 ts; 1534b325af3cSJonathan Lemon int gen; 1535b325af3cSJonathan Lemon 1536b325af3cSJonathan Lemon gen = ext->info->index - 1; 1537b325af3cSJonathan Lemon 1538b325af3cSJonathan Lemon iowrite32(0, ®->intr_mask); 1539b325af3cSJonathan Lemon iowrite32(0, ®->enable); 1540b325af3cSJonathan Lemon bp->signal[gen].running = false; 1541b325af3cSJonathan Lemon if (!enable) 1542b325af3cSJonathan Lemon return 0; 1543b325af3cSJonathan Lemon 1544b325af3cSJonathan Lemon ts = ktime_to_timespec64(bp->signal[gen].start); 1545b325af3cSJonathan Lemon iowrite32(ts.tv_sec, ®->start_sec); 1546b325af3cSJonathan Lemon iowrite32(ts.tv_nsec, ®->start_ns); 1547b325af3cSJonathan Lemon 1548b325af3cSJonathan Lemon ts = ktime_to_timespec64(bp->signal[gen].period); 1549b325af3cSJonathan Lemon iowrite32(ts.tv_sec, ®->period_sec); 1550b325af3cSJonathan Lemon iowrite32(ts.tv_nsec, ®->period_ns); 1551b325af3cSJonathan Lemon 1552b325af3cSJonathan Lemon ts = ktime_to_timespec64(bp->signal[gen].pulse); 1553b325af3cSJonathan Lemon iowrite32(ts.tv_sec, ®->pulse_sec); 1554b325af3cSJonathan Lemon iowrite32(ts.tv_nsec, ®->pulse_ns); 1555b325af3cSJonathan Lemon 1556b325af3cSJonathan Lemon iowrite32(bp->signal[gen].polarity, ®->polarity); 1557b325af3cSJonathan Lemon iowrite32(0, ®->repeat_count); 1558b325af3cSJonathan Lemon 1559b325af3cSJonathan Lemon iowrite32(0, ®->intr); /* clear interrupt state */ 1560b325af3cSJonathan Lemon iowrite32(1, ®->intr_mask); /* enable interrupt */ 1561b325af3cSJonathan Lemon iowrite32(3, ®->enable); /* valid & enable */ 1562b325af3cSJonathan Lemon 1563b325af3cSJonathan Lemon bp->signal[gen].running = true; 1564b325af3cSJonathan Lemon 1565b325af3cSJonathan Lemon return 0; 1566b325af3cSJonathan Lemon } 1567b325af3cSJonathan Lemon 1568773bda96SJonathan Lemon static irqreturn_t 1569773bda96SJonathan Lemon ptp_ocp_ts_irq(int irq, void *priv) 1570773bda96SJonathan Lemon { 1571773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1572773bda96SJonathan Lemon struct ts_reg __iomem *reg = ext->mem; 1573773bda96SJonathan Lemon struct ptp_clock_event ev; 1574773bda96SJonathan Lemon u32 sec, nsec; 1575773bda96SJonathan Lemon 1576a62a56d0SJonathan Lemon if (ext == ext->bp->pps) { 1577a62a56d0SJonathan Lemon if (ext->bp->pps_req_map & OCP_REQ_PPS) { 1578a62a56d0SJonathan Lemon ev.type = PTP_CLOCK_PPS; 1579a62a56d0SJonathan Lemon ptp_clock_event(ext->bp->ptp, &ev); 1580a62a56d0SJonathan Lemon } 1581a62a56d0SJonathan Lemon 1582a62a56d0SJonathan Lemon if ((ext->bp->pps_req_map & ~OCP_REQ_PPS) == 0) 1583a62a56d0SJonathan Lemon goto out; 1584a62a56d0SJonathan Lemon } 1585a62a56d0SJonathan Lemon 1586773bda96SJonathan Lemon /* XXX should fix API - this converts s/ns -> ts -> s/ns */ 1587773bda96SJonathan Lemon sec = ioread32(®->time_sec); 1588773bda96SJonathan Lemon nsec = ioread32(®->time_ns); 1589773bda96SJonathan Lemon 1590773bda96SJonathan Lemon ev.type = PTP_CLOCK_EXTTS; 1591773bda96SJonathan Lemon ev.index = ext->info->index; 15921acffc6eSJonathan Lemon ev.timestamp = sec * NSEC_PER_SEC + nsec; 1593773bda96SJonathan Lemon 1594773bda96SJonathan Lemon ptp_clock_event(ext->bp->ptp, &ev); 1595773bda96SJonathan Lemon 1596a62a56d0SJonathan Lemon out: 1597773bda96SJonathan Lemon iowrite32(1, ®->intr); /* write 1 to ack */ 1598773bda96SJonathan Lemon 1599773bda96SJonathan Lemon return IRQ_HANDLED; 1600773bda96SJonathan Lemon } 1601773bda96SJonathan Lemon 1602773bda96SJonathan Lemon static int 1603a62a56d0SJonathan Lemon ptp_ocp_ts_enable(void *priv, u32 req, bool enable) 1604773bda96SJonathan Lemon { 1605773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1606773bda96SJonathan Lemon struct ts_reg __iomem *reg = ext->mem; 1607a62a56d0SJonathan Lemon struct ptp_ocp *bp = ext->bp; 1608a62a56d0SJonathan Lemon 1609a62a56d0SJonathan Lemon if (ext == bp->pps) { 1610a62a56d0SJonathan Lemon u32 old_map = bp->pps_req_map; 1611a62a56d0SJonathan Lemon 1612a62a56d0SJonathan Lemon if (enable) 1613a62a56d0SJonathan Lemon bp->pps_req_map |= req; 1614a62a56d0SJonathan Lemon else 1615a62a56d0SJonathan Lemon bp->pps_req_map &= ~req; 1616a62a56d0SJonathan Lemon 1617a62a56d0SJonathan Lemon /* if no state change, just return */ 1618a62a56d0SJonathan Lemon if ((!!old_map ^ !!bp->pps_req_map) == 0) 1619a62a56d0SJonathan Lemon return 0; 1620a62a56d0SJonathan Lemon } 1621773bda96SJonathan Lemon 1622773bda96SJonathan Lemon if (enable) { 1623773bda96SJonathan Lemon iowrite32(1, ®->enable); 1624773bda96SJonathan Lemon iowrite32(1, ®->intr_mask); 1625773bda96SJonathan Lemon iowrite32(1, ®->intr); 1626773bda96SJonathan Lemon } else { 1627773bda96SJonathan Lemon iowrite32(0, ®->intr_mask); 1628773bda96SJonathan Lemon iowrite32(0, ®->enable); 1629773bda96SJonathan Lemon } 1630773bda96SJonathan Lemon 1631773bda96SJonathan Lemon return 0; 1632773bda96SJonathan Lemon } 1633773bda96SJonathan Lemon 1634773bda96SJonathan Lemon static void 1635773bda96SJonathan Lemon ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext) 1636773bda96SJonathan Lemon { 1637a62a56d0SJonathan Lemon ext->info->enable(ext, ~0, false); 1638773bda96SJonathan Lemon pci_free_irq(ext->bp->pdev, ext->irq_vec, ext); 1639773bda96SJonathan Lemon kfree(ext); 1640773bda96SJonathan Lemon } 1641773bda96SJonathan Lemon 1642773bda96SJonathan Lemon static int 1643773bda96SJonathan Lemon ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r) 1644773bda96SJonathan Lemon { 1645773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1646773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext; 1647773bda96SJonathan Lemon int err; 1648773bda96SJonathan Lemon 1649773bda96SJonathan Lemon ext = kzalloc(sizeof(*ext), GFP_KERNEL); 1650773bda96SJonathan Lemon if (!ext) 1651773bda96SJonathan Lemon return -ENOMEM; 1652773bda96SJonathan Lemon 1653773bda96SJonathan Lemon ext->mem = ptp_ocp_get_mem(bp, r); 1654c7521d3aSDan Carpenter if (IS_ERR(ext->mem)) { 1655c7521d3aSDan Carpenter err = PTR_ERR(ext->mem); 1656773bda96SJonathan Lemon goto out; 1657c7521d3aSDan Carpenter } 1658773bda96SJonathan Lemon 1659773bda96SJonathan Lemon ext->bp = bp; 1660773bda96SJonathan Lemon ext->info = r->extra; 1661773bda96SJonathan Lemon ext->irq_vec = r->irq_vec; 1662773bda96SJonathan Lemon 1663773bda96SJonathan Lemon err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL, 166456ec4403SJonathan Lemon ext, "ocp%d.%s", bp->id, r->name); 1665773bda96SJonathan Lemon if (err) { 1666773bda96SJonathan Lemon dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec); 1667773bda96SJonathan Lemon goto out; 1668773bda96SJonathan Lemon } 1669773bda96SJonathan Lemon 1670773bda96SJonathan Lemon bp_assign_entry(bp, r, ext); 1671773bda96SJonathan Lemon 1672773bda96SJonathan Lemon return 0; 1673773bda96SJonathan Lemon 1674773bda96SJonathan Lemon out: 1675773bda96SJonathan Lemon kfree(ext); 1676773bda96SJonathan Lemon return err; 1677773bda96SJonathan Lemon } 1678773bda96SJonathan Lemon 1679773bda96SJonathan Lemon static int 1680773bda96SJonathan Lemon ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r) 1681773bda96SJonathan Lemon { 1682773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1683773bda96SJonathan Lemon struct uart_8250_port uart; 1684773bda96SJonathan Lemon 1685773bda96SJonathan Lemon /* Setting UPF_IOREMAP and leaving port.membase unspecified lets 1686773bda96SJonathan Lemon * the serial port device claim and release the pci resource. 1687773bda96SJonathan Lemon */ 1688773bda96SJonathan Lemon memset(&uart, 0, sizeof(uart)); 1689773bda96SJonathan Lemon uart.port.dev = &pdev->dev; 1690773bda96SJonathan Lemon uart.port.iotype = UPIO_MEM; 1691773bda96SJonathan Lemon uart.port.regshift = 2; 1692773bda96SJonathan Lemon uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset; 1693773bda96SJonathan Lemon uart.port.irq = pci_irq_vector(pdev, r->irq_vec); 1694773bda96SJonathan Lemon uart.port.uartclk = 50000000; 1695c17c4059SJonathan Lemon uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP | UPF_NO_THRE_TEST; 1696773bda96SJonathan Lemon uart.port.type = PORT_16550A; 1697773bda96SJonathan Lemon 1698773bda96SJonathan Lemon return serial8250_register_8250_port(&uart); 1699773bda96SJonathan Lemon } 1700773bda96SJonathan Lemon 1701773bda96SJonathan Lemon static int 1702773bda96SJonathan Lemon ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r) 1703773bda96SJonathan Lemon { 1704773bda96SJonathan Lemon int port; 1705773bda96SJonathan Lemon 1706773bda96SJonathan Lemon port = ptp_ocp_serial_line(bp, r); 1707773bda96SJonathan Lemon if (port < 0) 1708773bda96SJonathan Lemon return port; 1709773bda96SJonathan Lemon 1710773bda96SJonathan Lemon bp_assign_entry(bp, r, port); 1711773bda96SJonathan Lemon 1712773bda96SJonathan Lemon return 0; 1713773bda96SJonathan Lemon } 1714773bda96SJonathan Lemon 1715773bda96SJonathan Lemon static int 1716773bda96SJonathan Lemon ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r) 1717773bda96SJonathan Lemon { 1718773bda96SJonathan Lemon void __iomem *mem; 1719773bda96SJonathan Lemon 1720773bda96SJonathan Lemon mem = ptp_ocp_get_mem(bp, r); 1721c7521d3aSDan Carpenter if (IS_ERR(mem)) 1722c7521d3aSDan Carpenter return PTR_ERR(mem); 1723773bda96SJonathan Lemon 1724773bda96SJonathan Lemon bp_assign_entry(bp, r, mem); 1725773bda96SJonathan Lemon 1726773bda96SJonathan Lemon return 0; 1727773bda96SJonathan Lemon } 1728773bda96SJonathan Lemon 1729e3516bb4SJonathan Lemon static void 1730e3516bb4SJonathan Lemon ptp_ocp_nmea_out_init(struct ptp_ocp *bp) 1731e3516bb4SJonathan Lemon { 1732e3516bb4SJonathan Lemon if (!bp->nmea_out) 1733e3516bb4SJonathan Lemon return; 1734e3516bb4SJonathan Lemon 1735e3516bb4SJonathan Lemon iowrite32(0, &bp->nmea_out->ctrl); /* disable */ 1736e3516bb4SJonathan Lemon iowrite32(7, &bp->nmea_out->uart_baud); /* 115200 */ 1737e3516bb4SJonathan Lemon iowrite32(1, &bp->nmea_out->ctrl); /* enable */ 1738e3516bb4SJonathan Lemon } 1739e3516bb4SJonathan Lemon 1740a509a7c6SJonathan Lemon static void 1741b325af3cSJonathan Lemon _ptp_ocp_signal_init(struct ptp_ocp_signal *s, struct signal_reg __iomem *reg) 1742b325af3cSJonathan Lemon { 1743b325af3cSJonathan Lemon u32 val; 1744b325af3cSJonathan Lemon 1745b325af3cSJonathan Lemon iowrite32(0, ®->enable); /* disable */ 1746b325af3cSJonathan Lemon 1747b325af3cSJonathan Lemon val = ioread32(®->polarity); 1748b325af3cSJonathan Lemon s->polarity = val ? true : false; 1749b325af3cSJonathan Lemon s->duty = 50; 1750b325af3cSJonathan Lemon } 1751b325af3cSJonathan Lemon 1752b325af3cSJonathan Lemon static void 1753b325af3cSJonathan Lemon ptp_ocp_signal_init(struct ptp_ocp *bp) 1754b325af3cSJonathan Lemon { 1755b325af3cSJonathan Lemon int i; 1756b325af3cSJonathan Lemon 1757b325af3cSJonathan Lemon for (i = 0; i < 4; i++) 1758b325af3cSJonathan Lemon if (bp->signal_out[i]) 1759b325af3cSJonathan Lemon _ptp_ocp_signal_init(&bp->signal[i], 1760b325af3cSJonathan Lemon bp->signal_out[i]->mem); 1761b325af3cSJonathan Lemon } 1762b325af3cSJonathan Lemon 1763b325af3cSJonathan Lemon static void 1764a509a7c6SJonathan Lemon ptp_ocp_sma_init(struct ptp_ocp *bp) 1765a509a7c6SJonathan Lemon { 1766a509a7c6SJonathan Lemon u32 reg; 1767a509a7c6SJonathan Lemon int i; 1768a509a7c6SJonathan Lemon 1769a509a7c6SJonathan Lemon /* defaults */ 1770a509a7c6SJonathan Lemon bp->sma[0].mode = SMA_MODE_IN; 1771a509a7c6SJonathan Lemon bp->sma[1].mode = SMA_MODE_IN; 1772a509a7c6SJonathan Lemon bp->sma[2].mode = SMA_MODE_OUT; 1773a509a7c6SJonathan Lemon bp->sma[3].mode = SMA_MODE_OUT; 1774a509a7c6SJonathan Lemon 1775a509a7c6SJonathan Lemon /* If no SMA1 map, the pin functions and directions are fixed. */ 1776a509a7c6SJonathan Lemon if (!bp->sma_map1) { 1777a509a7c6SJonathan Lemon for (i = 0; i < 4; i++) { 1778a509a7c6SJonathan Lemon bp->sma[i].fixed_fcn = true; 1779a509a7c6SJonathan Lemon bp->sma[i].fixed_dir = true; 1780a509a7c6SJonathan Lemon } 1781a509a7c6SJonathan Lemon return; 1782a509a7c6SJonathan Lemon } 1783a509a7c6SJonathan Lemon 1784a509a7c6SJonathan Lemon /* If SMA2 GPIO output map is all 1, it is not present. 1785a509a7c6SJonathan Lemon * This indicates the firmware has fixed direction SMA pins. 1786a509a7c6SJonathan Lemon */ 1787a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map2->gpio2); 1788a509a7c6SJonathan Lemon if (reg == 0xffffffff) { 1789a509a7c6SJonathan Lemon for (i = 0; i < 4; i++) 1790a509a7c6SJonathan Lemon bp->sma[i].fixed_dir = true; 1791a509a7c6SJonathan Lemon } else { 1792a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map1->gpio1); 1793a509a7c6SJonathan Lemon bp->sma[0].mode = reg & BIT(15) ? SMA_MODE_IN : SMA_MODE_OUT; 1794a509a7c6SJonathan Lemon bp->sma[1].mode = reg & BIT(31) ? SMA_MODE_IN : SMA_MODE_OUT; 1795a509a7c6SJonathan Lemon 1796a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map1->gpio2); 1797a509a7c6SJonathan Lemon bp->sma[2].mode = reg & BIT(15) ? SMA_MODE_OUT : SMA_MODE_IN; 1798a509a7c6SJonathan Lemon bp->sma[3].mode = reg & BIT(31) ? SMA_MODE_OUT : SMA_MODE_IN; 1799a509a7c6SJonathan Lemon } 1800a509a7c6SJonathan Lemon } 1801a509a7c6SJonathan Lemon 1802*1aa66a3aSJonathan Lemon static int 1803*1aa66a3aSJonathan Lemon ptp_ocp_fb_set_pins(struct ptp_ocp *bp) 1804*1aa66a3aSJonathan Lemon { 1805*1aa66a3aSJonathan Lemon struct ptp_pin_desc *config; 1806*1aa66a3aSJonathan Lemon int i; 1807*1aa66a3aSJonathan Lemon 1808*1aa66a3aSJonathan Lemon config = kzalloc(sizeof(*config) * 4, GFP_KERNEL); 1809*1aa66a3aSJonathan Lemon if (!config) 1810*1aa66a3aSJonathan Lemon return -ENOMEM; 1811*1aa66a3aSJonathan Lemon 1812*1aa66a3aSJonathan Lemon for (i = 0; i < 4; i++) { 1813*1aa66a3aSJonathan Lemon sprintf(config[i].name, "sma%d", i + 1); 1814*1aa66a3aSJonathan Lemon config[i].index = i; 1815*1aa66a3aSJonathan Lemon } 1816*1aa66a3aSJonathan Lemon 1817*1aa66a3aSJonathan Lemon bp->ptp_info.n_pins = 4; 1818*1aa66a3aSJonathan Lemon bp->ptp_info.pin_config = config; 1819*1aa66a3aSJonathan Lemon 1820*1aa66a3aSJonathan Lemon return 0; 1821*1aa66a3aSJonathan Lemon } 1822*1aa66a3aSJonathan Lemon 1823773bda96SJonathan Lemon /* FB specific board initializers; last "resource" registered. */ 1824773bda96SJonathan Lemon static int 1825773bda96SJonathan Lemon ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r) 1826773bda96SJonathan Lemon { 1827*1aa66a3aSJonathan Lemon int ver, err; 1828b325af3cSJonathan Lemon 1829773bda96SJonathan Lemon bp->flash_start = 1024 * 4096; 18300cfcdd1eSJonathan Lemon bp->eeprom_map = fb_eeprom_map; 1831b0ca789aSJonathan Lemon bp->fw_version = ioread32(&bp->image->version); 1832c205d53cSJonathan Lemon bp->attr_tbl = fb_timecard_groups; 1833c205d53cSJonathan Lemon bp->fw_cap = OCP_CAP_BASIC; 1834773bda96SJonathan Lemon 1835b325af3cSJonathan Lemon ver = bp->fw_version & 0xffff; 1836b325af3cSJonathan Lemon if (ver >= 19) 1837b325af3cSJonathan Lemon bp->fw_cap |= OCP_CAP_SIGNAL; 1838b325af3cSJonathan Lemon 1839065efcc5SJonathan Lemon ptp_ocp_tod_init(bp); 1840e3516bb4SJonathan Lemon ptp_ocp_nmea_out_init(bp); 1841a509a7c6SJonathan Lemon ptp_ocp_sma_init(bp); 1842b325af3cSJonathan Lemon ptp_ocp_signal_init(bp); 1843065efcc5SJonathan Lemon 1844*1aa66a3aSJonathan Lemon err = ptp_ocp_fb_set_pins(bp); 1845*1aa66a3aSJonathan Lemon if (err) 1846*1aa66a3aSJonathan Lemon return err; 1847*1aa66a3aSJonathan Lemon 1848773bda96SJonathan Lemon return ptp_ocp_init_clock(bp); 1849773bda96SJonathan Lemon } 1850773bda96SJonathan Lemon 185156ec4403SJonathan Lemon static bool 185256ec4403SJonathan Lemon ptp_ocp_allow_irq(struct ptp_ocp *bp, struct ocp_resource *r) 185356ec4403SJonathan Lemon { 185456ec4403SJonathan Lemon bool allow = !r->irq_vec || r->irq_vec < bp->n_irqs; 185556ec4403SJonathan Lemon 185656ec4403SJonathan Lemon if (!allow) 185756ec4403SJonathan Lemon dev_err(&bp->pdev->dev, "irq %d out of range, skipping %s\n", 185856ec4403SJonathan Lemon r->irq_vec, r->name); 185956ec4403SJonathan Lemon return allow; 186056ec4403SJonathan Lemon } 186156ec4403SJonathan Lemon 1862773bda96SJonathan Lemon static int 1863773bda96SJonathan Lemon ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data) 1864773bda96SJonathan Lemon { 1865773bda96SJonathan Lemon struct ocp_resource *r, *table; 1866773bda96SJonathan Lemon int err = 0; 1867773bda96SJonathan Lemon 1868773bda96SJonathan Lemon table = (struct ocp_resource *)driver_data; 1869773bda96SJonathan Lemon for (r = table; r->setup; r++) { 187056ec4403SJonathan Lemon if (!ptp_ocp_allow_irq(bp, r)) 187156ec4403SJonathan Lemon continue; 1872773bda96SJonathan Lemon err = r->setup(bp, r); 1873bceff290SJonathan Lemon if (err) { 1874bceff290SJonathan Lemon dev_err(&bp->pdev->dev, 1875bceff290SJonathan Lemon "Could not register %s: err %d\n", 1876bceff290SJonathan Lemon r->name, err); 1877773bda96SJonathan Lemon break; 1878773bda96SJonathan Lemon } 1879bceff290SJonathan Lemon } 1880773bda96SJonathan Lemon return err; 1881773bda96SJonathan Lemon } 1882773bda96SJonathan Lemon 18836baf2925SJonathan Lemon static void 18846baf2925SJonathan Lemon ptp_ocp_enable_fpga(u32 __iomem *reg, u32 bit, bool enable) 18856baf2925SJonathan Lemon { 18866baf2925SJonathan Lemon u32 ctrl; 18876baf2925SJonathan Lemon bool on; 18886baf2925SJonathan Lemon 18896baf2925SJonathan Lemon ctrl = ioread32(reg); 18906baf2925SJonathan Lemon on = ctrl & bit; 18916baf2925SJonathan Lemon if (on ^ enable) { 18926baf2925SJonathan Lemon ctrl &= ~bit; 18936baf2925SJonathan Lemon ctrl |= enable ? bit : 0; 18946baf2925SJonathan Lemon iowrite32(ctrl, reg); 18956baf2925SJonathan Lemon } 18966baf2925SJonathan Lemon } 18976baf2925SJonathan Lemon 18986baf2925SJonathan Lemon static void 18996baf2925SJonathan Lemon ptp_ocp_irig_out(struct ptp_ocp *bp, bool enable) 19006baf2925SJonathan Lemon { 19016baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->irig_out->ctrl, 19026baf2925SJonathan Lemon IRIG_M_CTRL_ENABLE, enable); 19036baf2925SJonathan Lemon } 19046baf2925SJonathan Lemon 19056baf2925SJonathan Lemon static void 19066baf2925SJonathan Lemon ptp_ocp_irig_in(struct ptp_ocp *bp, bool enable) 19076baf2925SJonathan Lemon { 19086baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->irig_in->ctrl, 19096baf2925SJonathan Lemon IRIG_S_CTRL_ENABLE, enable); 19106baf2925SJonathan Lemon } 19116baf2925SJonathan Lemon 19126baf2925SJonathan Lemon static void 19136baf2925SJonathan Lemon ptp_ocp_dcf_out(struct ptp_ocp *bp, bool enable) 19146baf2925SJonathan Lemon { 19156baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->dcf_out->ctrl, 19166baf2925SJonathan Lemon DCF_M_CTRL_ENABLE, enable); 19176baf2925SJonathan Lemon } 19186baf2925SJonathan Lemon 19196baf2925SJonathan Lemon static void 19206baf2925SJonathan Lemon ptp_ocp_dcf_in(struct ptp_ocp *bp, bool enable) 19216baf2925SJonathan Lemon { 19226baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->dcf_in->ctrl, 19236baf2925SJonathan Lemon DCF_S_CTRL_ENABLE, enable); 19246baf2925SJonathan Lemon } 19256baf2925SJonathan Lemon 19266baf2925SJonathan Lemon static void 19276baf2925SJonathan Lemon __handle_signal_outputs(struct ptp_ocp *bp, u32 val) 19286baf2925SJonathan Lemon { 19296baf2925SJonathan Lemon ptp_ocp_irig_out(bp, val & 0x00100010); 19306baf2925SJonathan Lemon ptp_ocp_dcf_out(bp, val & 0x00200020); 19316baf2925SJonathan Lemon } 19326baf2925SJonathan Lemon 19336baf2925SJonathan Lemon static void 19346baf2925SJonathan Lemon __handle_signal_inputs(struct ptp_ocp *bp, u32 val) 19356baf2925SJonathan Lemon { 19366baf2925SJonathan Lemon ptp_ocp_irig_in(bp, val & 0x00100010); 19376baf2925SJonathan Lemon ptp_ocp_dcf_in(bp, val & 0x00200020); 19386baf2925SJonathan Lemon } 19396baf2925SJonathan Lemon 1940e1daf0ecSJonathan Lemon /* 1941e1daf0ecSJonathan Lemon * ANT0 == gps (in) 1942e1daf0ecSJonathan Lemon * ANT1 == sma1 (in) 1943e1daf0ecSJonathan Lemon * ANT2 == sma2 (in) 1944e1daf0ecSJonathan Lemon * ANT3 == sma3 (out) 1945e1daf0ecSJonathan Lemon * ANT4 == sma4 (out) 1946e1daf0ecSJonathan Lemon */ 1947e1daf0ecSJonathan Lemon 1948e1daf0ecSJonathan Lemon static ssize_t 1949b2c4f0acSJonathan Lemon ptp_ocp_show_output(u32 val, char *buf, int def_val) 1950e1daf0ecSJonathan Lemon { 1951e1daf0ecSJonathan Lemon const char *name; 1952e1daf0ecSJonathan Lemon ssize_t count; 1953e1daf0ecSJonathan Lemon 1954e1daf0ecSJonathan Lemon count = sysfs_emit(buf, "OUT: "); 1955e1daf0ecSJonathan Lemon name = ptp_ocp_select_name_from_val(ptp_ocp_sma_out, val); 1956e1daf0ecSJonathan Lemon if (!name) 1957b2c4f0acSJonathan Lemon name = ptp_ocp_select_name_from_val(ptp_ocp_sma_out, def_val); 1958e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s\n", name); 1959e1daf0ecSJonathan Lemon return count; 1960e1daf0ecSJonathan Lemon } 1961e1daf0ecSJonathan Lemon 1962e1daf0ecSJonathan Lemon static ssize_t 1963b2c4f0acSJonathan Lemon ptp_ocp_show_inputs(u32 val, char *buf, int def_val) 1964e1daf0ecSJonathan Lemon { 1965e1daf0ecSJonathan Lemon const char *name; 1966e1daf0ecSJonathan Lemon ssize_t count; 1967e1daf0ecSJonathan Lemon int i; 1968e1daf0ecSJonathan Lemon 1969e1daf0ecSJonathan Lemon count = sysfs_emit(buf, "IN: "); 1970e1daf0ecSJonathan Lemon for (i = 0; i < ARRAY_SIZE(ptp_ocp_sma_in); i++) { 1971e1daf0ecSJonathan Lemon if (val & ptp_ocp_sma_in[i].value) { 1972e1daf0ecSJonathan Lemon name = ptp_ocp_sma_in[i].name; 1973e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", name); 1974e1daf0ecSJonathan Lemon } 1975e1daf0ecSJonathan Lemon } 1976b2c4f0acSJonathan Lemon if (!val && def_val >= 0) { 1977b2c4f0acSJonathan Lemon name = ptp_ocp_select_name_from_val(ptp_ocp_sma_in, def_val); 1978b2c4f0acSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", name); 1979b2c4f0acSJonathan Lemon } 1980e1daf0ecSJonathan Lemon if (count) 1981e1daf0ecSJonathan Lemon count--; 1982e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "\n"); 1983e1daf0ecSJonathan Lemon return count; 1984e1daf0ecSJonathan Lemon } 1985e1daf0ecSJonathan Lemon 1986e1daf0ecSJonathan Lemon static int 1987e1daf0ecSJonathan Lemon sma_parse_inputs(const char *buf, enum ptp_ocp_sma_mode *mode) 1988e1daf0ecSJonathan Lemon { 1989e1daf0ecSJonathan Lemon struct ocp_selector *tbl[] = { ptp_ocp_sma_in, ptp_ocp_sma_out }; 1990e1daf0ecSJonathan Lemon int idx, count, dir; 1991e1daf0ecSJonathan Lemon char **argv; 1992e1daf0ecSJonathan Lemon int ret; 1993e1daf0ecSJonathan Lemon 1994e1daf0ecSJonathan Lemon argv = argv_split(GFP_KERNEL, buf, &count); 1995e1daf0ecSJonathan Lemon if (!argv) 1996e1daf0ecSJonathan Lemon return -ENOMEM; 1997e1daf0ecSJonathan Lemon 1998e1daf0ecSJonathan Lemon ret = -EINVAL; 1999e1daf0ecSJonathan Lemon if (!count) 2000e1daf0ecSJonathan Lemon goto out; 2001e1daf0ecSJonathan Lemon 2002e1daf0ecSJonathan Lemon idx = 0; 2003e1daf0ecSJonathan Lemon dir = *mode == SMA_MODE_IN ? 0 : 1; 2004a509a7c6SJonathan Lemon if (!strcasecmp("IN:", argv[0])) { 2005e1daf0ecSJonathan Lemon dir = 0; 2006e1daf0ecSJonathan Lemon idx++; 2007e1daf0ecSJonathan Lemon } 2008e1daf0ecSJonathan Lemon if (!strcasecmp("OUT:", argv[0])) { 2009e1daf0ecSJonathan Lemon dir = 1; 2010e1daf0ecSJonathan Lemon idx++; 2011e1daf0ecSJonathan Lemon } 2012e1daf0ecSJonathan Lemon *mode = dir == 0 ? SMA_MODE_IN : SMA_MODE_OUT; 2013e1daf0ecSJonathan Lemon 2014e1daf0ecSJonathan Lemon ret = 0; 2015e1daf0ecSJonathan Lemon for (; idx < count; idx++) 2016e1daf0ecSJonathan Lemon ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]); 2017e1daf0ecSJonathan Lemon if (ret < 0) 2018e1daf0ecSJonathan Lemon ret = -EINVAL; 2019e1daf0ecSJonathan Lemon 2020e1daf0ecSJonathan Lemon out: 2021e1daf0ecSJonathan Lemon argv_free(argv); 2022e1daf0ecSJonathan Lemon return ret; 2023e1daf0ecSJonathan Lemon } 2024e1daf0ecSJonathan Lemon 2025a509a7c6SJonathan Lemon static u32 2026a509a7c6SJonathan Lemon ptp_ocp_sma_get(struct ptp_ocp *bp, int sma_nr, enum ptp_ocp_sma_mode mode) 2027e1daf0ecSJonathan Lemon { 2028a509a7c6SJonathan Lemon u32 __iomem *gpio; 2029a509a7c6SJonathan Lemon u32 shift; 2030a509a7c6SJonathan Lemon 2031a509a7c6SJonathan Lemon if (bp->sma[sma_nr - 1].fixed_fcn) 2032a509a7c6SJonathan Lemon return (sma_nr - 1) & 1; 2033a509a7c6SJonathan Lemon 2034a509a7c6SJonathan Lemon if (mode == SMA_MODE_IN) 2035a509a7c6SJonathan Lemon gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1; 2036a509a7c6SJonathan Lemon else 2037a509a7c6SJonathan Lemon gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2; 2038a509a7c6SJonathan Lemon shift = sma_nr & 1 ? 0 : 16; 2039a509a7c6SJonathan Lemon 2040a509a7c6SJonathan Lemon return (ioread32(gpio) >> shift) & 0xffff; 2041a509a7c6SJonathan Lemon } 2042a509a7c6SJonathan Lemon 2043a509a7c6SJonathan Lemon static ssize_t 2044a509a7c6SJonathan Lemon ptp_ocp_sma_show(struct ptp_ocp *bp, int sma_nr, char *buf, 2045b2c4f0acSJonathan Lemon int default_in_val, int default_out_val) 2046a509a7c6SJonathan Lemon { 2047a509a7c6SJonathan Lemon struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1]; 2048a509a7c6SJonathan Lemon u32 val; 2049a509a7c6SJonathan Lemon 2050a509a7c6SJonathan Lemon val = ptp_ocp_sma_get(bp, sma_nr, sma->mode) & SMA_SELECT_MASK; 2051e1daf0ecSJonathan Lemon 2052b2c4f0acSJonathan Lemon if (sma->mode == SMA_MODE_IN) { 2053b2c4f0acSJonathan Lemon if (sma->disabled) 2054b2c4f0acSJonathan Lemon val = SMA_DISABLE; 2055b2c4f0acSJonathan Lemon return ptp_ocp_show_inputs(val, buf, default_in_val); 2056b2c4f0acSJonathan Lemon } 2057e1daf0ecSJonathan Lemon 2058b2c4f0acSJonathan Lemon return ptp_ocp_show_output(val, buf, default_out_val); 2059e1daf0ecSJonathan Lemon } 2060e1daf0ecSJonathan Lemon 2061e1daf0ecSJonathan Lemon static ssize_t 2062e1daf0ecSJonathan Lemon sma1_show(struct device *dev, struct device_attribute *attr, char *buf) 2063e1daf0ecSJonathan Lemon { 2064e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2065e1daf0ecSJonathan Lemon 2066a509a7c6SJonathan Lemon return ptp_ocp_sma_show(bp, 1, buf, 0, 1); 2067e1daf0ecSJonathan Lemon } 2068e1daf0ecSJonathan Lemon 2069e1daf0ecSJonathan Lemon static ssize_t 2070e1daf0ecSJonathan Lemon sma2_show(struct device *dev, struct device_attribute *attr, char *buf) 2071e1daf0ecSJonathan Lemon { 2072e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2073e1daf0ecSJonathan Lemon 2074a509a7c6SJonathan Lemon return ptp_ocp_sma_show(bp, 2, buf, -1, 1); 2075e1daf0ecSJonathan Lemon } 2076e1daf0ecSJonathan Lemon 2077e1daf0ecSJonathan Lemon static ssize_t 2078e1daf0ecSJonathan Lemon sma3_show(struct device *dev, struct device_attribute *attr, char *buf) 2079e1daf0ecSJonathan Lemon { 2080e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2081e1daf0ecSJonathan Lemon 2082a509a7c6SJonathan Lemon return ptp_ocp_sma_show(bp, 3, buf, -1, 0); 2083e1daf0ecSJonathan Lemon } 2084e1daf0ecSJonathan Lemon 2085e1daf0ecSJonathan Lemon static ssize_t 2086e1daf0ecSJonathan Lemon sma4_show(struct device *dev, struct device_attribute *attr, char *buf) 2087e1daf0ecSJonathan Lemon { 2088e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2089e1daf0ecSJonathan Lemon 2090a509a7c6SJonathan Lemon return ptp_ocp_sma_show(bp, 4, buf, -1, 1); 2091e1daf0ecSJonathan Lemon } 2092e1daf0ecSJonathan Lemon 2093e1daf0ecSJonathan Lemon static void 2094a509a7c6SJonathan Lemon ptp_ocp_sma_store_output(struct ptp_ocp *bp, int sma_nr, u32 val) 2095e1daf0ecSJonathan Lemon { 2096a509a7c6SJonathan Lemon u32 reg, mask, shift; 2097e1daf0ecSJonathan Lemon unsigned long flags; 2098a509a7c6SJonathan Lemon u32 __iomem *gpio; 2099a509a7c6SJonathan Lemon 2100a509a7c6SJonathan Lemon gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2; 2101a509a7c6SJonathan Lemon shift = sma_nr & 1 ? 0 : 16; 2102e1daf0ecSJonathan Lemon 2103e1daf0ecSJonathan Lemon mask = 0xffff << (16 - shift); 2104e1daf0ecSJonathan Lemon 2105e1daf0ecSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 2106e1daf0ecSJonathan Lemon 2107a509a7c6SJonathan Lemon reg = ioread32(gpio); 2108a509a7c6SJonathan Lemon reg = (reg & mask) | (val << shift); 21096baf2925SJonathan Lemon 2110a509a7c6SJonathan Lemon __handle_signal_outputs(bp, reg); 21116baf2925SJonathan Lemon 2112a509a7c6SJonathan Lemon iowrite32(reg, gpio); 2113e1daf0ecSJonathan Lemon 2114e1daf0ecSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 2115e1daf0ecSJonathan Lemon } 2116e1daf0ecSJonathan Lemon 2117e1daf0ecSJonathan Lemon static void 2118a509a7c6SJonathan Lemon ptp_ocp_sma_store_inputs(struct ptp_ocp *bp, int sma_nr, u32 val) 2119e1daf0ecSJonathan Lemon { 2120a509a7c6SJonathan Lemon u32 reg, mask, shift; 2121e1daf0ecSJonathan Lemon unsigned long flags; 2122a509a7c6SJonathan Lemon u32 __iomem *gpio; 2123a509a7c6SJonathan Lemon 2124a509a7c6SJonathan Lemon gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1; 2125a509a7c6SJonathan Lemon shift = sma_nr & 1 ? 0 : 16; 2126e1daf0ecSJonathan Lemon 2127e1daf0ecSJonathan Lemon mask = 0xffff << (16 - shift); 2128e1daf0ecSJonathan Lemon 2129e1daf0ecSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 2130e1daf0ecSJonathan Lemon 2131a509a7c6SJonathan Lemon reg = ioread32(gpio); 2132a509a7c6SJonathan Lemon reg = (reg & mask) | (val << shift); 21336baf2925SJonathan Lemon 2134a509a7c6SJonathan Lemon __handle_signal_inputs(bp, reg); 21356baf2925SJonathan Lemon 2136a509a7c6SJonathan Lemon iowrite32(reg, gpio); 2137e1daf0ecSJonathan Lemon 2138e1daf0ecSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 2139e1daf0ecSJonathan Lemon } 2140e1daf0ecSJonathan Lemon 2141a509a7c6SJonathan Lemon static int 2142a509a7c6SJonathan Lemon ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr) 2143e1daf0ecSJonathan Lemon { 2144a509a7c6SJonathan Lemon struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1]; 2145e1daf0ecSJonathan Lemon enum ptp_ocp_sma_mode mode; 2146e1daf0ecSJonathan Lemon int val; 2147e1daf0ecSJonathan Lemon 2148e1daf0ecSJonathan Lemon mode = sma->mode; 2149e1daf0ecSJonathan Lemon val = sma_parse_inputs(buf, &mode); 2150e1daf0ecSJonathan Lemon if (val < 0) 2151e1daf0ecSJonathan Lemon return val; 2152e1daf0ecSJonathan Lemon 2153b2c4f0acSJonathan Lemon if (sma->fixed_dir && (mode != sma->mode || val & SMA_DISABLE)) 2154e1daf0ecSJonathan Lemon return -EOPNOTSUPP; 2155e1daf0ecSJonathan Lemon 2156a509a7c6SJonathan Lemon if (sma->fixed_fcn) { 2157a509a7c6SJonathan Lemon if (val != ((sma_nr - 1) & 1)) 2158e1daf0ecSJonathan Lemon return -EOPNOTSUPP; 2159a509a7c6SJonathan Lemon return 0; 2160e1daf0ecSJonathan Lemon } 2161e1daf0ecSJonathan Lemon 2162b2c4f0acSJonathan Lemon sma->disabled = !!(val & SMA_DISABLE); 2163b2c4f0acSJonathan Lemon 2164a509a7c6SJonathan Lemon if (mode != sma->mode) { 2165a509a7c6SJonathan Lemon if (mode == SMA_MODE_IN) 2166a509a7c6SJonathan Lemon ptp_ocp_sma_store_output(bp, sma_nr, 0); 2167e1daf0ecSJonathan Lemon else 2168a509a7c6SJonathan Lemon ptp_ocp_sma_store_inputs(bp, sma_nr, 0); 2169a509a7c6SJonathan Lemon sma->mode = mode; 2170a509a7c6SJonathan Lemon } 2171a509a7c6SJonathan Lemon 2172a509a7c6SJonathan Lemon if (!sma->fixed_dir) 2173a509a7c6SJonathan Lemon val |= SMA_ENABLE; /* add enable bit */ 2174a509a7c6SJonathan Lemon 2175b2c4f0acSJonathan Lemon if (sma->disabled) 2176b2c4f0acSJonathan Lemon val = 0; 2177b2c4f0acSJonathan Lemon 2178a509a7c6SJonathan Lemon if (mode == SMA_MODE_IN) 2179a509a7c6SJonathan Lemon ptp_ocp_sma_store_inputs(bp, sma_nr, val); 2180a509a7c6SJonathan Lemon else 2181a509a7c6SJonathan Lemon ptp_ocp_sma_store_output(bp, sma_nr, val); 2182e1daf0ecSJonathan Lemon 2183e1daf0ecSJonathan Lemon return 0; 2184e1daf0ecSJonathan Lemon } 2185e1daf0ecSJonathan Lemon 2186e1daf0ecSJonathan Lemon static ssize_t 2187e1daf0ecSJonathan Lemon sma1_store(struct device *dev, struct device_attribute *attr, 2188e1daf0ecSJonathan Lemon const char *buf, size_t count) 2189e1daf0ecSJonathan Lemon { 2190e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2191e1daf0ecSJonathan Lemon int err; 2192e1daf0ecSJonathan Lemon 2193a509a7c6SJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 1); 2194e1daf0ecSJonathan Lemon return err ? err : count; 2195e1daf0ecSJonathan Lemon } 2196e1daf0ecSJonathan Lemon 2197e1daf0ecSJonathan Lemon static ssize_t 2198e1daf0ecSJonathan Lemon sma2_store(struct device *dev, struct device_attribute *attr, 2199e1daf0ecSJonathan Lemon const char *buf, size_t count) 2200e1daf0ecSJonathan Lemon { 2201e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2202e1daf0ecSJonathan Lemon int err; 2203e1daf0ecSJonathan Lemon 2204a509a7c6SJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 2); 2205e1daf0ecSJonathan Lemon return err ? err : count; 2206e1daf0ecSJonathan Lemon } 2207e1daf0ecSJonathan Lemon 2208e1daf0ecSJonathan Lemon static ssize_t 2209e1daf0ecSJonathan Lemon sma3_store(struct device *dev, struct device_attribute *attr, 2210e1daf0ecSJonathan Lemon const char *buf, size_t count) 2211e1daf0ecSJonathan Lemon { 2212e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2213e1daf0ecSJonathan Lemon int err; 2214e1daf0ecSJonathan Lemon 2215a509a7c6SJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 3); 2216e1daf0ecSJonathan Lemon return err ? err : count; 2217e1daf0ecSJonathan Lemon } 2218e1daf0ecSJonathan Lemon 2219e1daf0ecSJonathan Lemon static ssize_t 2220e1daf0ecSJonathan Lemon sma4_store(struct device *dev, struct device_attribute *attr, 2221e1daf0ecSJonathan Lemon const char *buf, size_t count) 2222e1daf0ecSJonathan Lemon { 2223e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2224e1daf0ecSJonathan Lemon int err; 2225e1daf0ecSJonathan Lemon 2226a509a7c6SJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 4); 2227e1daf0ecSJonathan Lemon return err ? err : count; 2228e1daf0ecSJonathan Lemon } 2229e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma1); 2230e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma2); 2231e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma3); 2232e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma4); 2233e1daf0ecSJonathan Lemon 2234e1daf0ecSJonathan Lemon static ssize_t 2235e1daf0ecSJonathan Lemon available_sma_inputs_show(struct device *dev, 2236e1daf0ecSJonathan Lemon struct device_attribute *attr, char *buf) 2237e1daf0ecSJonathan Lemon { 2238e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_sma_in, buf); 2239e1daf0ecSJonathan Lemon } 2240e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_inputs); 2241e1daf0ecSJonathan Lemon 2242e1daf0ecSJonathan Lemon static ssize_t 2243e1daf0ecSJonathan Lemon available_sma_outputs_show(struct device *dev, 2244e1daf0ecSJonathan Lemon struct device_attribute *attr, char *buf) 2245e1daf0ecSJonathan Lemon { 2246e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_sma_out, buf); 2247e1daf0ecSJonathan Lemon } 2248e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_outputs); 2249e1daf0ecSJonathan Lemon 2250b325af3cSJonathan Lemon #define EXT_ATTR_RO(_group, _name, _val) \ 2251b325af3cSJonathan Lemon struct dev_ext_attribute dev_attr_##_group##_val##_##_name = \ 2252b325af3cSJonathan Lemon { __ATTR_RO(_name), (void *)_val } 2253b325af3cSJonathan Lemon #define EXT_ATTR_RW(_group, _name, _val) \ 2254b325af3cSJonathan Lemon struct dev_ext_attribute dev_attr_##_group##_val##_##_name = \ 2255b325af3cSJonathan Lemon { __ATTR_RW(_name), (void *)_val } 2256b325af3cSJonathan Lemon #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr) 2257b325af3cSJonathan Lemon 2258b325af3cSJonathan Lemon /* period [duty [phase [polarity]]] */ 2259b325af3cSJonathan Lemon static ssize_t 2260b325af3cSJonathan Lemon signal_store(struct device *dev, struct device_attribute *attr, 2261b325af3cSJonathan Lemon const char *buf, size_t count) 2262b325af3cSJonathan Lemon { 2263b325af3cSJonathan Lemon struct dev_ext_attribute *ea = to_ext_attr(attr); 2264b325af3cSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2265b325af3cSJonathan Lemon struct ptp_ocp_signal s = { }; 2266b325af3cSJonathan Lemon int gen = (uintptr_t)ea->var; 2267b325af3cSJonathan Lemon int argc, err; 2268b325af3cSJonathan Lemon char **argv; 2269b325af3cSJonathan Lemon 2270b325af3cSJonathan Lemon argv = argv_split(GFP_KERNEL, buf, &argc); 2271b325af3cSJonathan Lemon if (!argv) 2272b325af3cSJonathan Lemon return -ENOMEM; 2273b325af3cSJonathan Lemon 2274b325af3cSJonathan Lemon err = -EINVAL; 2275b325af3cSJonathan Lemon s.duty = bp->signal[gen].duty; 2276b325af3cSJonathan Lemon s.phase = bp->signal[gen].phase; 2277b325af3cSJonathan Lemon s.period = bp->signal[gen].period; 2278b325af3cSJonathan Lemon s.polarity = bp->signal[gen].polarity; 2279b325af3cSJonathan Lemon 2280b325af3cSJonathan Lemon switch (argc) { 2281b325af3cSJonathan Lemon case 4: 2282b325af3cSJonathan Lemon argc--; 2283b325af3cSJonathan Lemon err = kstrtobool(argv[argc], &s.polarity); 2284b325af3cSJonathan Lemon if (err) 2285b325af3cSJonathan Lemon goto out; 2286b325af3cSJonathan Lemon fallthrough; 2287b325af3cSJonathan Lemon case 3: 2288b325af3cSJonathan Lemon argc--; 2289b325af3cSJonathan Lemon err = kstrtou64(argv[argc], 0, &s.phase); 2290b325af3cSJonathan Lemon if (err) 2291b325af3cSJonathan Lemon goto out; 2292b325af3cSJonathan Lemon fallthrough; 2293b325af3cSJonathan Lemon case 2: 2294b325af3cSJonathan Lemon argc--; 2295b325af3cSJonathan Lemon err = kstrtoint(argv[argc], 0, &s.duty); 2296b325af3cSJonathan Lemon if (err) 2297b325af3cSJonathan Lemon goto out; 2298b325af3cSJonathan Lemon fallthrough; 2299b325af3cSJonathan Lemon case 1: 2300b325af3cSJonathan Lemon argc--; 2301b325af3cSJonathan Lemon err = kstrtou64(argv[argc], 0, &s.period); 2302b325af3cSJonathan Lemon if (err) 2303b325af3cSJonathan Lemon goto out; 2304b325af3cSJonathan Lemon break; 2305b325af3cSJonathan Lemon default: 2306b325af3cSJonathan Lemon goto out; 2307b325af3cSJonathan Lemon } 2308b325af3cSJonathan Lemon 2309b325af3cSJonathan Lemon err = ptp_ocp_signal_set(bp, gen, &s); 2310b325af3cSJonathan Lemon if (err) 2311b325af3cSJonathan Lemon goto out; 2312b325af3cSJonathan Lemon 2313b325af3cSJonathan Lemon err = ptp_ocp_signal_enable(bp->signal_out[gen], gen, s.period != 0); 2314b325af3cSJonathan Lemon 2315b325af3cSJonathan Lemon out: 2316b325af3cSJonathan Lemon argv_free(argv); 2317b325af3cSJonathan Lemon return err ? err : count; 2318b325af3cSJonathan Lemon } 2319b325af3cSJonathan Lemon 2320b325af3cSJonathan Lemon static ssize_t 2321b325af3cSJonathan Lemon signal_show(struct device *dev, struct device_attribute *attr, char *buf) 2322b325af3cSJonathan Lemon { 2323b325af3cSJonathan Lemon struct dev_ext_attribute *ea = to_ext_attr(attr); 2324b325af3cSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2325b325af3cSJonathan Lemon struct ptp_ocp_signal *signal; 2326b325af3cSJonathan Lemon struct timespec64 ts; 2327b325af3cSJonathan Lemon ssize_t count; 2328b325af3cSJonathan Lemon int i; 2329b325af3cSJonathan Lemon 2330b325af3cSJonathan Lemon i = (uintptr_t)ea->var; 2331b325af3cSJonathan Lemon signal = &bp->signal[i]; 2332b325af3cSJonathan Lemon 2333b325af3cSJonathan Lemon count = sysfs_emit(buf, "%llu %d %llu %d", signal->period, 2334b325af3cSJonathan Lemon signal->duty, signal->phase, signal->polarity); 2335b325af3cSJonathan Lemon 2336b325af3cSJonathan Lemon ts = ktime_to_timespec64(signal->start); 2337b325af3cSJonathan Lemon count += sysfs_emit_at(buf, count, " %ptT TAI\n", &ts); 2338b325af3cSJonathan Lemon 2339b325af3cSJonathan Lemon return count; 2340b325af3cSJonathan Lemon } 2341b325af3cSJonathan Lemon static EXT_ATTR_RW(signal, signal, 0); 2342b325af3cSJonathan Lemon static EXT_ATTR_RW(signal, signal, 1); 2343b325af3cSJonathan Lemon static EXT_ATTR_RW(signal, signal, 2); 2344b325af3cSJonathan Lemon static EXT_ATTR_RW(signal, signal, 3); 2345b325af3cSJonathan Lemon 2346b325af3cSJonathan Lemon static ssize_t 2347b325af3cSJonathan Lemon duty_show(struct device *dev, struct device_attribute *attr, char *buf) 2348b325af3cSJonathan Lemon { 2349b325af3cSJonathan Lemon struct dev_ext_attribute *ea = to_ext_attr(attr); 2350b325af3cSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2351b325af3cSJonathan Lemon int i = (uintptr_t)ea->var; 2352b325af3cSJonathan Lemon 2353b325af3cSJonathan Lemon return sysfs_emit(buf, "%d\n", bp->signal[i].duty); 2354b325af3cSJonathan Lemon } 2355b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, duty, 0); 2356b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, duty, 1); 2357b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, duty, 2); 2358b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, duty, 3); 2359b325af3cSJonathan Lemon 2360b325af3cSJonathan Lemon static ssize_t 2361b325af3cSJonathan Lemon period_show(struct device *dev, struct device_attribute *attr, char *buf) 2362b325af3cSJonathan Lemon { 2363b325af3cSJonathan Lemon struct dev_ext_attribute *ea = to_ext_attr(attr); 2364b325af3cSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2365b325af3cSJonathan Lemon int i = (uintptr_t)ea->var; 2366b325af3cSJonathan Lemon 2367b325af3cSJonathan Lemon return sysfs_emit(buf, "%llu\n", bp->signal[i].period); 2368b325af3cSJonathan Lemon } 2369b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, period, 0); 2370b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, period, 1); 2371b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, period, 2); 2372b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, period, 3); 2373b325af3cSJonathan Lemon 2374b325af3cSJonathan Lemon static ssize_t 2375b325af3cSJonathan Lemon phase_show(struct device *dev, struct device_attribute *attr, char *buf) 2376b325af3cSJonathan Lemon { 2377b325af3cSJonathan Lemon struct dev_ext_attribute *ea = to_ext_attr(attr); 2378b325af3cSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2379b325af3cSJonathan Lemon int i = (uintptr_t)ea->var; 2380b325af3cSJonathan Lemon 2381b325af3cSJonathan Lemon return sysfs_emit(buf, "%llu\n", bp->signal[i].phase); 2382b325af3cSJonathan Lemon } 2383b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, phase, 0); 2384b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, phase, 1); 2385b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, phase, 2); 2386b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, phase, 3); 2387b325af3cSJonathan Lemon 2388b325af3cSJonathan Lemon static ssize_t 2389b325af3cSJonathan Lemon polarity_show(struct device *dev, struct device_attribute *attr, 2390b325af3cSJonathan Lemon char *buf) 2391b325af3cSJonathan Lemon { 2392b325af3cSJonathan Lemon struct dev_ext_attribute *ea = to_ext_attr(attr); 2393b325af3cSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2394b325af3cSJonathan Lemon int i = (uintptr_t)ea->var; 2395b325af3cSJonathan Lemon 2396b325af3cSJonathan Lemon return sysfs_emit(buf, "%d\n", bp->signal[i].polarity); 2397b325af3cSJonathan Lemon } 2398b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, polarity, 0); 2399b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, polarity, 1); 2400b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, polarity, 2); 2401b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, polarity, 3); 2402b325af3cSJonathan Lemon 2403b325af3cSJonathan Lemon static ssize_t 2404b325af3cSJonathan Lemon running_show(struct device *dev, struct device_attribute *attr, char *buf) 2405b325af3cSJonathan Lemon { 2406b325af3cSJonathan Lemon struct dev_ext_attribute *ea = to_ext_attr(attr); 2407b325af3cSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2408b325af3cSJonathan Lemon int i = (uintptr_t)ea->var; 2409b325af3cSJonathan Lemon 2410b325af3cSJonathan Lemon return sysfs_emit(buf, "%d\n", bp->signal[i].running); 2411b325af3cSJonathan Lemon } 2412b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, running, 0); 2413b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, running, 1); 2414b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, running, 2); 2415b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, running, 3); 2416b325af3cSJonathan Lemon 2417b325af3cSJonathan Lemon static ssize_t 2418b325af3cSJonathan Lemon start_show(struct device *dev, struct device_attribute *attr, char *buf) 2419b325af3cSJonathan Lemon { 2420b325af3cSJonathan Lemon struct dev_ext_attribute *ea = to_ext_attr(attr); 2421b325af3cSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2422b325af3cSJonathan Lemon int i = (uintptr_t)ea->var; 2423b325af3cSJonathan Lemon struct timespec64 ts; 2424b325af3cSJonathan Lemon 2425b325af3cSJonathan Lemon ts = ktime_to_timespec64(bp->signal[i].start); 2426b325af3cSJonathan Lemon return sysfs_emit(buf, "%llu.%lu\n", ts.tv_sec, ts.tv_nsec); 2427b325af3cSJonathan Lemon } 2428b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, start, 0); 2429b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, start, 1); 2430b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, start, 2); 2431b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, start, 3); 2432b325af3cSJonathan Lemon 2433773bda96SJonathan Lemon static ssize_t 2434773bda96SJonathan Lemon serialnum_show(struct device *dev, struct device_attribute *attr, char *buf) 2435773bda96SJonathan Lemon { 2436773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2437773bda96SJonathan Lemon 24380cfcdd1eSJonathan Lemon if (!bp->has_eeprom_data) 24390cfcdd1eSJonathan Lemon ptp_ocp_read_eeprom(bp); 2440773bda96SJonathan Lemon 2441773bda96SJonathan Lemon return sysfs_emit(buf, "%pM\n", bp->serial); 2442773bda96SJonathan Lemon } 2443773bda96SJonathan Lemon static DEVICE_ATTR_RO(serialnum); 2444773bda96SJonathan Lemon 2445773bda96SJonathan Lemon static ssize_t 2446ef0cfb34SJonathan Lemon gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf) 2447773bda96SJonathan Lemon { 2448773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2449773bda96SJonathan Lemon ssize_t ret; 2450773bda96SJonathan Lemon 2451ef0cfb34SJonathan Lemon if (bp->gnss_lost) 2452ef0cfb34SJonathan Lemon ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost); 2453773bda96SJonathan Lemon else 2454773bda96SJonathan Lemon ret = sysfs_emit(buf, "SYNC\n"); 2455773bda96SJonathan Lemon 2456773bda96SJonathan Lemon return ret; 2457773bda96SJonathan Lemon } 2458ef0cfb34SJonathan Lemon static DEVICE_ATTR_RO(gnss_sync); 2459773bda96SJonathan Lemon 2460773bda96SJonathan Lemon static ssize_t 246189260d87SJonathan Lemon utc_tai_offset_show(struct device *dev, 246289260d87SJonathan Lemon struct device_attribute *attr, char *buf) 246389260d87SJonathan Lemon { 246489260d87SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 246589260d87SJonathan Lemon 246689260d87SJonathan Lemon return sysfs_emit(buf, "%d\n", bp->utc_tai_offset); 246789260d87SJonathan Lemon } 246889260d87SJonathan Lemon 246989260d87SJonathan Lemon static ssize_t 247089260d87SJonathan Lemon utc_tai_offset_store(struct device *dev, 247189260d87SJonathan Lemon struct device_attribute *attr, 247289260d87SJonathan Lemon const char *buf, size_t count) 247389260d87SJonathan Lemon { 247489260d87SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 247589260d87SJonathan Lemon int err; 247689260d87SJonathan Lemon u32 val; 247789260d87SJonathan Lemon 247889260d87SJonathan Lemon err = kstrtou32(buf, 0, &val); 247989260d87SJonathan Lemon if (err) 248089260d87SJonathan Lemon return err; 248189260d87SJonathan Lemon 248289260d87SJonathan Lemon ptp_ocp_utc_distribute(bp, val); 248389260d87SJonathan Lemon 248489260d87SJonathan Lemon return count; 248589260d87SJonathan Lemon } 248689260d87SJonathan Lemon static DEVICE_ATTR_RW(utc_tai_offset); 248789260d87SJonathan Lemon 248889260d87SJonathan Lemon static ssize_t 24891acffc6eSJonathan Lemon ts_window_adjust_show(struct device *dev, 24901acffc6eSJonathan Lemon struct device_attribute *attr, char *buf) 24911acffc6eSJonathan Lemon { 24921acffc6eSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 24931acffc6eSJonathan Lemon 24941acffc6eSJonathan Lemon return sysfs_emit(buf, "%d\n", bp->ts_window_adjust); 24951acffc6eSJonathan Lemon } 24961acffc6eSJonathan Lemon 24971acffc6eSJonathan Lemon static ssize_t 24981acffc6eSJonathan Lemon ts_window_adjust_store(struct device *dev, 24991acffc6eSJonathan Lemon struct device_attribute *attr, 25001acffc6eSJonathan Lemon const char *buf, size_t count) 25011acffc6eSJonathan Lemon { 25021acffc6eSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 25031acffc6eSJonathan Lemon int err; 25041acffc6eSJonathan Lemon u32 val; 25051acffc6eSJonathan Lemon 25061acffc6eSJonathan Lemon err = kstrtou32(buf, 0, &val); 25071acffc6eSJonathan Lemon if (err) 25081acffc6eSJonathan Lemon return err; 25091acffc6eSJonathan Lemon 25101acffc6eSJonathan Lemon bp->ts_window_adjust = val; 25111acffc6eSJonathan Lemon 25121acffc6eSJonathan Lemon return count; 25131acffc6eSJonathan Lemon } 25141acffc6eSJonathan Lemon static DEVICE_ATTR_RW(ts_window_adjust); 25151acffc6eSJonathan Lemon 25161acffc6eSJonathan Lemon static ssize_t 2517d14ee252SJonathan Lemon irig_b_mode_show(struct device *dev, struct device_attribute *attr, char *buf) 2518d14ee252SJonathan Lemon { 2519d14ee252SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2520d14ee252SJonathan Lemon u32 val; 2521d14ee252SJonathan Lemon 2522d14ee252SJonathan Lemon val = ioread32(&bp->irig_out->ctrl); 2523d14ee252SJonathan Lemon val = (val >> 16) & 0x07; 2524d14ee252SJonathan Lemon return sysfs_emit(buf, "%d\n", val); 2525d14ee252SJonathan Lemon } 2526d14ee252SJonathan Lemon 2527d14ee252SJonathan Lemon static ssize_t 2528d14ee252SJonathan Lemon irig_b_mode_store(struct device *dev, 2529d14ee252SJonathan Lemon struct device_attribute *attr, 2530d14ee252SJonathan Lemon const char *buf, size_t count) 2531d14ee252SJonathan Lemon { 2532d14ee252SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2533d14ee252SJonathan Lemon unsigned long flags; 2534d14ee252SJonathan Lemon int err; 2535d14ee252SJonathan Lemon u32 reg; 2536d14ee252SJonathan Lemon u8 val; 2537d14ee252SJonathan Lemon 2538d14ee252SJonathan Lemon err = kstrtou8(buf, 0, &val); 2539d14ee252SJonathan Lemon if (err) 2540d14ee252SJonathan Lemon return err; 2541d14ee252SJonathan Lemon if (val > 7) 2542d14ee252SJonathan Lemon return -EINVAL; 2543d14ee252SJonathan Lemon 2544d14ee252SJonathan Lemon reg = ((val & 0x7) << 16); 2545d14ee252SJonathan Lemon 2546d14ee252SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 2547d14ee252SJonathan Lemon iowrite32(0, &bp->irig_out->ctrl); /* disable */ 2548d14ee252SJonathan Lemon iowrite32(reg, &bp->irig_out->ctrl); /* change mode */ 2549d14ee252SJonathan Lemon iowrite32(reg | IRIG_M_CTRL_ENABLE, &bp->irig_out->ctrl); 2550d14ee252SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 2551d14ee252SJonathan Lemon 2552d14ee252SJonathan Lemon return count; 2553d14ee252SJonathan Lemon } 2554d14ee252SJonathan Lemon static DEVICE_ATTR_RW(irig_b_mode); 2555d14ee252SJonathan Lemon 2556d14ee252SJonathan Lemon static ssize_t 2557773bda96SJonathan Lemon clock_source_show(struct device *dev, struct device_attribute *attr, char *buf) 2558773bda96SJonathan Lemon { 2559773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2560773bda96SJonathan Lemon const char *p; 2561773bda96SJonathan Lemon u32 select; 2562773bda96SJonathan Lemon 2563773bda96SJonathan Lemon select = ioread32(&bp->reg->select); 2564e1daf0ecSJonathan Lemon p = ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16); 2565773bda96SJonathan Lemon 2566773bda96SJonathan Lemon return sysfs_emit(buf, "%s\n", p); 2567773bda96SJonathan Lemon } 2568773bda96SJonathan Lemon 2569773bda96SJonathan Lemon static ssize_t 2570773bda96SJonathan Lemon clock_source_store(struct device *dev, struct device_attribute *attr, 2571773bda96SJonathan Lemon const char *buf, size_t count) 2572773bda96SJonathan Lemon { 2573773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2574773bda96SJonathan Lemon unsigned long flags; 2575773bda96SJonathan Lemon int val; 2576773bda96SJonathan Lemon 2577e1daf0ecSJonathan Lemon val = ptp_ocp_select_val_from_name(ptp_ocp_clock, buf); 2578773bda96SJonathan Lemon if (val < 0) 2579773bda96SJonathan Lemon return val; 2580773bda96SJonathan Lemon 2581773bda96SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 2582773bda96SJonathan Lemon iowrite32(val, &bp->reg->select); 2583773bda96SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 2584773bda96SJonathan Lemon 2585773bda96SJonathan Lemon return count; 2586773bda96SJonathan Lemon } 2587773bda96SJonathan Lemon static DEVICE_ATTR_RW(clock_source); 2588773bda96SJonathan Lemon 2589773bda96SJonathan Lemon static ssize_t 2590773bda96SJonathan Lemon available_clock_sources_show(struct device *dev, 2591773bda96SJonathan Lemon struct device_attribute *attr, char *buf) 2592773bda96SJonathan Lemon { 2593e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_clock, buf); 2594773bda96SJonathan Lemon } 2595773bda96SJonathan Lemon static DEVICE_ATTR_RO(available_clock_sources); 2596773bda96SJonathan Lemon 25972f23f486SVadim Fedorenko static ssize_t 25982f23f486SVadim Fedorenko clock_status_drift_show(struct device *dev, 25992f23f486SVadim Fedorenko struct device_attribute *attr, char *buf) 26002f23f486SVadim Fedorenko { 26012f23f486SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 26022f23f486SVadim Fedorenko u32 val; 26032f23f486SVadim Fedorenko int res; 26042f23f486SVadim Fedorenko 26052f23f486SVadim Fedorenko val = ioread32(&bp->reg->status_drift); 26062f23f486SVadim Fedorenko res = (val & ~INT_MAX) ? -1 : 1; 26072f23f486SVadim Fedorenko res *= (val & INT_MAX); 26082f23f486SVadim Fedorenko return sysfs_emit(buf, "%d\n", res); 26092f23f486SVadim Fedorenko } 26102f23f486SVadim Fedorenko static DEVICE_ATTR_RO(clock_status_drift); 26112f23f486SVadim Fedorenko 26122f23f486SVadim Fedorenko static ssize_t 26132f23f486SVadim Fedorenko clock_status_offset_show(struct device *dev, 26142f23f486SVadim Fedorenko struct device_attribute *attr, char *buf) 26152f23f486SVadim Fedorenko { 26162f23f486SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 26172f23f486SVadim Fedorenko u32 val; 26182f23f486SVadim Fedorenko int res; 26192f23f486SVadim Fedorenko 26202f23f486SVadim Fedorenko val = ioread32(&bp->reg->status_offset); 26212f23f486SVadim Fedorenko res = (val & ~INT_MAX) ? -1 : 1; 26222f23f486SVadim Fedorenko res *= (val & INT_MAX); 26232f23f486SVadim Fedorenko return sysfs_emit(buf, "%d\n", res); 26242f23f486SVadim Fedorenko } 26252f23f486SVadim Fedorenko static DEVICE_ATTR_RO(clock_status_offset); 26262f23f486SVadim Fedorenko 262744a412d1SVadim Fedorenko static ssize_t 262844a412d1SVadim Fedorenko tod_correction_show(struct device *dev, 262944a412d1SVadim Fedorenko struct device_attribute *attr, char *buf) 263044a412d1SVadim Fedorenko { 263144a412d1SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 263244a412d1SVadim Fedorenko u32 val; 263344a412d1SVadim Fedorenko int res; 263444a412d1SVadim Fedorenko 263544a412d1SVadim Fedorenko val = ioread32(&bp->tod->adj_sec); 263644a412d1SVadim Fedorenko res = (val & ~INT_MAX) ? -1 : 1; 263744a412d1SVadim Fedorenko res *= (val & INT_MAX); 263844a412d1SVadim Fedorenko return sysfs_emit(buf, "%d\n", res); 263944a412d1SVadim Fedorenko } 264044a412d1SVadim Fedorenko 264144a412d1SVadim Fedorenko static ssize_t 264244a412d1SVadim Fedorenko tod_correction_store(struct device *dev, struct device_attribute *attr, 264344a412d1SVadim Fedorenko const char *buf, size_t count) 264444a412d1SVadim Fedorenko { 264544a412d1SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 264644a412d1SVadim Fedorenko unsigned long flags; 264744a412d1SVadim Fedorenko int err, res; 264844a412d1SVadim Fedorenko u32 val = 0; 264944a412d1SVadim Fedorenko 265044a412d1SVadim Fedorenko err = kstrtos32(buf, 0, &res); 265144a412d1SVadim Fedorenko if (err) 265244a412d1SVadim Fedorenko return err; 265344a412d1SVadim Fedorenko if (res < 0) { 265444a412d1SVadim Fedorenko res *= -1; 265544a412d1SVadim Fedorenko val |= BIT(31); 265644a412d1SVadim Fedorenko } 265744a412d1SVadim Fedorenko val |= res; 265844a412d1SVadim Fedorenko 265944a412d1SVadim Fedorenko spin_lock_irqsave(&bp->lock, flags); 266044a412d1SVadim Fedorenko iowrite32(val, &bp->tod->adj_sec); 266144a412d1SVadim Fedorenko spin_unlock_irqrestore(&bp->lock, flags); 266244a412d1SVadim Fedorenko 266344a412d1SVadim Fedorenko return count; 266444a412d1SVadim Fedorenko } 266544a412d1SVadim Fedorenko static DEVICE_ATTR_RW(tod_correction); 266644a412d1SVadim Fedorenko 2667b325af3cSJonathan Lemon #define _DEVICE_SIGNAL_GROUP_ATTRS(_nr) \ 2668b325af3cSJonathan Lemon static struct attribute *fb_timecard_signal##_nr##_attrs[] = { \ 2669b325af3cSJonathan Lemon &dev_attr_signal##_nr##_signal.attr.attr, \ 2670b325af3cSJonathan Lemon &dev_attr_signal##_nr##_duty.attr.attr, \ 2671b325af3cSJonathan Lemon &dev_attr_signal##_nr##_phase.attr.attr, \ 2672b325af3cSJonathan Lemon &dev_attr_signal##_nr##_period.attr.attr, \ 2673b325af3cSJonathan Lemon &dev_attr_signal##_nr##_polarity.attr.attr, \ 2674b325af3cSJonathan Lemon &dev_attr_signal##_nr##_running.attr.attr, \ 2675b325af3cSJonathan Lemon &dev_attr_signal##_nr##_start.attr.attr, \ 2676b325af3cSJonathan Lemon NULL, \ 2677b325af3cSJonathan Lemon } 2678b325af3cSJonathan Lemon 2679b325af3cSJonathan Lemon #define DEVICE_SIGNAL_GROUP(_name, _nr) \ 2680b325af3cSJonathan Lemon _DEVICE_SIGNAL_GROUP_ATTRS(_nr); \ 2681b325af3cSJonathan Lemon static const struct attribute_group \ 2682b325af3cSJonathan Lemon fb_timecard_signal##_nr##_group = { \ 2683b325af3cSJonathan Lemon .name = #_name, \ 2684b325af3cSJonathan Lemon .attrs = fb_timecard_signal##_nr##_attrs, \ 2685b325af3cSJonathan Lemon } 2686b325af3cSJonathan Lemon 2687b325af3cSJonathan Lemon DEVICE_SIGNAL_GROUP(gen1, 0); 2688b325af3cSJonathan Lemon DEVICE_SIGNAL_GROUP(gen2, 1); 2689b325af3cSJonathan Lemon DEVICE_SIGNAL_GROUP(gen3, 2); 2690b325af3cSJonathan Lemon DEVICE_SIGNAL_GROUP(gen4, 3); 2691b325af3cSJonathan Lemon 2692c205d53cSJonathan Lemon static struct attribute *fb_timecard_attrs[] = { 2693773bda96SJonathan Lemon &dev_attr_serialnum.attr, 2694ef0cfb34SJonathan Lemon &dev_attr_gnss_sync.attr, 2695773bda96SJonathan Lemon &dev_attr_clock_source.attr, 2696773bda96SJonathan Lemon &dev_attr_available_clock_sources.attr, 2697e1daf0ecSJonathan Lemon &dev_attr_sma1.attr, 2698e1daf0ecSJonathan Lemon &dev_attr_sma2.attr, 2699e1daf0ecSJonathan Lemon &dev_attr_sma3.attr, 2700e1daf0ecSJonathan Lemon &dev_attr_sma4.attr, 2701e1daf0ecSJonathan Lemon &dev_attr_available_sma_inputs.attr, 2702e1daf0ecSJonathan Lemon &dev_attr_available_sma_outputs.attr, 27032f23f486SVadim Fedorenko &dev_attr_clock_status_drift.attr, 27042f23f486SVadim Fedorenko &dev_attr_clock_status_offset.attr, 2705d14ee252SJonathan Lemon &dev_attr_irig_b_mode.attr, 270689260d87SJonathan Lemon &dev_attr_utc_tai_offset.attr, 27071acffc6eSJonathan Lemon &dev_attr_ts_window_adjust.attr, 270844a412d1SVadim Fedorenko &dev_attr_tod_correction.attr, 2709773bda96SJonathan Lemon NULL, 2710773bda96SJonathan Lemon }; 2711c205d53cSJonathan Lemon static const struct attribute_group fb_timecard_group = { 2712c205d53cSJonathan Lemon .attrs = fb_timecard_attrs, 2713c205d53cSJonathan Lemon }; 2714c205d53cSJonathan Lemon static const struct ocp_attr_group fb_timecard_groups[] = { 2715c205d53cSJonathan Lemon { .cap = OCP_CAP_BASIC, .group = &fb_timecard_group }, 2716b325af3cSJonathan Lemon { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal0_group }, 2717b325af3cSJonathan Lemon { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal1_group }, 2718b325af3cSJonathan Lemon { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal2_group }, 2719b325af3cSJonathan Lemon { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal3_group }, 2720c205d53cSJonathan Lemon { }, 2721c205d53cSJonathan Lemon }; 2722773bda96SJonathan Lemon 2723a509a7c6SJonathan Lemon static void 2724a509a7c6SJonathan Lemon gpio_input_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit, 2725a509a7c6SJonathan Lemon const char *def) 2726f67bf662SJonathan Lemon { 2727a509a7c6SJonathan Lemon int i; 2728f67bf662SJonathan Lemon 2729a509a7c6SJonathan Lemon for (i = 0; i < 4; i++) { 2730a509a7c6SJonathan Lemon if (bp->sma[i].mode != SMA_MODE_IN) 2731a509a7c6SJonathan Lemon continue; 2732a509a7c6SJonathan Lemon if (map[i][0] & (1 << bit)) { 2733a509a7c6SJonathan Lemon sprintf(buf, "sma%d", i + 1); 2734a509a7c6SJonathan Lemon return; 2735a509a7c6SJonathan Lemon } 2736a509a7c6SJonathan Lemon } 2737a509a7c6SJonathan Lemon if (!def) 2738a509a7c6SJonathan Lemon def = "----"; 2739a509a7c6SJonathan Lemon strcpy(buf, def); 2740f67bf662SJonathan Lemon } 2741f67bf662SJonathan Lemon 2742f67bf662SJonathan Lemon static void 2743a509a7c6SJonathan Lemon gpio_output_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit) 2744f67bf662SJonathan Lemon { 2745f67bf662SJonathan Lemon char *ans = buf; 2746a509a7c6SJonathan Lemon int i; 2747f67bf662SJonathan Lemon 2748a509a7c6SJonathan Lemon strcpy(ans, "----"); 2749a509a7c6SJonathan Lemon for (i = 0; i < 4; i++) { 2750a509a7c6SJonathan Lemon if (bp->sma[i].mode != SMA_MODE_OUT) 2751a509a7c6SJonathan Lemon continue; 2752a509a7c6SJonathan Lemon if (map[i][1] & (1 << bit)) 2753a509a7c6SJonathan Lemon ans += sprintf(ans, "sma%d ", i + 1); 2754a509a7c6SJonathan Lemon } 2755f67bf662SJonathan Lemon } 2756f67bf662SJonathan Lemon 2757b325af3cSJonathan Lemon static void 2758b325af3cSJonathan Lemon _signal_summary_show(struct seq_file *s, struct ptp_ocp *bp, int nr) 2759b325af3cSJonathan Lemon { 2760b325af3cSJonathan Lemon struct signal_reg __iomem *reg = bp->signal_out[nr]->mem; 2761b325af3cSJonathan Lemon struct ptp_ocp_signal *signal = &bp->signal[nr]; 2762b325af3cSJonathan Lemon char label[8]; 2763b325af3cSJonathan Lemon bool on; 2764b325af3cSJonathan Lemon u32 val; 2765b325af3cSJonathan Lemon 2766b325af3cSJonathan Lemon if (!signal) 2767b325af3cSJonathan Lemon return; 2768b325af3cSJonathan Lemon 2769b325af3cSJonathan Lemon on = signal->running; 2770b325af3cSJonathan Lemon sprintf(label, "GEN%d", nr); 2771b325af3cSJonathan Lemon seq_printf(s, "%7s: %s, period:%llu duty:%d%% phase:%llu pol:%d", 2772b325af3cSJonathan Lemon label, on ? " ON" : "OFF", 2773b325af3cSJonathan Lemon signal->period, signal->duty, signal->phase, 2774b325af3cSJonathan Lemon signal->polarity); 2775b325af3cSJonathan Lemon 2776b325af3cSJonathan Lemon val = ioread32(®->enable); 2777b325af3cSJonathan Lemon seq_printf(s, " [%x", val); 2778b325af3cSJonathan Lemon val = ioread32(®->status); 2779b325af3cSJonathan Lemon seq_printf(s, " %x]", val); 2780b325af3cSJonathan Lemon 2781b325af3cSJonathan Lemon seq_printf(s, " start:%llu\n", signal->start); 2782b325af3cSJonathan Lemon } 2783b325af3cSJonathan Lemon 2784f67bf662SJonathan Lemon static int 2785f67bf662SJonathan Lemon ptp_ocp_summary_show(struct seq_file *s, void *data) 2786f67bf662SJonathan Lemon { 2787f67bf662SJonathan Lemon struct device *dev = s->private; 2788f67bf662SJonathan Lemon struct ptp_system_timestamp sts; 2789a509a7c6SJonathan Lemon u16 sma_val[4][2], ctrl, val; 2790f67bf662SJonathan Lemon struct ts_reg __iomem *ts_reg; 2791f67bf662SJonathan Lemon struct timespec64 ts; 2792f67bf662SJonathan Lemon struct ptp_ocp *bp; 2793a509a7c6SJonathan Lemon char *src, *buf; 2794a62a56d0SJonathan Lemon bool on, map; 2795b325af3cSJonathan Lemon int i; 2796f67bf662SJonathan Lemon 2797f67bf662SJonathan Lemon buf = (char *)__get_free_page(GFP_KERNEL); 2798f67bf662SJonathan Lemon if (!buf) 2799f67bf662SJonathan Lemon return -ENOMEM; 2800f67bf662SJonathan Lemon 2801f67bf662SJonathan Lemon bp = dev_get_drvdata(dev); 2802f67bf662SJonathan Lemon 2803f67bf662SJonathan Lemon seq_printf(s, "%7s: /dev/ptp%d\n", "PTP", ptp_clock_index(bp->ptp)); 280461fd7ac2SJonathan Lemon if (bp->gnss_port != -1) 280561fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS1", bp->gnss_port); 280661fd7ac2SJonathan Lemon if (bp->gnss2_port != -1) 280761fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS2", bp->gnss2_port); 280861fd7ac2SJonathan Lemon if (bp->mac_port != -1) 280961fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "MAC", bp->mac_port); 281061fd7ac2SJonathan Lemon if (bp->nmea_port != -1) 281161fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "NMEA", bp->nmea_port); 2812f67bf662SJonathan Lemon 2813a509a7c6SJonathan Lemon memset(sma_val, 0xff, sizeof(sma_val)); 2814a509a7c6SJonathan Lemon if (bp->sma_map1) { 2815a509a7c6SJonathan Lemon u32 reg; 2816a509a7c6SJonathan Lemon 2817a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map1->gpio1); 2818a509a7c6SJonathan Lemon sma_val[0][0] = reg & 0xffff; 2819a509a7c6SJonathan Lemon sma_val[1][0] = reg >> 16; 2820a509a7c6SJonathan Lemon 2821a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map1->gpio2); 2822a509a7c6SJonathan Lemon sma_val[2][1] = reg & 0xffff; 2823a509a7c6SJonathan Lemon sma_val[3][1] = reg >> 16; 2824a509a7c6SJonathan Lemon 2825a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map2->gpio1); 2826a509a7c6SJonathan Lemon sma_val[2][0] = reg & 0xffff; 2827a509a7c6SJonathan Lemon sma_val[3][0] = reg >> 16; 2828a509a7c6SJonathan Lemon 2829a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map2->gpio2); 2830a509a7c6SJonathan Lemon sma_val[0][1] = reg & 0xffff; 2831a509a7c6SJonathan Lemon sma_val[1][1] = reg >> 16; 2832a509a7c6SJonathan Lemon } 2833a509a7c6SJonathan Lemon 2834f67bf662SJonathan Lemon sma1_show(dev, NULL, buf); 2835a509a7c6SJonathan Lemon seq_printf(s, " sma1: %04x,%04x %s", 2836a509a7c6SJonathan Lemon sma_val[0][0], sma_val[0][1], buf); 2837f67bf662SJonathan Lemon 2838f67bf662SJonathan Lemon sma2_show(dev, NULL, buf); 2839a509a7c6SJonathan Lemon seq_printf(s, " sma2: %04x,%04x %s", 2840a509a7c6SJonathan Lemon sma_val[1][0], sma_val[1][1], buf); 2841f67bf662SJonathan Lemon 2842f67bf662SJonathan Lemon sma3_show(dev, NULL, buf); 2843a509a7c6SJonathan Lemon seq_printf(s, " sma3: %04x,%04x %s", 2844a509a7c6SJonathan Lemon sma_val[2][0], sma_val[2][1], buf); 2845f67bf662SJonathan Lemon 2846f67bf662SJonathan Lemon sma4_show(dev, NULL, buf); 2847a509a7c6SJonathan Lemon seq_printf(s, " sma4: %04x,%04x %s", 2848a509a7c6SJonathan Lemon sma_val[3][0], sma_val[3][1], buf); 2849f67bf662SJonathan Lemon 2850f67bf662SJonathan Lemon if (bp->ts0) { 2851f67bf662SJonathan Lemon ts_reg = bp->ts0->mem; 2852f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 2853be69087cSJonathan Lemon src = "GNSS1"; 2854f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS0", 2855f67bf662SJonathan Lemon on ? " ON" : "OFF", src); 2856f67bf662SJonathan Lemon } 2857f67bf662SJonathan Lemon 2858f67bf662SJonathan Lemon if (bp->ts1) { 2859f67bf662SJonathan Lemon ts_reg = bp->ts1->mem; 2860f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 2861a509a7c6SJonathan Lemon gpio_input_map(buf, bp, sma_val, 2, NULL); 2862f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS1", 2863a509a7c6SJonathan Lemon on ? " ON" : "OFF", buf); 2864f67bf662SJonathan Lemon } 2865f67bf662SJonathan Lemon 2866f67bf662SJonathan Lemon if (bp->ts2) { 2867f67bf662SJonathan Lemon ts_reg = bp->ts2->mem; 2868f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 2869a509a7c6SJonathan Lemon gpio_input_map(buf, bp, sma_val, 3, NULL); 2870f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS2", 2871a509a7c6SJonathan Lemon on ? " ON" : "OFF", buf); 2872f67bf662SJonathan Lemon } 2873f67bf662SJonathan Lemon 2874a62a56d0SJonathan Lemon if (bp->pps) { 2875a62a56d0SJonathan Lemon ts_reg = bp->pps->mem; 2876a62a56d0SJonathan Lemon src = "PHC"; 2877a62a56d0SJonathan Lemon on = ioread32(&ts_reg->enable); 2878a62a56d0SJonathan Lemon map = !!(bp->pps_req_map & OCP_REQ_TIMESTAMP); 2879a62a56d0SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS3", 28801a575cdeSNathan Chancellor on && map ? " ON" : "OFF", src); 2881a62a56d0SJonathan Lemon 2882a62a56d0SJonathan Lemon map = !!(bp->pps_req_map & OCP_REQ_PPS); 2883a62a56d0SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "PPS", 28841a575cdeSNathan Chancellor on && map ? " ON" : "OFF", src); 2885a62a56d0SJonathan Lemon } 2886a62a56d0SJonathan Lemon 2887b325af3cSJonathan Lemon if (bp->fw_cap & OCP_CAP_SIGNAL) 2888b325af3cSJonathan Lemon for (i = 0; i < 4; i++) 2889b325af3cSJonathan Lemon _signal_summary_show(s, bp, i); 2890b325af3cSJonathan Lemon 2891f67bf662SJonathan Lemon if (bp->irig_out) { 2892f67bf662SJonathan Lemon ctrl = ioread32(&bp->irig_out->ctrl); 2893f67bf662SJonathan Lemon on = ctrl & IRIG_M_CTRL_ENABLE; 2894f67bf662SJonathan Lemon val = ioread32(&bp->irig_out->status); 2895a509a7c6SJonathan Lemon gpio_output_map(buf, bp, sma_val, 4); 2896f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, mode %d, out: %s\n", "IRIG", 2897f67bf662SJonathan Lemon on ? " ON" : "OFF", val, (ctrl >> 16), buf); 2898f67bf662SJonathan Lemon } 2899f67bf662SJonathan Lemon 2900f67bf662SJonathan Lemon if (bp->irig_in) { 2901f67bf662SJonathan Lemon on = ioread32(&bp->irig_in->ctrl) & IRIG_S_CTRL_ENABLE; 2902f67bf662SJonathan Lemon val = ioread32(&bp->irig_in->status); 2903a509a7c6SJonathan Lemon gpio_input_map(buf, bp, sma_val, 4, NULL); 2904f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, src: %s\n", "IRIG in", 2905a509a7c6SJonathan Lemon on ? " ON" : "OFF", val, buf); 2906f67bf662SJonathan Lemon } 2907f67bf662SJonathan Lemon 2908f67bf662SJonathan Lemon if (bp->dcf_out) { 2909f67bf662SJonathan Lemon on = ioread32(&bp->dcf_out->ctrl) & DCF_M_CTRL_ENABLE; 2910f67bf662SJonathan Lemon val = ioread32(&bp->dcf_out->status); 2911a509a7c6SJonathan Lemon gpio_output_map(buf, bp, sma_val, 5); 2912f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, out: %s\n", "DCF", 2913f67bf662SJonathan Lemon on ? " ON" : "OFF", val, buf); 2914f67bf662SJonathan Lemon } 2915f67bf662SJonathan Lemon 2916f67bf662SJonathan Lemon if (bp->dcf_in) { 2917f67bf662SJonathan Lemon on = ioread32(&bp->dcf_in->ctrl) & DCF_S_CTRL_ENABLE; 2918f67bf662SJonathan Lemon val = ioread32(&bp->dcf_in->status); 2919a509a7c6SJonathan Lemon gpio_input_map(buf, bp, sma_val, 5, NULL); 2920f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, src: %s\n", "DCF in", 2921a509a7c6SJonathan Lemon on ? " ON" : "OFF", val, buf); 2922f67bf662SJonathan Lemon } 2923f67bf662SJonathan Lemon 2924e3516bb4SJonathan Lemon if (bp->nmea_out) { 2925e3516bb4SJonathan Lemon on = ioread32(&bp->nmea_out->ctrl) & 1; 2926e3516bb4SJonathan Lemon val = ioread32(&bp->nmea_out->status); 2927e3516bb4SJonathan Lemon seq_printf(s, "%7s: %s, error: %d\n", "NMEA", 2928e3516bb4SJonathan Lemon on ? " ON" : "OFF", val); 2929e3516bb4SJonathan Lemon } 2930e3516bb4SJonathan Lemon 2931f67bf662SJonathan Lemon /* compute src for PPS1, used below. */ 2932f67bf662SJonathan Lemon if (bp->pps_select) { 2933f67bf662SJonathan Lemon val = ioread32(&bp->pps_select->gpio1); 2934a509a7c6SJonathan Lemon src = &buf[80]; 2935f67bf662SJonathan Lemon if (val & 0x01) 2936a509a7c6SJonathan Lemon gpio_input_map(src, bp, sma_val, 0, NULL); 2937f67bf662SJonathan Lemon else if (val & 0x02) 2938f67bf662SJonathan Lemon src = "MAC"; 2939f67bf662SJonathan Lemon else if (val & 0x04) 2940be69087cSJonathan Lemon src = "GNSS1"; 2941f67bf662SJonathan Lemon else 2942f67bf662SJonathan Lemon src = "----"; 2943f67bf662SJonathan Lemon } else { 2944f67bf662SJonathan Lemon src = "?"; 2945f67bf662SJonathan Lemon } 2946f67bf662SJonathan Lemon 2947f67bf662SJonathan Lemon /* assumes automatic switchover/selection */ 2948f67bf662SJonathan Lemon val = ioread32(&bp->reg->select); 2949f67bf662SJonathan Lemon switch (val >> 16) { 2950f67bf662SJonathan Lemon case 0: 2951f67bf662SJonathan Lemon sprintf(buf, "----"); 2952f67bf662SJonathan Lemon break; 2953f67bf662SJonathan Lemon case 2: 2954f67bf662SJonathan Lemon sprintf(buf, "IRIG"); 2955f67bf662SJonathan Lemon break; 2956f67bf662SJonathan Lemon case 3: 2957f67bf662SJonathan Lemon sprintf(buf, "%s via PPS1", src); 2958f67bf662SJonathan Lemon break; 2959f67bf662SJonathan Lemon case 6: 2960f67bf662SJonathan Lemon sprintf(buf, "DCF"); 2961f67bf662SJonathan Lemon break; 2962f67bf662SJonathan Lemon default: 2963f67bf662SJonathan Lemon strcpy(buf, "unknown"); 2964f67bf662SJonathan Lemon break; 2965f67bf662SJonathan Lemon } 2966f67bf662SJonathan Lemon val = ioread32(&bp->reg->status); 2967f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, state: %s\n", "PHC src", buf, 2968f67bf662SJonathan Lemon val & OCP_STATUS_IN_SYNC ? "sync" : "unsynced"); 2969f67bf662SJonathan Lemon 2970f67bf662SJonathan Lemon /* reuses PPS1 src from earlier */ 2971f67bf662SJonathan Lemon seq_printf(s, "MAC PPS1 src: %s\n", src); 2972f67bf662SJonathan Lemon 2973a509a7c6SJonathan Lemon gpio_input_map(buf, bp, sma_val, 1, "GNSS2"); 2974a509a7c6SJonathan Lemon seq_printf(s, "MAC PPS2 src: %s\n", buf); 2975f67bf662SJonathan Lemon 2976f67bf662SJonathan Lemon if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts)) { 2977f67bf662SJonathan Lemon struct timespec64 sys_ts; 2978f67bf662SJonathan Lemon s64 pre_ns, post_ns, ns; 2979f67bf662SJonathan Lemon 2980f67bf662SJonathan Lemon pre_ns = timespec64_to_ns(&sts.pre_ts); 2981f67bf662SJonathan Lemon post_ns = timespec64_to_ns(&sts.post_ts); 2982f67bf662SJonathan Lemon ns = (pre_ns + post_ns) / 2; 2983f67bf662SJonathan Lemon ns += (s64)bp->utc_tai_offset * NSEC_PER_SEC; 2984f67bf662SJonathan Lemon sys_ts = ns_to_timespec64(ns); 2985f67bf662SJonathan Lemon 2986f67bf662SJonathan Lemon seq_printf(s, "%7s: %lld.%ld == %ptT TAI\n", "PHC", 2987f67bf662SJonathan Lemon ts.tv_sec, ts.tv_nsec, &ts); 2988f67bf662SJonathan Lemon seq_printf(s, "%7s: %lld.%ld == %ptT UTC offset %d\n", "SYS", 2989f67bf662SJonathan Lemon sys_ts.tv_sec, sys_ts.tv_nsec, &sys_ts, 2990f67bf662SJonathan Lemon bp->utc_tai_offset); 2991f67bf662SJonathan Lemon seq_printf(s, "%7s: PHC:SYS offset: %lld window: %lld\n", "", 2992f67bf662SJonathan Lemon timespec64_to_ns(&ts) - ns, 2993f67bf662SJonathan Lemon post_ns - pre_ns); 2994f67bf662SJonathan Lemon } 2995f67bf662SJonathan Lemon 2996f67bf662SJonathan Lemon free_page((unsigned long)buf); 2997f67bf662SJonathan Lemon return 0; 2998f67bf662SJonathan Lemon } 2999f67bf662SJonathan Lemon DEFINE_SHOW_ATTRIBUTE(ptp_ocp_summary); 3000f67bf662SJonathan Lemon 30019f492c4cSVadim Fedorenko static int 30029f492c4cSVadim Fedorenko ptp_ocp_tod_status_show(struct seq_file *s, void *data) 30039f492c4cSVadim Fedorenko { 30049f492c4cSVadim Fedorenko struct device *dev = s->private; 30059f492c4cSVadim Fedorenko struct ptp_ocp *bp; 30069f492c4cSVadim Fedorenko u32 val; 30079f492c4cSVadim Fedorenko int idx; 30089f492c4cSVadim Fedorenko 30099f492c4cSVadim Fedorenko bp = dev_get_drvdata(dev); 30109f492c4cSVadim Fedorenko 30119f492c4cSVadim Fedorenko val = ioread32(&bp->tod->ctrl); 30129f492c4cSVadim Fedorenko if (!(val & TOD_CTRL_ENABLE)) { 30139f492c4cSVadim Fedorenko seq_printf(s, "TOD Slave disabled\n"); 30149f492c4cSVadim Fedorenko return 0; 30159f492c4cSVadim Fedorenko } 30169f492c4cSVadim Fedorenko seq_printf(s, "TOD Slave enabled, Control Register 0x%08X\n", val); 30179f492c4cSVadim Fedorenko 30189f492c4cSVadim Fedorenko idx = val & TOD_CTRL_PROTOCOL ? 4 : 0; 30199f492c4cSVadim Fedorenko idx += (val >> 16) & 3; 30209f492c4cSVadim Fedorenko seq_printf(s, "Protocol %s\n", ptp_ocp_tod_proto_name(idx)); 30219f492c4cSVadim Fedorenko 30229f492c4cSVadim Fedorenko idx = (val >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK; 30239f492c4cSVadim Fedorenko seq_printf(s, "GNSS %s\n", ptp_ocp_tod_gnss_name(idx)); 30249f492c4cSVadim Fedorenko 30259f492c4cSVadim Fedorenko val = ioread32(&bp->tod->version); 30269f492c4cSVadim Fedorenko seq_printf(s, "TOD Version %d.%d.%d\n", 30279f492c4cSVadim Fedorenko val >> 24, (val >> 16) & 0xff, val & 0xffff); 30289f492c4cSVadim Fedorenko 30299f492c4cSVadim Fedorenko val = ioread32(&bp->tod->status); 30309f492c4cSVadim Fedorenko seq_printf(s, "Status register: 0x%08X\n", val); 30319f492c4cSVadim Fedorenko 30329f492c4cSVadim Fedorenko val = ioread32(&bp->tod->adj_sec); 30339f492c4cSVadim Fedorenko idx = (val & ~INT_MAX) ? -1 : 1; 30349f492c4cSVadim Fedorenko idx *= (val & INT_MAX); 30359f492c4cSVadim Fedorenko seq_printf(s, "Correction seconds: %d\n", idx); 30369f492c4cSVadim Fedorenko 30379f492c4cSVadim Fedorenko val = ioread32(&bp->tod->utc_status); 30389f492c4cSVadim Fedorenko seq_printf(s, "UTC status register: 0x%08X\n", val); 30399f492c4cSVadim Fedorenko seq_printf(s, "UTC offset: %d valid:%d\n", 30409f492c4cSVadim Fedorenko val & TOD_STATUS_UTC_MASK, val & TOD_STATUS_UTC_VALID ? 1 : 0); 30419f492c4cSVadim Fedorenko seq_printf(s, "Leap second info valid:%d, Leap second announce %d\n", 30429f492c4cSVadim Fedorenko val & TOD_STATUS_LEAP_VALID ? 1 : 0, 30439f492c4cSVadim Fedorenko val & TOD_STATUS_LEAP_ANNOUNCE ? 1 : 0); 30449f492c4cSVadim Fedorenko 30459f492c4cSVadim Fedorenko val = ioread32(&bp->tod->leap); 30469f492c4cSVadim Fedorenko seq_printf(s, "Time to next leap second (in sec): %d\n", (s32) val); 30479f492c4cSVadim Fedorenko 30489f492c4cSVadim Fedorenko return 0; 30499f492c4cSVadim Fedorenko } 30509f492c4cSVadim Fedorenko DEFINE_SHOW_ATTRIBUTE(ptp_ocp_tod_status); 30519f492c4cSVadim Fedorenko 3052f67bf662SJonathan Lemon static struct dentry *ptp_ocp_debugfs_root; 3053f67bf662SJonathan Lemon 3054f67bf662SJonathan Lemon static void 3055f67bf662SJonathan Lemon ptp_ocp_debugfs_add_device(struct ptp_ocp *bp) 3056f67bf662SJonathan Lemon { 3057f67bf662SJonathan Lemon struct dentry *d; 3058f67bf662SJonathan Lemon 3059f67bf662SJonathan Lemon d = debugfs_create_dir(dev_name(&bp->dev), ptp_ocp_debugfs_root); 3060f67bf662SJonathan Lemon bp->debug_root = d; 3061f67bf662SJonathan Lemon debugfs_create_file("summary", 0444, bp->debug_root, 3062f67bf662SJonathan Lemon &bp->dev, &ptp_ocp_summary_fops); 30639f492c4cSVadim Fedorenko if (bp->tod) 30649f492c4cSVadim Fedorenko debugfs_create_file("tod_status", 0444, bp->debug_root, 30659f492c4cSVadim Fedorenko &bp->dev, &ptp_ocp_tod_status_fops); 3066f67bf662SJonathan Lemon } 3067f67bf662SJonathan Lemon 3068f67bf662SJonathan Lemon static void 3069f67bf662SJonathan Lemon ptp_ocp_debugfs_remove_device(struct ptp_ocp *bp) 3070f67bf662SJonathan Lemon { 3071f67bf662SJonathan Lemon debugfs_remove_recursive(bp->debug_root); 3072f67bf662SJonathan Lemon } 3073f67bf662SJonathan Lemon 3074f67bf662SJonathan Lemon static void 3075f67bf662SJonathan Lemon ptp_ocp_debugfs_init(void) 3076f67bf662SJonathan Lemon { 3077f67bf662SJonathan Lemon ptp_ocp_debugfs_root = debugfs_create_dir("timecard", NULL); 3078f67bf662SJonathan Lemon } 3079f67bf662SJonathan Lemon 3080f67bf662SJonathan Lemon static void 3081f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(void) 3082f67bf662SJonathan Lemon { 3083f67bf662SJonathan Lemon debugfs_remove_recursive(ptp_ocp_debugfs_root); 3084f67bf662SJonathan Lemon } 3085f67bf662SJonathan Lemon 3086773bda96SJonathan Lemon static void 3087773bda96SJonathan Lemon ptp_ocp_dev_release(struct device *dev) 3088773bda96SJonathan Lemon { 3089773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 3090773bda96SJonathan Lemon 3091773bda96SJonathan Lemon mutex_lock(&ptp_ocp_lock); 3092773bda96SJonathan Lemon idr_remove(&ptp_ocp_idr, bp->id); 3093773bda96SJonathan Lemon mutex_unlock(&ptp_ocp_lock); 3094773bda96SJonathan Lemon } 3095773bda96SJonathan Lemon 3096773bda96SJonathan Lemon static int 3097773bda96SJonathan Lemon ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev) 3098773bda96SJonathan Lemon { 3099773bda96SJonathan Lemon int err; 3100773bda96SJonathan Lemon 3101773bda96SJonathan Lemon mutex_lock(&ptp_ocp_lock); 3102773bda96SJonathan Lemon err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL); 3103773bda96SJonathan Lemon mutex_unlock(&ptp_ocp_lock); 3104773bda96SJonathan Lemon if (err < 0) { 3105773bda96SJonathan Lemon dev_err(&pdev->dev, "idr_alloc failed: %d\n", err); 3106773bda96SJonathan Lemon return err; 3107773bda96SJonathan Lemon } 3108773bda96SJonathan Lemon bp->id = err; 3109773bda96SJonathan Lemon 3110773bda96SJonathan Lemon bp->ptp_info = ptp_ocp_clock_info; 3111773bda96SJonathan Lemon spin_lock_init(&bp->lock); 3112ef0cfb34SJonathan Lemon bp->gnss_port = -1; 311371d7e085SJonathan Lemon bp->gnss2_port = -1; 3114773bda96SJonathan Lemon bp->mac_port = -1; 3115e3516bb4SJonathan Lemon bp->nmea_port = -1; 3116773bda96SJonathan Lemon bp->pdev = pdev; 3117773bda96SJonathan Lemon 3118773bda96SJonathan Lemon device_initialize(&bp->dev); 3119773bda96SJonathan Lemon dev_set_name(&bp->dev, "ocp%d", bp->id); 3120773bda96SJonathan Lemon bp->dev.class = &timecard_class; 3121773bda96SJonathan Lemon bp->dev.parent = &pdev->dev; 3122773bda96SJonathan Lemon bp->dev.release = ptp_ocp_dev_release; 3123773bda96SJonathan Lemon dev_set_drvdata(&bp->dev, bp); 3124773bda96SJonathan Lemon 3125773bda96SJonathan Lemon err = device_add(&bp->dev); 3126773bda96SJonathan Lemon if (err) { 3127773bda96SJonathan Lemon dev_err(&bp->dev, "device add failed: %d\n", err); 3128773bda96SJonathan Lemon goto out; 3129773bda96SJonathan Lemon } 3130773bda96SJonathan Lemon 3131773bda96SJonathan Lemon pci_set_drvdata(pdev, bp); 3132773bda96SJonathan Lemon 3133773bda96SJonathan Lemon return 0; 3134773bda96SJonathan Lemon 3135773bda96SJonathan Lemon out: 3136773bda96SJonathan Lemon ptp_ocp_dev_release(&bp->dev); 3137d12f23faSJonathan Lemon put_device(&bp->dev); 3138773bda96SJonathan Lemon return err; 3139773bda96SJonathan Lemon } 3140773bda96SJonathan Lemon 3141773bda96SJonathan Lemon static void 3142773bda96SJonathan Lemon ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link) 3143773bda96SJonathan Lemon { 3144773bda96SJonathan Lemon struct device *dev = &bp->dev; 3145773bda96SJonathan Lemon 3146773bda96SJonathan Lemon if (sysfs_create_link(&dev->kobj, &child->kobj, link)) 3147773bda96SJonathan Lemon dev_err(dev, "%s symlink failed\n", link); 3148773bda96SJonathan Lemon } 3149773bda96SJonathan Lemon 3150773bda96SJonathan Lemon static void 3151773bda96SJonathan Lemon ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link) 3152773bda96SJonathan Lemon { 3153773bda96SJonathan Lemon struct device *dev, *child; 3154773bda96SJonathan Lemon 3155773bda96SJonathan Lemon dev = &bp->pdev->dev; 3156773bda96SJonathan Lemon 3157773bda96SJonathan Lemon child = device_find_child_by_name(dev, name); 3158773bda96SJonathan Lemon if (!child) { 3159773bda96SJonathan Lemon dev_err(dev, "Could not find device %s\n", name); 3160773bda96SJonathan Lemon return; 3161773bda96SJonathan Lemon } 3162773bda96SJonathan Lemon 3163773bda96SJonathan Lemon ptp_ocp_symlink(bp, child, link); 3164773bda96SJonathan Lemon put_device(child); 3165773bda96SJonathan Lemon } 3166773bda96SJonathan Lemon 3167773bda96SJonathan Lemon static int 3168773bda96SJonathan Lemon ptp_ocp_complete(struct ptp_ocp *bp) 3169773bda96SJonathan Lemon { 3170773bda96SJonathan Lemon struct pps_device *pps; 3171773bda96SJonathan Lemon char buf[32]; 3172c205d53cSJonathan Lemon int i, err; 3173773bda96SJonathan Lemon 3174ef0cfb34SJonathan Lemon if (bp->gnss_port != -1) { 3175ef0cfb34SJonathan Lemon sprintf(buf, "ttyS%d", bp->gnss_port); 3176ef0cfb34SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyGNSS"); 3177773bda96SJonathan Lemon } 317871d7e085SJonathan Lemon if (bp->gnss2_port != -1) { 317971d7e085SJonathan Lemon sprintf(buf, "ttyS%d", bp->gnss2_port); 318071d7e085SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyGNSS2"); 318171d7e085SJonathan Lemon } 3182773bda96SJonathan Lemon if (bp->mac_port != -1) { 3183773bda96SJonathan Lemon sprintf(buf, "ttyS%d", bp->mac_port); 3184773bda96SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyMAC"); 3185773bda96SJonathan Lemon } 3186e3516bb4SJonathan Lemon if (bp->nmea_port != -1) { 3187e3516bb4SJonathan Lemon sprintf(buf, "ttyS%d", bp->nmea_port); 3188e3516bb4SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyNMEA"); 3189e3516bb4SJonathan Lemon } 3190773bda96SJonathan Lemon sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp)); 3191773bda96SJonathan Lemon ptp_ocp_link_child(bp, buf, "ptp"); 3192773bda96SJonathan Lemon 3193773bda96SJonathan Lemon pps = pps_lookup_dev(bp->ptp); 3194773bda96SJonathan Lemon if (pps) 3195773bda96SJonathan Lemon ptp_ocp_symlink(bp, pps->dev, "pps"); 3196773bda96SJonathan Lemon 3197c205d53cSJonathan Lemon for (i = 0; bp->attr_tbl[i].cap; i++) { 3198c205d53cSJonathan Lemon if (!(bp->attr_tbl[i].cap & bp->fw_cap)) 3199c205d53cSJonathan Lemon continue; 3200c205d53cSJonathan Lemon err = sysfs_create_group(&bp->dev.kobj, bp->attr_tbl[i].group); 3201c205d53cSJonathan Lemon if (err) 3202c205d53cSJonathan Lemon return err; 3203c205d53cSJonathan Lemon } 3204773bda96SJonathan Lemon 3205f67bf662SJonathan Lemon ptp_ocp_debugfs_add_device(bp); 3206f67bf662SJonathan Lemon 3207773bda96SJonathan Lemon return 0; 3208773bda96SJonathan Lemon } 3209773bda96SJonathan Lemon 3210773bda96SJonathan Lemon static void 3211065efcc5SJonathan Lemon ptp_ocp_phc_info(struct ptp_ocp *bp) 3212065efcc5SJonathan Lemon { 3213065efcc5SJonathan Lemon struct timespec64 ts; 3214065efcc5SJonathan Lemon u32 version, select; 3215065efcc5SJonathan Lemon bool sync; 3216065efcc5SJonathan Lemon 3217065efcc5SJonathan Lemon version = ioread32(&bp->reg->version); 3218065efcc5SJonathan Lemon select = ioread32(&bp->reg->select); 3219065efcc5SJonathan Lemon dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n", 3220065efcc5SJonathan Lemon version >> 24, (version >> 16) & 0xff, version & 0xffff, 3221065efcc5SJonathan Lemon ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16), 3222065efcc5SJonathan Lemon ptp_clock_index(bp->ptp)); 3223065efcc5SJonathan Lemon 3224065efcc5SJonathan Lemon sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC; 3225065efcc5SJonathan Lemon if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL)) 3226065efcc5SJonathan Lemon dev_info(&bp->pdev->dev, "Time: %lld.%ld, %s\n", 3227065efcc5SJonathan Lemon ts.tv_sec, ts.tv_nsec, 3228065efcc5SJonathan Lemon sync ? "in-sync" : "UNSYNCED"); 3229065efcc5SJonathan Lemon } 3230065efcc5SJonathan Lemon 3231065efcc5SJonathan Lemon static void 3232065efcc5SJonathan Lemon ptp_ocp_serial_info(struct device *dev, const char *name, int port, int baud) 3233065efcc5SJonathan Lemon { 3234065efcc5SJonathan Lemon if (port != -1) 3235065efcc5SJonathan Lemon dev_info(dev, "%5s: /dev/ttyS%-2d @ %6d\n", name, port, baud); 3236065efcc5SJonathan Lemon } 3237065efcc5SJonathan Lemon 3238065efcc5SJonathan Lemon static void 3239065efcc5SJonathan Lemon ptp_ocp_info(struct ptp_ocp *bp) 3240773bda96SJonathan Lemon { 3241e3516bb4SJonathan Lemon static int nmea_baud[] = { 3242e3516bb4SJonathan Lemon 1200, 2400, 4800, 9600, 19200, 38400, 3243e3516bb4SJonathan Lemon 57600, 115200, 230400, 460800, 921600, 3244e3516bb4SJonathan Lemon 1000000, 2000000 3245e3516bb4SJonathan Lemon }; 3246773bda96SJonathan Lemon struct device *dev = &bp->pdev->dev; 3247e3516bb4SJonathan Lemon u32 reg; 3248773bda96SJonathan Lemon 3249065efcc5SJonathan Lemon ptp_ocp_phc_info(bp); 3250065efcc5SJonathan Lemon 3251b0ca789aSJonathan Lemon dev_info(dev, "version %x\n", bp->fw_version); 3252b0ca789aSJonathan Lemon if (bp->fw_version & 0xffff) 3253773bda96SJonathan Lemon dev_info(dev, "regular image, version %d\n", 3254b0ca789aSJonathan Lemon bp->fw_version & 0xffff); 3255773bda96SJonathan Lemon else 3256773bda96SJonathan Lemon dev_info(dev, "golden image, version %d\n", 3257b0ca789aSJonathan Lemon bp->fw_version >> 16); 3258b0ca789aSJonathan Lemon 3259065efcc5SJonathan Lemon ptp_ocp_serial_info(dev, "GNSS", bp->gnss_port, 115200); 326071d7e085SJonathan Lemon ptp_ocp_serial_info(dev, "GNSS2", bp->gnss2_port, 115200); 3261065efcc5SJonathan Lemon ptp_ocp_serial_info(dev, "MAC", bp->mac_port, 57600); 3262e3516bb4SJonathan Lemon if (bp->nmea_out && bp->nmea_port != -1) { 3263e3516bb4SJonathan Lemon int baud = -1; 3264e3516bb4SJonathan Lemon 3265e3516bb4SJonathan Lemon reg = ioread32(&bp->nmea_out->uart_baud); 3266e3516bb4SJonathan Lemon if (reg < ARRAY_SIZE(nmea_baud)) 3267e3516bb4SJonathan Lemon baud = nmea_baud[reg]; 3268e3516bb4SJonathan Lemon ptp_ocp_serial_info(dev, "NMEA", bp->nmea_port, baud); 3269e3516bb4SJonathan Lemon } 3270773bda96SJonathan Lemon } 3271773bda96SJonathan Lemon 3272773bda96SJonathan Lemon static void 3273773bda96SJonathan Lemon ptp_ocp_detach_sysfs(struct ptp_ocp *bp) 3274773bda96SJonathan Lemon { 3275773bda96SJonathan Lemon struct device *dev = &bp->dev; 3276c205d53cSJonathan Lemon int i; 3277773bda96SJonathan Lemon 3278ef0cfb34SJonathan Lemon sysfs_remove_link(&dev->kobj, "ttyGNSS"); 3279773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "ttyMAC"); 3280773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "ptp"); 3281773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "pps"); 3282c205d53cSJonathan Lemon if (bp->attr_tbl) 3283c205d53cSJonathan Lemon for (i = 0; bp->attr_tbl[i].cap; i++) 3284c205d53cSJonathan Lemon sysfs_remove_group(&dev->kobj, bp->attr_tbl[i].group); 3285773bda96SJonathan Lemon } 3286773bda96SJonathan Lemon 3287773bda96SJonathan Lemon static void 3288773bda96SJonathan Lemon ptp_ocp_detach(struct ptp_ocp *bp) 3289773bda96SJonathan Lemon { 3290b325af3cSJonathan Lemon int i; 3291b325af3cSJonathan Lemon 3292f67bf662SJonathan Lemon ptp_ocp_debugfs_remove_device(bp); 3293773bda96SJonathan Lemon ptp_ocp_detach_sysfs(bp); 3294773bda96SJonathan Lemon if (timer_pending(&bp->watchdog)) 3295773bda96SJonathan Lemon del_timer_sync(&bp->watchdog); 3296773bda96SJonathan Lemon if (bp->ts0) 3297773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->ts0); 3298773bda96SJonathan Lemon if (bp->ts1) 3299773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->ts1); 3300dcf61469SJonathan Lemon if (bp->ts2) 3301dcf61469SJonathan Lemon ptp_ocp_unregister_ext(bp->ts2); 3302773bda96SJonathan Lemon if (bp->pps) 3303773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->pps); 3304b325af3cSJonathan Lemon for (i = 0; i < 4; i++) 3305b325af3cSJonathan Lemon if (bp->signal_out[i]) 3306b325af3cSJonathan Lemon ptp_ocp_unregister_ext(bp->signal_out[i]); 3307ef0cfb34SJonathan Lemon if (bp->gnss_port != -1) 3308ef0cfb34SJonathan Lemon serial8250_unregister_port(bp->gnss_port); 330971d7e085SJonathan Lemon if (bp->gnss2_port != -1) 331071d7e085SJonathan Lemon serial8250_unregister_port(bp->gnss2_port); 3311773bda96SJonathan Lemon if (bp->mac_port != -1) 3312773bda96SJonathan Lemon serial8250_unregister_port(bp->mac_port); 3313e3516bb4SJonathan Lemon if (bp->nmea_port != -1) 3314e3516bb4SJonathan Lemon serial8250_unregister_port(bp->nmea_port); 3315773bda96SJonathan Lemon if (bp->spi_flash) 3316773bda96SJonathan Lemon platform_device_unregister(bp->spi_flash); 3317773bda96SJonathan Lemon if (bp->i2c_ctrl) 3318773bda96SJonathan Lemon platform_device_unregister(bp->i2c_ctrl); 3319773bda96SJonathan Lemon if (bp->i2c_clk) 3320773bda96SJonathan Lemon clk_hw_unregister_fixed_rate(bp->i2c_clk); 3321773bda96SJonathan Lemon if (bp->n_irqs) 3322773bda96SJonathan Lemon pci_free_irq_vectors(bp->pdev); 3323773bda96SJonathan Lemon if (bp->ptp) 3324773bda96SJonathan Lemon ptp_clock_unregister(bp->ptp); 3325*1aa66a3aSJonathan Lemon kfree(bp->ptp_info.pin_config); 3326773bda96SJonathan Lemon device_unregister(&bp->dev); 3327773bda96SJonathan Lemon } 3328773bda96SJonathan Lemon 3329a7e1abadSJonathan Lemon static int 3330a7e1abadSJonathan Lemon ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id) 3331a7e1abadSJonathan Lemon { 3332773bda96SJonathan Lemon struct devlink *devlink; 3333a7e1abadSJonathan Lemon struct ptp_ocp *bp; 3334a7e1abadSJonathan Lemon int err; 3335a7e1abadSJonathan Lemon 3336919d13a7SLeon Romanovsky devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp), &pdev->dev); 3337773bda96SJonathan Lemon if (!devlink) { 3338773bda96SJonathan Lemon dev_err(&pdev->dev, "devlink_alloc failed\n"); 3339a7e1abadSJonathan Lemon return -ENOMEM; 3340773bda96SJonathan Lemon } 3341773bda96SJonathan Lemon 3342a7e1abadSJonathan Lemon err = pci_enable_device(pdev); 3343a7e1abadSJonathan Lemon if (err) { 3344a7e1abadSJonathan Lemon dev_err(&pdev->dev, "pci_enable_device\n"); 33454587369bSJonathan Lemon goto out_free; 3346a7e1abadSJonathan Lemon } 3347a7e1abadSJonathan Lemon 3348773bda96SJonathan Lemon bp = devlink_priv(devlink); 3349773bda96SJonathan Lemon err = ptp_ocp_device_init(bp, pdev); 3350773bda96SJonathan Lemon if (err) 3351d9fdbf13SJonathan Lemon goto out_disable; 3352a7e1abadSJonathan Lemon 3353773bda96SJonathan Lemon /* compat mode. 3354773bda96SJonathan Lemon * Older FPGA firmware only returns 2 irq's. 3355773bda96SJonathan Lemon * allow this - if not all of the IRQ's are returned, skip the 3356773bda96SJonathan Lemon * extra devices and just register the clock. 3357773bda96SJonathan Lemon */ 3358b325af3cSJonathan Lemon err = pci_alloc_irq_vectors(pdev, 1, 15, PCI_IRQ_MSI | PCI_IRQ_MSIX); 3359773bda96SJonathan Lemon if (err < 0) { 3360773bda96SJonathan Lemon dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err); 3361773bda96SJonathan Lemon goto out; 3362a7e1abadSJonathan Lemon } 3363773bda96SJonathan Lemon bp->n_irqs = err; 3364773bda96SJonathan Lemon pci_set_master(pdev); 3365a7e1abadSJonathan Lemon 3366773bda96SJonathan Lemon err = ptp_ocp_register_resources(bp, id->driver_data); 3367a7e1abadSJonathan Lemon if (err) 3368a7e1abadSJonathan Lemon goto out; 3369a7e1abadSJonathan Lemon 3370a7e1abadSJonathan Lemon bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev); 3371a7e1abadSJonathan Lemon if (IS_ERR(bp->ptp)) { 3372a7e1abadSJonathan Lemon err = PTR_ERR(bp->ptp); 3373773bda96SJonathan Lemon dev_err(&pdev->dev, "ptp_clock_register: %d\n", err); 3374773bda96SJonathan Lemon bp->ptp = NULL; 3375a7e1abadSJonathan Lemon goto out; 3376a7e1abadSJonathan Lemon } 3377a7e1abadSJonathan Lemon 3378773bda96SJonathan Lemon err = ptp_ocp_complete(bp); 3379773bda96SJonathan Lemon if (err) 3380773bda96SJonathan Lemon goto out; 3381773bda96SJonathan Lemon 3382a7e1abadSJonathan Lemon ptp_ocp_info(bp); 3383c89f78e9SLeon Romanovsky devlink_register(devlink); 3384a7e1abadSJonathan Lemon return 0; 3385a7e1abadSJonathan Lemon 3386a7e1abadSJonathan Lemon out: 3387773bda96SJonathan Lemon ptp_ocp_detach(bp); 3388773bda96SJonathan Lemon pci_set_drvdata(pdev, NULL); 3389d9fdbf13SJonathan Lemon out_disable: 3390d9fdbf13SJonathan Lemon pci_disable_device(pdev); 33914587369bSJonathan Lemon out_free: 3392773bda96SJonathan Lemon devlink_free(devlink); 3393a7e1abadSJonathan Lemon return err; 3394a7e1abadSJonathan Lemon } 3395a7e1abadSJonathan Lemon 3396a7e1abadSJonathan Lemon static void 3397a7e1abadSJonathan Lemon ptp_ocp_remove(struct pci_dev *pdev) 3398a7e1abadSJonathan Lemon { 3399a7e1abadSJonathan Lemon struct ptp_ocp *bp = pci_get_drvdata(pdev); 3400773bda96SJonathan Lemon struct devlink *devlink = priv_to_devlink(bp); 3401a7e1abadSJonathan Lemon 3402c89f78e9SLeon Romanovsky devlink_unregister(devlink); 3403773bda96SJonathan Lemon ptp_ocp_detach(bp); 3404a7e1abadSJonathan Lemon pci_set_drvdata(pdev, NULL); 3405d9fdbf13SJonathan Lemon pci_disable_device(pdev); 3406773bda96SJonathan Lemon 3407773bda96SJonathan Lemon devlink_free(devlink); 3408a7e1abadSJonathan Lemon } 3409a7e1abadSJonathan Lemon 3410a7e1abadSJonathan Lemon static struct pci_driver ptp_ocp_driver = { 3411a7e1abadSJonathan Lemon .name = KBUILD_MODNAME, 3412a7e1abadSJonathan Lemon .id_table = ptp_ocp_pcidev_id, 3413a7e1abadSJonathan Lemon .probe = ptp_ocp_probe, 3414a7e1abadSJonathan Lemon .remove = ptp_ocp_remove, 3415a7e1abadSJonathan Lemon }; 3416a7e1abadSJonathan Lemon 3417773bda96SJonathan Lemon static int 3418773bda96SJonathan Lemon ptp_ocp_i2c_notifier_call(struct notifier_block *nb, 3419773bda96SJonathan Lemon unsigned long action, void *data) 3420773bda96SJonathan Lemon { 3421773bda96SJonathan Lemon struct device *dev, *child = data; 3422773bda96SJonathan Lemon struct ptp_ocp *bp; 3423773bda96SJonathan Lemon bool add; 3424773bda96SJonathan Lemon 3425773bda96SJonathan Lemon switch (action) { 3426773bda96SJonathan Lemon case BUS_NOTIFY_ADD_DEVICE: 3427773bda96SJonathan Lemon case BUS_NOTIFY_DEL_DEVICE: 3428773bda96SJonathan Lemon add = action == BUS_NOTIFY_ADD_DEVICE; 3429773bda96SJonathan Lemon break; 3430773bda96SJonathan Lemon default: 3431773bda96SJonathan Lemon return 0; 3432773bda96SJonathan Lemon } 3433773bda96SJonathan Lemon 3434773bda96SJonathan Lemon if (!i2c_verify_adapter(child)) 3435773bda96SJonathan Lemon return 0; 3436773bda96SJonathan Lemon 3437773bda96SJonathan Lemon dev = child; 3438773bda96SJonathan Lemon while ((dev = dev->parent)) 3439773bda96SJonathan Lemon if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME)) 3440773bda96SJonathan Lemon goto found; 3441773bda96SJonathan Lemon return 0; 3442773bda96SJonathan Lemon 3443773bda96SJonathan Lemon found: 3444773bda96SJonathan Lemon bp = dev_get_drvdata(dev); 3445773bda96SJonathan Lemon if (add) 3446773bda96SJonathan Lemon ptp_ocp_symlink(bp, child, "i2c"); 3447773bda96SJonathan Lemon else 3448773bda96SJonathan Lemon sysfs_remove_link(&bp->dev.kobj, "i2c"); 3449773bda96SJonathan Lemon 3450773bda96SJonathan Lemon return 0; 3451773bda96SJonathan Lemon } 3452773bda96SJonathan Lemon 3453773bda96SJonathan Lemon static struct notifier_block ptp_ocp_i2c_notifier = { 3454773bda96SJonathan Lemon .notifier_call = ptp_ocp_i2c_notifier_call, 3455773bda96SJonathan Lemon }; 3456773bda96SJonathan Lemon 3457a7e1abadSJonathan Lemon static int __init 3458a7e1abadSJonathan Lemon ptp_ocp_init(void) 3459a7e1abadSJonathan Lemon { 3460773bda96SJonathan Lemon const char *what; 3461a7e1abadSJonathan Lemon int err; 3462a7e1abadSJonathan Lemon 3463f67bf662SJonathan Lemon ptp_ocp_debugfs_init(); 3464f67bf662SJonathan Lemon 3465773bda96SJonathan Lemon what = "timecard class"; 3466773bda96SJonathan Lemon err = class_register(&timecard_class); 3467773bda96SJonathan Lemon if (err) 3468773bda96SJonathan Lemon goto out; 3469773bda96SJonathan Lemon 3470773bda96SJonathan Lemon what = "i2c notifier"; 3471773bda96SJonathan Lemon err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 3472773bda96SJonathan Lemon if (err) 3473773bda96SJonathan Lemon goto out_notifier; 3474773bda96SJonathan Lemon 3475773bda96SJonathan Lemon what = "ptp_ocp driver"; 3476a7e1abadSJonathan Lemon err = pci_register_driver(&ptp_ocp_driver); 3477773bda96SJonathan Lemon if (err) 3478773bda96SJonathan Lemon goto out_register; 3479773bda96SJonathan Lemon 3480773bda96SJonathan Lemon return 0; 3481773bda96SJonathan Lemon 3482773bda96SJonathan Lemon out_register: 3483773bda96SJonathan Lemon bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 3484773bda96SJonathan Lemon out_notifier: 3485773bda96SJonathan Lemon class_unregister(&timecard_class); 3486773bda96SJonathan Lemon out: 3487f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(); 3488773bda96SJonathan Lemon pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err); 3489a7e1abadSJonathan Lemon return err; 3490a7e1abadSJonathan Lemon } 3491a7e1abadSJonathan Lemon 3492a7e1abadSJonathan Lemon static void __exit 3493a7e1abadSJonathan Lemon ptp_ocp_fini(void) 3494a7e1abadSJonathan Lemon { 3495773bda96SJonathan Lemon bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 3496a7e1abadSJonathan Lemon pci_unregister_driver(&ptp_ocp_driver); 3497773bda96SJonathan Lemon class_unregister(&timecard_class); 3498f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(); 3499a7e1abadSJonathan Lemon } 3500a7e1abadSJonathan Lemon 3501a7e1abadSJonathan Lemon module_init(ptp_ocp_init); 3502a7e1abadSJonathan Lemon module_exit(ptp_ocp_fini); 3503a7e1abadSJonathan Lemon 3504a7e1abadSJonathan Lemon MODULE_DESCRIPTION("OpenCompute TimeCard driver"); 3505a7e1abadSJonathan Lemon MODULE_LICENSE("GPL v2"); 3506