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> 14a7e1abadSJonathan Lemon #include <linux/ptp_clock_kernel.h> 15773bda96SJonathan Lemon #include <linux/spi/spi.h> 16773bda96SJonathan Lemon #include <linux/spi/xilinx_spi.h> 17773bda96SJonathan Lemon #include <net/devlink.h> 18773bda96SJonathan Lemon #include <linux/i2c.h> 19773bda96SJonathan Lemon #include <linux/mtd/mtd.h> 20a7e1abadSJonathan Lemon 21773bda96SJonathan Lemon #ifndef PCI_VENDOR_ID_FACEBOOK 22773bda96SJonathan Lemon #define PCI_VENDOR_ID_FACEBOOK 0x1d9b 23773bda96SJonathan Lemon #endif 24773bda96SJonathan Lemon 25773bda96SJonathan Lemon #ifndef PCI_DEVICE_ID_FACEBOOK_TIMECARD 26773bda96SJonathan Lemon #define PCI_DEVICE_ID_FACEBOOK_TIMECARD 0x0400 27773bda96SJonathan Lemon #endif 28773bda96SJonathan Lemon 29773bda96SJonathan Lemon static struct class timecard_class = { 30773bda96SJonathan Lemon .owner = THIS_MODULE, 31773bda96SJonathan Lemon .name = "timecard", 32a7e1abadSJonathan Lemon }; 33a7e1abadSJonathan Lemon 34a7e1abadSJonathan Lemon struct ocp_reg { 35a7e1abadSJonathan Lemon u32 ctrl; 36a7e1abadSJonathan Lemon u32 status; 37a7e1abadSJonathan Lemon u32 select; 38a7e1abadSJonathan Lemon u32 version; 39a7e1abadSJonathan Lemon u32 time_ns; 40a7e1abadSJonathan Lemon u32 time_sec; 41a7e1abadSJonathan Lemon u32 __pad0[2]; 42a7e1abadSJonathan Lemon u32 adjust_ns; 43a7e1abadSJonathan Lemon u32 adjust_sec; 44a7e1abadSJonathan Lemon u32 __pad1[2]; 45a7e1abadSJonathan Lemon u32 offset_ns; 46a7e1abadSJonathan Lemon u32 offset_window_ns; 47773bda96SJonathan Lemon u32 __pad2[2]; 48773bda96SJonathan Lemon u32 drift_ns; 49773bda96SJonathan Lemon u32 drift_window_ns; 50773bda96SJonathan Lemon u32 __pad3[6]; 51773bda96SJonathan Lemon u32 servo_offset_p; 52773bda96SJonathan Lemon u32 servo_offset_i; 53773bda96SJonathan Lemon u32 servo_drift_p; 54773bda96SJonathan Lemon u32 servo_drift_i; 552f23f486SVadim Fedorenko u32 status_offset; 562f23f486SVadim Fedorenko u32 status_drift; 57a7e1abadSJonathan Lemon }; 58a7e1abadSJonathan Lemon 59a7e1abadSJonathan Lemon #define OCP_CTRL_ENABLE BIT(0) 60a7e1abadSJonathan Lemon #define OCP_CTRL_ADJUST_TIME BIT(1) 61a7e1abadSJonathan Lemon #define OCP_CTRL_ADJUST_OFFSET BIT(2) 62773bda96SJonathan Lemon #define OCP_CTRL_ADJUST_DRIFT BIT(3) 63773bda96SJonathan Lemon #define OCP_CTRL_ADJUST_SERVO BIT(8) 64a7e1abadSJonathan Lemon #define OCP_CTRL_READ_TIME_REQ BIT(30) 65a7e1abadSJonathan Lemon #define OCP_CTRL_READ_TIME_DONE BIT(31) 66a7e1abadSJonathan Lemon 67a7e1abadSJonathan Lemon #define OCP_STATUS_IN_SYNC BIT(0) 68773bda96SJonathan Lemon #define OCP_STATUS_IN_HOLDOVER BIT(1) 69a7e1abadSJonathan Lemon 70a7e1abadSJonathan Lemon #define OCP_SELECT_CLK_NONE 0 71773bda96SJonathan Lemon #define OCP_SELECT_CLK_REG 0xfe 72a7e1abadSJonathan Lemon 73a7e1abadSJonathan Lemon struct tod_reg { 74a7e1abadSJonathan Lemon u32 ctrl; 75a7e1abadSJonathan Lemon u32 status; 76a7e1abadSJonathan Lemon u32 uart_polarity; 77a7e1abadSJonathan Lemon u32 version; 78065efcc5SJonathan Lemon u32 adj_sec; 79a7e1abadSJonathan Lemon u32 __pad0[3]; 80a7e1abadSJonathan Lemon u32 uart_baud; 81a7e1abadSJonathan Lemon u32 __pad1[3]; 82a7e1abadSJonathan Lemon u32 utc_status; 83a7e1abadSJonathan Lemon u32 leap; 84a7e1abadSJonathan Lemon }; 85a7e1abadSJonathan Lemon 86a7e1abadSJonathan Lemon #define TOD_CTRL_PROTOCOL BIT(28) 87a7e1abadSJonathan Lemon #define TOD_CTRL_DISABLE_FMT_A BIT(17) 88a7e1abadSJonathan Lemon #define TOD_CTRL_DISABLE_FMT_B BIT(16) 89a7e1abadSJonathan Lemon #define TOD_CTRL_ENABLE BIT(0) 90a7e1abadSJonathan Lemon #define TOD_CTRL_GNSS_MASK ((1U << 4) - 1) 91a7e1abadSJonathan Lemon #define TOD_CTRL_GNSS_SHIFT 24 92a7e1abadSJonathan Lemon 93a7e1abadSJonathan Lemon #define TOD_STATUS_UTC_MASK 0xff 94a7e1abadSJonathan Lemon #define TOD_STATUS_UTC_VALID BIT(8) 959f492c4cSVadim Fedorenko #define TOD_STATUS_LEAP_ANNOUNCE BIT(12) 96a7e1abadSJonathan Lemon #define TOD_STATUS_LEAP_VALID BIT(16) 97a7e1abadSJonathan Lemon 98773bda96SJonathan Lemon struct ts_reg { 99773bda96SJonathan Lemon u32 enable; 100773bda96SJonathan Lemon u32 error; 101773bda96SJonathan Lemon u32 polarity; 102773bda96SJonathan Lemon u32 version; 103773bda96SJonathan Lemon u32 __pad0[4]; 104773bda96SJonathan Lemon u32 cable_delay; 105773bda96SJonathan Lemon u32 __pad1[3]; 106773bda96SJonathan Lemon u32 intr; 107773bda96SJonathan Lemon u32 intr_mask; 108773bda96SJonathan Lemon u32 event_count; 109773bda96SJonathan Lemon u32 __pad2[1]; 110773bda96SJonathan Lemon u32 ts_count; 111773bda96SJonathan Lemon u32 time_ns; 112773bda96SJonathan Lemon u32 time_sec; 113773bda96SJonathan Lemon u32 data_width; 114773bda96SJonathan Lemon u32 data; 115773bda96SJonathan Lemon }; 116773bda96SJonathan Lemon 117773bda96SJonathan Lemon struct pps_reg { 118773bda96SJonathan Lemon u32 ctrl; 119773bda96SJonathan Lemon u32 status; 1200d43d4f2SJonathan Lemon u32 __pad0[6]; 1210d43d4f2SJonathan Lemon u32 cable_delay; 122773bda96SJonathan Lemon }; 123773bda96SJonathan Lemon 124773bda96SJonathan Lemon #define PPS_STATUS_FILTER_ERR BIT(0) 125773bda96SJonathan Lemon #define PPS_STATUS_SUPERV_ERR BIT(1) 126773bda96SJonathan Lemon 127773bda96SJonathan Lemon struct img_reg { 128773bda96SJonathan Lemon u32 version; 129773bda96SJonathan Lemon }; 130773bda96SJonathan Lemon 131e1daf0ecSJonathan Lemon struct gpio_reg { 132e1daf0ecSJonathan Lemon u32 gpio1; 133e1daf0ecSJonathan Lemon u32 __pad0; 134e1daf0ecSJonathan Lemon u32 gpio2; 135e1daf0ecSJonathan Lemon u32 __pad1; 136e1daf0ecSJonathan Lemon }; 137e1daf0ecSJonathan Lemon 1386baf2925SJonathan Lemon struct irig_master_reg { 1396baf2925SJonathan Lemon u32 ctrl; 1406baf2925SJonathan Lemon u32 status; 1416baf2925SJonathan Lemon u32 __pad0; 1426baf2925SJonathan Lemon u32 version; 1436baf2925SJonathan Lemon u32 adj_sec; 1446baf2925SJonathan Lemon u32 mode_ctrl; 1456baf2925SJonathan Lemon }; 1466baf2925SJonathan Lemon 1476baf2925SJonathan Lemon #define IRIG_M_CTRL_ENABLE BIT(0) 1486baf2925SJonathan Lemon 1496baf2925SJonathan Lemon struct irig_slave_reg { 1506baf2925SJonathan Lemon u32 ctrl; 1516baf2925SJonathan Lemon u32 status; 1526baf2925SJonathan Lemon u32 __pad0; 1536baf2925SJonathan Lemon u32 version; 1546baf2925SJonathan Lemon u32 adj_sec; 1556baf2925SJonathan Lemon u32 mode_ctrl; 1566baf2925SJonathan Lemon }; 1576baf2925SJonathan Lemon 1586baf2925SJonathan Lemon #define IRIG_S_CTRL_ENABLE BIT(0) 1596baf2925SJonathan Lemon 1606baf2925SJonathan Lemon struct dcf_master_reg { 1616baf2925SJonathan Lemon u32 ctrl; 1626baf2925SJonathan Lemon u32 status; 1636baf2925SJonathan Lemon u32 __pad0; 1646baf2925SJonathan Lemon u32 version; 1656baf2925SJonathan Lemon u32 adj_sec; 1666baf2925SJonathan Lemon }; 1676baf2925SJonathan Lemon 1686baf2925SJonathan Lemon #define DCF_M_CTRL_ENABLE BIT(0) 1696baf2925SJonathan Lemon 1706baf2925SJonathan Lemon struct dcf_slave_reg { 1716baf2925SJonathan Lemon u32 ctrl; 1726baf2925SJonathan Lemon u32 status; 1736baf2925SJonathan Lemon u32 __pad0; 1746baf2925SJonathan Lemon u32 version; 1756baf2925SJonathan Lemon u32 adj_sec; 1766baf2925SJonathan Lemon }; 1776baf2925SJonathan Lemon 1786baf2925SJonathan Lemon #define DCF_S_CTRL_ENABLE BIT(0) 1796baf2925SJonathan Lemon 180773bda96SJonathan Lemon struct ptp_ocp_flash_info { 181773bda96SJonathan Lemon const char *name; 182773bda96SJonathan Lemon int pci_offset; 183773bda96SJonathan Lemon int data_size; 184773bda96SJonathan Lemon void *data; 185773bda96SJonathan Lemon }; 186773bda96SJonathan Lemon 1871618df6aSJonathan Lemon struct ptp_ocp_i2c_info { 1881618df6aSJonathan Lemon const char *name; 1891618df6aSJonathan Lemon unsigned long fixed_rate; 1901618df6aSJonathan Lemon size_t data_size; 1911618df6aSJonathan Lemon void *data; 1921618df6aSJonathan Lemon }; 1931618df6aSJonathan Lemon 194773bda96SJonathan Lemon struct ptp_ocp_ext_info { 195773bda96SJonathan Lemon int index; 196773bda96SJonathan Lemon irqreturn_t (*irq_fcn)(int irq, void *priv); 197a62a56d0SJonathan Lemon int (*enable)(void *priv, u32 req, bool enable); 198773bda96SJonathan Lemon }; 199773bda96SJonathan Lemon 200773bda96SJonathan Lemon struct ptp_ocp_ext_src { 201773bda96SJonathan Lemon void __iomem *mem; 202773bda96SJonathan Lemon struct ptp_ocp *bp; 203773bda96SJonathan Lemon struct ptp_ocp_ext_info *info; 204773bda96SJonathan Lemon int irq_vec; 205773bda96SJonathan Lemon }; 206773bda96SJonathan Lemon 207a7e1abadSJonathan Lemon struct ptp_ocp { 208a7e1abadSJonathan Lemon struct pci_dev *pdev; 209773bda96SJonathan Lemon struct device dev; 210a7e1abadSJonathan Lemon spinlock_t lock; 211a7e1abadSJonathan Lemon struct ocp_reg __iomem *reg; 212a7e1abadSJonathan Lemon struct tod_reg __iomem *tod; 2130d43d4f2SJonathan Lemon struct pps_reg __iomem *pps_to_ext; 2140d43d4f2SJonathan Lemon struct pps_reg __iomem *pps_to_clk; 215f67bf662SJonathan Lemon struct gpio_reg __iomem *pps_select; 216e1daf0ecSJonathan Lemon struct gpio_reg __iomem *sma; 2176baf2925SJonathan Lemon struct irig_master_reg __iomem *irig_out; 2186baf2925SJonathan Lemon struct irig_slave_reg __iomem *irig_in; 2196baf2925SJonathan Lemon struct dcf_master_reg __iomem *dcf_out; 2206baf2925SJonathan Lemon struct dcf_slave_reg __iomem *dcf_in; 221e3516bb4SJonathan Lemon struct tod_reg __iomem *nmea_out; 222773bda96SJonathan Lemon struct ptp_ocp_ext_src *pps; 223773bda96SJonathan Lemon struct ptp_ocp_ext_src *ts0; 224773bda96SJonathan Lemon struct ptp_ocp_ext_src *ts1; 225dcf61469SJonathan Lemon struct ptp_ocp_ext_src *ts2; 226773bda96SJonathan Lemon struct img_reg __iomem *image; 227a7e1abadSJonathan Lemon struct ptp_clock *ptp; 228a7e1abadSJonathan Lemon struct ptp_clock_info ptp_info; 229773bda96SJonathan Lemon struct platform_device *i2c_ctrl; 230773bda96SJonathan Lemon struct platform_device *spi_flash; 231773bda96SJonathan Lemon struct clk_hw *i2c_clk; 232773bda96SJonathan Lemon struct timer_list watchdog; 233f67bf662SJonathan Lemon struct dentry *debug_root; 234ef0cfb34SJonathan Lemon time64_t gnss_lost; 235773bda96SJonathan Lemon int id; 236773bda96SJonathan Lemon int n_irqs; 237ef0cfb34SJonathan Lemon int gnss_port; 23871d7e085SJonathan Lemon int gnss2_port; 239773bda96SJonathan Lemon int mac_port; /* miniature atomic clock */ 240e3516bb4SJonathan Lemon int nmea_port; 241773bda96SJonathan Lemon u8 serial[6]; 242773bda96SJonathan Lemon bool has_serial; 243a62a56d0SJonathan Lemon u32 pps_req_map; 24489260d87SJonathan Lemon int flash_start; 24589260d87SJonathan Lemon u32 utc_tai_offset; 2461acffc6eSJonathan Lemon u32 ts_window_adjust; 247a7e1abadSJonathan Lemon }; 248a7e1abadSJonathan Lemon 249a62a56d0SJonathan Lemon #define OCP_REQ_TIMESTAMP BIT(0) 250a62a56d0SJonathan Lemon #define OCP_REQ_PPS BIT(1) 251a62a56d0SJonathan Lemon 252773bda96SJonathan Lemon struct ocp_resource { 253773bda96SJonathan Lemon unsigned long offset; 254773bda96SJonathan Lemon int size; 255773bda96SJonathan Lemon int irq_vec; 256773bda96SJonathan Lemon int (*setup)(struct ptp_ocp *bp, struct ocp_resource *r); 257773bda96SJonathan Lemon void *extra; 258773bda96SJonathan Lemon unsigned long bp_offset; 25956ec4403SJonathan Lemon const char * const name; 260773bda96SJonathan Lemon }; 261773bda96SJonathan Lemon 262773bda96SJonathan Lemon static int ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r); 263773bda96SJonathan Lemon static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r); 264773bda96SJonathan Lemon static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r); 265773bda96SJonathan Lemon static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r); 266773bda96SJonathan Lemon static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r); 267773bda96SJonathan Lemon static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r); 268773bda96SJonathan Lemon static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv); 269a62a56d0SJonathan Lemon static int ptp_ocp_ts_enable(void *priv, u32 req, bool enable); 270773bda96SJonathan Lemon 271773bda96SJonathan Lemon #define bp_assign_entry(bp, res, val) ({ \ 272773bda96SJonathan Lemon uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset; \ 273773bda96SJonathan Lemon *(typeof(val) *)addr = val; \ 274773bda96SJonathan Lemon }) 275773bda96SJonathan Lemon 276773bda96SJonathan Lemon #define OCP_RES_LOCATION(member) \ 27756ec4403SJonathan Lemon .name = #member, .bp_offset = offsetof(struct ptp_ocp, member) 278773bda96SJonathan Lemon 279773bda96SJonathan Lemon #define OCP_MEM_RESOURCE(member) \ 280773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_mem 281773bda96SJonathan Lemon 282773bda96SJonathan Lemon #define OCP_SERIAL_RESOURCE(member) \ 283773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_serial 284773bda96SJonathan Lemon 285773bda96SJonathan Lemon #define OCP_I2C_RESOURCE(member) \ 286773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_i2c 287773bda96SJonathan Lemon 288773bda96SJonathan Lemon #define OCP_SPI_RESOURCE(member) \ 289773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_spi 290773bda96SJonathan Lemon 291773bda96SJonathan Lemon #define OCP_EXT_RESOURCE(member) \ 292773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_ext 293773bda96SJonathan Lemon 294773bda96SJonathan Lemon /* This is the MSI vector mapping used. 295a62a56d0SJonathan Lemon * 0: TS3 (and PPS) 296773bda96SJonathan Lemon * 1: TS0 297773bda96SJonathan Lemon * 2: TS1 29871d7e085SJonathan Lemon * 3: GNSS 29971d7e085SJonathan Lemon * 4: GNSS2 300773bda96SJonathan Lemon * 5: MAC 301dcf61469SJonathan Lemon * 6: TS2 3021447149dSJonathan Lemon * 7: I2C controller 303e3516bb4SJonathan Lemon * 8: HWICAP (notused) 304773bda96SJonathan Lemon * 9: SPI Flash 305e3516bb4SJonathan Lemon * 10: NMEA 306773bda96SJonathan Lemon */ 307773bda96SJonathan Lemon 308773bda96SJonathan Lemon static struct ocp_resource ocp_fb_resource[] = { 309773bda96SJonathan Lemon { 310773bda96SJonathan Lemon OCP_MEM_RESOURCE(reg), 311773bda96SJonathan Lemon .offset = 0x01000000, .size = 0x10000, 312773bda96SJonathan Lemon }, 313773bda96SJonathan Lemon { 314773bda96SJonathan Lemon OCP_EXT_RESOURCE(ts0), 315773bda96SJonathan Lemon .offset = 0x01010000, .size = 0x10000, .irq_vec = 1, 316773bda96SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 31756ec4403SJonathan Lemon .index = 0, 318773bda96SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 319773bda96SJonathan Lemon .enable = ptp_ocp_ts_enable, 320773bda96SJonathan Lemon }, 321773bda96SJonathan Lemon }, 322773bda96SJonathan Lemon { 323773bda96SJonathan Lemon OCP_EXT_RESOURCE(ts1), 324773bda96SJonathan Lemon .offset = 0x01020000, .size = 0x10000, .irq_vec = 2, 325773bda96SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 32656ec4403SJonathan Lemon .index = 1, 327773bda96SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 328773bda96SJonathan Lemon .enable = ptp_ocp_ts_enable, 329773bda96SJonathan Lemon }, 330773bda96SJonathan Lemon }, 331773bda96SJonathan Lemon { 332dcf61469SJonathan Lemon OCP_EXT_RESOURCE(ts2), 333dcf61469SJonathan Lemon .offset = 0x01060000, .size = 0x10000, .irq_vec = 6, 334dcf61469SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 335dcf61469SJonathan Lemon .index = 2, 336dcf61469SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 337dcf61469SJonathan Lemon .enable = ptp_ocp_ts_enable, 338dcf61469SJonathan Lemon }, 339dcf61469SJonathan Lemon }, 340dcf61469SJonathan Lemon { 341a62a56d0SJonathan Lemon OCP_EXT_RESOURCE(pps), 342a62a56d0SJonathan Lemon .offset = 0x010C0000, .size = 0x10000, .irq_vec = 0, 343a62a56d0SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 344a62a56d0SJonathan Lemon .index = 3, 345a62a56d0SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 346a62a56d0SJonathan Lemon .enable = ptp_ocp_ts_enable, 347a62a56d0SJonathan Lemon }, 348a62a56d0SJonathan Lemon }, 349a62a56d0SJonathan Lemon { 3500d43d4f2SJonathan Lemon OCP_MEM_RESOURCE(pps_to_ext), 3510d43d4f2SJonathan Lemon .offset = 0x01030000, .size = 0x10000, 3520d43d4f2SJonathan Lemon }, 3530d43d4f2SJonathan Lemon { 3540d43d4f2SJonathan Lemon OCP_MEM_RESOURCE(pps_to_clk), 355773bda96SJonathan Lemon .offset = 0x01040000, .size = 0x10000, 356773bda96SJonathan Lemon }, 357773bda96SJonathan Lemon { 358773bda96SJonathan Lemon OCP_MEM_RESOURCE(tod), 359773bda96SJonathan Lemon .offset = 0x01050000, .size = 0x10000, 360773bda96SJonathan Lemon }, 361773bda96SJonathan Lemon { 3626baf2925SJonathan Lemon OCP_MEM_RESOURCE(irig_in), 3636baf2925SJonathan Lemon .offset = 0x01070000, .size = 0x10000, 3646baf2925SJonathan Lemon }, 3656baf2925SJonathan Lemon { 3666baf2925SJonathan Lemon OCP_MEM_RESOURCE(irig_out), 3676baf2925SJonathan Lemon .offset = 0x01080000, .size = 0x10000, 3686baf2925SJonathan Lemon }, 3696baf2925SJonathan Lemon { 3706baf2925SJonathan Lemon OCP_MEM_RESOURCE(dcf_in), 3716baf2925SJonathan Lemon .offset = 0x01090000, .size = 0x10000, 3726baf2925SJonathan Lemon }, 3736baf2925SJonathan Lemon { 3746baf2925SJonathan Lemon OCP_MEM_RESOURCE(dcf_out), 3756baf2925SJonathan Lemon .offset = 0x010A0000, .size = 0x10000, 3766baf2925SJonathan Lemon }, 3776baf2925SJonathan Lemon { 378e3516bb4SJonathan Lemon OCP_MEM_RESOURCE(nmea_out), 379e3516bb4SJonathan Lemon .offset = 0x010B0000, .size = 0x10000, 380e3516bb4SJonathan Lemon }, 381e3516bb4SJonathan Lemon { 382773bda96SJonathan Lemon OCP_MEM_RESOURCE(image), 383773bda96SJonathan Lemon .offset = 0x00020000, .size = 0x1000, 384773bda96SJonathan Lemon }, 385773bda96SJonathan Lemon { 386f67bf662SJonathan Lemon OCP_MEM_RESOURCE(pps_select), 387f67bf662SJonathan Lemon .offset = 0x00130000, .size = 0x1000, 388f67bf662SJonathan Lemon }, 389f67bf662SJonathan Lemon { 390e1daf0ecSJonathan Lemon OCP_MEM_RESOURCE(sma), 391e1daf0ecSJonathan Lemon .offset = 0x00140000, .size = 0x1000, 392e1daf0ecSJonathan Lemon }, 393e1daf0ecSJonathan Lemon { 394773bda96SJonathan Lemon OCP_I2C_RESOURCE(i2c_ctrl), 395773bda96SJonathan Lemon .offset = 0x00150000, .size = 0x10000, .irq_vec = 7, 3961618df6aSJonathan Lemon .extra = &(struct ptp_ocp_i2c_info) { 3971618df6aSJonathan Lemon .name = "xiic-i2c", 3981618df6aSJonathan Lemon .fixed_rate = 50000000, 3991618df6aSJonathan Lemon }, 400773bda96SJonathan Lemon }, 401773bda96SJonathan Lemon { 402ef0cfb34SJonathan Lemon OCP_SERIAL_RESOURCE(gnss_port), 403773bda96SJonathan Lemon .offset = 0x00160000 + 0x1000, .irq_vec = 3, 404773bda96SJonathan Lemon }, 405773bda96SJonathan Lemon { 40671d7e085SJonathan Lemon OCP_SERIAL_RESOURCE(gnss2_port), 40771d7e085SJonathan Lemon .offset = 0x00170000 + 0x1000, .irq_vec = 4, 40871d7e085SJonathan Lemon }, 40971d7e085SJonathan Lemon { 410773bda96SJonathan Lemon OCP_SERIAL_RESOURCE(mac_port), 411773bda96SJonathan Lemon .offset = 0x00180000 + 0x1000, .irq_vec = 5, 412773bda96SJonathan Lemon }, 413773bda96SJonathan Lemon { 414e3516bb4SJonathan Lemon OCP_SERIAL_RESOURCE(nmea_port), 415e3516bb4SJonathan Lemon .offset = 0x00190000 + 0x1000, .irq_vec = 10, 416e3516bb4SJonathan Lemon }, 417e3516bb4SJonathan Lemon { 418773bda96SJonathan Lemon OCP_SPI_RESOURCE(spi_flash), 419773bda96SJonathan Lemon .offset = 0x00310000, .size = 0x10000, .irq_vec = 9, 420773bda96SJonathan Lemon .extra = &(struct ptp_ocp_flash_info) { 421773bda96SJonathan Lemon .name = "xilinx_spi", .pci_offset = 0, 422773bda96SJonathan Lemon .data_size = sizeof(struct xspi_platform_data), 423773bda96SJonathan Lemon .data = &(struct xspi_platform_data) { 424773bda96SJonathan Lemon .num_chipselect = 1, 425773bda96SJonathan Lemon .bits_per_word = 8, 426773bda96SJonathan Lemon .num_devices = 1, 427773bda96SJonathan Lemon .devices = &(struct spi_board_info) { 428773bda96SJonathan Lemon .modalias = "spi-nor", 429773bda96SJonathan Lemon }, 430773bda96SJonathan Lemon }, 431773bda96SJonathan Lemon }, 432773bda96SJonathan Lemon }, 433773bda96SJonathan Lemon { 434773bda96SJonathan Lemon .setup = ptp_ocp_fb_board_init, 435773bda96SJonathan Lemon }, 436773bda96SJonathan Lemon { } 437773bda96SJonathan Lemon }; 438773bda96SJonathan Lemon 439773bda96SJonathan Lemon static const struct pci_device_id ptp_ocp_pcidev_id[] = { 440773bda96SJonathan Lemon { PCI_DEVICE_DATA(FACEBOOK, TIMECARD, &ocp_fb_resource) }, 441773bda96SJonathan Lemon { 0 } 442773bda96SJonathan Lemon }; 443773bda96SJonathan Lemon MODULE_DEVICE_TABLE(pci, ptp_ocp_pcidev_id); 444773bda96SJonathan Lemon 445773bda96SJonathan Lemon static DEFINE_MUTEX(ptp_ocp_lock); 446773bda96SJonathan Lemon static DEFINE_IDR(ptp_ocp_idr); 447773bda96SJonathan Lemon 448e1daf0ecSJonathan Lemon struct ocp_selector { 449773bda96SJonathan Lemon const char *name; 450773bda96SJonathan Lemon int value; 451e1daf0ecSJonathan Lemon }; 452e1daf0ecSJonathan Lemon 453e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_clock[] = { 454773bda96SJonathan Lemon { .name = "NONE", .value = 0 }, 455773bda96SJonathan Lemon { .name = "TOD", .value = 1 }, 456773bda96SJonathan Lemon { .name = "IRIG", .value = 2 }, 457773bda96SJonathan Lemon { .name = "PPS", .value = 3 }, 458773bda96SJonathan Lemon { .name = "PTP", .value = 4 }, 459773bda96SJonathan Lemon { .name = "RTC", .value = 5 }, 460773bda96SJonathan Lemon { .name = "DCF", .value = 6 }, 461773bda96SJonathan Lemon { .name = "REGS", .value = 0xfe }, 462773bda96SJonathan Lemon { .name = "EXT", .value = 0xff }, 463e1daf0ecSJonathan Lemon { } 464e1daf0ecSJonathan Lemon }; 465e1daf0ecSJonathan Lemon 466e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_sma_in[] = { 467e1daf0ecSJonathan Lemon { .name = "10Mhz", .value = 0x00 }, 468e1daf0ecSJonathan Lemon { .name = "PPS1", .value = 0x01 }, 469e1daf0ecSJonathan Lemon { .name = "PPS2", .value = 0x02 }, 470e1daf0ecSJonathan Lemon { .name = "TS1", .value = 0x04 }, 471e1daf0ecSJonathan Lemon { .name = "TS2", .value = 0x08 }, 4726baf2925SJonathan Lemon { .name = "IRIG", .value = 0x10 }, 4736baf2925SJonathan Lemon { .name = "DCF", .value = 0x20 }, 474e1daf0ecSJonathan Lemon { } 475e1daf0ecSJonathan Lemon }; 476e1daf0ecSJonathan Lemon 477e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_sma_out[] = { 478e1daf0ecSJonathan Lemon { .name = "10Mhz", .value = 0x00 }, 479e1daf0ecSJonathan Lemon { .name = "PHC", .value = 0x01 }, 480e1daf0ecSJonathan Lemon { .name = "MAC", .value = 0x02 }, 481e1daf0ecSJonathan Lemon { .name = "GNSS", .value = 0x04 }, 482e1daf0ecSJonathan Lemon { .name = "GNSS2", .value = 0x08 }, 4836baf2925SJonathan Lemon { .name = "IRIG", .value = 0x10 }, 4846baf2925SJonathan Lemon { .name = "DCF", .value = 0x20 }, 485e1daf0ecSJonathan Lemon { } 486773bda96SJonathan Lemon }; 487773bda96SJonathan Lemon 488773bda96SJonathan Lemon static const char * 489e1daf0ecSJonathan Lemon ptp_ocp_select_name_from_val(struct ocp_selector *tbl, int val) 490773bda96SJonathan Lemon { 491773bda96SJonathan Lemon int i; 492773bda96SJonathan Lemon 493e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) 494e1daf0ecSJonathan Lemon if (tbl[i].value == val) 495e1daf0ecSJonathan Lemon return tbl[i].name; 496773bda96SJonathan Lemon return NULL; 497773bda96SJonathan Lemon } 498773bda96SJonathan Lemon 499773bda96SJonathan Lemon static int 500e1daf0ecSJonathan Lemon ptp_ocp_select_val_from_name(struct ocp_selector *tbl, const char *name) 501773bda96SJonathan Lemon { 502e1daf0ecSJonathan Lemon const char *select; 503773bda96SJonathan Lemon int i; 504773bda96SJonathan Lemon 505e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) { 506e1daf0ecSJonathan Lemon select = tbl[i].name; 507e1daf0ecSJonathan Lemon if (!strncasecmp(name, select, strlen(select))) 508e1daf0ecSJonathan Lemon return tbl[i].value; 509773bda96SJonathan Lemon } 510773bda96SJonathan Lemon return -EINVAL; 511773bda96SJonathan Lemon } 512773bda96SJonathan Lemon 513e1daf0ecSJonathan Lemon static ssize_t 514e1daf0ecSJonathan Lemon ptp_ocp_select_table_show(struct ocp_selector *tbl, char *buf) 515e1daf0ecSJonathan Lemon { 516e1daf0ecSJonathan Lemon ssize_t count; 517e1daf0ecSJonathan Lemon int i; 518e1daf0ecSJonathan Lemon 519e1daf0ecSJonathan Lemon count = 0; 520e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) 521e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", tbl[i].name); 522e1daf0ecSJonathan Lemon if (count) 523e1daf0ecSJonathan Lemon count--; 524e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "\n"); 525e1daf0ecSJonathan Lemon return count; 526e1daf0ecSJonathan Lemon } 527e1daf0ecSJonathan Lemon 528a7e1abadSJonathan Lemon static int 529a7e1abadSJonathan Lemon __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts, 530a7e1abadSJonathan Lemon struct ptp_system_timestamp *sts) 531a7e1abadSJonathan Lemon { 532a7e1abadSJonathan Lemon u32 ctrl, time_sec, time_ns; 533a7e1abadSJonathan Lemon int i; 534a7e1abadSJonathan Lemon 535a7e1abadSJonathan Lemon ptp_read_system_prets(sts); 5361acffc6eSJonathan Lemon 5371acffc6eSJonathan Lemon ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE; 538a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 539a7e1abadSJonathan Lemon 540a7e1abadSJonathan Lemon for (i = 0; i < 100; i++) { 541a7e1abadSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 542a7e1abadSJonathan Lemon if (ctrl & OCP_CTRL_READ_TIME_DONE) 543a7e1abadSJonathan Lemon break; 544a7e1abadSJonathan Lemon } 545a7e1abadSJonathan Lemon ptp_read_system_postts(sts); 546a7e1abadSJonathan Lemon 5471acffc6eSJonathan Lemon if (sts && bp->ts_window_adjust) { 5481acffc6eSJonathan Lemon s64 ns = timespec64_to_ns(&sts->post_ts); 5491acffc6eSJonathan Lemon 5501acffc6eSJonathan Lemon sts->post_ts = ns_to_timespec64(ns - bp->ts_window_adjust); 5511acffc6eSJonathan Lemon } 5521acffc6eSJonathan Lemon 553a7e1abadSJonathan Lemon time_ns = ioread32(&bp->reg->time_ns); 554a7e1abadSJonathan Lemon time_sec = ioread32(&bp->reg->time_sec); 555a7e1abadSJonathan Lemon 556a7e1abadSJonathan Lemon ts->tv_sec = time_sec; 557a7e1abadSJonathan Lemon ts->tv_nsec = time_ns; 558a7e1abadSJonathan Lemon 559a7e1abadSJonathan Lemon return ctrl & OCP_CTRL_READ_TIME_DONE ? 0 : -ETIMEDOUT; 560a7e1abadSJonathan Lemon } 561a7e1abadSJonathan Lemon 562a7e1abadSJonathan Lemon static int 563a7e1abadSJonathan Lemon ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts, 564a7e1abadSJonathan Lemon struct ptp_system_timestamp *sts) 565a7e1abadSJonathan Lemon { 566a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 567a7e1abadSJonathan Lemon unsigned long flags; 568a7e1abadSJonathan Lemon int err; 569a7e1abadSJonathan Lemon 570a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 571a7e1abadSJonathan Lemon err = __ptp_ocp_gettime_locked(bp, ts, sts); 572a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 573a7e1abadSJonathan Lemon 574a7e1abadSJonathan Lemon return err; 575a7e1abadSJonathan Lemon } 576a7e1abadSJonathan Lemon 577a7e1abadSJonathan Lemon static void 578a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts) 579a7e1abadSJonathan Lemon { 580a7e1abadSJonathan Lemon u32 ctrl, time_sec, time_ns; 581a7e1abadSJonathan Lemon u32 select; 582a7e1abadSJonathan Lemon 583a7e1abadSJonathan Lemon time_ns = ts->tv_nsec; 584a7e1abadSJonathan Lemon time_sec = ts->tv_sec; 585a7e1abadSJonathan Lemon 586a7e1abadSJonathan Lemon select = ioread32(&bp->reg->select); 587a7e1abadSJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 588a7e1abadSJonathan Lemon 589a7e1abadSJonathan Lemon iowrite32(time_ns, &bp->reg->adjust_ns); 590a7e1abadSJonathan Lemon iowrite32(time_sec, &bp->reg->adjust_sec); 591a7e1abadSJonathan Lemon 5921acffc6eSJonathan Lemon ctrl = OCP_CTRL_ADJUST_TIME | OCP_CTRL_ENABLE; 593a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 594a7e1abadSJonathan Lemon 595a7e1abadSJonathan Lemon /* restore clock selection */ 596a7e1abadSJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 597a7e1abadSJonathan Lemon } 598a7e1abadSJonathan Lemon 599a7e1abadSJonathan Lemon static int 600a7e1abadSJonathan Lemon ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts) 601a7e1abadSJonathan Lemon { 602a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 603a7e1abadSJonathan Lemon unsigned long flags; 604a7e1abadSJonathan Lemon 605a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 606a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(bp, ts); 607a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 608a7e1abadSJonathan Lemon 609a7e1abadSJonathan Lemon return 0; 610a7e1abadSJonathan Lemon } 611a7e1abadSJonathan Lemon 6126d59d4faSJonathan Lemon static void 61390f8f4c0SJonathan Lemon __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val) 6146d59d4faSJonathan Lemon { 6156d59d4faSJonathan Lemon u32 select, ctrl; 6166d59d4faSJonathan Lemon 6176d59d4faSJonathan Lemon select = ioread32(&bp->reg->select); 6186d59d4faSJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 6196d59d4faSJonathan Lemon 6206d59d4faSJonathan Lemon iowrite32(adj_val, &bp->reg->offset_ns); 62190f8f4c0SJonathan Lemon iowrite32(NSEC_PER_SEC, &bp->reg->offset_window_ns); 6226d59d4faSJonathan Lemon 6236d59d4faSJonathan Lemon ctrl = OCP_CTRL_ADJUST_OFFSET | OCP_CTRL_ENABLE; 6246d59d4faSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 6256d59d4faSJonathan Lemon 6266d59d4faSJonathan Lemon /* restore clock selection */ 6276d59d4faSJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 6286d59d4faSJonathan Lemon } 6296d59d4faSJonathan Lemon 63090f8f4c0SJonathan Lemon static void 63190f8f4c0SJonathan Lemon ptp_ocp_adjtime_coarse(struct ptp_ocp *bp, u64 delta_ns) 63290f8f4c0SJonathan Lemon { 63390f8f4c0SJonathan Lemon struct timespec64 ts; 63490f8f4c0SJonathan Lemon unsigned long flags; 63590f8f4c0SJonathan Lemon int err; 63690f8f4c0SJonathan Lemon 63790f8f4c0SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 63890f8f4c0SJonathan Lemon err = __ptp_ocp_gettime_locked(bp, &ts, NULL); 63990f8f4c0SJonathan Lemon if (likely(!err)) { 64090f8f4c0SJonathan Lemon timespec64_add_ns(&ts, delta_ns); 64190f8f4c0SJonathan Lemon __ptp_ocp_settime_locked(bp, &ts); 64290f8f4c0SJonathan Lemon } 64390f8f4c0SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 64490f8f4c0SJonathan Lemon } 64590f8f4c0SJonathan Lemon 646a7e1abadSJonathan Lemon static int 647a7e1abadSJonathan Lemon ptp_ocp_adjtime(struct ptp_clock_info *ptp_info, s64 delta_ns) 648a7e1abadSJonathan Lemon { 649a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 650a7e1abadSJonathan Lemon unsigned long flags; 6516d59d4faSJonathan Lemon u32 adj_ns, sign; 652a7e1abadSJonathan Lemon 65390f8f4c0SJonathan Lemon if (delta_ns > NSEC_PER_SEC || -delta_ns > NSEC_PER_SEC) { 65490f8f4c0SJonathan Lemon ptp_ocp_adjtime_coarse(bp, delta_ns); 65590f8f4c0SJonathan Lemon return 0; 65690f8f4c0SJonathan Lemon } 65790f8f4c0SJonathan Lemon 6586d59d4faSJonathan Lemon sign = delta_ns < 0 ? BIT(31) : 0; 6596d59d4faSJonathan Lemon adj_ns = sign ? -delta_ns : delta_ns; 660a7e1abadSJonathan Lemon 661a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 6626d59d4faSJonathan Lemon __ptp_ocp_adjtime_locked(bp, sign | adj_ns); 663a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 664a7e1abadSJonathan Lemon 6656d59d4faSJonathan Lemon return 0; 666a7e1abadSJonathan Lemon } 667a7e1abadSJonathan Lemon 668a7e1abadSJonathan Lemon static int 669a7e1abadSJonathan Lemon ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm) 670a7e1abadSJonathan Lemon { 671a7e1abadSJonathan Lemon if (scaled_ppm == 0) 672a7e1abadSJonathan Lemon return 0; 673a7e1abadSJonathan Lemon 674a7e1abadSJonathan Lemon return -EOPNOTSUPP; 675a7e1abadSJonathan Lemon } 676a7e1abadSJonathan Lemon 677773bda96SJonathan Lemon static int 6786d59d4faSJonathan Lemon ptp_ocp_null_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns) 679773bda96SJonathan Lemon { 680773bda96SJonathan Lemon return -EOPNOTSUPP; 681773bda96SJonathan Lemon } 682773bda96SJonathan Lemon 683773bda96SJonathan Lemon static int 684773bda96SJonathan Lemon ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq, 685773bda96SJonathan Lemon int on) 686773bda96SJonathan Lemon { 687773bda96SJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 688773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = NULL; 689a62a56d0SJonathan Lemon u32 req; 690773bda96SJonathan Lemon int err; 691773bda96SJonathan Lemon 692773bda96SJonathan Lemon switch (rq->type) { 693773bda96SJonathan Lemon case PTP_CLK_REQ_EXTTS: 694a62a56d0SJonathan Lemon req = OCP_REQ_TIMESTAMP; 695773bda96SJonathan Lemon switch (rq->extts.index) { 696773bda96SJonathan Lemon case 0: 697773bda96SJonathan Lemon ext = bp->ts0; 698773bda96SJonathan Lemon break; 699773bda96SJonathan Lemon case 1: 700773bda96SJonathan Lemon ext = bp->ts1; 701773bda96SJonathan Lemon break; 702dcf61469SJonathan Lemon case 2: 703dcf61469SJonathan Lemon ext = bp->ts2; 704dcf61469SJonathan Lemon break; 705a62a56d0SJonathan Lemon case 3: 706a62a56d0SJonathan Lemon ext = bp->pps; 707a62a56d0SJonathan Lemon break; 708773bda96SJonathan Lemon } 709773bda96SJonathan Lemon break; 710773bda96SJonathan Lemon case PTP_CLK_REQ_PPS: 711a62a56d0SJonathan Lemon req = OCP_REQ_PPS; 712773bda96SJonathan Lemon ext = bp->pps; 713773bda96SJonathan Lemon break; 714a62a56d0SJonathan Lemon case PTP_CLK_REQ_PEROUT: 715a62a56d0SJonathan Lemon if (on && 716a62a56d0SJonathan Lemon (rq->perout.period.sec != 1 || rq->perout.period.nsec != 0)) 717a62a56d0SJonathan Lemon return -EINVAL; 718a62a56d0SJonathan Lemon /* This is a request for 1PPS on an output SMA. 719a62a56d0SJonathan Lemon * Allow, but assume manual configuration. 720a62a56d0SJonathan Lemon */ 721a62a56d0SJonathan Lemon return 0; 722773bda96SJonathan Lemon default: 723773bda96SJonathan Lemon return -EOPNOTSUPP; 724773bda96SJonathan Lemon } 725773bda96SJonathan Lemon 726773bda96SJonathan Lemon err = -ENXIO; 727773bda96SJonathan Lemon if (ext) 728a62a56d0SJonathan Lemon err = ext->info->enable(ext, req, on); 729773bda96SJonathan Lemon 730773bda96SJonathan Lemon return err; 731773bda96SJonathan Lemon } 732773bda96SJonathan Lemon 733a7e1abadSJonathan Lemon static const struct ptp_clock_info ptp_ocp_clock_info = { 734a7e1abadSJonathan Lemon .owner = THIS_MODULE, 735a7e1abadSJonathan Lemon .name = KBUILD_MODNAME, 736a7e1abadSJonathan Lemon .max_adj = 100000000, 737a7e1abadSJonathan Lemon .gettimex64 = ptp_ocp_gettimex, 738a7e1abadSJonathan Lemon .settime64 = ptp_ocp_settime, 739a7e1abadSJonathan Lemon .adjtime = ptp_ocp_adjtime, 740a7e1abadSJonathan Lemon .adjfine = ptp_ocp_null_adjfine, 7416d59d4faSJonathan Lemon .adjphase = ptp_ocp_null_adjphase, 742773bda96SJonathan Lemon .enable = ptp_ocp_enable, 743773bda96SJonathan Lemon .pps = true, 744a62a56d0SJonathan Lemon .n_ext_ts = 4, 745a62a56d0SJonathan Lemon .n_per_out = 1, 746a7e1abadSJonathan Lemon }; 747a7e1abadSJonathan Lemon 748773bda96SJonathan Lemon static void 749773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp) 750773bda96SJonathan Lemon { 751773bda96SJonathan Lemon u32 ctrl, select; 752773bda96SJonathan Lemon 753773bda96SJonathan Lemon select = ioread32(&bp->reg->select); 754773bda96SJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 755773bda96SJonathan Lemon 756773bda96SJonathan Lemon iowrite32(0, &bp->reg->drift_ns); 757773bda96SJonathan Lemon 7581acffc6eSJonathan Lemon ctrl = OCP_CTRL_ADJUST_DRIFT | OCP_CTRL_ENABLE; 759773bda96SJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 760773bda96SJonathan Lemon 761773bda96SJonathan Lemon /* restore clock selection */ 762773bda96SJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 763773bda96SJonathan Lemon } 764773bda96SJonathan Lemon 765773bda96SJonathan Lemon static void 766e68462a0SVadim Fedorenko ptp_ocp_utc_distribute(struct ptp_ocp *bp, u32 val) 767e68462a0SVadim Fedorenko { 768e68462a0SVadim Fedorenko unsigned long flags; 769e68462a0SVadim Fedorenko 770e68462a0SVadim Fedorenko spin_lock_irqsave(&bp->lock, flags); 771e68462a0SVadim Fedorenko 772e68462a0SVadim Fedorenko bp->utc_tai_offset = val; 773e68462a0SVadim Fedorenko 774e68462a0SVadim Fedorenko if (bp->irig_out) 775e68462a0SVadim Fedorenko iowrite32(val, &bp->irig_out->adj_sec); 776e68462a0SVadim Fedorenko if (bp->dcf_out) 777e68462a0SVadim Fedorenko iowrite32(val, &bp->dcf_out->adj_sec); 778e68462a0SVadim Fedorenko if (bp->nmea_out) 779e68462a0SVadim Fedorenko iowrite32(val, &bp->nmea_out->adj_sec); 780e68462a0SVadim Fedorenko 781e68462a0SVadim Fedorenko spin_unlock_irqrestore(&bp->lock, flags); 782e68462a0SVadim Fedorenko } 783e68462a0SVadim Fedorenko 784e68462a0SVadim Fedorenko static void 785773bda96SJonathan Lemon ptp_ocp_watchdog(struct timer_list *t) 786773bda96SJonathan Lemon { 787773bda96SJonathan Lemon struct ptp_ocp *bp = from_timer(bp, t, watchdog); 788773bda96SJonathan Lemon unsigned long flags; 789e68462a0SVadim Fedorenko u32 status, utc_offset; 790773bda96SJonathan Lemon 7910d43d4f2SJonathan Lemon status = ioread32(&bp->pps_to_clk->status); 792773bda96SJonathan Lemon 793773bda96SJonathan Lemon if (status & PPS_STATUS_SUPERV_ERR) { 7940d43d4f2SJonathan Lemon iowrite32(status, &bp->pps_to_clk->status); 795ef0cfb34SJonathan Lemon if (!bp->gnss_lost) { 796773bda96SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 797773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(bp); 798773bda96SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 799ef0cfb34SJonathan Lemon bp->gnss_lost = ktime_get_real_seconds(); 800773bda96SJonathan Lemon } 801773bda96SJonathan Lemon 802ef0cfb34SJonathan Lemon } else if (bp->gnss_lost) { 803ef0cfb34SJonathan Lemon bp->gnss_lost = 0; 804773bda96SJonathan Lemon } 805773bda96SJonathan Lemon 806e68462a0SVadim Fedorenko /* if GNSS provides correct data we can rely on 807e68462a0SVadim Fedorenko * it to get leap second information 808e68462a0SVadim Fedorenko */ 809e68462a0SVadim Fedorenko if (bp->tod) { 810e68462a0SVadim Fedorenko status = ioread32(&bp->tod->utc_status); 811e68462a0SVadim Fedorenko utc_offset = status & TOD_STATUS_UTC_MASK; 812e68462a0SVadim Fedorenko if (status & TOD_STATUS_UTC_VALID && 813e68462a0SVadim Fedorenko utc_offset != bp->utc_tai_offset) 814e68462a0SVadim Fedorenko ptp_ocp_utc_distribute(bp, utc_offset); 815e68462a0SVadim Fedorenko } 816e68462a0SVadim Fedorenko 817773bda96SJonathan Lemon mod_timer(&bp->watchdog, jiffies + HZ); 818773bda96SJonathan Lemon } 819773bda96SJonathan Lemon 8201acffc6eSJonathan Lemon static void 8211acffc6eSJonathan Lemon ptp_ocp_estimate_pci_timing(struct ptp_ocp *bp) 8221acffc6eSJonathan Lemon { 8231acffc6eSJonathan Lemon ktime_t start, end; 8241acffc6eSJonathan Lemon ktime_t delay; 8251acffc6eSJonathan Lemon u32 ctrl; 8261acffc6eSJonathan Lemon 8271acffc6eSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 8281acffc6eSJonathan Lemon ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE; 8291acffc6eSJonathan Lemon 8301acffc6eSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 8311acffc6eSJonathan Lemon 8321acffc6eSJonathan Lemon start = ktime_get_ns(); 8331acffc6eSJonathan Lemon 8341acffc6eSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 8351acffc6eSJonathan Lemon 8361acffc6eSJonathan Lemon end = ktime_get_ns(); 8371acffc6eSJonathan Lemon 8381acffc6eSJonathan Lemon delay = end - start; 8391acffc6eSJonathan Lemon bp->ts_window_adjust = (delay >> 5) * 3; 8401acffc6eSJonathan Lemon } 8411acffc6eSJonathan Lemon 842a7e1abadSJonathan Lemon static int 843773bda96SJonathan Lemon ptp_ocp_init_clock(struct ptp_ocp *bp) 844a7e1abadSJonathan Lemon { 845a7e1abadSJonathan Lemon struct timespec64 ts; 846a7e1abadSJonathan Lemon bool sync; 847a7e1abadSJonathan Lemon u32 ctrl; 848a7e1abadSJonathan Lemon 8491acffc6eSJonathan Lemon ctrl = OCP_CTRL_ENABLE; 850a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 851a7e1abadSJonathan Lemon 852773bda96SJonathan Lemon /* NO DRIFT Correction */ 853773bda96SJonathan Lemon /* offset_p:i 1/8, offset_i: 1/16, drift_p: 0, drift_i: 0 */ 854773bda96SJonathan Lemon iowrite32(0x2000, &bp->reg->servo_offset_p); 855773bda96SJonathan Lemon iowrite32(0x1000, &bp->reg->servo_offset_i); 856773bda96SJonathan Lemon iowrite32(0, &bp->reg->servo_drift_p); 857773bda96SJonathan Lemon iowrite32(0, &bp->reg->servo_drift_i); 858773bda96SJonathan Lemon 859773bda96SJonathan Lemon /* latch servo values */ 860773bda96SJonathan Lemon ctrl |= OCP_CTRL_ADJUST_SERVO; 861773bda96SJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 862773bda96SJonathan Lemon 863a7e1abadSJonathan Lemon if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) { 864a7e1abadSJonathan Lemon dev_err(&bp->pdev->dev, "clock not enabled\n"); 865a7e1abadSJonathan Lemon return -ENODEV; 866a7e1abadSJonathan Lemon } 867a7e1abadSJonathan Lemon 8681acffc6eSJonathan Lemon ptp_ocp_estimate_pci_timing(bp); 8691acffc6eSJonathan Lemon 870a7e1abadSJonathan Lemon sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC; 871a7e1abadSJonathan Lemon if (!sync) { 872065efcc5SJonathan Lemon ktime_get_clocktai_ts64(&ts); 873a7e1abadSJonathan Lemon ptp_ocp_settime(&bp->ptp_info, &ts); 874a7e1abadSJonathan Lemon } 875a7e1abadSJonathan Lemon 876065efcc5SJonathan Lemon /* If there is a clock supervisor, then enable the watchdog */ 877065efcc5SJonathan Lemon if (bp->pps_to_clk) { 878773bda96SJonathan Lemon timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0); 879773bda96SJonathan Lemon mod_timer(&bp->watchdog, jiffies + HZ); 880065efcc5SJonathan Lemon } 881773bda96SJonathan Lemon 882a7e1abadSJonathan Lemon return 0; 883a7e1abadSJonathan Lemon } 884a7e1abadSJonathan Lemon 885a7e1abadSJonathan Lemon static void 886065efcc5SJonathan Lemon ptp_ocp_tod_init(struct ptp_ocp *bp) 887065efcc5SJonathan Lemon { 888065efcc5SJonathan Lemon u32 ctrl, reg; 889065efcc5SJonathan Lemon 890065efcc5SJonathan Lemon ctrl = ioread32(&bp->tod->ctrl); 891065efcc5SJonathan Lemon ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE; 892065efcc5SJonathan Lemon ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B); 893065efcc5SJonathan Lemon iowrite32(ctrl, &bp->tod->ctrl); 894065efcc5SJonathan Lemon 895065efcc5SJonathan Lemon reg = ioread32(&bp->tod->utc_status); 896065efcc5SJonathan Lemon if (reg & TOD_STATUS_UTC_VALID) 897065efcc5SJonathan Lemon ptp_ocp_utc_distribute(bp, reg & TOD_STATUS_UTC_MASK); 898065efcc5SJonathan Lemon } 899065efcc5SJonathan Lemon 9009f492c4cSVadim Fedorenko static const char * 9019f492c4cSVadim Fedorenko ptp_ocp_tod_proto_name(const int idx) 902a7e1abadSJonathan Lemon { 903a7e1abadSJonathan Lemon static const char * const proto_name[] = { 904a7e1abadSJonathan Lemon "NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none", 905a7e1abadSJonathan Lemon "UBX", "UBX_UTC", "UBX_LS", "UBX_none" 906a7e1abadSJonathan Lemon }; 9079f492c4cSVadim Fedorenko return proto_name[idx]; 9089f492c4cSVadim Fedorenko } 9099f492c4cSVadim Fedorenko 9109f492c4cSVadim Fedorenko static const char * 9119f492c4cSVadim Fedorenko ptp_ocp_tod_gnss_name(int idx) 9129f492c4cSVadim Fedorenko { 913a7e1abadSJonathan Lemon static const char * const gnss_name[] = { 914a7e1abadSJonathan Lemon "ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU", 9159f492c4cSVadim Fedorenko "Unknown" 916a7e1abadSJonathan Lemon }; 91772f00505SDan Carpenter if (idx >= ARRAY_SIZE(gnss_name)) 9189f492c4cSVadim Fedorenko idx = ARRAY_SIZE(gnss_name) - 1; 9199f492c4cSVadim Fedorenko return gnss_name[idx]; 920a7e1abadSJonathan Lemon } 921a7e1abadSJonathan Lemon 922773bda96SJonathan Lemon static int 923773bda96SJonathan Lemon ptp_ocp_firstchild(struct device *dev, void *data) 924773bda96SJonathan Lemon { 925773bda96SJonathan Lemon return 1; 926773bda96SJonathan Lemon } 927773bda96SJonathan Lemon 928773bda96SJonathan Lemon static int 929773bda96SJonathan Lemon ptp_ocp_read_i2c(struct i2c_adapter *adap, u8 addr, u8 reg, u8 sz, u8 *data) 930773bda96SJonathan Lemon { 931773bda96SJonathan Lemon struct i2c_msg msgs[2] = { 932773bda96SJonathan Lemon { 933773bda96SJonathan Lemon .addr = addr, 934773bda96SJonathan Lemon .len = 1, 935773bda96SJonathan Lemon .buf = ®, 936773bda96SJonathan Lemon }, 937773bda96SJonathan Lemon { 938773bda96SJonathan Lemon .addr = addr, 939773bda96SJonathan Lemon .flags = I2C_M_RD, 940773bda96SJonathan Lemon .len = 2, 941773bda96SJonathan Lemon .buf = data, 942773bda96SJonathan Lemon }, 943773bda96SJonathan Lemon }; 944773bda96SJonathan Lemon int err; 945773bda96SJonathan Lemon u8 len; 946773bda96SJonathan Lemon 947773bda96SJonathan Lemon /* xiic-i2c for some stupid reason only does 2 byte reads. */ 948773bda96SJonathan Lemon while (sz) { 949773bda96SJonathan Lemon len = min_t(u8, sz, 2); 950773bda96SJonathan Lemon msgs[1].len = len; 951773bda96SJonathan Lemon err = i2c_transfer(adap, msgs, 2); 952773bda96SJonathan Lemon if (err != msgs[1].len) 953773bda96SJonathan Lemon return err; 954773bda96SJonathan Lemon msgs[1].buf += len; 955773bda96SJonathan Lemon reg += len; 956773bda96SJonathan Lemon sz -= len; 957773bda96SJonathan Lemon } 958773bda96SJonathan Lemon return 0; 959773bda96SJonathan Lemon } 960773bda96SJonathan Lemon 961773bda96SJonathan Lemon static void 962773bda96SJonathan Lemon ptp_ocp_get_serial_number(struct ptp_ocp *bp) 963773bda96SJonathan Lemon { 964773bda96SJonathan Lemon struct i2c_adapter *adap; 965773bda96SJonathan Lemon struct device *dev; 966773bda96SJonathan Lemon int err; 967773bda96SJonathan Lemon 9681447149dSJonathan Lemon if (!bp->i2c_ctrl) 9691447149dSJonathan Lemon return; 9701447149dSJonathan Lemon 971773bda96SJonathan Lemon dev = device_find_child(&bp->i2c_ctrl->dev, NULL, ptp_ocp_firstchild); 972773bda96SJonathan Lemon if (!dev) { 973773bda96SJonathan Lemon dev_err(&bp->pdev->dev, "Can't find I2C adapter\n"); 974773bda96SJonathan Lemon return; 975773bda96SJonathan Lemon } 976773bda96SJonathan Lemon 977773bda96SJonathan Lemon adap = i2c_verify_adapter(dev); 978773bda96SJonathan Lemon if (!adap) { 979773bda96SJonathan Lemon dev_err(&bp->pdev->dev, "device '%s' isn't an I2C adapter\n", 980773bda96SJonathan Lemon dev_name(dev)); 981773bda96SJonathan Lemon goto out; 982773bda96SJonathan Lemon } 983773bda96SJonathan Lemon 984773bda96SJonathan Lemon err = ptp_ocp_read_i2c(adap, 0x58, 0x9A, 6, bp->serial); 985773bda96SJonathan Lemon if (err) { 986773bda96SJonathan Lemon dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", err); 987773bda96SJonathan Lemon goto out; 988773bda96SJonathan Lemon } 989773bda96SJonathan Lemon 990773bda96SJonathan Lemon bp->has_serial = true; 991773bda96SJonathan Lemon 992773bda96SJonathan Lemon out: 993773bda96SJonathan Lemon put_device(dev); 994773bda96SJonathan Lemon } 995773bda96SJonathan Lemon 996773bda96SJonathan Lemon static struct device * 997773bda96SJonathan Lemon ptp_ocp_find_flash(struct ptp_ocp *bp) 998773bda96SJonathan Lemon { 999773bda96SJonathan Lemon struct device *dev, *last; 1000773bda96SJonathan Lemon 1001773bda96SJonathan Lemon last = NULL; 1002773bda96SJonathan Lemon dev = &bp->spi_flash->dev; 1003773bda96SJonathan Lemon 1004773bda96SJonathan Lemon while ((dev = device_find_child(dev, NULL, ptp_ocp_firstchild))) { 1005773bda96SJonathan Lemon if (!strcmp("mtd", dev_bus_name(dev))) 1006773bda96SJonathan Lemon break; 1007773bda96SJonathan Lemon put_device(last); 1008773bda96SJonathan Lemon last = dev; 1009773bda96SJonathan Lemon } 1010773bda96SJonathan Lemon put_device(last); 1011773bda96SJonathan Lemon 1012773bda96SJonathan Lemon return dev; 1013773bda96SJonathan Lemon } 1014773bda96SJonathan Lemon 1015773bda96SJonathan Lemon static int 1016773bda96SJonathan Lemon ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev, 1017773bda96SJonathan Lemon const struct firmware *fw) 1018773bda96SJonathan Lemon { 1019773bda96SJonathan Lemon struct mtd_info *mtd = dev_get_drvdata(dev); 1020773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 1021773bda96SJonathan Lemon size_t off, len, resid, wrote; 1022773bda96SJonathan Lemon struct erase_info erase; 1023773bda96SJonathan Lemon size_t base, blksz; 10247c807572SJonathan Lemon int err = 0; 1025773bda96SJonathan Lemon 1026773bda96SJonathan Lemon off = 0; 1027773bda96SJonathan Lemon base = bp->flash_start; 1028773bda96SJonathan Lemon blksz = 4096; 1029773bda96SJonathan Lemon resid = fw->size; 1030773bda96SJonathan Lemon 1031773bda96SJonathan Lemon while (resid) { 1032773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, "Flashing", 1033773bda96SJonathan Lemon NULL, off, fw->size); 1034773bda96SJonathan Lemon 1035773bda96SJonathan Lemon len = min_t(size_t, resid, blksz); 1036773bda96SJonathan Lemon erase.addr = base + off; 1037773bda96SJonathan Lemon erase.len = blksz; 1038773bda96SJonathan Lemon 1039773bda96SJonathan Lemon err = mtd_erase(mtd, &erase); 1040773bda96SJonathan Lemon if (err) 1041773bda96SJonathan Lemon goto out; 1042773bda96SJonathan Lemon 1043773bda96SJonathan Lemon err = mtd_write(mtd, base + off, len, &wrote, &fw->data[off]); 1044773bda96SJonathan Lemon if (err) 1045773bda96SJonathan Lemon goto out; 1046773bda96SJonathan Lemon 1047773bda96SJonathan Lemon off += blksz; 1048773bda96SJonathan Lemon resid -= len; 1049773bda96SJonathan Lemon } 1050773bda96SJonathan Lemon out: 1051773bda96SJonathan Lemon return err; 1052773bda96SJonathan Lemon } 1053773bda96SJonathan Lemon 1054773bda96SJonathan Lemon static int 1055773bda96SJonathan Lemon ptp_ocp_devlink_flash_update(struct devlink *devlink, 1056773bda96SJonathan Lemon struct devlink_flash_update_params *params, 1057773bda96SJonathan Lemon struct netlink_ext_ack *extack) 1058773bda96SJonathan Lemon { 1059773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 1060773bda96SJonathan Lemon struct device *dev; 1061773bda96SJonathan Lemon const char *msg; 1062773bda96SJonathan Lemon int err; 1063773bda96SJonathan Lemon 1064773bda96SJonathan Lemon dev = ptp_ocp_find_flash(bp); 1065773bda96SJonathan Lemon if (!dev) { 1066773bda96SJonathan Lemon dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n"); 1067773bda96SJonathan Lemon return -ENODEV; 1068773bda96SJonathan Lemon } 1069773bda96SJonathan Lemon 1070773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, "Preparing to flash", 1071773bda96SJonathan Lemon NULL, 0, 0); 1072773bda96SJonathan Lemon 1073773bda96SJonathan Lemon err = ptp_ocp_devlink_flash(devlink, dev, params->fw); 1074773bda96SJonathan Lemon 1075773bda96SJonathan Lemon msg = err ? "Flash error" : "Flash complete"; 1076773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0); 1077773bda96SJonathan Lemon 1078773bda96SJonathan Lemon put_device(dev); 1079773bda96SJonathan Lemon return err; 1080773bda96SJonathan Lemon } 1081773bda96SJonathan Lemon 1082773bda96SJonathan Lemon static int 1083773bda96SJonathan Lemon ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req, 1084773bda96SJonathan Lemon struct netlink_ext_ack *extack) 1085773bda96SJonathan Lemon { 1086773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 1087773bda96SJonathan Lemon char buf[32]; 1088773bda96SJonathan Lemon int err; 1089773bda96SJonathan Lemon 1090773bda96SJonathan Lemon err = devlink_info_driver_name_put(req, KBUILD_MODNAME); 1091773bda96SJonathan Lemon if (err) 1092773bda96SJonathan Lemon return err; 1093773bda96SJonathan Lemon 1094773bda96SJonathan Lemon if (bp->image) { 1095773bda96SJonathan Lemon u32 ver = ioread32(&bp->image->version); 1096773bda96SJonathan Lemon 1097773bda96SJonathan Lemon if (ver & 0xffff) { 1098773bda96SJonathan Lemon sprintf(buf, "%d", ver); 1099773bda96SJonathan Lemon err = devlink_info_version_running_put(req, 11001a052da9SJonathan Lemon "fw", 1101773bda96SJonathan Lemon buf); 1102773bda96SJonathan Lemon } else { 1103773bda96SJonathan Lemon sprintf(buf, "%d", ver >> 16); 1104773bda96SJonathan Lemon err = devlink_info_version_running_put(req, 11051a052da9SJonathan Lemon "loader", 1106773bda96SJonathan Lemon buf); 1107773bda96SJonathan Lemon } 1108773bda96SJonathan Lemon if (err) 1109773bda96SJonathan Lemon return err; 1110773bda96SJonathan Lemon } 1111773bda96SJonathan Lemon 1112773bda96SJonathan Lemon if (!bp->has_serial) 1113773bda96SJonathan Lemon ptp_ocp_get_serial_number(bp); 1114773bda96SJonathan Lemon 1115773bda96SJonathan Lemon if (bp->has_serial) { 1116773bda96SJonathan Lemon sprintf(buf, "%pM", bp->serial); 1117773bda96SJonathan Lemon err = devlink_info_serial_number_put(req, buf); 1118773bda96SJonathan Lemon if (err) 1119773bda96SJonathan Lemon return err; 1120773bda96SJonathan Lemon } 1121773bda96SJonathan Lemon 1122773bda96SJonathan Lemon return 0; 1123773bda96SJonathan Lemon } 1124773bda96SJonathan Lemon 1125773bda96SJonathan Lemon static const struct devlink_ops ptp_ocp_devlink_ops = { 1126773bda96SJonathan Lemon .flash_update = ptp_ocp_devlink_flash_update, 1127773bda96SJonathan Lemon .info_get = ptp_ocp_devlink_info_get, 1128773bda96SJonathan Lemon }; 1129773bda96SJonathan Lemon 1130773bda96SJonathan Lemon static void __iomem * 1131773bda96SJonathan Lemon __ptp_ocp_get_mem(struct ptp_ocp *bp, unsigned long start, int size) 1132773bda96SJonathan Lemon { 1133773bda96SJonathan Lemon struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp"); 1134773bda96SJonathan Lemon 1135773bda96SJonathan Lemon return devm_ioremap_resource(&bp->pdev->dev, &res); 1136773bda96SJonathan Lemon } 1137773bda96SJonathan Lemon 1138773bda96SJonathan Lemon static void __iomem * 1139773bda96SJonathan Lemon ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r) 1140773bda96SJonathan Lemon { 1141773bda96SJonathan Lemon unsigned long start; 1142773bda96SJonathan Lemon 1143773bda96SJonathan Lemon start = pci_resource_start(bp->pdev, 0) + r->offset; 1144773bda96SJonathan Lemon return __ptp_ocp_get_mem(bp, start, r->size); 1145773bda96SJonathan Lemon } 1146773bda96SJonathan Lemon 1147773bda96SJonathan Lemon static void 1148773bda96SJonathan Lemon ptp_ocp_set_irq_resource(struct resource *res, int irq) 1149773bda96SJonathan Lemon { 1150773bda96SJonathan Lemon struct resource r = DEFINE_RES_IRQ(irq); 1151773bda96SJonathan Lemon *res = r; 1152773bda96SJonathan Lemon } 1153773bda96SJonathan Lemon 1154773bda96SJonathan Lemon static void 1155773bda96SJonathan Lemon ptp_ocp_set_mem_resource(struct resource *res, unsigned long start, int size) 1156773bda96SJonathan Lemon { 1157773bda96SJonathan Lemon struct resource r = DEFINE_RES_MEM(start, size); 1158773bda96SJonathan Lemon *res = r; 1159773bda96SJonathan Lemon } 1160773bda96SJonathan Lemon 1161773bda96SJonathan Lemon static int 1162773bda96SJonathan Lemon ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r) 1163773bda96SJonathan Lemon { 1164773bda96SJonathan Lemon struct ptp_ocp_flash_info *info; 1165773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1166773bda96SJonathan Lemon struct platform_device *p; 1167773bda96SJonathan Lemon struct resource res[2]; 1168773bda96SJonathan Lemon unsigned long start; 1169773bda96SJonathan Lemon int id; 1170773bda96SJonathan Lemon 1171773bda96SJonathan Lemon start = pci_resource_start(pdev, 0) + r->offset; 1172773bda96SJonathan Lemon ptp_ocp_set_mem_resource(&res[0], start, r->size); 1173773bda96SJonathan Lemon ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1174773bda96SJonathan Lemon 1175773bda96SJonathan Lemon info = r->extra; 1176773bda96SJonathan Lemon id = pci_dev_id(pdev) << 1; 1177773bda96SJonathan Lemon id += info->pci_offset; 1178773bda96SJonathan Lemon 1179773bda96SJonathan Lemon p = platform_device_register_resndata(&pdev->dev, info->name, id, 1180773bda96SJonathan Lemon res, 2, info->data, 1181773bda96SJonathan Lemon info->data_size); 1182773bda96SJonathan Lemon if (IS_ERR(p)) 1183773bda96SJonathan Lemon return PTR_ERR(p); 1184773bda96SJonathan Lemon 1185773bda96SJonathan Lemon bp_assign_entry(bp, r, p); 1186773bda96SJonathan Lemon 1187773bda96SJonathan Lemon return 0; 1188773bda96SJonathan Lemon } 1189773bda96SJonathan Lemon 1190773bda96SJonathan Lemon static struct platform_device * 1191773bda96SJonathan Lemon ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id) 1192773bda96SJonathan Lemon { 11931618df6aSJonathan Lemon struct ptp_ocp_i2c_info *info; 1194773bda96SJonathan Lemon struct resource res[2]; 1195773bda96SJonathan Lemon unsigned long start; 1196773bda96SJonathan Lemon 11971618df6aSJonathan Lemon info = r->extra; 1198773bda96SJonathan Lemon start = pci_resource_start(pdev, 0) + r->offset; 1199773bda96SJonathan Lemon ptp_ocp_set_mem_resource(&res[0], start, r->size); 1200773bda96SJonathan Lemon ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1201773bda96SJonathan Lemon 12021618df6aSJonathan Lemon return platform_device_register_resndata(&pdev->dev, info->name, 12031618df6aSJonathan Lemon id, res, 2, 12041618df6aSJonathan Lemon info->data, info->data_size); 1205773bda96SJonathan Lemon } 1206773bda96SJonathan Lemon 1207773bda96SJonathan Lemon static int 1208773bda96SJonathan Lemon ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r) 1209773bda96SJonathan Lemon { 1210773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 12111618df6aSJonathan Lemon struct ptp_ocp_i2c_info *info; 1212773bda96SJonathan Lemon struct platform_device *p; 1213773bda96SJonathan Lemon struct clk_hw *clk; 1214773bda96SJonathan Lemon char buf[32]; 1215773bda96SJonathan Lemon int id; 1216773bda96SJonathan Lemon 12171618df6aSJonathan Lemon info = r->extra; 1218773bda96SJonathan Lemon id = pci_dev_id(bp->pdev); 1219773bda96SJonathan Lemon 1220773bda96SJonathan Lemon sprintf(buf, "AXI.%d", id); 12211618df6aSJonathan Lemon clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0, 12221618df6aSJonathan Lemon info->fixed_rate); 1223773bda96SJonathan Lemon if (IS_ERR(clk)) 1224773bda96SJonathan Lemon return PTR_ERR(clk); 1225773bda96SJonathan Lemon bp->i2c_clk = clk; 1226773bda96SJonathan Lemon 12271618df6aSJonathan Lemon sprintf(buf, "%s.%d", info->name, id); 1228773bda96SJonathan Lemon devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf); 1229773bda96SJonathan Lemon p = ptp_ocp_i2c_bus(bp->pdev, r, id); 1230773bda96SJonathan Lemon if (IS_ERR(p)) 1231773bda96SJonathan Lemon return PTR_ERR(p); 1232773bda96SJonathan Lemon 1233773bda96SJonathan Lemon bp_assign_entry(bp, r, p); 1234773bda96SJonathan Lemon 1235773bda96SJonathan Lemon return 0; 1236773bda96SJonathan Lemon } 1237773bda96SJonathan Lemon 1238773bda96SJonathan Lemon static irqreturn_t 1239773bda96SJonathan Lemon ptp_ocp_ts_irq(int irq, void *priv) 1240773bda96SJonathan Lemon { 1241773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1242773bda96SJonathan Lemon struct ts_reg __iomem *reg = ext->mem; 1243773bda96SJonathan Lemon struct ptp_clock_event ev; 1244773bda96SJonathan Lemon u32 sec, nsec; 1245773bda96SJonathan Lemon 1246a62a56d0SJonathan Lemon if (ext == ext->bp->pps) { 1247a62a56d0SJonathan Lemon if (ext->bp->pps_req_map & OCP_REQ_PPS) { 1248a62a56d0SJonathan Lemon ev.type = PTP_CLOCK_PPS; 1249a62a56d0SJonathan Lemon ptp_clock_event(ext->bp->ptp, &ev); 1250a62a56d0SJonathan Lemon } 1251a62a56d0SJonathan Lemon 1252a62a56d0SJonathan Lemon if ((ext->bp->pps_req_map & ~OCP_REQ_PPS) == 0) 1253a62a56d0SJonathan Lemon goto out; 1254a62a56d0SJonathan Lemon } 1255a62a56d0SJonathan Lemon 1256773bda96SJonathan Lemon /* XXX should fix API - this converts s/ns -> ts -> s/ns */ 1257773bda96SJonathan Lemon sec = ioread32(®->time_sec); 1258773bda96SJonathan Lemon nsec = ioread32(®->time_ns); 1259773bda96SJonathan Lemon 1260773bda96SJonathan Lemon ev.type = PTP_CLOCK_EXTTS; 1261773bda96SJonathan Lemon ev.index = ext->info->index; 12621acffc6eSJonathan Lemon ev.timestamp = sec * NSEC_PER_SEC + nsec; 1263773bda96SJonathan Lemon 1264773bda96SJonathan Lemon ptp_clock_event(ext->bp->ptp, &ev); 1265773bda96SJonathan Lemon 1266a62a56d0SJonathan Lemon out: 1267773bda96SJonathan Lemon iowrite32(1, ®->intr); /* write 1 to ack */ 1268773bda96SJonathan Lemon 1269773bda96SJonathan Lemon return IRQ_HANDLED; 1270773bda96SJonathan Lemon } 1271773bda96SJonathan Lemon 1272773bda96SJonathan Lemon static int 1273a62a56d0SJonathan Lemon ptp_ocp_ts_enable(void *priv, u32 req, bool enable) 1274773bda96SJonathan Lemon { 1275773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1276773bda96SJonathan Lemon struct ts_reg __iomem *reg = ext->mem; 1277a62a56d0SJonathan Lemon struct ptp_ocp *bp = ext->bp; 1278a62a56d0SJonathan Lemon 1279a62a56d0SJonathan Lemon if (ext == bp->pps) { 1280a62a56d0SJonathan Lemon u32 old_map = bp->pps_req_map; 1281a62a56d0SJonathan Lemon 1282a62a56d0SJonathan Lemon if (enable) 1283a62a56d0SJonathan Lemon bp->pps_req_map |= req; 1284a62a56d0SJonathan Lemon else 1285a62a56d0SJonathan Lemon bp->pps_req_map &= ~req; 1286a62a56d0SJonathan Lemon 1287a62a56d0SJonathan Lemon /* if no state change, just return */ 1288a62a56d0SJonathan Lemon if ((!!old_map ^ !!bp->pps_req_map) == 0) 1289a62a56d0SJonathan Lemon return 0; 1290a62a56d0SJonathan Lemon } 1291773bda96SJonathan Lemon 1292773bda96SJonathan Lemon if (enable) { 1293773bda96SJonathan Lemon iowrite32(1, ®->enable); 1294773bda96SJonathan Lemon iowrite32(1, ®->intr_mask); 1295773bda96SJonathan Lemon iowrite32(1, ®->intr); 1296773bda96SJonathan Lemon } else { 1297773bda96SJonathan Lemon iowrite32(0, ®->intr_mask); 1298773bda96SJonathan Lemon iowrite32(0, ®->enable); 1299773bda96SJonathan Lemon } 1300773bda96SJonathan Lemon 1301773bda96SJonathan Lemon return 0; 1302773bda96SJonathan Lemon } 1303773bda96SJonathan Lemon 1304773bda96SJonathan Lemon static void 1305773bda96SJonathan Lemon ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext) 1306773bda96SJonathan Lemon { 1307a62a56d0SJonathan Lemon ext->info->enable(ext, ~0, false); 1308773bda96SJonathan Lemon pci_free_irq(ext->bp->pdev, ext->irq_vec, ext); 1309773bda96SJonathan Lemon kfree(ext); 1310773bda96SJonathan Lemon } 1311773bda96SJonathan Lemon 1312773bda96SJonathan Lemon static int 1313773bda96SJonathan Lemon ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r) 1314773bda96SJonathan Lemon { 1315773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1316773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext; 1317773bda96SJonathan Lemon int err; 1318773bda96SJonathan Lemon 1319773bda96SJonathan Lemon ext = kzalloc(sizeof(*ext), GFP_KERNEL); 1320773bda96SJonathan Lemon if (!ext) 1321773bda96SJonathan Lemon return -ENOMEM; 1322773bda96SJonathan Lemon 1323773bda96SJonathan Lemon ext->mem = ptp_ocp_get_mem(bp, r); 1324c7521d3aSDan Carpenter if (IS_ERR(ext->mem)) { 1325c7521d3aSDan Carpenter err = PTR_ERR(ext->mem); 1326773bda96SJonathan Lemon goto out; 1327c7521d3aSDan Carpenter } 1328773bda96SJonathan Lemon 1329773bda96SJonathan Lemon ext->bp = bp; 1330773bda96SJonathan Lemon ext->info = r->extra; 1331773bda96SJonathan Lemon ext->irq_vec = r->irq_vec; 1332773bda96SJonathan Lemon 1333773bda96SJonathan Lemon err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL, 133456ec4403SJonathan Lemon ext, "ocp%d.%s", bp->id, r->name); 1335773bda96SJonathan Lemon if (err) { 1336773bda96SJonathan Lemon dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec); 1337773bda96SJonathan Lemon goto out; 1338773bda96SJonathan Lemon } 1339773bda96SJonathan Lemon 1340773bda96SJonathan Lemon bp_assign_entry(bp, r, ext); 1341773bda96SJonathan Lemon 1342773bda96SJonathan Lemon return 0; 1343773bda96SJonathan Lemon 1344773bda96SJonathan Lemon out: 1345773bda96SJonathan Lemon kfree(ext); 1346773bda96SJonathan Lemon return err; 1347773bda96SJonathan Lemon } 1348773bda96SJonathan Lemon 1349773bda96SJonathan Lemon static int 1350773bda96SJonathan Lemon ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r) 1351773bda96SJonathan Lemon { 1352773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1353773bda96SJonathan Lemon struct uart_8250_port uart; 1354773bda96SJonathan Lemon 1355773bda96SJonathan Lemon /* Setting UPF_IOREMAP and leaving port.membase unspecified lets 1356773bda96SJonathan Lemon * the serial port device claim and release the pci resource. 1357773bda96SJonathan Lemon */ 1358773bda96SJonathan Lemon memset(&uart, 0, sizeof(uart)); 1359773bda96SJonathan Lemon uart.port.dev = &pdev->dev; 1360773bda96SJonathan Lemon uart.port.iotype = UPIO_MEM; 1361773bda96SJonathan Lemon uart.port.regshift = 2; 1362773bda96SJonathan Lemon uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset; 1363773bda96SJonathan Lemon uart.port.irq = pci_irq_vector(pdev, r->irq_vec); 1364773bda96SJonathan Lemon uart.port.uartclk = 50000000; 1365773bda96SJonathan Lemon uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP; 1366773bda96SJonathan Lemon uart.port.type = PORT_16550A; 1367773bda96SJonathan Lemon 1368773bda96SJonathan Lemon return serial8250_register_8250_port(&uart); 1369773bda96SJonathan Lemon } 1370773bda96SJonathan Lemon 1371773bda96SJonathan Lemon static int 1372773bda96SJonathan Lemon ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r) 1373773bda96SJonathan Lemon { 1374773bda96SJonathan Lemon int port; 1375773bda96SJonathan Lemon 1376773bda96SJonathan Lemon port = ptp_ocp_serial_line(bp, r); 1377773bda96SJonathan Lemon if (port < 0) 1378773bda96SJonathan Lemon return port; 1379773bda96SJonathan Lemon 1380773bda96SJonathan Lemon bp_assign_entry(bp, r, port); 1381773bda96SJonathan Lemon 1382773bda96SJonathan Lemon return 0; 1383773bda96SJonathan Lemon } 1384773bda96SJonathan Lemon 1385773bda96SJonathan Lemon static int 1386773bda96SJonathan Lemon ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r) 1387773bda96SJonathan Lemon { 1388773bda96SJonathan Lemon void __iomem *mem; 1389773bda96SJonathan Lemon 1390773bda96SJonathan Lemon mem = ptp_ocp_get_mem(bp, r); 1391c7521d3aSDan Carpenter if (IS_ERR(mem)) 1392c7521d3aSDan Carpenter return PTR_ERR(mem); 1393773bda96SJonathan Lemon 1394773bda96SJonathan Lemon bp_assign_entry(bp, r, mem); 1395773bda96SJonathan Lemon 1396773bda96SJonathan Lemon return 0; 1397773bda96SJonathan Lemon } 1398773bda96SJonathan Lemon 1399e3516bb4SJonathan Lemon static void 1400e3516bb4SJonathan Lemon ptp_ocp_nmea_out_init(struct ptp_ocp *bp) 1401e3516bb4SJonathan Lemon { 1402e3516bb4SJonathan Lemon if (!bp->nmea_out) 1403e3516bb4SJonathan Lemon return; 1404e3516bb4SJonathan Lemon 1405e3516bb4SJonathan Lemon iowrite32(0, &bp->nmea_out->ctrl); /* disable */ 1406e3516bb4SJonathan Lemon iowrite32(7, &bp->nmea_out->uart_baud); /* 115200 */ 1407e3516bb4SJonathan Lemon iowrite32(1, &bp->nmea_out->ctrl); /* enable */ 1408e3516bb4SJonathan Lemon } 1409e3516bb4SJonathan Lemon 1410773bda96SJonathan Lemon /* FB specific board initializers; last "resource" registered. */ 1411773bda96SJonathan Lemon static int 1412773bda96SJonathan Lemon ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r) 1413773bda96SJonathan Lemon { 1414773bda96SJonathan Lemon bp->flash_start = 1024 * 4096; 1415773bda96SJonathan Lemon 1416065efcc5SJonathan Lemon ptp_ocp_tod_init(bp); 1417e3516bb4SJonathan Lemon ptp_ocp_nmea_out_init(bp); 1418065efcc5SJonathan Lemon 1419773bda96SJonathan Lemon return ptp_ocp_init_clock(bp); 1420773bda96SJonathan Lemon } 1421773bda96SJonathan Lemon 142256ec4403SJonathan Lemon static bool 142356ec4403SJonathan Lemon ptp_ocp_allow_irq(struct ptp_ocp *bp, struct ocp_resource *r) 142456ec4403SJonathan Lemon { 142556ec4403SJonathan Lemon bool allow = !r->irq_vec || r->irq_vec < bp->n_irqs; 142656ec4403SJonathan Lemon 142756ec4403SJonathan Lemon if (!allow) 142856ec4403SJonathan Lemon dev_err(&bp->pdev->dev, "irq %d out of range, skipping %s\n", 142956ec4403SJonathan Lemon r->irq_vec, r->name); 143056ec4403SJonathan Lemon return allow; 143156ec4403SJonathan Lemon } 143256ec4403SJonathan Lemon 1433773bda96SJonathan Lemon static int 1434773bda96SJonathan Lemon ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data) 1435773bda96SJonathan Lemon { 1436773bda96SJonathan Lemon struct ocp_resource *r, *table; 1437773bda96SJonathan Lemon int err = 0; 1438773bda96SJonathan Lemon 1439773bda96SJonathan Lemon table = (struct ocp_resource *)driver_data; 1440773bda96SJonathan Lemon for (r = table; r->setup; r++) { 144156ec4403SJonathan Lemon if (!ptp_ocp_allow_irq(bp, r)) 144256ec4403SJonathan Lemon continue; 1443773bda96SJonathan Lemon err = r->setup(bp, r); 1444bceff290SJonathan Lemon if (err) { 1445bceff290SJonathan Lemon dev_err(&bp->pdev->dev, 1446bceff290SJonathan Lemon "Could not register %s: err %d\n", 1447bceff290SJonathan Lemon r->name, err); 1448773bda96SJonathan Lemon break; 1449773bda96SJonathan Lemon } 1450bceff290SJonathan Lemon } 1451773bda96SJonathan Lemon return err; 1452773bda96SJonathan Lemon } 1453773bda96SJonathan Lemon 14546baf2925SJonathan Lemon static void 14556baf2925SJonathan Lemon ptp_ocp_enable_fpga(u32 __iomem *reg, u32 bit, bool enable) 14566baf2925SJonathan Lemon { 14576baf2925SJonathan Lemon u32 ctrl; 14586baf2925SJonathan Lemon bool on; 14596baf2925SJonathan Lemon 14606baf2925SJonathan Lemon ctrl = ioread32(reg); 14616baf2925SJonathan Lemon on = ctrl & bit; 14626baf2925SJonathan Lemon if (on ^ enable) { 14636baf2925SJonathan Lemon ctrl &= ~bit; 14646baf2925SJonathan Lemon ctrl |= enable ? bit : 0; 14656baf2925SJonathan Lemon iowrite32(ctrl, reg); 14666baf2925SJonathan Lemon } 14676baf2925SJonathan Lemon } 14686baf2925SJonathan Lemon 14696baf2925SJonathan Lemon static void 14706baf2925SJonathan Lemon ptp_ocp_irig_out(struct ptp_ocp *bp, bool enable) 14716baf2925SJonathan Lemon { 14726baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->irig_out->ctrl, 14736baf2925SJonathan Lemon IRIG_M_CTRL_ENABLE, enable); 14746baf2925SJonathan Lemon } 14756baf2925SJonathan Lemon 14766baf2925SJonathan Lemon static void 14776baf2925SJonathan Lemon ptp_ocp_irig_in(struct ptp_ocp *bp, bool enable) 14786baf2925SJonathan Lemon { 14796baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->irig_in->ctrl, 14806baf2925SJonathan Lemon IRIG_S_CTRL_ENABLE, enable); 14816baf2925SJonathan Lemon } 14826baf2925SJonathan Lemon 14836baf2925SJonathan Lemon static void 14846baf2925SJonathan Lemon ptp_ocp_dcf_out(struct ptp_ocp *bp, bool enable) 14856baf2925SJonathan Lemon { 14866baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->dcf_out->ctrl, 14876baf2925SJonathan Lemon DCF_M_CTRL_ENABLE, enable); 14886baf2925SJonathan Lemon } 14896baf2925SJonathan Lemon 14906baf2925SJonathan Lemon static void 14916baf2925SJonathan Lemon ptp_ocp_dcf_in(struct ptp_ocp *bp, bool enable) 14926baf2925SJonathan Lemon { 14936baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->dcf_in->ctrl, 14946baf2925SJonathan Lemon DCF_S_CTRL_ENABLE, enable); 14956baf2925SJonathan Lemon } 14966baf2925SJonathan Lemon 14976baf2925SJonathan Lemon static void 14986baf2925SJonathan Lemon __handle_signal_outputs(struct ptp_ocp *bp, u32 val) 14996baf2925SJonathan Lemon { 15006baf2925SJonathan Lemon ptp_ocp_irig_out(bp, val & 0x00100010); 15016baf2925SJonathan Lemon ptp_ocp_dcf_out(bp, val & 0x00200020); 15026baf2925SJonathan Lemon } 15036baf2925SJonathan Lemon 15046baf2925SJonathan Lemon static void 15056baf2925SJonathan Lemon __handle_signal_inputs(struct ptp_ocp *bp, u32 val) 15066baf2925SJonathan Lemon { 15076baf2925SJonathan Lemon ptp_ocp_irig_in(bp, val & 0x00100010); 15086baf2925SJonathan Lemon ptp_ocp_dcf_in(bp, val & 0x00200020); 15096baf2925SJonathan Lemon } 15106baf2925SJonathan Lemon 1511e1daf0ecSJonathan Lemon /* 1512e1daf0ecSJonathan Lemon * ANT0 == gps (in) 1513e1daf0ecSJonathan Lemon * ANT1 == sma1 (in) 1514e1daf0ecSJonathan Lemon * ANT2 == sma2 (in) 1515e1daf0ecSJonathan Lemon * ANT3 == sma3 (out) 1516e1daf0ecSJonathan Lemon * ANT4 == sma4 (out) 1517e1daf0ecSJonathan Lemon */ 1518e1daf0ecSJonathan Lemon 1519e1daf0ecSJonathan Lemon enum ptp_ocp_sma_mode { 1520e1daf0ecSJonathan Lemon SMA_MODE_IN, 1521e1daf0ecSJonathan Lemon SMA_MODE_OUT, 1522e1daf0ecSJonathan Lemon }; 1523e1daf0ecSJonathan Lemon 1524e1daf0ecSJonathan Lemon static struct ptp_ocp_sma_connector { 1525e1daf0ecSJonathan Lemon enum ptp_ocp_sma_mode mode; 1526e1daf0ecSJonathan Lemon bool fixed_mode; 1527e1daf0ecSJonathan Lemon u16 default_out_idx; 1528e1daf0ecSJonathan Lemon } ptp_ocp_sma_map[4] = { 1529e1daf0ecSJonathan Lemon { 1530e1daf0ecSJonathan Lemon .mode = SMA_MODE_IN, 1531e1daf0ecSJonathan Lemon .fixed_mode = true, 1532e1daf0ecSJonathan Lemon }, 1533e1daf0ecSJonathan Lemon { 1534e1daf0ecSJonathan Lemon .mode = SMA_MODE_IN, 1535e1daf0ecSJonathan Lemon .fixed_mode = true, 1536e1daf0ecSJonathan Lemon }, 1537e1daf0ecSJonathan Lemon { 1538e1daf0ecSJonathan Lemon .mode = SMA_MODE_OUT, 1539e1daf0ecSJonathan Lemon .fixed_mode = true, 1540e1daf0ecSJonathan Lemon .default_out_idx = 0, /* 10Mhz */ 1541e1daf0ecSJonathan Lemon }, 1542e1daf0ecSJonathan Lemon { 1543e1daf0ecSJonathan Lemon .mode = SMA_MODE_OUT, 1544e1daf0ecSJonathan Lemon .fixed_mode = true, 1545e1daf0ecSJonathan Lemon .default_out_idx = 1, /* PHC */ 1546e1daf0ecSJonathan Lemon }, 1547e1daf0ecSJonathan Lemon }; 1548e1daf0ecSJonathan Lemon 1549e1daf0ecSJonathan Lemon static ssize_t 1550e1daf0ecSJonathan Lemon ptp_ocp_show_output(u32 val, char *buf, int default_idx) 1551e1daf0ecSJonathan Lemon { 1552e1daf0ecSJonathan Lemon const char *name; 1553e1daf0ecSJonathan Lemon ssize_t count; 1554e1daf0ecSJonathan Lemon 1555e1daf0ecSJonathan Lemon count = sysfs_emit(buf, "OUT: "); 1556e1daf0ecSJonathan Lemon name = ptp_ocp_select_name_from_val(ptp_ocp_sma_out, val); 1557e1daf0ecSJonathan Lemon if (!name) 1558e1daf0ecSJonathan Lemon name = ptp_ocp_sma_out[default_idx].name; 1559e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s\n", name); 1560e1daf0ecSJonathan Lemon return count; 1561e1daf0ecSJonathan Lemon } 1562e1daf0ecSJonathan Lemon 1563e1daf0ecSJonathan Lemon static ssize_t 1564e1daf0ecSJonathan Lemon ptp_ocp_show_inputs(u32 val, char *buf, const char *zero_in) 1565e1daf0ecSJonathan Lemon { 1566e1daf0ecSJonathan Lemon const char *name; 1567e1daf0ecSJonathan Lemon ssize_t count; 1568e1daf0ecSJonathan Lemon int i; 1569e1daf0ecSJonathan Lemon 1570e1daf0ecSJonathan Lemon count = sysfs_emit(buf, "IN: "); 1571e1daf0ecSJonathan Lemon for (i = 0; i < ARRAY_SIZE(ptp_ocp_sma_in); i++) { 1572e1daf0ecSJonathan Lemon if (val & ptp_ocp_sma_in[i].value) { 1573e1daf0ecSJonathan Lemon name = ptp_ocp_sma_in[i].name; 1574e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", name); 1575e1daf0ecSJonathan Lemon } 1576e1daf0ecSJonathan Lemon } 1577e1daf0ecSJonathan Lemon if (!val && zero_in) 1578e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", zero_in); 1579e1daf0ecSJonathan Lemon if (count) 1580e1daf0ecSJonathan Lemon count--; 1581e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "\n"); 1582e1daf0ecSJonathan Lemon return count; 1583e1daf0ecSJonathan Lemon } 1584e1daf0ecSJonathan Lemon 1585e1daf0ecSJonathan Lemon static int 1586e1daf0ecSJonathan Lemon sma_parse_inputs(const char *buf, enum ptp_ocp_sma_mode *mode) 1587e1daf0ecSJonathan Lemon { 1588e1daf0ecSJonathan Lemon struct ocp_selector *tbl[] = { ptp_ocp_sma_in, ptp_ocp_sma_out }; 1589e1daf0ecSJonathan Lemon int idx, count, dir; 1590e1daf0ecSJonathan Lemon char **argv; 1591e1daf0ecSJonathan Lemon int ret; 1592e1daf0ecSJonathan Lemon 1593e1daf0ecSJonathan Lemon argv = argv_split(GFP_KERNEL, buf, &count); 1594e1daf0ecSJonathan Lemon if (!argv) 1595e1daf0ecSJonathan Lemon return -ENOMEM; 1596e1daf0ecSJonathan Lemon 1597e1daf0ecSJonathan Lemon ret = -EINVAL; 1598e1daf0ecSJonathan Lemon if (!count) 1599e1daf0ecSJonathan Lemon goto out; 1600e1daf0ecSJonathan Lemon 1601e1daf0ecSJonathan Lemon idx = 0; 1602e1daf0ecSJonathan Lemon dir = *mode == SMA_MODE_IN ? 0 : 1; 1603e1daf0ecSJonathan Lemon if (!strcasecmp("IN:", argv[idx])) { 1604e1daf0ecSJonathan Lemon dir = 0; 1605e1daf0ecSJonathan Lemon idx++; 1606e1daf0ecSJonathan Lemon } 1607e1daf0ecSJonathan Lemon if (!strcasecmp("OUT:", argv[0])) { 1608e1daf0ecSJonathan Lemon dir = 1; 1609e1daf0ecSJonathan Lemon idx++; 1610e1daf0ecSJonathan Lemon } 1611e1daf0ecSJonathan Lemon *mode = dir == 0 ? SMA_MODE_IN : SMA_MODE_OUT; 1612e1daf0ecSJonathan Lemon 1613e1daf0ecSJonathan Lemon ret = 0; 1614e1daf0ecSJonathan Lemon for (; idx < count; idx++) 1615e1daf0ecSJonathan Lemon ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]); 1616e1daf0ecSJonathan Lemon if (ret < 0) 1617e1daf0ecSJonathan Lemon ret = -EINVAL; 1618e1daf0ecSJonathan Lemon 1619e1daf0ecSJonathan Lemon out: 1620e1daf0ecSJonathan Lemon argv_free(argv); 1621e1daf0ecSJonathan Lemon return ret; 1622e1daf0ecSJonathan Lemon } 1623e1daf0ecSJonathan Lemon 1624e1daf0ecSJonathan Lemon static ssize_t 1625e1daf0ecSJonathan Lemon ptp_ocp_sma_show(struct ptp_ocp *bp, int sma_nr, u32 val, char *buf, 1626e1daf0ecSJonathan Lemon const char *zero_in) 1627e1daf0ecSJonathan Lemon { 1628e1daf0ecSJonathan Lemon struct ptp_ocp_sma_connector *sma = &ptp_ocp_sma_map[sma_nr - 1]; 1629e1daf0ecSJonathan Lemon 1630e1daf0ecSJonathan Lemon if (sma->mode == SMA_MODE_IN) 1631e1daf0ecSJonathan Lemon return ptp_ocp_show_inputs(val, buf, zero_in); 1632e1daf0ecSJonathan Lemon 1633e1daf0ecSJonathan Lemon return ptp_ocp_show_output(val, buf, sma->default_out_idx); 1634e1daf0ecSJonathan Lemon } 1635e1daf0ecSJonathan Lemon 1636e1daf0ecSJonathan Lemon static ssize_t 1637e1daf0ecSJonathan Lemon sma1_show(struct device *dev, struct device_attribute *attr, char *buf) 1638e1daf0ecSJonathan Lemon { 1639e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1640e1daf0ecSJonathan Lemon u32 val; 1641e1daf0ecSJonathan Lemon 1642e1daf0ecSJonathan Lemon val = ioread32(&bp->sma->gpio1) & 0x3f; 1643e1daf0ecSJonathan Lemon return ptp_ocp_sma_show(bp, 1, val, buf, ptp_ocp_sma_in[0].name); 1644e1daf0ecSJonathan Lemon } 1645e1daf0ecSJonathan Lemon 1646e1daf0ecSJonathan Lemon static ssize_t 1647e1daf0ecSJonathan Lemon sma2_show(struct device *dev, struct device_attribute *attr, char *buf) 1648e1daf0ecSJonathan Lemon { 1649e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1650e1daf0ecSJonathan Lemon u32 val; 1651e1daf0ecSJonathan Lemon 1652e1daf0ecSJonathan Lemon val = (ioread32(&bp->sma->gpio1) >> 16) & 0x3f; 1653e1daf0ecSJonathan Lemon return ptp_ocp_sma_show(bp, 2, val, buf, NULL); 1654e1daf0ecSJonathan Lemon } 1655e1daf0ecSJonathan Lemon 1656e1daf0ecSJonathan Lemon static ssize_t 1657e1daf0ecSJonathan Lemon sma3_show(struct device *dev, struct device_attribute *attr, char *buf) 1658e1daf0ecSJonathan Lemon { 1659e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1660e1daf0ecSJonathan Lemon u32 val; 1661e1daf0ecSJonathan Lemon 1662e1daf0ecSJonathan Lemon val = ioread32(&bp->sma->gpio2) & 0x3f; 1663e1daf0ecSJonathan Lemon return ptp_ocp_sma_show(bp, 3, val, buf, NULL); 1664e1daf0ecSJonathan Lemon } 1665e1daf0ecSJonathan Lemon 1666e1daf0ecSJonathan Lemon static ssize_t 1667e1daf0ecSJonathan Lemon sma4_show(struct device *dev, struct device_attribute *attr, char *buf) 1668e1daf0ecSJonathan Lemon { 1669e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1670e1daf0ecSJonathan Lemon u32 val; 1671e1daf0ecSJonathan Lemon 1672e1daf0ecSJonathan Lemon val = (ioread32(&bp->sma->gpio2) >> 16) & 0x3f; 1673e1daf0ecSJonathan Lemon return ptp_ocp_sma_show(bp, 4, val, buf, NULL); 1674e1daf0ecSJonathan Lemon } 1675e1daf0ecSJonathan Lemon 1676e1daf0ecSJonathan Lemon static void 1677e1daf0ecSJonathan Lemon ptp_ocp_sma_store_output(struct ptp_ocp *bp, u32 val, u32 shift) 1678e1daf0ecSJonathan Lemon { 1679e1daf0ecSJonathan Lemon unsigned long flags; 1680e1daf0ecSJonathan Lemon u32 gpio, mask; 1681e1daf0ecSJonathan Lemon 1682e1daf0ecSJonathan Lemon mask = 0xffff << (16 - shift); 1683e1daf0ecSJonathan Lemon 1684e1daf0ecSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1685e1daf0ecSJonathan Lemon 1686e1daf0ecSJonathan Lemon gpio = ioread32(&bp->sma->gpio2); 1687e1daf0ecSJonathan Lemon gpio = (gpio & mask) | (val << shift); 16886baf2925SJonathan Lemon 16896baf2925SJonathan Lemon __handle_signal_outputs(bp, gpio); 16906baf2925SJonathan Lemon 1691e1daf0ecSJonathan Lemon iowrite32(gpio, &bp->sma->gpio2); 1692e1daf0ecSJonathan Lemon 1693e1daf0ecSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1694e1daf0ecSJonathan Lemon } 1695e1daf0ecSJonathan Lemon 1696e1daf0ecSJonathan Lemon static void 1697e1daf0ecSJonathan Lemon ptp_ocp_sma_store_inputs(struct ptp_ocp *bp, u32 val, u32 shift) 1698e1daf0ecSJonathan Lemon { 1699e1daf0ecSJonathan Lemon unsigned long flags; 1700e1daf0ecSJonathan Lemon u32 gpio, mask; 1701e1daf0ecSJonathan Lemon 1702e1daf0ecSJonathan Lemon mask = 0xffff << (16 - shift); 1703e1daf0ecSJonathan Lemon 1704e1daf0ecSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1705e1daf0ecSJonathan Lemon 1706e1daf0ecSJonathan Lemon gpio = ioread32(&bp->sma->gpio1); 1707e1daf0ecSJonathan Lemon gpio = (gpio & mask) | (val << shift); 17086baf2925SJonathan Lemon 17096baf2925SJonathan Lemon __handle_signal_inputs(bp, gpio); 17106baf2925SJonathan Lemon 1711e1daf0ecSJonathan Lemon iowrite32(gpio, &bp->sma->gpio1); 1712e1daf0ecSJonathan Lemon 1713e1daf0ecSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1714e1daf0ecSJonathan Lemon } 1715e1daf0ecSJonathan Lemon 1716e1daf0ecSJonathan Lemon static ssize_t 1717e1daf0ecSJonathan Lemon ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr, u32 shift) 1718e1daf0ecSJonathan Lemon { 1719e1daf0ecSJonathan Lemon struct ptp_ocp_sma_connector *sma = &ptp_ocp_sma_map[sma_nr - 1]; 1720e1daf0ecSJonathan Lemon enum ptp_ocp_sma_mode mode; 1721e1daf0ecSJonathan Lemon int val; 1722e1daf0ecSJonathan Lemon 1723e1daf0ecSJonathan Lemon mode = sma->mode; 1724e1daf0ecSJonathan Lemon val = sma_parse_inputs(buf, &mode); 1725e1daf0ecSJonathan Lemon if (val < 0) 1726e1daf0ecSJonathan Lemon return val; 1727e1daf0ecSJonathan Lemon 1728e1daf0ecSJonathan Lemon if (mode != sma->mode && sma->fixed_mode) 1729e1daf0ecSJonathan Lemon return -EOPNOTSUPP; 1730e1daf0ecSJonathan Lemon 1731e1daf0ecSJonathan Lemon if (mode != sma->mode) { 1732e1daf0ecSJonathan Lemon pr_err("Mode changes not supported yet.\n"); 1733e1daf0ecSJonathan Lemon return -EOPNOTSUPP; 1734e1daf0ecSJonathan Lemon } 1735e1daf0ecSJonathan Lemon 1736e1daf0ecSJonathan Lemon if (sma->mode == SMA_MODE_IN) 1737e1daf0ecSJonathan Lemon ptp_ocp_sma_store_inputs(bp, val, shift); 1738e1daf0ecSJonathan Lemon else 1739e1daf0ecSJonathan Lemon ptp_ocp_sma_store_output(bp, val, shift); 1740e1daf0ecSJonathan Lemon 1741e1daf0ecSJonathan Lemon return 0; 1742e1daf0ecSJonathan Lemon } 1743e1daf0ecSJonathan Lemon 1744e1daf0ecSJonathan Lemon static ssize_t 1745e1daf0ecSJonathan Lemon sma1_store(struct device *dev, struct device_attribute *attr, 1746e1daf0ecSJonathan Lemon const char *buf, size_t count) 1747e1daf0ecSJonathan Lemon { 1748e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1749e1daf0ecSJonathan Lemon int err; 1750e1daf0ecSJonathan Lemon 1751e1daf0ecSJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 1, 0); 1752e1daf0ecSJonathan Lemon return err ? err : count; 1753e1daf0ecSJonathan Lemon } 1754e1daf0ecSJonathan Lemon 1755e1daf0ecSJonathan Lemon static ssize_t 1756e1daf0ecSJonathan Lemon sma2_store(struct device *dev, struct device_attribute *attr, 1757e1daf0ecSJonathan Lemon const char *buf, size_t count) 1758e1daf0ecSJonathan Lemon { 1759e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1760e1daf0ecSJonathan Lemon int err; 1761e1daf0ecSJonathan Lemon 1762e1daf0ecSJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 2, 16); 1763e1daf0ecSJonathan Lemon return err ? err : count; 1764e1daf0ecSJonathan Lemon } 1765e1daf0ecSJonathan Lemon 1766e1daf0ecSJonathan Lemon static ssize_t 1767e1daf0ecSJonathan Lemon sma3_store(struct device *dev, struct device_attribute *attr, 1768e1daf0ecSJonathan Lemon const char *buf, size_t count) 1769e1daf0ecSJonathan Lemon { 1770e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1771e1daf0ecSJonathan Lemon int err; 1772e1daf0ecSJonathan Lemon 1773e1daf0ecSJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 3, 0); 1774e1daf0ecSJonathan Lemon return err ? err : count; 1775e1daf0ecSJonathan Lemon } 1776e1daf0ecSJonathan Lemon 1777e1daf0ecSJonathan Lemon static ssize_t 1778e1daf0ecSJonathan Lemon sma4_store(struct device *dev, struct device_attribute *attr, 1779e1daf0ecSJonathan Lemon const char *buf, size_t count) 1780e1daf0ecSJonathan Lemon { 1781e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1782e1daf0ecSJonathan Lemon int err; 1783e1daf0ecSJonathan Lemon 1784e1daf0ecSJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 4, 16); 1785e1daf0ecSJonathan Lemon return err ? err : count; 1786e1daf0ecSJonathan Lemon } 1787e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma1); 1788e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma2); 1789e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma3); 1790e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma4); 1791e1daf0ecSJonathan Lemon 1792e1daf0ecSJonathan Lemon static ssize_t 1793e1daf0ecSJonathan Lemon available_sma_inputs_show(struct device *dev, 1794e1daf0ecSJonathan Lemon struct device_attribute *attr, char *buf) 1795e1daf0ecSJonathan Lemon { 1796e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_sma_in, buf); 1797e1daf0ecSJonathan Lemon } 1798e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_inputs); 1799e1daf0ecSJonathan Lemon 1800e1daf0ecSJonathan Lemon static ssize_t 1801e1daf0ecSJonathan Lemon available_sma_outputs_show(struct device *dev, 1802e1daf0ecSJonathan Lemon struct device_attribute *attr, char *buf) 1803e1daf0ecSJonathan Lemon { 1804e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_sma_out, buf); 1805e1daf0ecSJonathan Lemon } 1806e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_outputs); 1807e1daf0ecSJonathan Lemon 1808773bda96SJonathan Lemon static ssize_t 1809773bda96SJonathan Lemon serialnum_show(struct device *dev, struct device_attribute *attr, char *buf) 1810773bda96SJonathan Lemon { 1811773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1812773bda96SJonathan Lemon 1813773bda96SJonathan Lemon if (!bp->has_serial) 1814773bda96SJonathan Lemon ptp_ocp_get_serial_number(bp); 1815773bda96SJonathan Lemon 1816773bda96SJonathan Lemon return sysfs_emit(buf, "%pM\n", bp->serial); 1817773bda96SJonathan Lemon } 1818773bda96SJonathan Lemon static DEVICE_ATTR_RO(serialnum); 1819773bda96SJonathan Lemon 1820773bda96SJonathan Lemon static ssize_t 1821ef0cfb34SJonathan Lemon gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf) 1822773bda96SJonathan Lemon { 1823773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1824773bda96SJonathan Lemon ssize_t ret; 1825773bda96SJonathan Lemon 1826ef0cfb34SJonathan Lemon if (bp->gnss_lost) 1827ef0cfb34SJonathan Lemon ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost); 1828773bda96SJonathan Lemon else 1829773bda96SJonathan Lemon ret = sysfs_emit(buf, "SYNC\n"); 1830773bda96SJonathan Lemon 1831773bda96SJonathan Lemon return ret; 1832773bda96SJonathan Lemon } 1833ef0cfb34SJonathan Lemon static DEVICE_ATTR_RO(gnss_sync); 1834773bda96SJonathan Lemon 1835773bda96SJonathan Lemon static ssize_t 183689260d87SJonathan Lemon utc_tai_offset_show(struct device *dev, 183789260d87SJonathan Lemon struct device_attribute *attr, char *buf) 183889260d87SJonathan Lemon { 183989260d87SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 184089260d87SJonathan Lemon 184189260d87SJonathan Lemon return sysfs_emit(buf, "%d\n", bp->utc_tai_offset); 184289260d87SJonathan Lemon } 184389260d87SJonathan Lemon 184489260d87SJonathan Lemon static ssize_t 184589260d87SJonathan Lemon utc_tai_offset_store(struct device *dev, 184689260d87SJonathan Lemon struct device_attribute *attr, 184789260d87SJonathan Lemon const char *buf, size_t count) 184889260d87SJonathan Lemon { 184989260d87SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 185089260d87SJonathan Lemon int err; 185189260d87SJonathan Lemon u32 val; 185289260d87SJonathan Lemon 185389260d87SJonathan Lemon err = kstrtou32(buf, 0, &val); 185489260d87SJonathan Lemon if (err) 185589260d87SJonathan Lemon return err; 185689260d87SJonathan Lemon 185789260d87SJonathan Lemon ptp_ocp_utc_distribute(bp, val); 185889260d87SJonathan Lemon 185989260d87SJonathan Lemon return count; 186089260d87SJonathan Lemon } 186189260d87SJonathan Lemon static DEVICE_ATTR_RW(utc_tai_offset); 186289260d87SJonathan Lemon 186389260d87SJonathan Lemon static ssize_t 18641acffc6eSJonathan Lemon ts_window_adjust_show(struct device *dev, 18651acffc6eSJonathan Lemon struct device_attribute *attr, char *buf) 18661acffc6eSJonathan Lemon { 18671acffc6eSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 18681acffc6eSJonathan Lemon 18691acffc6eSJonathan Lemon return sysfs_emit(buf, "%d\n", bp->ts_window_adjust); 18701acffc6eSJonathan Lemon } 18711acffc6eSJonathan Lemon 18721acffc6eSJonathan Lemon static ssize_t 18731acffc6eSJonathan Lemon ts_window_adjust_store(struct device *dev, 18741acffc6eSJonathan Lemon struct device_attribute *attr, 18751acffc6eSJonathan Lemon const char *buf, size_t count) 18761acffc6eSJonathan Lemon { 18771acffc6eSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 18781acffc6eSJonathan Lemon int err; 18791acffc6eSJonathan Lemon u32 val; 18801acffc6eSJonathan Lemon 18811acffc6eSJonathan Lemon err = kstrtou32(buf, 0, &val); 18821acffc6eSJonathan Lemon if (err) 18831acffc6eSJonathan Lemon return err; 18841acffc6eSJonathan Lemon 18851acffc6eSJonathan Lemon bp->ts_window_adjust = val; 18861acffc6eSJonathan Lemon 18871acffc6eSJonathan Lemon return count; 18881acffc6eSJonathan Lemon } 18891acffc6eSJonathan Lemon static DEVICE_ATTR_RW(ts_window_adjust); 18901acffc6eSJonathan Lemon 18911acffc6eSJonathan Lemon static ssize_t 1892d14ee252SJonathan Lemon irig_b_mode_show(struct device *dev, struct device_attribute *attr, char *buf) 1893d14ee252SJonathan Lemon { 1894d14ee252SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1895d14ee252SJonathan Lemon u32 val; 1896d14ee252SJonathan Lemon 1897d14ee252SJonathan Lemon val = ioread32(&bp->irig_out->ctrl); 1898d14ee252SJonathan Lemon val = (val >> 16) & 0x07; 1899d14ee252SJonathan Lemon return sysfs_emit(buf, "%d\n", val); 1900d14ee252SJonathan Lemon } 1901d14ee252SJonathan Lemon 1902d14ee252SJonathan Lemon static ssize_t 1903d14ee252SJonathan Lemon irig_b_mode_store(struct device *dev, 1904d14ee252SJonathan Lemon struct device_attribute *attr, 1905d14ee252SJonathan Lemon const char *buf, size_t count) 1906d14ee252SJonathan Lemon { 1907d14ee252SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1908d14ee252SJonathan Lemon unsigned long flags; 1909d14ee252SJonathan Lemon int err; 1910d14ee252SJonathan Lemon u32 reg; 1911d14ee252SJonathan Lemon u8 val; 1912d14ee252SJonathan Lemon 1913d14ee252SJonathan Lemon err = kstrtou8(buf, 0, &val); 1914d14ee252SJonathan Lemon if (err) 1915d14ee252SJonathan Lemon return err; 1916d14ee252SJonathan Lemon if (val > 7) 1917d14ee252SJonathan Lemon return -EINVAL; 1918d14ee252SJonathan Lemon 1919d14ee252SJonathan Lemon reg = ((val & 0x7) << 16); 1920d14ee252SJonathan Lemon 1921d14ee252SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1922d14ee252SJonathan Lemon iowrite32(0, &bp->irig_out->ctrl); /* disable */ 1923d14ee252SJonathan Lemon iowrite32(reg, &bp->irig_out->ctrl); /* change mode */ 1924d14ee252SJonathan Lemon iowrite32(reg | IRIG_M_CTRL_ENABLE, &bp->irig_out->ctrl); 1925d14ee252SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1926d14ee252SJonathan Lemon 1927d14ee252SJonathan Lemon return count; 1928d14ee252SJonathan Lemon } 1929d14ee252SJonathan Lemon static DEVICE_ATTR_RW(irig_b_mode); 1930d14ee252SJonathan Lemon 1931d14ee252SJonathan Lemon static ssize_t 1932773bda96SJonathan Lemon clock_source_show(struct device *dev, struct device_attribute *attr, char *buf) 1933773bda96SJonathan Lemon { 1934773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1935773bda96SJonathan Lemon const char *p; 1936773bda96SJonathan Lemon u32 select; 1937773bda96SJonathan Lemon 1938773bda96SJonathan Lemon select = ioread32(&bp->reg->select); 1939e1daf0ecSJonathan Lemon p = ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16); 1940773bda96SJonathan Lemon 1941773bda96SJonathan Lemon return sysfs_emit(buf, "%s\n", p); 1942773bda96SJonathan Lemon } 1943773bda96SJonathan Lemon 1944773bda96SJonathan Lemon static ssize_t 1945773bda96SJonathan Lemon clock_source_store(struct device *dev, struct device_attribute *attr, 1946773bda96SJonathan Lemon const char *buf, size_t count) 1947773bda96SJonathan Lemon { 1948773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1949773bda96SJonathan Lemon unsigned long flags; 1950773bda96SJonathan Lemon int val; 1951773bda96SJonathan Lemon 1952e1daf0ecSJonathan Lemon val = ptp_ocp_select_val_from_name(ptp_ocp_clock, buf); 1953773bda96SJonathan Lemon if (val < 0) 1954773bda96SJonathan Lemon return val; 1955773bda96SJonathan Lemon 1956773bda96SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1957773bda96SJonathan Lemon iowrite32(val, &bp->reg->select); 1958773bda96SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1959773bda96SJonathan Lemon 1960773bda96SJonathan Lemon return count; 1961773bda96SJonathan Lemon } 1962773bda96SJonathan Lemon static DEVICE_ATTR_RW(clock_source); 1963773bda96SJonathan Lemon 1964773bda96SJonathan Lemon static ssize_t 1965773bda96SJonathan Lemon available_clock_sources_show(struct device *dev, 1966773bda96SJonathan Lemon struct device_attribute *attr, char *buf) 1967773bda96SJonathan Lemon { 1968e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_clock, buf); 1969773bda96SJonathan Lemon } 1970773bda96SJonathan Lemon static DEVICE_ATTR_RO(available_clock_sources); 1971773bda96SJonathan Lemon 19722f23f486SVadim Fedorenko static ssize_t 19732f23f486SVadim Fedorenko clock_status_drift_show(struct device *dev, 19742f23f486SVadim Fedorenko struct device_attribute *attr, char *buf) 19752f23f486SVadim Fedorenko { 19762f23f486SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 19772f23f486SVadim Fedorenko u32 val; 19782f23f486SVadim Fedorenko int res; 19792f23f486SVadim Fedorenko 19802f23f486SVadim Fedorenko val = ioread32(&bp->reg->status_drift); 19812f23f486SVadim Fedorenko res = (val & ~INT_MAX) ? -1 : 1; 19822f23f486SVadim Fedorenko res *= (val & INT_MAX); 19832f23f486SVadim Fedorenko return sysfs_emit(buf, "%d\n", res); 19842f23f486SVadim Fedorenko } 19852f23f486SVadim Fedorenko static DEVICE_ATTR_RO(clock_status_drift); 19862f23f486SVadim Fedorenko 19872f23f486SVadim Fedorenko static ssize_t 19882f23f486SVadim Fedorenko clock_status_offset_show(struct device *dev, 19892f23f486SVadim Fedorenko struct device_attribute *attr, char *buf) 19902f23f486SVadim Fedorenko { 19912f23f486SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 19922f23f486SVadim Fedorenko u32 val; 19932f23f486SVadim Fedorenko int res; 19942f23f486SVadim Fedorenko 19952f23f486SVadim Fedorenko val = ioread32(&bp->reg->status_offset); 19962f23f486SVadim Fedorenko res = (val & ~INT_MAX) ? -1 : 1; 19972f23f486SVadim Fedorenko res *= (val & INT_MAX); 19982f23f486SVadim Fedorenko return sysfs_emit(buf, "%d\n", res); 19992f23f486SVadim Fedorenko } 20002f23f486SVadim Fedorenko static DEVICE_ATTR_RO(clock_status_offset); 20012f23f486SVadim Fedorenko 200244a412d1SVadim Fedorenko static ssize_t 200344a412d1SVadim Fedorenko tod_correction_show(struct device *dev, 200444a412d1SVadim Fedorenko struct device_attribute *attr, char *buf) 200544a412d1SVadim Fedorenko { 200644a412d1SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 200744a412d1SVadim Fedorenko u32 val; 200844a412d1SVadim Fedorenko int res; 200944a412d1SVadim Fedorenko 201044a412d1SVadim Fedorenko val = ioread32(&bp->tod->adj_sec); 201144a412d1SVadim Fedorenko res = (val & ~INT_MAX) ? -1 : 1; 201244a412d1SVadim Fedorenko res *= (val & INT_MAX); 201344a412d1SVadim Fedorenko return sysfs_emit(buf, "%d\n", res); 201444a412d1SVadim Fedorenko } 201544a412d1SVadim Fedorenko 201644a412d1SVadim Fedorenko static ssize_t 201744a412d1SVadim Fedorenko tod_correction_store(struct device *dev, struct device_attribute *attr, 201844a412d1SVadim Fedorenko const char *buf, size_t count) 201944a412d1SVadim Fedorenko { 202044a412d1SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 202144a412d1SVadim Fedorenko unsigned long flags; 202244a412d1SVadim Fedorenko int err, res; 202344a412d1SVadim Fedorenko u32 val = 0; 202444a412d1SVadim Fedorenko 202544a412d1SVadim Fedorenko err = kstrtos32(buf, 0, &res); 202644a412d1SVadim Fedorenko if (err) 202744a412d1SVadim Fedorenko return err; 202844a412d1SVadim Fedorenko if (res < 0) { 202944a412d1SVadim Fedorenko res *= -1; 203044a412d1SVadim Fedorenko val |= BIT(31); 203144a412d1SVadim Fedorenko } 203244a412d1SVadim Fedorenko val |= res; 203344a412d1SVadim Fedorenko 203444a412d1SVadim Fedorenko spin_lock_irqsave(&bp->lock, flags); 203544a412d1SVadim Fedorenko iowrite32(val, &bp->tod->adj_sec); 203644a412d1SVadim Fedorenko spin_unlock_irqrestore(&bp->lock, flags); 203744a412d1SVadim Fedorenko 203844a412d1SVadim Fedorenko return count; 203944a412d1SVadim Fedorenko } 204044a412d1SVadim Fedorenko static DEVICE_ATTR_RW(tod_correction); 204144a412d1SVadim Fedorenko 2042773bda96SJonathan Lemon static struct attribute *timecard_attrs[] = { 2043773bda96SJonathan Lemon &dev_attr_serialnum.attr, 2044ef0cfb34SJonathan Lemon &dev_attr_gnss_sync.attr, 2045773bda96SJonathan Lemon &dev_attr_clock_source.attr, 2046773bda96SJonathan Lemon &dev_attr_available_clock_sources.attr, 2047e1daf0ecSJonathan Lemon &dev_attr_sma1.attr, 2048e1daf0ecSJonathan Lemon &dev_attr_sma2.attr, 2049e1daf0ecSJonathan Lemon &dev_attr_sma3.attr, 2050e1daf0ecSJonathan Lemon &dev_attr_sma4.attr, 2051e1daf0ecSJonathan Lemon &dev_attr_available_sma_inputs.attr, 2052e1daf0ecSJonathan Lemon &dev_attr_available_sma_outputs.attr, 20532f23f486SVadim Fedorenko &dev_attr_clock_status_drift.attr, 20542f23f486SVadim Fedorenko &dev_attr_clock_status_offset.attr, 2055d14ee252SJonathan Lemon &dev_attr_irig_b_mode.attr, 205689260d87SJonathan Lemon &dev_attr_utc_tai_offset.attr, 20571acffc6eSJonathan Lemon &dev_attr_ts_window_adjust.attr, 205844a412d1SVadim Fedorenko &dev_attr_tod_correction.attr, 2059773bda96SJonathan Lemon NULL, 2060773bda96SJonathan Lemon }; 2061773bda96SJonathan Lemon ATTRIBUTE_GROUPS(timecard); 2062773bda96SJonathan Lemon 2063f67bf662SJonathan Lemon static const char * 2064f67bf662SJonathan Lemon gpio_map(u32 gpio, u32 bit, const char *pri, const char *sec, const char *def) 2065f67bf662SJonathan Lemon { 2066f67bf662SJonathan Lemon const char *ans; 2067f67bf662SJonathan Lemon 2068f67bf662SJonathan Lemon if (gpio & (1 << bit)) 2069f67bf662SJonathan Lemon ans = pri; 2070f67bf662SJonathan Lemon else if (gpio & (1 << (bit + 16))) 2071f67bf662SJonathan Lemon ans = sec; 2072f67bf662SJonathan Lemon else 2073f67bf662SJonathan Lemon ans = def; 2074f67bf662SJonathan Lemon return ans; 2075f67bf662SJonathan Lemon } 2076f67bf662SJonathan Lemon 2077f67bf662SJonathan Lemon static void 2078f67bf662SJonathan Lemon gpio_multi_map(char *buf, u32 gpio, u32 bit, 2079f67bf662SJonathan Lemon const char *pri, const char *sec, const char *def) 2080f67bf662SJonathan Lemon { 2081f67bf662SJonathan Lemon char *ans = buf; 2082f67bf662SJonathan Lemon 2083f67bf662SJonathan Lemon strcpy(ans, def); 2084f67bf662SJonathan Lemon if (gpio & (1 << bit)) 2085f67bf662SJonathan Lemon ans += sprintf(ans, "%s ", pri); 2086f67bf662SJonathan Lemon if (gpio & (1 << (bit + 16))) 2087f67bf662SJonathan Lemon ans += sprintf(ans, "%s ", sec); 2088f67bf662SJonathan Lemon } 2089f67bf662SJonathan Lemon 2090f67bf662SJonathan Lemon static int 2091f67bf662SJonathan Lemon ptp_ocp_summary_show(struct seq_file *s, void *data) 2092f67bf662SJonathan Lemon { 2093f67bf662SJonathan Lemon struct device *dev = s->private; 2094f67bf662SJonathan Lemon struct ptp_system_timestamp sts; 2095f67bf662SJonathan Lemon u32 sma_in, sma_out, ctrl, val; 2096f67bf662SJonathan Lemon struct ts_reg __iomem *ts_reg; 2097f67bf662SJonathan Lemon struct timespec64 ts; 2098f67bf662SJonathan Lemon struct ptp_ocp *bp; 2099f67bf662SJonathan Lemon const char *src; 2100a62a56d0SJonathan Lemon bool on, map; 2101f67bf662SJonathan Lemon char *buf; 2102f67bf662SJonathan Lemon 2103f67bf662SJonathan Lemon buf = (char *)__get_free_page(GFP_KERNEL); 2104f67bf662SJonathan Lemon if (!buf) 2105f67bf662SJonathan Lemon return -ENOMEM; 2106f67bf662SJonathan Lemon 2107f67bf662SJonathan Lemon bp = dev_get_drvdata(dev); 2108f67bf662SJonathan Lemon sma_in = ioread32(&bp->sma->gpio1); 2109f67bf662SJonathan Lemon sma_out = ioread32(&bp->sma->gpio2); 2110f67bf662SJonathan Lemon 2111f67bf662SJonathan Lemon seq_printf(s, "%7s: /dev/ptp%d\n", "PTP", ptp_clock_index(bp->ptp)); 211261fd7ac2SJonathan Lemon if (bp->gnss_port != -1) 211361fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS1", bp->gnss_port); 211461fd7ac2SJonathan Lemon if (bp->gnss2_port != -1) 211561fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS2", bp->gnss2_port); 211661fd7ac2SJonathan Lemon if (bp->mac_port != -1) 211761fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "MAC", bp->mac_port); 211861fd7ac2SJonathan Lemon if (bp->nmea_port != -1) 211961fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "NMEA", bp->nmea_port); 2120f67bf662SJonathan Lemon 2121f67bf662SJonathan Lemon sma1_show(dev, NULL, buf); 2122f67bf662SJonathan Lemon seq_printf(s, " sma1: %s", buf); 2123f67bf662SJonathan Lemon 2124f67bf662SJonathan Lemon sma2_show(dev, NULL, buf); 2125f67bf662SJonathan Lemon seq_printf(s, " sma2: %s", buf); 2126f67bf662SJonathan Lemon 2127f67bf662SJonathan Lemon sma3_show(dev, NULL, buf); 2128f67bf662SJonathan Lemon seq_printf(s, " sma3: %s", buf); 2129f67bf662SJonathan Lemon 2130f67bf662SJonathan Lemon sma4_show(dev, NULL, buf); 2131f67bf662SJonathan Lemon seq_printf(s, " sma4: %s", buf); 2132f67bf662SJonathan Lemon 2133f67bf662SJonathan Lemon if (bp->ts0) { 2134f67bf662SJonathan Lemon ts_reg = bp->ts0->mem; 2135f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 2136f67bf662SJonathan Lemon src = "GNSS"; 2137f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS0", 2138f67bf662SJonathan Lemon on ? " ON" : "OFF", src); 2139f67bf662SJonathan Lemon } 2140f67bf662SJonathan Lemon 2141f67bf662SJonathan Lemon if (bp->ts1) { 2142f67bf662SJonathan Lemon ts_reg = bp->ts1->mem; 2143f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 2144f67bf662SJonathan Lemon src = gpio_map(sma_in, 2, "sma1", "sma2", "----"); 2145f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS1", 2146f67bf662SJonathan Lemon on ? " ON" : "OFF", src); 2147f67bf662SJonathan Lemon } 2148f67bf662SJonathan Lemon 2149f67bf662SJonathan Lemon if (bp->ts2) { 2150f67bf662SJonathan Lemon ts_reg = bp->ts2->mem; 2151f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 2152f67bf662SJonathan Lemon src = gpio_map(sma_in, 3, "sma1", "sma2", "----"); 2153f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS2", 2154f67bf662SJonathan Lemon on ? " ON" : "OFF", src); 2155f67bf662SJonathan Lemon } 2156f67bf662SJonathan Lemon 2157a62a56d0SJonathan Lemon if (bp->pps) { 2158a62a56d0SJonathan Lemon ts_reg = bp->pps->mem; 2159a62a56d0SJonathan Lemon src = "PHC"; 2160a62a56d0SJonathan Lemon on = ioread32(&ts_reg->enable); 2161a62a56d0SJonathan Lemon map = !!(bp->pps_req_map & OCP_REQ_TIMESTAMP); 2162a62a56d0SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS3", 21631a575cdeSNathan Chancellor on && map ? " ON" : "OFF", src); 2164a62a56d0SJonathan Lemon 2165a62a56d0SJonathan Lemon map = !!(bp->pps_req_map & OCP_REQ_PPS); 2166a62a56d0SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "PPS", 21671a575cdeSNathan Chancellor on && map ? " ON" : "OFF", src); 2168a62a56d0SJonathan Lemon } 2169a62a56d0SJonathan Lemon 2170f67bf662SJonathan Lemon if (bp->irig_out) { 2171f67bf662SJonathan Lemon ctrl = ioread32(&bp->irig_out->ctrl); 2172f67bf662SJonathan Lemon on = ctrl & IRIG_M_CTRL_ENABLE; 2173f67bf662SJonathan Lemon val = ioread32(&bp->irig_out->status); 2174f67bf662SJonathan Lemon gpio_multi_map(buf, sma_out, 4, "sma3", "sma4", "----"); 2175f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, mode %d, out: %s\n", "IRIG", 2176f67bf662SJonathan Lemon on ? " ON" : "OFF", val, (ctrl >> 16), buf); 2177f67bf662SJonathan Lemon } 2178f67bf662SJonathan Lemon 2179f67bf662SJonathan Lemon if (bp->irig_in) { 2180f67bf662SJonathan Lemon on = ioread32(&bp->irig_in->ctrl) & IRIG_S_CTRL_ENABLE; 2181f67bf662SJonathan Lemon val = ioread32(&bp->irig_in->status); 2182f67bf662SJonathan Lemon src = gpio_map(sma_in, 4, "sma1", "sma2", "----"); 2183f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, src: %s\n", "IRIG in", 2184f67bf662SJonathan Lemon on ? " ON" : "OFF", val, src); 2185f67bf662SJonathan Lemon } 2186f67bf662SJonathan Lemon 2187f67bf662SJonathan Lemon if (bp->dcf_out) { 2188f67bf662SJonathan Lemon on = ioread32(&bp->dcf_out->ctrl) & DCF_M_CTRL_ENABLE; 2189f67bf662SJonathan Lemon val = ioread32(&bp->dcf_out->status); 2190f67bf662SJonathan Lemon gpio_multi_map(buf, sma_out, 5, "sma3", "sma4", "----"); 2191f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, out: %s\n", "DCF", 2192f67bf662SJonathan Lemon on ? " ON" : "OFF", val, buf); 2193f67bf662SJonathan Lemon } 2194f67bf662SJonathan Lemon 2195f67bf662SJonathan Lemon if (bp->dcf_in) { 2196f67bf662SJonathan Lemon on = ioread32(&bp->dcf_in->ctrl) & DCF_S_CTRL_ENABLE; 2197f67bf662SJonathan Lemon val = ioread32(&bp->dcf_in->status); 2198f67bf662SJonathan Lemon src = gpio_map(sma_in, 5, "sma1", "sma2", "----"); 2199f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, src: %s\n", "DCF in", 2200f67bf662SJonathan Lemon on ? " ON" : "OFF", val, src); 2201f67bf662SJonathan Lemon } 2202f67bf662SJonathan Lemon 2203e3516bb4SJonathan Lemon if (bp->nmea_out) { 2204e3516bb4SJonathan Lemon on = ioread32(&bp->nmea_out->ctrl) & 1; 2205e3516bb4SJonathan Lemon val = ioread32(&bp->nmea_out->status); 2206e3516bb4SJonathan Lemon seq_printf(s, "%7s: %s, error: %d\n", "NMEA", 2207e3516bb4SJonathan Lemon on ? " ON" : "OFF", val); 2208e3516bb4SJonathan Lemon } 2209e3516bb4SJonathan Lemon 2210f67bf662SJonathan Lemon /* compute src for PPS1, used below. */ 2211f67bf662SJonathan Lemon if (bp->pps_select) { 2212f67bf662SJonathan Lemon val = ioread32(&bp->pps_select->gpio1); 2213f67bf662SJonathan Lemon if (val & 0x01) 2214f67bf662SJonathan Lemon src = gpio_map(sma_in, 0, "sma1", "sma2", "----"); 2215f67bf662SJonathan Lemon else if (val & 0x02) 2216f67bf662SJonathan Lemon src = "MAC"; 2217f67bf662SJonathan Lemon else if (val & 0x04) 2218f67bf662SJonathan Lemon src = "GNSS"; 2219f67bf662SJonathan Lemon else 2220f67bf662SJonathan Lemon src = "----"; 2221f67bf662SJonathan Lemon } else { 2222f67bf662SJonathan Lemon src = "?"; 2223f67bf662SJonathan Lemon } 2224f67bf662SJonathan Lemon 2225f67bf662SJonathan Lemon /* assumes automatic switchover/selection */ 2226f67bf662SJonathan Lemon val = ioread32(&bp->reg->select); 2227f67bf662SJonathan Lemon switch (val >> 16) { 2228f67bf662SJonathan Lemon case 0: 2229f67bf662SJonathan Lemon sprintf(buf, "----"); 2230f67bf662SJonathan Lemon break; 2231f67bf662SJonathan Lemon case 2: 2232f67bf662SJonathan Lemon sprintf(buf, "IRIG"); 2233f67bf662SJonathan Lemon break; 2234f67bf662SJonathan Lemon case 3: 2235f67bf662SJonathan Lemon sprintf(buf, "%s via PPS1", src); 2236f67bf662SJonathan Lemon break; 2237f67bf662SJonathan Lemon case 6: 2238f67bf662SJonathan Lemon sprintf(buf, "DCF"); 2239f67bf662SJonathan Lemon break; 2240f67bf662SJonathan Lemon default: 2241f67bf662SJonathan Lemon strcpy(buf, "unknown"); 2242f67bf662SJonathan Lemon break; 2243f67bf662SJonathan Lemon } 2244f67bf662SJonathan Lemon val = ioread32(&bp->reg->status); 2245f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, state: %s\n", "PHC src", buf, 2246f67bf662SJonathan Lemon val & OCP_STATUS_IN_SYNC ? "sync" : "unsynced"); 2247f67bf662SJonathan Lemon 2248f67bf662SJonathan Lemon /* reuses PPS1 src from earlier */ 2249f67bf662SJonathan Lemon seq_printf(s, "MAC PPS1 src: %s\n", src); 2250f67bf662SJonathan Lemon 2251f67bf662SJonathan Lemon src = gpio_map(sma_in, 1, "sma1", "sma2", "GNSS2"); 2252f67bf662SJonathan Lemon seq_printf(s, "MAC PPS2 src: %s\n", src); 2253f67bf662SJonathan Lemon 2254f67bf662SJonathan Lemon if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts)) { 2255f67bf662SJonathan Lemon struct timespec64 sys_ts; 2256f67bf662SJonathan Lemon s64 pre_ns, post_ns, ns; 2257f67bf662SJonathan Lemon 2258f67bf662SJonathan Lemon pre_ns = timespec64_to_ns(&sts.pre_ts); 2259f67bf662SJonathan Lemon post_ns = timespec64_to_ns(&sts.post_ts); 2260f67bf662SJonathan Lemon ns = (pre_ns + post_ns) / 2; 2261f67bf662SJonathan Lemon ns += (s64)bp->utc_tai_offset * NSEC_PER_SEC; 2262f67bf662SJonathan Lemon sys_ts = ns_to_timespec64(ns); 2263f67bf662SJonathan Lemon 2264f67bf662SJonathan Lemon seq_printf(s, "%7s: %lld.%ld == %ptT TAI\n", "PHC", 2265f67bf662SJonathan Lemon ts.tv_sec, ts.tv_nsec, &ts); 2266f67bf662SJonathan Lemon seq_printf(s, "%7s: %lld.%ld == %ptT UTC offset %d\n", "SYS", 2267f67bf662SJonathan Lemon sys_ts.tv_sec, sys_ts.tv_nsec, &sys_ts, 2268f67bf662SJonathan Lemon bp->utc_tai_offset); 2269f67bf662SJonathan Lemon seq_printf(s, "%7s: PHC:SYS offset: %lld window: %lld\n", "", 2270f67bf662SJonathan Lemon timespec64_to_ns(&ts) - ns, 2271f67bf662SJonathan Lemon post_ns - pre_ns); 2272f67bf662SJonathan Lemon } 2273f67bf662SJonathan Lemon 2274f67bf662SJonathan Lemon free_page((unsigned long)buf); 2275f67bf662SJonathan Lemon return 0; 2276f67bf662SJonathan Lemon } 2277f67bf662SJonathan Lemon DEFINE_SHOW_ATTRIBUTE(ptp_ocp_summary); 2278f67bf662SJonathan Lemon 22799f492c4cSVadim Fedorenko static int 22809f492c4cSVadim Fedorenko ptp_ocp_tod_status_show(struct seq_file *s, void *data) 22819f492c4cSVadim Fedorenko { 22829f492c4cSVadim Fedorenko struct device *dev = s->private; 22839f492c4cSVadim Fedorenko struct ptp_ocp *bp; 22849f492c4cSVadim Fedorenko u32 val; 22859f492c4cSVadim Fedorenko int idx; 22869f492c4cSVadim Fedorenko 22879f492c4cSVadim Fedorenko bp = dev_get_drvdata(dev); 22889f492c4cSVadim Fedorenko 22899f492c4cSVadim Fedorenko val = ioread32(&bp->tod->ctrl); 22909f492c4cSVadim Fedorenko if (!(val & TOD_CTRL_ENABLE)) { 22919f492c4cSVadim Fedorenko seq_printf(s, "TOD Slave disabled\n"); 22929f492c4cSVadim Fedorenko return 0; 22939f492c4cSVadim Fedorenko } 22949f492c4cSVadim Fedorenko seq_printf(s, "TOD Slave enabled, Control Register 0x%08X\n", val); 22959f492c4cSVadim Fedorenko 22969f492c4cSVadim Fedorenko idx = val & TOD_CTRL_PROTOCOL ? 4 : 0; 22979f492c4cSVadim Fedorenko idx += (val >> 16) & 3; 22989f492c4cSVadim Fedorenko seq_printf(s, "Protocol %s\n", ptp_ocp_tod_proto_name(idx)); 22999f492c4cSVadim Fedorenko 23009f492c4cSVadim Fedorenko idx = (val >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK; 23019f492c4cSVadim Fedorenko seq_printf(s, "GNSS %s\n", ptp_ocp_tod_gnss_name(idx)); 23029f492c4cSVadim Fedorenko 23039f492c4cSVadim Fedorenko val = ioread32(&bp->tod->version); 23049f492c4cSVadim Fedorenko seq_printf(s, "TOD Version %d.%d.%d\n", 23059f492c4cSVadim Fedorenko val >> 24, (val >> 16) & 0xff, val & 0xffff); 23069f492c4cSVadim Fedorenko 23079f492c4cSVadim Fedorenko val = ioread32(&bp->tod->status); 23089f492c4cSVadim Fedorenko seq_printf(s, "Status register: 0x%08X\n", val); 23099f492c4cSVadim Fedorenko 23109f492c4cSVadim Fedorenko val = ioread32(&bp->tod->adj_sec); 23119f492c4cSVadim Fedorenko idx = (val & ~INT_MAX) ? -1 : 1; 23129f492c4cSVadim Fedorenko idx *= (val & INT_MAX); 23139f492c4cSVadim Fedorenko seq_printf(s, "Correction seconds: %d\n", idx); 23149f492c4cSVadim Fedorenko 23159f492c4cSVadim Fedorenko val = ioread32(&bp->tod->utc_status); 23169f492c4cSVadim Fedorenko seq_printf(s, "UTC status register: 0x%08X\n", val); 23179f492c4cSVadim Fedorenko seq_printf(s, "UTC offset: %d valid:%d\n", 23189f492c4cSVadim Fedorenko val & TOD_STATUS_UTC_MASK, val & TOD_STATUS_UTC_VALID ? 1 : 0); 23199f492c4cSVadim Fedorenko seq_printf(s, "Leap second info valid:%d, Leap second announce %d\n", 23209f492c4cSVadim Fedorenko val & TOD_STATUS_LEAP_VALID ? 1 : 0, 23219f492c4cSVadim Fedorenko val & TOD_STATUS_LEAP_ANNOUNCE ? 1 : 0); 23229f492c4cSVadim Fedorenko 23239f492c4cSVadim Fedorenko val = ioread32(&bp->tod->leap); 23249f492c4cSVadim Fedorenko seq_printf(s, "Time to next leap second (in sec): %d\n", (s32) val); 23259f492c4cSVadim Fedorenko 23269f492c4cSVadim Fedorenko return 0; 23279f492c4cSVadim Fedorenko } 23289f492c4cSVadim Fedorenko DEFINE_SHOW_ATTRIBUTE(ptp_ocp_tod_status); 23299f492c4cSVadim Fedorenko 2330f67bf662SJonathan Lemon static struct dentry *ptp_ocp_debugfs_root; 2331f67bf662SJonathan Lemon 2332f67bf662SJonathan Lemon static void 2333f67bf662SJonathan Lemon ptp_ocp_debugfs_add_device(struct ptp_ocp *bp) 2334f67bf662SJonathan Lemon { 2335f67bf662SJonathan Lemon struct dentry *d; 2336f67bf662SJonathan Lemon 2337f67bf662SJonathan Lemon d = debugfs_create_dir(dev_name(&bp->dev), ptp_ocp_debugfs_root); 2338f67bf662SJonathan Lemon bp->debug_root = d; 2339f67bf662SJonathan Lemon debugfs_create_file("summary", 0444, bp->debug_root, 2340f67bf662SJonathan Lemon &bp->dev, &ptp_ocp_summary_fops); 23419f492c4cSVadim Fedorenko if (bp->tod) 23429f492c4cSVadim Fedorenko debugfs_create_file("tod_status", 0444, bp->debug_root, 23439f492c4cSVadim Fedorenko &bp->dev, &ptp_ocp_tod_status_fops); 2344f67bf662SJonathan Lemon } 2345f67bf662SJonathan Lemon 2346f67bf662SJonathan Lemon static void 2347f67bf662SJonathan Lemon ptp_ocp_debugfs_remove_device(struct ptp_ocp *bp) 2348f67bf662SJonathan Lemon { 2349f67bf662SJonathan Lemon debugfs_remove_recursive(bp->debug_root); 2350f67bf662SJonathan Lemon } 2351f67bf662SJonathan Lemon 2352f67bf662SJonathan Lemon static void 2353f67bf662SJonathan Lemon ptp_ocp_debugfs_init(void) 2354f67bf662SJonathan Lemon { 2355f67bf662SJonathan Lemon ptp_ocp_debugfs_root = debugfs_create_dir("timecard", NULL); 2356f67bf662SJonathan Lemon } 2357f67bf662SJonathan Lemon 2358f67bf662SJonathan Lemon static void 2359f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(void) 2360f67bf662SJonathan Lemon { 2361f67bf662SJonathan Lemon debugfs_remove_recursive(ptp_ocp_debugfs_root); 2362f67bf662SJonathan Lemon } 2363f67bf662SJonathan Lemon 2364773bda96SJonathan Lemon static void 2365773bda96SJonathan Lemon ptp_ocp_dev_release(struct device *dev) 2366773bda96SJonathan Lemon { 2367773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2368773bda96SJonathan Lemon 2369773bda96SJonathan Lemon mutex_lock(&ptp_ocp_lock); 2370773bda96SJonathan Lemon idr_remove(&ptp_ocp_idr, bp->id); 2371773bda96SJonathan Lemon mutex_unlock(&ptp_ocp_lock); 2372773bda96SJonathan Lemon } 2373773bda96SJonathan Lemon 2374773bda96SJonathan Lemon static int 2375773bda96SJonathan Lemon ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev) 2376773bda96SJonathan Lemon { 2377773bda96SJonathan Lemon int err; 2378773bda96SJonathan Lemon 2379773bda96SJonathan Lemon mutex_lock(&ptp_ocp_lock); 2380773bda96SJonathan Lemon err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL); 2381773bda96SJonathan Lemon mutex_unlock(&ptp_ocp_lock); 2382773bda96SJonathan Lemon if (err < 0) { 2383773bda96SJonathan Lemon dev_err(&pdev->dev, "idr_alloc failed: %d\n", err); 2384773bda96SJonathan Lemon return err; 2385773bda96SJonathan Lemon } 2386773bda96SJonathan Lemon bp->id = err; 2387773bda96SJonathan Lemon 2388773bda96SJonathan Lemon bp->ptp_info = ptp_ocp_clock_info; 2389773bda96SJonathan Lemon spin_lock_init(&bp->lock); 2390ef0cfb34SJonathan Lemon bp->gnss_port = -1; 239171d7e085SJonathan Lemon bp->gnss2_port = -1; 2392773bda96SJonathan Lemon bp->mac_port = -1; 2393e3516bb4SJonathan Lemon bp->nmea_port = -1; 2394773bda96SJonathan Lemon bp->pdev = pdev; 2395773bda96SJonathan Lemon 2396773bda96SJonathan Lemon device_initialize(&bp->dev); 2397773bda96SJonathan Lemon dev_set_name(&bp->dev, "ocp%d", bp->id); 2398773bda96SJonathan Lemon bp->dev.class = &timecard_class; 2399773bda96SJonathan Lemon bp->dev.parent = &pdev->dev; 2400773bda96SJonathan Lemon bp->dev.release = ptp_ocp_dev_release; 2401773bda96SJonathan Lemon dev_set_drvdata(&bp->dev, bp); 2402773bda96SJonathan Lemon 2403773bda96SJonathan Lemon err = device_add(&bp->dev); 2404773bda96SJonathan Lemon if (err) { 2405773bda96SJonathan Lemon dev_err(&bp->dev, "device add failed: %d\n", err); 2406773bda96SJonathan Lemon goto out; 2407773bda96SJonathan Lemon } 2408773bda96SJonathan Lemon 2409773bda96SJonathan Lemon pci_set_drvdata(pdev, bp); 2410773bda96SJonathan Lemon 2411773bda96SJonathan Lemon return 0; 2412773bda96SJonathan Lemon 2413773bda96SJonathan Lemon out: 2414773bda96SJonathan Lemon ptp_ocp_dev_release(&bp->dev); 2415d12f23faSJonathan Lemon put_device(&bp->dev); 2416773bda96SJonathan Lemon return err; 2417773bda96SJonathan Lemon } 2418773bda96SJonathan Lemon 2419773bda96SJonathan Lemon static void 2420773bda96SJonathan Lemon ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link) 2421773bda96SJonathan Lemon { 2422773bda96SJonathan Lemon struct device *dev = &bp->dev; 2423773bda96SJonathan Lemon 2424773bda96SJonathan Lemon if (sysfs_create_link(&dev->kobj, &child->kobj, link)) 2425773bda96SJonathan Lemon dev_err(dev, "%s symlink failed\n", link); 2426773bda96SJonathan Lemon } 2427773bda96SJonathan Lemon 2428773bda96SJonathan Lemon static void 2429773bda96SJonathan Lemon ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link) 2430773bda96SJonathan Lemon { 2431773bda96SJonathan Lemon struct device *dev, *child; 2432773bda96SJonathan Lemon 2433773bda96SJonathan Lemon dev = &bp->pdev->dev; 2434773bda96SJonathan Lemon 2435773bda96SJonathan Lemon child = device_find_child_by_name(dev, name); 2436773bda96SJonathan Lemon if (!child) { 2437773bda96SJonathan Lemon dev_err(dev, "Could not find device %s\n", name); 2438773bda96SJonathan Lemon return; 2439773bda96SJonathan Lemon } 2440773bda96SJonathan Lemon 2441773bda96SJonathan Lemon ptp_ocp_symlink(bp, child, link); 2442773bda96SJonathan Lemon put_device(child); 2443773bda96SJonathan Lemon } 2444773bda96SJonathan Lemon 2445773bda96SJonathan Lemon static int 2446773bda96SJonathan Lemon ptp_ocp_complete(struct ptp_ocp *bp) 2447773bda96SJonathan Lemon { 2448773bda96SJonathan Lemon struct pps_device *pps; 2449773bda96SJonathan Lemon char buf[32]; 2450773bda96SJonathan Lemon 2451ef0cfb34SJonathan Lemon if (bp->gnss_port != -1) { 2452ef0cfb34SJonathan Lemon sprintf(buf, "ttyS%d", bp->gnss_port); 2453ef0cfb34SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyGNSS"); 2454773bda96SJonathan Lemon } 245571d7e085SJonathan Lemon if (bp->gnss2_port != -1) { 245671d7e085SJonathan Lemon sprintf(buf, "ttyS%d", bp->gnss2_port); 245771d7e085SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyGNSS2"); 245871d7e085SJonathan Lemon } 2459773bda96SJonathan Lemon if (bp->mac_port != -1) { 2460773bda96SJonathan Lemon sprintf(buf, "ttyS%d", bp->mac_port); 2461773bda96SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyMAC"); 2462773bda96SJonathan Lemon } 2463e3516bb4SJonathan Lemon if (bp->nmea_port != -1) { 2464e3516bb4SJonathan Lemon sprintf(buf, "ttyS%d", bp->nmea_port); 2465e3516bb4SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyNMEA"); 2466e3516bb4SJonathan Lemon } 2467773bda96SJonathan Lemon sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp)); 2468773bda96SJonathan Lemon ptp_ocp_link_child(bp, buf, "ptp"); 2469773bda96SJonathan Lemon 2470773bda96SJonathan Lemon pps = pps_lookup_dev(bp->ptp); 2471773bda96SJonathan Lemon if (pps) 2472773bda96SJonathan Lemon ptp_ocp_symlink(bp, pps->dev, "pps"); 2473773bda96SJonathan Lemon 2474773bda96SJonathan Lemon if (device_add_groups(&bp->dev, timecard_groups)) 2475773bda96SJonathan Lemon pr_err("device add groups failed\n"); 2476773bda96SJonathan Lemon 2477f67bf662SJonathan Lemon ptp_ocp_debugfs_add_device(bp); 2478f67bf662SJonathan Lemon 2479773bda96SJonathan Lemon return 0; 2480773bda96SJonathan Lemon } 2481773bda96SJonathan Lemon 2482773bda96SJonathan Lemon static void 2483065efcc5SJonathan Lemon ptp_ocp_phc_info(struct ptp_ocp *bp) 2484065efcc5SJonathan Lemon { 2485065efcc5SJonathan Lemon struct timespec64 ts; 2486065efcc5SJonathan Lemon u32 version, select; 2487065efcc5SJonathan Lemon bool sync; 2488065efcc5SJonathan Lemon 2489065efcc5SJonathan Lemon version = ioread32(&bp->reg->version); 2490065efcc5SJonathan Lemon select = ioread32(&bp->reg->select); 2491065efcc5SJonathan Lemon dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n", 2492065efcc5SJonathan Lemon version >> 24, (version >> 16) & 0xff, version & 0xffff, 2493065efcc5SJonathan Lemon ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16), 2494065efcc5SJonathan Lemon ptp_clock_index(bp->ptp)); 2495065efcc5SJonathan Lemon 2496065efcc5SJonathan Lemon sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC; 2497065efcc5SJonathan Lemon if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL)) 2498065efcc5SJonathan Lemon dev_info(&bp->pdev->dev, "Time: %lld.%ld, %s\n", 2499065efcc5SJonathan Lemon ts.tv_sec, ts.tv_nsec, 2500065efcc5SJonathan Lemon sync ? "in-sync" : "UNSYNCED"); 2501065efcc5SJonathan Lemon } 2502065efcc5SJonathan Lemon 2503065efcc5SJonathan Lemon static void 2504065efcc5SJonathan Lemon ptp_ocp_serial_info(struct device *dev, const char *name, int port, int baud) 2505065efcc5SJonathan Lemon { 2506065efcc5SJonathan Lemon if (port != -1) 2507065efcc5SJonathan Lemon dev_info(dev, "%5s: /dev/ttyS%-2d @ %6d\n", name, port, baud); 2508065efcc5SJonathan Lemon } 2509065efcc5SJonathan Lemon 2510065efcc5SJonathan Lemon static void 2511065efcc5SJonathan Lemon ptp_ocp_info(struct ptp_ocp *bp) 2512773bda96SJonathan Lemon { 2513e3516bb4SJonathan Lemon static int nmea_baud[] = { 2514e3516bb4SJonathan Lemon 1200, 2400, 4800, 9600, 19200, 38400, 2515e3516bb4SJonathan Lemon 57600, 115200, 230400, 460800, 921600, 2516e3516bb4SJonathan Lemon 1000000, 2000000 2517e3516bb4SJonathan Lemon }; 2518773bda96SJonathan Lemon struct device *dev = &bp->pdev->dev; 2519e3516bb4SJonathan Lemon u32 reg; 2520773bda96SJonathan Lemon 2521065efcc5SJonathan Lemon ptp_ocp_phc_info(bp); 2522065efcc5SJonathan Lemon 2523773bda96SJonathan Lemon if (bp->image) { 2524773bda96SJonathan Lemon u32 ver = ioread32(&bp->image->version); 2525773bda96SJonathan Lemon 2526773bda96SJonathan Lemon dev_info(dev, "version %x\n", ver); 2527773bda96SJonathan Lemon if (ver & 0xffff) 2528773bda96SJonathan Lemon dev_info(dev, "regular image, version %d\n", 2529773bda96SJonathan Lemon ver & 0xffff); 2530773bda96SJonathan Lemon else 2531773bda96SJonathan Lemon dev_info(dev, "golden image, version %d\n", 2532773bda96SJonathan Lemon ver >> 16); 2533773bda96SJonathan Lemon } 2534065efcc5SJonathan Lemon ptp_ocp_serial_info(dev, "GNSS", bp->gnss_port, 115200); 253571d7e085SJonathan Lemon ptp_ocp_serial_info(dev, "GNSS2", bp->gnss2_port, 115200); 2536065efcc5SJonathan Lemon ptp_ocp_serial_info(dev, "MAC", bp->mac_port, 57600); 2537e3516bb4SJonathan Lemon if (bp->nmea_out && bp->nmea_port != -1) { 2538e3516bb4SJonathan Lemon int baud = -1; 2539e3516bb4SJonathan Lemon 2540e3516bb4SJonathan Lemon reg = ioread32(&bp->nmea_out->uart_baud); 2541e3516bb4SJonathan Lemon if (reg < ARRAY_SIZE(nmea_baud)) 2542e3516bb4SJonathan Lemon baud = nmea_baud[reg]; 2543e3516bb4SJonathan Lemon ptp_ocp_serial_info(dev, "NMEA", bp->nmea_port, baud); 2544e3516bb4SJonathan Lemon } 2545773bda96SJonathan Lemon } 2546773bda96SJonathan Lemon 2547773bda96SJonathan Lemon static void 2548773bda96SJonathan Lemon ptp_ocp_detach_sysfs(struct ptp_ocp *bp) 2549773bda96SJonathan Lemon { 2550773bda96SJonathan Lemon struct device *dev = &bp->dev; 2551773bda96SJonathan Lemon 2552ef0cfb34SJonathan Lemon sysfs_remove_link(&dev->kobj, "ttyGNSS"); 2553773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "ttyMAC"); 2554773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "ptp"); 2555773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "pps"); 2556773bda96SJonathan Lemon device_remove_groups(dev, timecard_groups); 2557773bda96SJonathan Lemon } 2558773bda96SJonathan Lemon 2559773bda96SJonathan Lemon static void 2560773bda96SJonathan Lemon ptp_ocp_detach(struct ptp_ocp *bp) 2561773bda96SJonathan Lemon { 2562f67bf662SJonathan Lemon ptp_ocp_debugfs_remove_device(bp); 2563773bda96SJonathan Lemon ptp_ocp_detach_sysfs(bp); 2564773bda96SJonathan Lemon if (timer_pending(&bp->watchdog)) 2565773bda96SJonathan Lemon del_timer_sync(&bp->watchdog); 2566773bda96SJonathan Lemon if (bp->ts0) 2567773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->ts0); 2568773bda96SJonathan Lemon if (bp->ts1) 2569773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->ts1); 2570dcf61469SJonathan Lemon if (bp->ts2) 2571dcf61469SJonathan Lemon ptp_ocp_unregister_ext(bp->ts2); 2572773bda96SJonathan Lemon if (bp->pps) 2573773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->pps); 2574ef0cfb34SJonathan Lemon if (bp->gnss_port != -1) 2575ef0cfb34SJonathan Lemon serial8250_unregister_port(bp->gnss_port); 257671d7e085SJonathan Lemon if (bp->gnss2_port != -1) 257771d7e085SJonathan Lemon serial8250_unregister_port(bp->gnss2_port); 2578773bda96SJonathan Lemon if (bp->mac_port != -1) 2579773bda96SJonathan Lemon serial8250_unregister_port(bp->mac_port); 2580e3516bb4SJonathan Lemon if (bp->nmea_port != -1) 2581e3516bb4SJonathan Lemon serial8250_unregister_port(bp->nmea_port); 2582773bda96SJonathan Lemon if (bp->spi_flash) 2583773bda96SJonathan Lemon platform_device_unregister(bp->spi_flash); 2584773bda96SJonathan Lemon if (bp->i2c_ctrl) 2585773bda96SJonathan Lemon platform_device_unregister(bp->i2c_ctrl); 2586773bda96SJonathan Lemon if (bp->i2c_clk) 2587773bda96SJonathan Lemon clk_hw_unregister_fixed_rate(bp->i2c_clk); 2588773bda96SJonathan Lemon if (bp->n_irqs) 2589773bda96SJonathan Lemon pci_free_irq_vectors(bp->pdev); 2590773bda96SJonathan Lemon if (bp->ptp) 2591773bda96SJonathan Lemon ptp_clock_unregister(bp->ptp); 2592773bda96SJonathan Lemon device_unregister(&bp->dev); 2593773bda96SJonathan Lemon } 2594773bda96SJonathan Lemon 2595a7e1abadSJonathan Lemon static int 2596a7e1abadSJonathan Lemon ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id) 2597a7e1abadSJonathan Lemon { 2598773bda96SJonathan Lemon struct devlink *devlink; 2599a7e1abadSJonathan Lemon struct ptp_ocp *bp; 2600a7e1abadSJonathan Lemon int err; 2601a7e1abadSJonathan Lemon 2602919d13a7SLeon Romanovsky devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp), &pdev->dev); 2603773bda96SJonathan Lemon if (!devlink) { 2604773bda96SJonathan Lemon dev_err(&pdev->dev, "devlink_alloc failed\n"); 2605a7e1abadSJonathan Lemon return -ENOMEM; 2606773bda96SJonathan Lemon } 2607773bda96SJonathan Lemon 2608a7e1abadSJonathan Lemon err = pci_enable_device(pdev); 2609a7e1abadSJonathan Lemon if (err) { 2610a7e1abadSJonathan Lemon dev_err(&pdev->dev, "pci_enable_device\n"); 2611*4587369bSJonathan Lemon goto out_free; 2612a7e1abadSJonathan Lemon } 2613a7e1abadSJonathan Lemon 2614773bda96SJonathan Lemon bp = devlink_priv(devlink); 2615773bda96SJonathan Lemon err = ptp_ocp_device_init(bp, pdev); 2616773bda96SJonathan Lemon if (err) 2617d9fdbf13SJonathan Lemon goto out_disable; 2618a7e1abadSJonathan Lemon 2619773bda96SJonathan Lemon /* compat mode. 2620773bda96SJonathan Lemon * Older FPGA firmware only returns 2 irq's. 2621773bda96SJonathan Lemon * allow this - if not all of the IRQ's are returned, skip the 2622773bda96SJonathan Lemon * extra devices and just register the clock. 2623773bda96SJonathan Lemon */ 2624e3516bb4SJonathan Lemon err = pci_alloc_irq_vectors(pdev, 1, 11, PCI_IRQ_MSI | PCI_IRQ_MSIX); 2625773bda96SJonathan Lemon if (err < 0) { 2626773bda96SJonathan Lemon dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err); 2627773bda96SJonathan Lemon goto out; 2628a7e1abadSJonathan Lemon } 2629773bda96SJonathan Lemon bp->n_irqs = err; 2630773bda96SJonathan Lemon pci_set_master(pdev); 2631a7e1abadSJonathan Lemon 2632773bda96SJonathan Lemon err = ptp_ocp_register_resources(bp, id->driver_data); 2633a7e1abadSJonathan Lemon if (err) 2634a7e1abadSJonathan Lemon goto out; 2635a7e1abadSJonathan Lemon 2636a7e1abadSJonathan Lemon bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev); 2637a7e1abadSJonathan Lemon if (IS_ERR(bp->ptp)) { 2638a7e1abadSJonathan Lemon err = PTR_ERR(bp->ptp); 2639773bda96SJonathan Lemon dev_err(&pdev->dev, "ptp_clock_register: %d\n", err); 2640773bda96SJonathan Lemon bp->ptp = NULL; 2641a7e1abadSJonathan Lemon goto out; 2642a7e1abadSJonathan Lemon } 2643a7e1abadSJonathan Lemon 2644773bda96SJonathan Lemon err = ptp_ocp_complete(bp); 2645773bda96SJonathan Lemon if (err) 2646773bda96SJonathan Lemon goto out; 2647773bda96SJonathan Lemon 2648a7e1abadSJonathan Lemon ptp_ocp_info(bp); 2649c89f78e9SLeon Romanovsky devlink_register(devlink); 2650a7e1abadSJonathan Lemon return 0; 2651a7e1abadSJonathan Lemon 2652a7e1abadSJonathan Lemon out: 2653773bda96SJonathan Lemon ptp_ocp_detach(bp); 2654773bda96SJonathan Lemon pci_set_drvdata(pdev, NULL); 2655d9fdbf13SJonathan Lemon out_disable: 2656d9fdbf13SJonathan Lemon pci_disable_device(pdev); 2657*4587369bSJonathan Lemon out_free: 2658773bda96SJonathan Lemon devlink_free(devlink); 2659a7e1abadSJonathan Lemon return err; 2660a7e1abadSJonathan Lemon } 2661a7e1abadSJonathan Lemon 2662a7e1abadSJonathan Lemon static void 2663a7e1abadSJonathan Lemon ptp_ocp_remove(struct pci_dev *pdev) 2664a7e1abadSJonathan Lemon { 2665a7e1abadSJonathan Lemon struct ptp_ocp *bp = pci_get_drvdata(pdev); 2666773bda96SJonathan Lemon struct devlink *devlink = priv_to_devlink(bp); 2667a7e1abadSJonathan Lemon 2668c89f78e9SLeon Romanovsky devlink_unregister(devlink); 2669773bda96SJonathan Lemon ptp_ocp_detach(bp); 2670a7e1abadSJonathan Lemon pci_set_drvdata(pdev, NULL); 2671d9fdbf13SJonathan Lemon pci_disable_device(pdev); 2672773bda96SJonathan Lemon 2673773bda96SJonathan Lemon devlink_free(devlink); 2674a7e1abadSJonathan Lemon } 2675a7e1abadSJonathan Lemon 2676a7e1abadSJonathan Lemon static struct pci_driver ptp_ocp_driver = { 2677a7e1abadSJonathan Lemon .name = KBUILD_MODNAME, 2678a7e1abadSJonathan Lemon .id_table = ptp_ocp_pcidev_id, 2679a7e1abadSJonathan Lemon .probe = ptp_ocp_probe, 2680a7e1abadSJonathan Lemon .remove = ptp_ocp_remove, 2681a7e1abadSJonathan Lemon }; 2682a7e1abadSJonathan Lemon 2683773bda96SJonathan Lemon static int 2684773bda96SJonathan Lemon ptp_ocp_i2c_notifier_call(struct notifier_block *nb, 2685773bda96SJonathan Lemon unsigned long action, void *data) 2686773bda96SJonathan Lemon { 2687773bda96SJonathan Lemon struct device *dev, *child = data; 2688773bda96SJonathan Lemon struct ptp_ocp *bp; 2689773bda96SJonathan Lemon bool add; 2690773bda96SJonathan Lemon 2691773bda96SJonathan Lemon switch (action) { 2692773bda96SJonathan Lemon case BUS_NOTIFY_ADD_DEVICE: 2693773bda96SJonathan Lemon case BUS_NOTIFY_DEL_DEVICE: 2694773bda96SJonathan Lemon add = action == BUS_NOTIFY_ADD_DEVICE; 2695773bda96SJonathan Lemon break; 2696773bda96SJonathan Lemon default: 2697773bda96SJonathan Lemon return 0; 2698773bda96SJonathan Lemon } 2699773bda96SJonathan Lemon 2700773bda96SJonathan Lemon if (!i2c_verify_adapter(child)) 2701773bda96SJonathan Lemon return 0; 2702773bda96SJonathan Lemon 2703773bda96SJonathan Lemon dev = child; 2704773bda96SJonathan Lemon while ((dev = dev->parent)) 2705773bda96SJonathan Lemon if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME)) 2706773bda96SJonathan Lemon goto found; 2707773bda96SJonathan Lemon return 0; 2708773bda96SJonathan Lemon 2709773bda96SJonathan Lemon found: 2710773bda96SJonathan Lemon bp = dev_get_drvdata(dev); 2711773bda96SJonathan Lemon if (add) 2712773bda96SJonathan Lemon ptp_ocp_symlink(bp, child, "i2c"); 2713773bda96SJonathan Lemon else 2714773bda96SJonathan Lemon sysfs_remove_link(&bp->dev.kobj, "i2c"); 2715773bda96SJonathan Lemon 2716773bda96SJonathan Lemon return 0; 2717773bda96SJonathan Lemon } 2718773bda96SJonathan Lemon 2719773bda96SJonathan Lemon static struct notifier_block ptp_ocp_i2c_notifier = { 2720773bda96SJonathan Lemon .notifier_call = ptp_ocp_i2c_notifier_call, 2721773bda96SJonathan Lemon }; 2722773bda96SJonathan Lemon 2723a7e1abadSJonathan Lemon static int __init 2724a7e1abadSJonathan Lemon ptp_ocp_init(void) 2725a7e1abadSJonathan Lemon { 2726773bda96SJonathan Lemon const char *what; 2727a7e1abadSJonathan Lemon int err; 2728a7e1abadSJonathan Lemon 2729f67bf662SJonathan Lemon ptp_ocp_debugfs_init(); 2730f67bf662SJonathan Lemon 2731773bda96SJonathan Lemon what = "timecard class"; 2732773bda96SJonathan Lemon err = class_register(&timecard_class); 2733773bda96SJonathan Lemon if (err) 2734773bda96SJonathan Lemon goto out; 2735773bda96SJonathan Lemon 2736773bda96SJonathan Lemon what = "i2c notifier"; 2737773bda96SJonathan Lemon err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2738773bda96SJonathan Lemon if (err) 2739773bda96SJonathan Lemon goto out_notifier; 2740773bda96SJonathan Lemon 2741773bda96SJonathan Lemon what = "ptp_ocp driver"; 2742a7e1abadSJonathan Lemon err = pci_register_driver(&ptp_ocp_driver); 2743773bda96SJonathan Lemon if (err) 2744773bda96SJonathan Lemon goto out_register; 2745773bda96SJonathan Lemon 2746773bda96SJonathan Lemon return 0; 2747773bda96SJonathan Lemon 2748773bda96SJonathan Lemon out_register: 2749773bda96SJonathan Lemon bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2750773bda96SJonathan Lemon out_notifier: 2751773bda96SJonathan Lemon class_unregister(&timecard_class); 2752773bda96SJonathan Lemon out: 2753f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(); 2754773bda96SJonathan Lemon pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err); 2755a7e1abadSJonathan Lemon return err; 2756a7e1abadSJonathan Lemon } 2757a7e1abadSJonathan Lemon 2758a7e1abadSJonathan Lemon static void __exit 2759a7e1abadSJonathan Lemon ptp_ocp_fini(void) 2760a7e1abadSJonathan Lemon { 2761773bda96SJonathan Lemon bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2762a7e1abadSJonathan Lemon pci_unregister_driver(&ptp_ocp_driver); 2763773bda96SJonathan Lemon class_unregister(&timecard_class); 2764f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(); 2765a7e1abadSJonathan Lemon } 2766a7e1abadSJonathan Lemon 2767a7e1abadSJonathan Lemon module_init(ptp_ocp_init); 2768a7e1abadSJonathan Lemon module_exit(ptp_ocp_fini); 2769a7e1abadSJonathan Lemon 2770a7e1abadSJonathan Lemon MODULE_DESCRIPTION("OpenCompute TimeCard driver"); 2771a7e1abadSJonathan Lemon MODULE_LICENSE("GPL v2"); 2772