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> 7*f67bf662SJonathan 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; 55a7e1abadSJonathan Lemon }; 56a7e1abadSJonathan Lemon 57a7e1abadSJonathan Lemon #define OCP_CTRL_ENABLE BIT(0) 58a7e1abadSJonathan Lemon #define OCP_CTRL_ADJUST_TIME BIT(1) 59a7e1abadSJonathan Lemon #define OCP_CTRL_ADJUST_OFFSET BIT(2) 60773bda96SJonathan Lemon #define OCP_CTRL_ADJUST_DRIFT BIT(3) 61773bda96SJonathan Lemon #define OCP_CTRL_ADJUST_SERVO BIT(8) 62a7e1abadSJonathan Lemon #define OCP_CTRL_READ_TIME_REQ BIT(30) 63a7e1abadSJonathan Lemon #define OCP_CTRL_READ_TIME_DONE BIT(31) 64a7e1abadSJonathan Lemon 65a7e1abadSJonathan Lemon #define OCP_STATUS_IN_SYNC BIT(0) 66773bda96SJonathan Lemon #define OCP_STATUS_IN_HOLDOVER BIT(1) 67a7e1abadSJonathan Lemon 68a7e1abadSJonathan Lemon #define OCP_SELECT_CLK_NONE 0 69773bda96SJonathan Lemon #define OCP_SELECT_CLK_REG 0xfe 70a7e1abadSJonathan Lemon 71a7e1abadSJonathan Lemon struct tod_reg { 72a7e1abadSJonathan Lemon u32 ctrl; 73a7e1abadSJonathan Lemon u32 status; 74a7e1abadSJonathan Lemon u32 uart_polarity; 75a7e1abadSJonathan Lemon u32 version; 76065efcc5SJonathan Lemon u32 adj_sec; 77a7e1abadSJonathan Lemon u32 __pad0[3]; 78a7e1abadSJonathan Lemon u32 uart_baud; 79a7e1abadSJonathan Lemon u32 __pad1[3]; 80a7e1abadSJonathan Lemon u32 utc_status; 81a7e1abadSJonathan Lemon u32 leap; 82a7e1abadSJonathan Lemon }; 83a7e1abadSJonathan Lemon 84a7e1abadSJonathan Lemon #define TOD_CTRL_PROTOCOL BIT(28) 85a7e1abadSJonathan Lemon #define TOD_CTRL_DISABLE_FMT_A BIT(17) 86a7e1abadSJonathan Lemon #define TOD_CTRL_DISABLE_FMT_B BIT(16) 87a7e1abadSJonathan Lemon #define TOD_CTRL_ENABLE BIT(0) 88a7e1abadSJonathan Lemon #define TOD_CTRL_GNSS_MASK ((1U << 4) - 1) 89a7e1abadSJonathan Lemon #define TOD_CTRL_GNSS_SHIFT 24 90a7e1abadSJonathan Lemon 91a7e1abadSJonathan Lemon #define TOD_STATUS_UTC_MASK 0xff 92a7e1abadSJonathan Lemon #define TOD_STATUS_UTC_VALID BIT(8) 93a7e1abadSJonathan Lemon #define TOD_STATUS_LEAP_VALID BIT(16) 94a7e1abadSJonathan Lemon 95773bda96SJonathan Lemon struct ts_reg { 96773bda96SJonathan Lemon u32 enable; 97773bda96SJonathan Lemon u32 error; 98773bda96SJonathan Lemon u32 polarity; 99773bda96SJonathan Lemon u32 version; 100773bda96SJonathan Lemon u32 __pad0[4]; 101773bda96SJonathan Lemon u32 cable_delay; 102773bda96SJonathan Lemon u32 __pad1[3]; 103773bda96SJonathan Lemon u32 intr; 104773bda96SJonathan Lemon u32 intr_mask; 105773bda96SJonathan Lemon u32 event_count; 106773bda96SJonathan Lemon u32 __pad2[1]; 107773bda96SJonathan Lemon u32 ts_count; 108773bda96SJonathan Lemon u32 time_ns; 109773bda96SJonathan Lemon u32 time_sec; 110773bda96SJonathan Lemon u32 data_width; 111773bda96SJonathan Lemon u32 data; 112773bda96SJonathan Lemon }; 113773bda96SJonathan Lemon 114773bda96SJonathan Lemon struct pps_reg { 115773bda96SJonathan Lemon u32 ctrl; 116773bda96SJonathan Lemon u32 status; 1170d43d4f2SJonathan Lemon u32 __pad0[6]; 1180d43d4f2SJonathan Lemon u32 cable_delay; 119773bda96SJonathan Lemon }; 120773bda96SJonathan Lemon 121773bda96SJonathan Lemon #define PPS_STATUS_FILTER_ERR BIT(0) 122773bda96SJonathan Lemon #define PPS_STATUS_SUPERV_ERR BIT(1) 123773bda96SJonathan Lemon 124773bda96SJonathan Lemon struct img_reg { 125773bda96SJonathan Lemon u32 version; 126773bda96SJonathan Lemon }; 127773bda96SJonathan Lemon 128e1daf0ecSJonathan Lemon struct gpio_reg { 129e1daf0ecSJonathan Lemon u32 gpio1; 130e1daf0ecSJonathan Lemon u32 __pad0; 131e1daf0ecSJonathan Lemon u32 gpio2; 132e1daf0ecSJonathan Lemon u32 __pad1; 133e1daf0ecSJonathan Lemon }; 134e1daf0ecSJonathan Lemon 1356baf2925SJonathan Lemon struct irig_master_reg { 1366baf2925SJonathan Lemon u32 ctrl; 1376baf2925SJonathan Lemon u32 status; 1386baf2925SJonathan Lemon u32 __pad0; 1396baf2925SJonathan Lemon u32 version; 1406baf2925SJonathan Lemon u32 adj_sec; 1416baf2925SJonathan Lemon u32 mode_ctrl; 1426baf2925SJonathan Lemon }; 1436baf2925SJonathan Lemon 1446baf2925SJonathan Lemon #define IRIG_M_CTRL_ENABLE BIT(0) 1456baf2925SJonathan Lemon 1466baf2925SJonathan Lemon struct irig_slave_reg { 1476baf2925SJonathan Lemon u32 ctrl; 1486baf2925SJonathan Lemon u32 status; 1496baf2925SJonathan Lemon u32 __pad0; 1506baf2925SJonathan Lemon u32 version; 1516baf2925SJonathan Lemon u32 adj_sec; 1526baf2925SJonathan Lemon u32 mode_ctrl; 1536baf2925SJonathan Lemon }; 1546baf2925SJonathan Lemon 1556baf2925SJonathan Lemon #define IRIG_S_CTRL_ENABLE BIT(0) 1566baf2925SJonathan Lemon 1576baf2925SJonathan Lemon struct dcf_master_reg { 1586baf2925SJonathan Lemon u32 ctrl; 1596baf2925SJonathan Lemon u32 status; 1606baf2925SJonathan Lemon u32 __pad0; 1616baf2925SJonathan Lemon u32 version; 1626baf2925SJonathan Lemon u32 adj_sec; 1636baf2925SJonathan Lemon }; 1646baf2925SJonathan Lemon 1656baf2925SJonathan Lemon #define DCF_M_CTRL_ENABLE BIT(0) 1666baf2925SJonathan Lemon 1676baf2925SJonathan Lemon struct dcf_slave_reg { 1686baf2925SJonathan Lemon u32 ctrl; 1696baf2925SJonathan Lemon u32 status; 1706baf2925SJonathan Lemon u32 __pad0; 1716baf2925SJonathan Lemon u32 version; 1726baf2925SJonathan Lemon u32 adj_sec; 1736baf2925SJonathan Lemon }; 1746baf2925SJonathan Lemon 1756baf2925SJonathan Lemon #define DCF_S_CTRL_ENABLE BIT(0) 1766baf2925SJonathan Lemon 177773bda96SJonathan Lemon struct ptp_ocp_flash_info { 178773bda96SJonathan Lemon const char *name; 179773bda96SJonathan Lemon int pci_offset; 180773bda96SJonathan Lemon int data_size; 181773bda96SJonathan Lemon void *data; 182773bda96SJonathan Lemon }; 183773bda96SJonathan Lemon 1841618df6aSJonathan Lemon struct ptp_ocp_i2c_info { 1851618df6aSJonathan Lemon const char *name; 1861618df6aSJonathan Lemon unsigned long fixed_rate; 1871618df6aSJonathan Lemon size_t data_size; 1881618df6aSJonathan Lemon void *data; 1891618df6aSJonathan Lemon }; 1901618df6aSJonathan Lemon 191773bda96SJonathan Lemon struct ptp_ocp_ext_info { 192773bda96SJonathan Lemon int index; 193773bda96SJonathan Lemon irqreturn_t (*irq_fcn)(int irq, void *priv); 194773bda96SJonathan Lemon int (*enable)(void *priv, bool enable); 195773bda96SJonathan Lemon }; 196773bda96SJonathan Lemon 197773bda96SJonathan Lemon struct ptp_ocp_ext_src { 198773bda96SJonathan Lemon void __iomem *mem; 199773bda96SJonathan Lemon struct ptp_ocp *bp; 200773bda96SJonathan Lemon struct ptp_ocp_ext_info *info; 201773bda96SJonathan Lemon int irq_vec; 202773bda96SJonathan Lemon }; 203773bda96SJonathan Lemon 204a7e1abadSJonathan Lemon struct ptp_ocp { 205a7e1abadSJonathan Lemon struct pci_dev *pdev; 206773bda96SJonathan Lemon struct device dev; 207a7e1abadSJonathan Lemon spinlock_t lock; 208a7e1abadSJonathan Lemon struct ocp_reg __iomem *reg; 209a7e1abadSJonathan Lemon struct tod_reg __iomem *tod; 2100d43d4f2SJonathan Lemon struct pps_reg __iomem *pps_to_ext; 2110d43d4f2SJonathan Lemon struct pps_reg __iomem *pps_to_clk; 212*f67bf662SJonathan Lemon struct gpio_reg __iomem *pps_select; 213e1daf0ecSJonathan Lemon struct gpio_reg __iomem *sma; 2146baf2925SJonathan Lemon struct irig_master_reg __iomem *irig_out; 2156baf2925SJonathan Lemon struct irig_slave_reg __iomem *irig_in; 2166baf2925SJonathan Lemon struct dcf_master_reg __iomem *dcf_out; 2176baf2925SJonathan Lemon struct dcf_slave_reg __iomem *dcf_in; 218773bda96SJonathan Lemon struct ptp_ocp_ext_src *pps; 219773bda96SJonathan Lemon struct ptp_ocp_ext_src *ts0; 220773bda96SJonathan Lemon struct ptp_ocp_ext_src *ts1; 221dcf61469SJonathan Lemon struct ptp_ocp_ext_src *ts2; 222773bda96SJonathan Lemon struct img_reg __iomem *image; 223a7e1abadSJonathan Lemon struct ptp_clock *ptp; 224a7e1abadSJonathan Lemon struct ptp_clock_info ptp_info; 225773bda96SJonathan Lemon struct platform_device *i2c_ctrl; 226773bda96SJonathan Lemon struct platform_device *spi_flash; 227773bda96SJonathan Lemon struct clk_hw *i2c_clk; 228773bda96SJonathan Lemon struct timer_list watchdog; 229*f67bf662SJonathan Lemon struct dentry *debug_root; 230ef0cfb34SJonathan Lemon time64_t gnss_lost; 231773bda96SJonathan Lemon int id; 232773bda96SJonathan Lemon int n_irqs; 233ef0cfb34SJonathan Lemon int gnss_port; 234773bda96SJonathan Lemon int mac_port; /* miniature atomic clock */ 235773bda96SJonathan Lemon u8 serial[6]; 236773bda96SJonathan Lemon bool has_serial; 23789260d87SJonathan Lemon int flash_start; 23889260d87SJonathan Lemon u32 utc_tai_offset; 239a7e1abadSJonathan Lemon }; 240a7e1abadSJonathan Lemon 241773bda96SJonathan Lemon struct ocp_resource { 242773bda96SJonathan Lemon unsigned long offset; 243773bda96SJonathan Lemon int size; 244773bda96SJonathan Lemon int irq_vec; 245773bda96SJonathan Lemon int (*setup)(struct ptp_ocp *bp, struct ocp_resource *r); 246773bda96SJonathan Lemon void *extra; 247773bda96SJonathan Lemon unsigned long bp_offset; 24856ec4403SJonathan Lemon const char * const name; 249773bda96SJonathan Lemon }; 250773bda96SJonathan Lemon 251773bda96SJonathan Lemon static int ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r); 252773bda96SJonathan Lemon static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r); 253773bda96SJonathan Lemon static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r); 254773bda96SJonathan Lemon static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r); 255773bda96SJonathan Lemon static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r); 256773bda96SJonathan Lemon static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r); 257773bda96SJonathan Lemon static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv); 258773bda96SJonathan Lemon static int ptp_ocp_ts_enable(void *priv, bool enable); 259773bda96SJonathan Lemon 260773bda96SJonathan Lemon #define bp_assign_entry(bp, res, val) ({ \ 261773bda96SJonathan Lemon uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset; \ 262773bda96SJonathan Lemon *(typeof(val) *)addr = val; \ 263773bda96SJonathan Lemon }) 264773bda96SJonathan Lemon 265773bda96SJonathan Lemon #define OCP_RES_LOCATION(member) \ 26656ec4403SJonathan Lemon .name = #member, .bp_offset = offsetof(struct ptp_ocp, member) 267773bda96SJonathan Lemon 268773bda96SJonathan Lemon #define OCP_MEM_RESOURCE(member) \ 269773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_mem 270773bda96SJonathan Lemon 271773bda96SJonathan Lemon #define OCP_SERIAL_RESOURCE(member) \ 272773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_serial 273773bda96SJonathan Lemon 274773bda96SJonathan Lemon #define OCP_I2C_RESOURCE(member) \ 275773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_i2c 276773bda96SJonathan Lemon 277773bda96SJonathan Lemon #define OCP_SPI_RESOURCE(member) \ 278773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_spi 279773bda96SJonathan Lemon 280773bda96SJonathan Lemon #define OCP_EXT_RESOURCE(member) \ 281773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_ext 282773bda96SJonathan Lemon 283773bda96SJonathan Lemon /* This is the MSI vector mapping used. 284773bda96SJonathan Lemon * 0: N/C 285773bda96SJonathan Lemon * 1: TS0 286773bda96SJonathan Lemon * 2: TS1 287773bda96SJonathan Lemon * 3: GPS 288773bda96SJonathan Lemon * 4: GPS2 (n/c) 289773bda96SJonathan Lemon * 5: MAC 290dcf61469SJonathan Lemon * 6: TS2 2911447149dSJonathan Lemon * 7: I2C controller 292773bda96SJonathan Lemon * 8: HWICAP 293773bda96SJonathan Lemon * 9: SPI Flash 294773bda96SJonathan Lemon */ 295773bda96SJonathan Lemon 296773bda96SJonathan Lemon static struct ocp_resource ocp_fb_resource[] = { 297773bda96SJonathan Lemon { 298773bda96SJonathan Lemon OCP_MEM_RESOURCE(reg), 299773bda96SJonathan Lemon .offset = 0x01000000, .size = 0x10000, 300773bda96SJonathan Lemon }, 301773bda96SJonathan Lemon { 302773bda96SJonathan Lemon OCP_EXT_RESOURCE(ts0), 303773bda96SJonathan Lemon .offset = 0x01010000, .size = 0x10000, .irq_vec = 1, 304773bda96SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 30556ec4403SJonathan Lemon .index = 0, 306773bda96SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 307773bda96SJonathan Lemon .enable = ptp_ocp_ts_enable, 308773bda96SJonathan Lemon }, 309773bda96SJonathan Lemon }, 310773bda96SJonathan Lemon { 311773bda96SJonathan Lemon OCP_EXT_RESOURCE(ts1), 312773bda96SJonathan Lemon .offset = 0x01020000, .size = 0x10000, .irq_vec = 2, 313773bda96SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 31456ec4403SJonathan Lemon .index = 1, 315773bda96SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 316773bda96SJonathan Lemon .enable = ptp_ocp_ts_enable, 317773bda96SJonathan Lemon }, 318773bda96SJonathan Lemon }, 319773bda96SJonathan Lemon { 320dcf61469SJonathan Lemon OCP_EXT_RESOURCE(ts2), 321dcf61469SJonathan Lemon .offset = 0x01060000, .size = 0x10000, .irq_vec = 6, 322dcf61469SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 323dcf61469SJonathan Lemon .index = 2, 324dcf61469SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 325dcf61469SJonathan Lemon .enable = ptp_ocp_ts_enable, 326dcf61469SJonathan Lemon }, 327dcf61469SJonathan Lemon }, 328dcf61469SJonathan Lemon { 3290d43d4f2SJonathan Lemon OCP_MEM_RESOURCE(pps_to_ext), 3300d43d4f2SJonathan Lemon .offset = 0x01030000, .size = 0x10000, 3310d43d4f2SJonathan Lemon }, 3320d43d4f2SJonathan Lemon { 3330d43d4f2SJonathan Lemon OCP_MEM_RESOURCE(pps_to_clk), 334773bda96SJonathan Lemon .offset = 0x01040000, .size = 0x10000, 335773bda96SJonathan Lemon }, 336773bda96SJonathan Lemon { 337773bda96SJonathan Lemon OCP_MEM_RESOURCE(tod), 338773bda96SJonathan Lemon .offset = 0x01050000, .size = 0x10000, 339773bda96SJonathan Lemon }, 340773bda96SJonathan Lemon { 3416baf2925SJonathan Lemon OCP_MEM_RESOURCE(irig_in), 3426baf2925SJonathan Lemon .offset = 0x01070000, .size = 0x10000, 3436baf2925SJonathan Lemon }, 3446baf2925SJonathan Lemon { 3456baf2925SJonathan Lemon OCP_MEM_RESOURCE(irig_out), 3466baf2925SJonathan Lemon .offset = 0x01080000, .size = 0x10000, 3476baf2925SJonathan Lemon }, 3486baf2925SJonathan Lemon { 3496baf2925SJonathan Lemon OCP_MEM_RESOURCE(dcf_in), 3506baf2925SJonathan Lemon .offset = 0x01090000, .size = 0x10000, 3516baf2925SJonathan Lemon }, 3526baf2925SJonathan Lemon { 3536baf2925SJonathan Lemon OCP_MEM_RESOURCE(dcf_out), 3546baf2925SJonathan Lemon .offset = 0x010A0000, .size = 0x10000, 3556baf2925SJonathan Lemon }, 3566baf2925SJonathan Lemon { 357773bda96SJonathan Lemon OCP_MEM_RESOURCE(image), 358773bda96SJonathan Lemon .offset = 0x00020000, .size = 0x1000, 359773bda96SJonathan Lemon }, 360773bda96SJonathan Lemon { 361*f67bf662SJonathan Lemon OCP_MEM_RESOURCE(pps_select), 362*f67bf662SJonathan Lemon .offset = 0x00130000, .size = 0x1000, 363*f67bf662SJonathan Lemon }, 364*f67bf662SJonathan Lemon { 365e1daf0ecSJonathan Lemon OCP_MEM_RESOURCE(sma), 366e1daf0ecSJonathan Lemon .offset = 0x00140000, .size = 0x1000, 367e1daf0ecSJonathan Lemon }, 368e1daf0ecSJonathan Lemon { 369773bda96SJonathan Lemon OCP_I2C_RESOURCE(i2c_ctrl), 370773bda96SJonathan Lemon .offset = 0x00150000, .size = 0x10000, .irq_vec = 7, 3711618df6aSJonathan Lemon .extra = &(struct ptp_ocp_i2c_info) { 3721618df6aSJonathan Lemon .name = "xiic-i2c", 3731618df6aSJonathan Lemon .fixed_rate = 50000000, 3741618df6aSJonathan Lemon }, 375773bda96SJonathan Lemon }, 376773bda96SJonathan Lemon { 377ef0cfb34SJonathan Lemon OCP_SERIAL_RESOURCE(gnss_port), 378773bda96SJonathan Lemon .offset = 0x00160000 + 0x1000, .irq_vec = 3, 379773bda96SJonathan Lemon }, 380773bda96SJonathan Lemon { 381773bda96SJonathan Lemon OCP_SERIAL_RESOURCE(mac_port), 382773bda96SJonathan Lemon .offset = 0x00180000 + 0x1000, .irq_vec = 5, 383773bda96SJonathan Lemon }, 384773bda96SJonathan Lemon { 385773bda96SJonathan Lemon OCP_SPI_RESOURCE(spi_flash), 386773bda96SJonathan Lemon .offset = 0x00310000, .size = 0x10000, .irq_vec = 9, 387773bda96SJonathan Lemon .extra = &(struct ptp_ocp_flash_info) { 388773bda96SJonathan Lemon .name = "xilinx_spi", .pci_offset = 0, 389773bda96SJonathan Lemon .data_size = sizeof(struct xspi_platform_data), 390773bda96SJonathan Lemon .data = &(struct xspi_platform_data) { 391773bda96SJonathan Lemon .num_chipselect = 1, 392773bda96SJonathan Lemon .bits_per_word = 8, 393773bda96SJonathan Lemon .num_devices = 1, 394773bda96SJonathan Lemon .devices = &(struct spi_board_info) { 395773bda96SJonathan Lemon .modalias = "spi-nor", 396773bda96SJonathan Lemon }, 397773bda96SJonathan Lemon }, 398773bda96SJonathan Lemon }, 399773bda96SJonathan Lemon }, 400773bda96SJonathan Lemon { 401773bda96SJonathan Lemon .setup = ptp_ocp_fb_board_init, 402773bda96SJonathan Lemon }, 403773bda96SJonathan Lemon { } 404773bda96SJonathan Lemon }; 405773bda96SJonathan Lemon 406773bda96SJonathan Lemon static const struct pci_device_id ptp_ocp_pcidev_id[] = { 407773bda96SJonathan Lemon { PCI_DEVICE_DATA(FACEBOOK, TIMECARD, &ocp_fb_resource) }, 408773bda96SJonathan Lemon { 0 } 409773bda96SJonathan Lemon }; 410773bda96SJonathan Lemon MODULE_DEVICE_TABLE(pci, ptp_ocp_pcidev_id); 411773bda96SJonathan Lemon 412773bda96SJonathan Lemon static DEFINE_MUTEX(ptp_ocp_lock); 413773bda96SJonathan Lemon static DEFINE_IDR(ptp_ocp_idr); 414773bda96SJonathan Lemon 415e1daf0ecSJonathan Lemon struct ocp_selector { 416773bda96SJonathan Lemon const char *name; 417773bda96SJonathan Lemon int value; 418e1daf0ecSJonathan Lemon }; 419e1daf0ecSJonathan Lemon 420e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_clock[] = { 421773bda96SJonathan Lemon { .name = "NONE", .value = 0 }, 422773bda96SJonathan Lemon { .name = "TOD", .value = 1 }, 423773bda96SJonathan Lemon { .name = "IRIG", .value = 2 }, 424773bda96SJonathan Lemon { .name = "PPS", .value = 3 }, 425773bda96SJonathan Lemon { .name = "PTP", .value = 4 }, 426773bda96SJonathan Lemon { .name = "RTC", .value = 5 }, 427773bda96SJonathan Lemon { .name = "DCF", .value = 6 }, 428773bda96SJonathan Lemon { .name = "REGS", .value = 0xfe }, 429773bda96SJonathan Lemon { .name = "EXT", .value = 0xff }, 430e1daf0ecSJonathan Lemon { } 431e1daf0ecSJonathan Lemon }; 432e1daf0ecSJonathan Lemon 433e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_sma_in[] = { 434e1daf0ecSJonathan Lemon { .name = "10Mhz", .value = 0x00 }, 435e1daf0ecSJonathan Lemon { .name = "PPS1", .value = 0x01 }, 436e1daf0ecSJonathan Lemon { .name = "PPS2", .value = 0x02 }, 437e1daf0ecSJonathan Lemon { .name = "TS1", .value = 0x04 }, 438e1daf0ecSJonathan Lemon { .name = "TS2", .value = 0x08 }, 4396baf2925SJonathan Lemon { .name = "IRIG", .value = 0x10 }, 4406baf2925SJonathan Lemon { .name = "DCF", .value = 0x20 }, 441e1daf0ecSJonathan Lemon { } 442e1daf0ecSJonathan Lemon }; 443e1daf0ecSJonathan Lemon 444e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_sma_out[] = { 445e1daf0ecSJonathan Lemon { .name = "10Mhz", .value = 0x00 }, 446e1daf0ecSJonathan Lemon { .name = "PHC", .value = 0x01 }, 447e1daf0ecSJonathan Lemon { .name = "MAC", .value = 0x02 }, 448e1daf0ecSJonathan Lemon { .name = "GNSS", .value = 0x04 }, 449e1daf0ecSJonathan Lemon { .name = "GNSS2", .value = 0x08 }, 4506baf2925SJonathan Lemon { .name = "IRIG", .value = 0x10 }, 4516baf2925SJonathan Lemon { .name = "DCF", .value = 0x20 }, 452e1daf0ecSJonathan Lemon { } 453773bda96SJonathan Lemon }; 454773bda96SJonathan Lemon 455773bda96SJonathan Lemon static const char * 456e1daf0ecSJonathan Lemon ptp_ocp_select_name_from_val(struct ocp_selector *tbl, int val) 457773bda96SJonathan Lemon { 458773bda96SJonathan Lemon int i; 459773bda96SJonathan Lemon 460e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) 461e1daf0ecSJonathan Lemon if (tbl[i].value == val) 462e1daf0ecSJonathan Lemon return tbl[i].name; 463773bda96SJonathan Lemon return NULL; 464773bda96SJonathan Lemon } 465773bda96SJonathan Lemon 466773bda96SJonathan Lemon static int 467e1daf0ecSJonathan Lemon ptp_ocp_select_val_from_name(struct ocp_selector *tbl, const char *name) 468773bda96SJonathan Lemon { 469e1daf0ecSJonathan Lemon const char *select; 470773bda96SJonathan Lemon int i; 471773bda96SJonathan Lemon 472e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) { 473e1daf0ecSJonathan Lemon select = tbl[i].name; 474e1daf0ecSJonathan Lemon if (!strncasecmp(name, select, strlen(select))) 475e1daf0ecSJonathan Lemon return tbl[i].value; 476773bda96SJonathan Lemon } 477773bda96SJonathan Lemon return -EINVAL; 478773bda96SJonathan Lemon } 479773bda96SJonathan Lemon 480e1daf0ecSJonathan Lemon static ssize_t 481e1daf0ecSJonathan Lemon ptp_ocp_select_table_show(struct ocp_selector *tbl, char *buf) 482e1daf0ecSJonathan Lemon { 483e1daf0ecSJonathan Lemon ssize_t count; 484e1daf0ecSJonathan Lemon int i; 485e1daf0ecSJonathan Lemon 486e1daf0ecSJonathan Lemon count = 0; 487e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) 488e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", tbl[i].name); 489e1daf0ecSJonathan Lemon if (count) 490e1daf0ecSJonathan Lemon count--; 491e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "\n"); 492e1daf0ecSJonathan Lemon return count; 493e1daf0ecSJonathan Lemon } 494e1daf0ecSJonathan Lemon 495a7e1abadSJonathan Lemon static int 496a7e1abadSJonathan Lemon __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts, 497a7e1abadSJonathan Lemon struct ptp_system_timestamp *sts) 498a7e1abadSJonathan Lemon { 499a7e1abadSJonathan Lemon u32 ctrl, time_sec, time_ns; 500a7e1abadSJonathan Lemon int i; 501a7e1abadSJonathan Lemon 502a7e1abadSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 503a7e1abadSJonathan Lemon ctrl |= OCP_CTRL_READ_TIME_REQ; 504a7e1abadSJonathan Lemon 505a7e1abadSJonathan Lemon ptp_read_system_prets(sts); 506a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 507a7e1abadSJonathan Lemon 508a7e1abadSJonathan Lemon for (i = 0; i < 100; i++) { 509a7e1abadSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 510a7e1abadSJonathan Lemon if (ctrl & OCP_CTRL_READ_TIME_DONE) 511a7e1abadSJonathan Lemon break; 512a7e1abadSJonathan Lemon } 513a7e1abadSJonathan Lemon ptp_read_system_postts(sts); 514a7e1abadSJonathan Lemon 515a7e1abadSJonathan Lemon time_ns = ioread32(&bp->reg->time_ns); 516a7e1abadSJonathan Lemon time_sec = ioread32(&bp->reg->time_sec); 517a7e1abadSJonathan Lemon 518a7e1abadSJonathan Lemon ts->tv_sec = time_sec; 519a7e1abadSJonathan Lemon ts->tv_nsec = time_ns; 520a7e1abadSJonathan Lemon 521a7e1abadSJonathan Lemon return ctrl & OCP_CTRL_READ_TIME_DONE ? 0 : -ETIMEDOUT; 522a7e1abadSJonathan Lemon } 523a7e1abadSJonathan Lemon 524a7e1abadSJonathan Lemon static int 525a7e1abadSJonathan Lemon ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts, 526a7e1abadSJonathan Lemon struct ptp_system_timestamp *sts) 527a7e1abadSJonathan Lemon { 528a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 529a7e1abadSJonathan Lemon unsigned long flags; 530a7e1abadSJonathan Lemon int err; 531a7e1abadSJonathan Lemon 532a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 533a7e1abadSJonathan Lemon err = __ptp_ocp_gettime_locked(bp, ts, sts); 534a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 535a7e1abadSJonathan Lemon 536a7e1abadSJonathan Lemon return err; 537a7e1abadSJonathan Lemon } 538a7e1abadSJonathan Lemon 539a7e1abadSJonathan Lemon static void 540a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts) 541a7e1abadSJonathan Lemon { 542a7e1abadSJonathan Lemon u32 ctrl, time_sec, time_ns; 543a7e1abadSJonathan Lemon u32 select; 544a7e1abadSJonathan Lemon 545a7e1abadSJonathan Lemon time_ns = ts->tv_nsec; 546a7e1abadSJonathan Lemon time_sec = ts->tv_sec; 547a7e1abadSJonathan Lemon 548a7e1abadSJonathan Lemon select = ioread32(&bp->reg->select); 549a7e1abadSJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 550a7e1abadSJonathan Lemon 551a7e1abadSJonathan Lemon iowrite32(time_ns, &bp->reg->adjust_ns); 552a7e1abadSJonathan Lemon iowrite32(time_sec, &bp->reg->adjust_sec); 553a7e1abadSJonathan Lemon 554a7e1abadSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 555a7e1abadSJonathan Lemon ctrl |= OCP_CTRL_ADJUST_TIME; 556a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 557a7e1abadSJonathan Lemon 558a7e1abadSJonathan Lemon /* restore clock selection */ 559a7e1abadSJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 560a7e1abadSJonathan Lemon } 561a7e1abadSJonathan Lemon 562a7e1abadSJonathan Lemon static int 563a7e1abadSJonathan Lemon ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts) 564a7e1abadSJonathan Lemon { 565a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 566a7e1abadSJonathan Lemon unsigned long flags; 567a7e1abadSJonathan Lemon 568a7e1abadSJonathan Lemon if (ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC) 569a7e1abadSJonathan Lemon return 0; 570a7e1abadSJonathan Lemon 571a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 572a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(bp, ts); 573a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 574a7e1abadSJonathan Lemon 575a7e1abadSJonathan Lemon return 0; 576a7e1abadSJonathan Lemon } 577a7e1abadSJonathan Lemon 578a7e1abadSJonathan Lemon static int 579a7e1abadSJonathan Lemon ptp_ocp_adjtime(struct ptp_clock_info *ptp_info, s64 delta_ns) 580a7e1abadSJonathan Lemon { 581a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 582a7e1abadSJonathan Lemon struct timespec64 ts; 583a7e1abadSJonathan Lemon unsigned long flags; 584a7e1abadSJonathan Lemon int err; 585a7e1abadSJonathan Lemon 586a7e1abadSJonathan Lemon if (ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC) 587a7e1abadSJonathan Lemon return 0; 588a7e1abadSJonathan Lemon 589a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 590a7e1abadSJonathan Lemon err = __ptp_ocp_gettime_locked(bp, &ts, NULL); 591a7e1abadSJonathan Lemon if (likely(!err)) { 592a7e1abadSJonathan Lemon timespec64_add_ns(&ts, delta_ns); 593a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(bp, &ts); 594a7e1abadSJonathan Lemon } 595a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 596a7e1abadSJonathan Lemon 597a7e1abadSJonathan Lemon return err; 598a7e1abadSJonathan Lemon } 599a7e1abadSJonathan Lemon 600a7e1abadSJonathan Lemon static int 601a7e1abadSJonathan Lemon ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm) 602a7e1abadSJonathan Lemon { 603a7e1abadSJonathan Lemon if (scaled_ppm == 0) 604a7e1abadSJonathan Lemon return 0; 605a7e1abadSJonathan Lemon 606a7e1abadSJonathan Lemon return -EOPNOTSUPP; 607a7e1abadSJonathan Lemon } 608a7e1abadSJonathan Lemon 609773bda96SJonathan Lemon static int 610773bda96SJonathan Lemon ptp_ocp_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns) 611773bda96SJonathan Lemon { 612773bda96SJonathan Lemon return -EOPNOTSUPP; 613773bda96SJonathan Lemon } 614773bda96SJonathan Lemon 615773bda96SJonathan Lemon static int 616773bda96SJonathan Lemon ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq, 617773bda96SJonathan Lemon int on) 618773bda96SJonathan Lemon { 619773bda96SJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 620773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = NULL; 621773bda96SJonathan Lemon int err; 622773bda96SJonathan Lemon 623773bda96SJonathan Lemon switch (rq->type) { 624773bda96SJonathan Lemon case PTP_CLK_REQ_EXTTS: 625773bda96SJonathan Lemon switch (rq->extts.index) { 626773bda96SJonathan Lemon case 0: 627773bda96SJonathan Lemon ext = bp->ts0; 628773bda96SJonathan Lemon break; 629773bda96SJonathan Lemon case 1: 630773bda96SJonathan Lemon ext = bp->ts1; 631773bda96SJonathan Lemon break; 632dcf61469SJonathan Lemon case 2: 633dcf61469SJonathan Lemon ext = bp->ts2; 634dcf61469SJonathan Lemon break; 635773bda96SJonathan Lemon } 636773bda96SJonathan Lemon break; 637773bda96SJonathan Lemon case PTP_CLK_REQ_PPS: 638773bda96SJonathan Lemon ext = bp->pps; 639773bda96SJonathan Lemon break; 640773bda96SJonathan Lemon default: 641773bda96SJonathan Lemon return -EOPNOTSUPP; 642773bda96SJonathan Lemon } 643773bda96SJonathan Lemon 644773bda96SJonathan Lemon err = -ENXIO; 645773bda96SJonathan Lemon if (ext) 646773bda96SJonathan Lemon err = ext->info->enable(ext, on); 647773bda96SJonathan Lemon 648773bda96SJonathan Lemon return err; 649773bda96SJonathan Lemon } 650773bda96SJonathan Lemon 651a7e1abadSJonathan Lemon static const struct ptp_clock_info ptp_ocp_clock_info = { 652a7e1abadSJonathan Lemon .owner = THIS_MODULE, 653a7e1abadSJonathan Lemon .name = KBUILD_MODNAME, 654a7e1abadSJonathan Lemon .max_adj = 100000000, 655a7e1abadSJonathan Lemon .gettimex64 = ptp_ocp_gettimex, 656a7e1abadSJonathan Lemon .settime64 = ptp_ocp_settime, 657a7e1abadSJonathan Lemon .adjtime = ptp_ocp_adjtime, 658a7e1abadSJonathan Lemon .adjfine = ptp_ocp_null_adjfine, 659773bda96SJonathan Lemon .adjphase = ptp_ocp_adjphase, 660773bda96SJonathan Lemon .enable = ptp_ocp_enable, 661773bda96SJonathan Lemon .pps = true, 662dcf61469SJonathan Lemon .n_ext_ts = 3, 663a7e1abadSJonathan Lemon }; 664a7e1abadSJonathan Lemon 665773bda96SJonathan Lemon static void 666773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp) 667773bda96SJonathan Lemon { 668773bda96SJonathan Lemon u32 ctrl, select; 669773bda96SJonathan Lemon 670773bda96SJonathan Lemon select = ioread32(&bp->reg->select); 671773bda96SJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 672773bda96SJonathan Lemon 673773bda96SJonathan Lemon iowrite32(0, &bp->reg->drift_ns); 674773bda96SJonathan Lemon 675773bda96SJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 676773bda96SJonathan Lemon ctrl |= OCP_CTRL_ADJUST_DRIFT; 677773bda96SJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 678773bda96SJonathan Lemon 679773bda96SJonathan Lemon /* restore clock selection */ 680773bda96SJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 681773bda96SJonathan Lemon } 682773bda96SJonathan Lemon 683773bda96SJonathan Lemon static void 684773bda96SJonathan Lemon ptp_ocp_watchdog(struct timer_list *t) 685773bda96SJonathan Lemon { 686773bda96SJonathan Lemon struct ptp_ocp *bp = from_timer(bp, t, watchdog); 687773bda96SJonathan Lemon unsigned long flags; 688773bda96SJonathan Lemon u32 status; 689773bda96SJonathan Lemon 6900d43d4f2SJonathan Lemon status = ioread32(&bp->pps_to_clk->status); 691773bda96SJonathan Lemon 692773bda96SJonathan Lemon if (status & PPS_STATUS_SUPERV_ERR) { 6930d43d4f2SJonathan Lemon iowrite32(status, &bp->pps_to_clk->status); 694ef0cfb34SJonathan Lemon if (!bp->gnss_lost) { 695773bda96SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 696773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(bp); 697773bda96SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 698ef0cfb34SJonathan Lemon bp->gnss_lost = ktime_get_real_seconds(); 699773bda96SJonathan Lemon } 700773bda96SJonathan Lemon 701ef0cfb34SJonathan Lemon } else if (bp->gnss_lost) { 702ef0cfb34SJonathan Lemon bp->gnss_lost = 0; 703773bda96SJonathan Lemon } 704773bda96SJonathan Lemon 705773bda96SJonathan Lemon mod_timer(&bp->watchdog, jiffies + HZ); 706773bda96SJonathan Lemon } 707773bda96SJonathan Lemon 708a7e1abadSJonathan Lemon static int 709773bda96SJonathan Lemon ptp_ocp_init_clock(struct ptp_ocp *bp) 710a7e1abadSJonathan Lemon { 711a7e1abadSJonathan Lemon struct timespec64 ts; 712a7e1abadSJonathan Lemon bool sync; 713a7e1abadSJonathan Lemon u32 ctrl; 714a7e1abadSJonathan Lemon 715a7e1abadSJonathan Lemon /* make sure clock is enabled */ 716a7e1abadSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 717a7e1abadSJonathan Lemon ctrl |= OCP_CTRL_ENABLE; 718a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 719a7e1abadSJonathan Lemon 720773bda96SJonathan Lemon /* NO DRIFT Correction */ 721773bda96SJonathan Lemon /* offset_p:i 1/8, offset_i: 1/16, drift_p: 0, drift_i: 0 */ 722773bda96SJonathan Lemon iowrite32(0x2000, &bp->reg->servo_offset_p); 723773bda96SJonathan Lemon iowrite32(0x1000, &bp->reg->servo_offset_i); 724773bda96SJonathan Lemon iowrite32(0, &bp->reg->servo_drift_p); 725773bda96SJonathan Lemon iowrite32(0, &bp->reg->servo_drift_i); 726773bda96SJonathan Lemon 727773bda96SJonathan Lemon /* latch servo values */ 728773bda96SJonathan Lemon ctrl |= OCP_CTRL_ADJUST_SERVO; 729773bda96SJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 730773bda96SJonathan Lemon 731a7e1abadSJonathan Lemon if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) { 732a7e1abadSJonathan Lemon dev_err(&bp->pdev->dev, "clock not enabled\n"); 733a7e1abadSJonathan Lemon return -ENODEV; 734a7e1abadSJonathan Lemon } 735a7e1abadSJonathan Lemon 736a7e1abadSJonathan Lemon sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC; 737a7e1abadSJonathan Lemon if (!sync) { 738065efcc5SJonathan Lemon ktime_get_clocktai_ts64(&ts); 739a7e1abadSJonathan Lemon ptp_ocp_settime(&bp->ptp_info, &ts); 740a7e1abadSJonathan Lemon } 741a7e1abadSJonathan Lemon 742065efcc5SJonathan Lemon /* If there is a clock supervisor, then enable the watchdog */ 743065efcc5SJonathan Lemon if (bp->pps_to_clk) { 744773bda96SJonathan Lemon timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0); 745773bda96SJonathan Lemon mod_timer(&bp->watchdog, jiffies + HZ); 746065efcc5SJonathan Lemon } 747773bda96SJonathan Lemon 748a7e1abadSJonathan Lemon return 0; 749a7e1abadSJonathan Lemon } 750a7e1abadSJonathan Lemon 751a7e1abadSJonathan Lemon static void 75289260d87SJonathan Lemon ptp_ocp_utc_distribute(struct ptp_ocp *bp, u32 val) 75389260d87SJonathan Lemon { 75489260d87SJonathan Lemon unsigned long flags; 75589260d87SJonathan Lemon 75689260d87SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 75789260d87SJonathan Lemon 75889260d87SJonathan Lemon bp->utc_tai_offset = val; 75989260d87SJonathan Lemon 76089260d87SJonathan Lemon if (bp->irig_out) 76189260d87SJonathan Lemon iowrite32(val, &bp->irig_out->adj_sec); 76289260d87SJonathan Lemon if (bp->dcf_out) 76389260d87SJonathan Lemon iowrite32(val, &bp->dcf_out->adj_sec); 76489260d87SJonathan Lemon 76589260d87SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 76689260d87SJonathan Lemon } 76789260d87SJonathan Lemon 76889260d87SJonathan Lemon static void 769065efcc5SJonathan Lemon ptp_ocp_tod_init(struct ptp_ocp *bp) 770065efcc5SJonathan Lemon { 771065efcc5SJonathan Lemon u32 ctrl, reg; 772065efcc5SJonathan Lemon 773065efcc5SJonathan Lemon ctrl = ioread32(&bp->tod->ctrl); 774065efcc5SJonathan Lemon ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE; 775065efcc5SJonathan Lemon ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B); 776065efcc5SJonathan Lemon iowrite32(ctrl, &bp->tod->ctrl); 777065efcc5SJonathan Lemon 778065efcc5SJonathan Lemon reg = ioread32(&bp->tod->utc_status); 779065efcc5SJonathan Lemon if (reg & TOD_STATUS_UTC_VALID) 780065efcc5SJonathan Lemon ptp_ocp_utc_distribute(bp, reg & TOD_STATUS_UTC_MASK); 781065efcc5SJonathan Lemon } 782065efcc5SJonathan Lemon 783065efcc5SJonathan Lemon static void 784a7e1abadSJonathan Lemon ptp_ocp_tod_info(struct ptp_ocp *bp) 785a7e1abadSJonathan Lemon { 786a7e1abadSJonathan Lemon static const char * const proto_name[] = { 787a7e1abadSJonathan Lemon "NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none", 788a7e1abadSJonathan Lemon "UBX", "UBX_UTC", "UBX_LS", "UBX_none" 789a7e1abadSJonathan Lemon }; 790a7e1abadSJonathan Lemon static const char * const gnss_name[] = { 791a7e1abadSJonathan Lemon "ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU", 792a7e1abadSJonathan Lemon }; 793a7e1abadSJonathan Lemon u32 version, ctrl, reg; 794a7e1abadSJonathan Lemon int idx; 795a7e1abadSJonathan Lemon 796a7e1abadSJonathan Lemon version = ioread32(&bp->tod->version); 797a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "TOD Version %d.%d.%d\n", 798a7e1abadSJonathan Lemon version >> 24, (version >> 16) & 0xff, version & 0xffff); 799a7e1abadSJonathan Lemon 800a7e1abadSJonathan Lemon ctrl = ioread32(&bp->tod->ctrl); 801a7e1abadSJonathan Lemon idx = ctrl & TOD_CTRL_PROTOCOL ? 4 : 0; 802a7e1abadSJonathan Lemon idx += (ctrl >> 16) & 3; 803a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "control: %x\n", ctrl); 804a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "TOD Protocol %s %s\n", proto_name[idx], 805a7e1abadSJonathan Lemon ctrl & TOD_CTRL_ENABLE ? "enabled" : ""); 806a7e1abadSJonathan Lemon 807a7e1abadSJonathan Lemon idx = (ctrl >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK; 808a7e1abadSJonathan Lemon if (idx < ARRAY_SIZE(gnss_name)) 809a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "GNSS %s\n", gnss_name[idx]); 810a7e1abadSJonathan Lemon 811a7e1abadSJonathan Lemon reg = ioread32(&bp->tod->status); 812a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "status: %x\n", reg); 813a7e1abadSJonathan Lemon 814065efcc5SJonathan Lemon reg = ioread32(&bp->tod->adj_sec); 815a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "correction: %d\n", reg); 816a7e1abadSJonathan Lemon 817a7e1abadSJonathan Lemon reg = ioread32(&bp->tod->utc_status); 818a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "utc_status: %x\n", reg); 819a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "utc_offset: %d valid:%d leap_valid:%d\n", 820a7e1abadSJonathan Lemon reg & TOD_STATUS_UTC_MASK, reg & TOD_STATUS_UTC_VALID ? 1 : 0, 821a7e1abadSJonathan Lemon reg & TOD_STATUS_LEAP_VALID ? 1 : 0); 822a7e1abadSJonathan Lemon } 823a7e1abadSJonathan Lemon 824773bda96SJonathan Lemon static int 825773bda96SJonathan Lemon ptp_ocp_firstchild(struct device *dev, void *data) 826773bda96SJonathan Lemon { 827773bda96SJonathan Lemon return 1; 828773bda96SJonathan Lemon } 829773bda96SJonathan Lemon 830773bda96SJonathan Lemon static int 831773bda96SJonathan Lemon ptp_ocp_read_i2c(struct i2c_adapter *adap, u8 addr, u8 reg, u8 sz, u8 *data) 832773bda96SJonathan Lemon { 833773bda96SJonathan Lemon struct i2c_msg msgs[2] = { 834773bda96SJonathan Lemon { 835773bda96SJonathan Lemon .addr = addr, 836773bda96SJonathan Lemon .len = 1, 837773bda96SJonathan Lemon .buf = ®, 838773bda96SJonathan Lemon }, 839773bda96SJonathan Lemon { 840773bda96SJonathan Lemon .addr = addr, 841773bda96SJonathan Lemon .flags = I2C_M_RD, 842773bda96SJonathan Lemon .len = 2, 843773bda96SJonathan Lemon .buf = data, 844773bda96SJonathan Lemon }, 845773bda96SJonathan Lemon }; 846773bda96SJonathan Lemon int err; 847773bda96SJonathan Lemon u8 len; 848773bda96SJonathan Lemon 849773bda96SJonathan Lemon /* xiic-i2c for some stupid reason only does 2 byte reads. */ 850773bda96SJonathan Lemon while (sz) { 851773bda96SJonathan Lemon len = min_t(u8, sz, 2); 852773bda96SJonathan Lemon msgs[1].len = len; 853773bda96SJonathan Lemon err = i2c_transfer(adap, msgs, 2); 854773bda96SJonathan Lemon if (err != msgs[1].len) 855773bda96SJonathan Lemon return err; 856773bda96SJonathan Lemon msgs[1].buf += len; 857773bda96SJonathan Lemon reg += len; 858773bda96SJonathan Lemon sz -= len; 859773bda96SJonathan Lemon } 860773bda96SJonathan Lemon return 0; 861773bda96SJonathan Lemon } 862773bda96SJonathan Lemon 863773bda96SJonathan Lemon static void 864773bda96SJonathan Lemon ptp_ocp_get_serial_number(struct ptp_ocp *bp) 865773bda96SJonathan Lemon { 866773bda96SJonathan Lemon struct i2c_adapter *adap; 867773bda96SJonathan Lemon struct device *dev; 868773bda96SJonathan Lemon int err; 869773bda96SJonathan Lemon 8701447149dSJonathan Lemon if (!bp->i2c_ctrl) 8711447149dSJonathan Lemon return; 8721447149dSJonathan Lemon 873773bda96SJonathan Lemon dev = device_find_child(&bp->i2c_ctrl->dev, NULL, ptp_ocp_firstchild); 874773bda96SJonathan Lemon if (!dev) { 875773bda96SJonathan Lemon dev_err(&bp->pdev->dev, "Can't find I2C adapter\n"); 876773bda96SJonathan Lemon return; 877773bda96SJonathan Lemon } 878773bda96SJonathan Lemon 879773bda96SJonathan Lemon adap = i2c_verify_adapter(dev); 880773bda96SJonathan Lemon if (!adap) { 881773bda96SJonathan Lemon dev_err(&bp->pdev->dev, "device '%s' isn't an I2C adapter\n", 882773bda96SJonathan Lemon dev_name(dev)); 883773bda96SJonathan Lemon goto out; 884773bda96SJonathan Lemon } 885773bda96SJonathan Lemon 886773bda96SJonathan Lemon err = ptp_ocp_read_i2c(adap, 0x58, 0x9A, 6, bp->serial); 887773bda96SJonathan Lemon if (err) { 888773bda96SJonathan Lemon dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", err); 889773bda96SJonathan Lemon goto out; 890773bda96SJonathan Lemon } 891773bda96SJonathan Lemon 892773bda96SJonathan Lemon bp->has_serial = true; 893773bda96SJonathan Lemon 894773bda96SJonathan Lemon out: 895773bda96SJonathan Lemon put_device(dev); 896773bda96SJonathan Lemon } 897773bda96SJonathan Lemon 898773bda96SJonathan Lemon static struct device * 899773bda96SJonathan Lemon ptp_ocp_find_flash(struct ptp_ocp *bp) 900773bda96SJonathan Lemon { 901773bda96SJonathan Lemon struct device *dev, *last; 902773bda96SJonathan Lemon 903773bda96SJonathan Lemon last = NULL; 904773bda96SJonathan Lemon dev = &bp->spi_flash->dev; 905773bda96SJonathan Lemon 906773bda96SJonathan Lemon while ((dev = device_find_child(dev, NULL, ptp_ocp_firstchild))) { 907773bda96SJonathan Lemon if (!strcmp("mtd", dev_bus_name(dev))) 908773bda96SJonathan Lemon break; 909773bda96SJonathan Lemon put_device(last); 910773bda96SJonathan Lemon last = dev; 911773bda96SJonathan Lemon } 912773bda96SJonathan Lemon put_device(last); 913773bda96SJonathan Lemon 914773bda96SJonathan Lemon return dev; 915773bda96SJonathan Lemon } 916773bda96SJonathan Lemon 917773bda96SJonathan Lemon static int 918773bda96SJonathan Lemon ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev, 919773bda96SJonathan Lemon const struct firmware *fw) 920773bda96SJonathan Lemon { 921773bda96SJonathan Lemon struct mtd_info *mtd = dev_get_drvdata(dev); 922773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 923773bda96SJonathan Lemon size_t off, len, resid, wrote; 924773bda96SJonathan Lemon struct erase_info erase; 925773bda96SJonathan Lemon size_t base, blksz; 9267c807572SJonathan Lemon int err = 0; 927773bda96SJonathan Lemon 928773bda96SJonathan Lemon off = 0; 929773bda96SJonathan Lemon base = bp->flash_start; 930773bda96SJonathan Lemon blksz = 4096; 931773bda96SJonathan Lemon resid = fw->size; 932773bda96SJonathan Lemon 933773bda96SJonathan Lemon while (resid) { 934773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, "Flashing", 935773bda96SJonathan Lemon NULL, off, fw->size); 936773bda96SJonathan Lemon 937773bda96SJonathan Lemon len = min_t(size_t, resid, blksz); 938773bda96SJonathan Lemon erase.addr = base + off; 939773bda96SJonathan Lemon erase.len = blksz; 940773bda96SJonathan Lemon 941773bda96SJonathan Lemon err = mtd_erase(mtd, &erase); 942773bda96SJonathan Lemon if (err) 943773bda96SJonathan Lemon goto out; 944773bda96SJonathan Lemon 945773bda96SJonathan Lemon err = mtd_write(mtd, base + off, len, &wrote, &fw->data[off]); 946773bda96SJonathan Lemon if (err) 947773bda96SJonathan Lemon goto out; 948773bda96SJonathan Lemon 949773bda96SJonathan Lemon off += blksz; 950773bda96SJonathan Lemon resid -= len; 951773bda96SJonathan Lemon } 952773bda96SJonathan Lemon out: 953773bda96SJonathan Lemon return err; 954773bda96SJonathan Lemon } 955773bda96SJonathan Lemon 956773bda96SJonathan Lemon static int 957773bda96SJonathan Lemon ptp_ocp_devlink_flash_update(struct devlink *devlink, 958773bda96SJonathan Lemon struct devlink_flash_update_params *params, 959773bda96SJonathan Lemon struct netlink_ext_ack *extack) 960773bda96SJonathan Lemon { 961773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 962773bda96SJonathan Lemon struct device *dev; 963773bda96SJonathan Lemon const char *msg; 964773bda96SJonathan Lemon int err; 965773bda96SJonathan Lemon 966773bda96SJonathan Lemon dev = ptp_ocp_find_flash(bp); 967773bda96SJonathan Lemon if (!dev) { 968773bda96SJonathan Lemon dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n"); 969773bda96SJonathan Lemon return -ENODEV; 970773bda96SJonathan Lemon } 971773bda96SJonathan Lemon 972773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, "Preparing to flash", 973773bda96SJonathan Lemon NULL, 0, 0); 974773bda96SJonathan Lemon 975773bda96SJonathan Lemon err = ptp_ocp_devlink_flash(devlink, dev, params->fw); 976773bda96SJonathan Lemon 977773bda96SJonathan Lemon msg = err ? "Flash error" : "Flash complete"; 978773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0); 979773bda96SJonathan Lemon 980773bda96SJonathan Lemon put_device(dev); 981773bda96SJonathan Lemon return err; 982773bda96SJonathan Lemon } 983773bda96SJonathan Lemon 984773bda96SJonathan Lemon static int 985773bda96SJonathan Lemon ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req, 986773bda96SJonathan Lemon struct netlink_ext_ack *extack) 987773bda96SJonathan Lemon { 988773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 989773bda96SJonathan Lemon char buf[32]; 990773bda96SJonathan Lemon int err; 991773bda96SJonathan Lemon 992773bda96SJonathan Lemon err = devlink_info_driver_name_put(req, KBUILD_MODNAME); 993773bda96SJonathan Lemon if (err) 994773bda96SJonathan Lemon return err; 995773bda96SJonathan Lemon 996773bda96SJonathan Lemon if (bp->image) { 997773bda96SJonathan Lemon u32 ver = ioread32(&bp->image->version); 998773bda96SJonathan Lemon 999773bda96SJonathan Lemon if (ver & 0xffff) { 1000773bda96SJonathan Lemon sprintf(buf, "%d", ver); 1001773bda96SJonathan Lemon err = devlink_info_version_running_put(req, 10021a052da9SJonathan Lemon "fw", 1003773bda96SJonathan Lemon buf); 1004773bda96SJonathan Lemon } else { 1005773bda96SJonathan Lemon sprintf(buf, "%d", ver >> 16); 1006773bda96SJonathan Lemon err = devlink_info_version_running_put(req, 10071a052da9SJonathan Lemon "loader", 1008773bda96SJonathan Lemon buf); 1009773bda96SJonathan Lemon } 1010773bda96SJonathan Lemon if (err) 1011773bda96SJonathan Lemon return err; 1012773bda96SJonathan Lemon } 1013773bda96SJonathan Lemon 1014773bda96SJonathan Lemon if (!bp->has_serial) 1015773bda96SJonathan Lemon ptp_ocp_get_serial_number(bp); 1016773bda96SJonathan Lemon 1017773bda96SJonathan Lemon if (bp->has_serial) { 1018773bda96SJonathan Lemon sprintf(buf, "%pM", bp->serial); 1019773bda96SJonathan Lemon err = devlink_info_serial_number_put(req, buf); 1020773bda96SJonathan Lemon if (err) 1021773bda96SJonathan Lemon return err; 1022773bda96SJonathan Lemon } 1023773bda96SJonathan Lemon 1024773bda96SJonathan Lemon return 0; 1025773bda96SJonathan Lemon } 1026773bda96SJonathan Lemon 1027773bda96SJonathan Lemon static const struct devlink_ops ptp_ocp_devlink_ops = { 1028773bda96SJonathan Lemon .flash_update = ptp_ocp_devlink_flash_update, 1029773bda96SJonathan Lemon .info_get = ptp_ocp_devlink_info_get, 1030773bda96SJonathan Lemon }; 1031773bda96SJonathan Lemon 1032773bda96SJonathan Lemon static void __iomem * 1033773bda96SJonathan Lemon __ptp_ocp_get_mem(struct ptp_ocp *bp, unsigned long start, int size) 1034773bda96SJonathan Lemon { 1035773bda96SJonathan Lemon struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp"); 1036773bda96SJonathan Lemon 1037773bda96SJonathan Lemon return devm_ioremap_resource(&bp->pdev->dev, &res); 1038773bda96SJonathan Lemon } 1039773bda96SJonathan Lemon 1040773bda96SJonathan Lemon static void __iomem * 1041773bda96SJonathan Lemon ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r) 1042773bda96SJonathan Lemon { 1043773bda96SJonathan Lemon unsigned long start; 1044773bda96SJonathan Lemon 1045773bda96SJonathan Lemon start = pci_resource_start(bp->pdev, 0) + r->offset; 1046773bda96SJonathan Lemon return __ptp_ocp_get_mem(bp, start, r->size); 1047773bda96SJonathan Lemon } 1048773bda96SJonathan Lemon 1049773bda96SJonathan Lemon static void 1050773bda96SJonathan Lemon ptp_ocp_set_irq_resource(struct resource *res, int irq) 1051773bda96SJonathan Lemon { 1052773bda96SJonathan Lemon struct resource r = DEFINE_RES_IRQ(irq); 1053773bda96SJonathan Lemon *res = r; 1054773bda96SJonathan Lemon } 1055773bda96SJonathan Lemon 1056773bda96SJonathan Lemon static void 1057773bda96SJonathan Lemon ptp_ocp_set_mem_resource(struct resource *res, unsigned long start, int size) 1058773bda96SJonathan Lemon { 1059773bda96SJonathan Lemon struct resource r = DEFINE_RES_MEM(start, size); 1060773bda96SJonathan Lemon *res = r; 1061773bda96SJonathan Lemon } 1062773bda96SJonathan Lemon 1063773bda96SJonathan Lemon static int 1064773bda96SJonathan Lemon ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r) 1065773bda96SJonathan Lemon { 1066773bda96SJonathan Lemon struct ptp_ocp_flash_info *info; 1067773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1068773bda96SJonathan Lemon struct platform_device *p; 1069773bda96SJonathan Lemon struct resource res[2]; 1070773bda96SJonathan Lemon unsigned long start; 1071773bda96SJonathan Lemon int id; 1072773bda96SJonathan Lemon 1073773bda96SJonathan Lemon start = pci_resource_start(pdev, 0) + r->offset; 1074773bda96SJonathan Lemon ptp_ocp_set_mem_resource(&res[0], start, r->size); 1075773bda96SJonathan Lemon ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1076773bda96SJonathan Lemon 1077773bda96SJonathan Lemon info = r->extra; 1078773bda96SJonathan Lemon id = pci_dev_id(pdev) << 1; 1079773bda96SJonathan Lemon id += info->pci_offset; 1080773bda96SJonathan Lemon 1081773bda96SJonathan Lemon p = platform_device_register_resndata(&pdev->dev, info->name, id, 1082773bda96SJonathan Lemon res, 2, info->data, 1083773bda96SJonathan Lemon info->data_size); 1084773bda96SJonathan Lemon if (IS_ERR(p)) 1085773bda96SJonathan Lemon return PTR_ERR(p); 1086773bda96SJonathan Lemon 1087773bda96SJonathan Lemon bp_assign_entry(bp, r, p); 1088773bda96SJonathan Lemon 1089773bda96SJonathan Lemon return 0; 1090773bda96SJonathan Lemon } 1091773bda96SJonathan Lemon 1092773bda96SJonathan Lemon static struct platform_device * 1093773bda96SJonathan Lemon ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id) 1094773bda96SJonathan Lemon { 10951618df6aSJonathan Lemon struct ptp_ocp_i2c_info *info; 1096773bda96SJonathan Lemon struct resource res[2]; 1097773bda96SJonathan Lemon unsigned long start; 1098773bda96SJonathan Lemon 10991618df6aSJonathan Lemon info = r->extra; 1100773bda96SJonathan Lemon start = pci_resource_start(pdev, 0) + r->offset; 1101773bda96SJonathan Lemon ptp_ocp_set_mem_resource(&res[0], start, r->size); 1102773bda96SJonathan Lemon ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1103773bda96SJonathan Lemon 11041618df6aSJonathan Lemon return platform_device_register_resndata(&pdev->dev, info->name, 11051618df6aSJonathan Lemon id, res, 2, 11061618df6aSJonathan Lemon info->data, info->data_size); 1107773bda96SJonathan Lemon } 1108773bda96SJonathan Lemon 1109773bda96SJonathan Lemon static int 1110773bda96SJonathan Lemon ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r) 1111773bda96SJonathan Lemon { 1112773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 11131618df6aSJonathan Lemon struct ptp_ocp_i2c_info *info; 1114773bda96SJonathan Lemon struct platform_device *p; 1115773bda96SJonathan Lemon struct clk_hw *clk; 1116773bda96SJonathan Lemon char buf[32]; 1117773bda96SJonathan Lemon int id; 1118773bda96SJonathan Lemon 11191618df6aSJonathan Lemon info = r->extra; 1120773bda96SJonathan Lemon id = pci_dev_id(bp->pdev); 1121773bda96SJonathan Lemon 1122773bda96SJonathan Lemon sprintf(buf, "AXI.%d", id); 11231618df6aSJonathan Lemon clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0, 11241618df6aSJonathan Lemon info->fixed_rate); 1125773bda96SJonathan Lemon if (IS_ERR(clk)) 1126773bda96SJonathan Lemon return PTR_ERR(clk); 1127773bda96SJonathan Lemon bp->i2c_clk = clk; 1128773bda96SJonathan Lemon 11291618df6aSJonathan Lemon sprintf(buf, "%s.%d", info->name, id); 1130773bda96SJonathan Lemon devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf); 1131773bda96SJonathan Lemon p = ptp_ocp_i2c_bus(bp->pdev, r, id); 1132773bda96SJonathan Lemon if (IS_ERR(p)) 1133773bda96SJonathan Lemon return PTR_ERR(p); 1134773bda96SJonathan Lemon 1135773bda96SJonathan Lemon bp_assign_entry(bp, r, p); 1136773bda96SJonathan Lemon 1137773bda96SJonathan Lemon return 0; 1138773bda96SJonathan Lemon } 1139773bda96SJonathan Lemon 1140773bda96SJonathan Lemon static irqreturn_t 1141773bda96SJonathan Lemon ptp_ocp_ts_irq(int irq, void *priv) 1142773bda96SJonathan Lemon { 1143773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1144773bda96SJonathan Lemon struct ts_reg __iomem *reg = ext->mem; 1145773bda96SJonathan Lemon struct ptp_clock_event ev; 1146773bda96SJonathan Lemon u32 sec, nsec; 1147773bda96SJonathan Lemon 1148773bda96SJonathan Lemon /* XXX should fix API - this converts s/ns -> ts -> s/ns */ 1149773bda96SJonathan Lemon sec = ioread32(®->time_sec); 1150773bda96SJonathan Lemon nsec = ioread32(®->time_ns); 1151773bda96SJonathan Lemon 1152773bda96SJonathan Lemon ev.type = PTP_CLOCK_EXTTS; 1153773bda96SJonathan Lemon ev.index = ext->info->index; 1154773bda96SJonathan Lemon ev.timestamp = sec * 1000000000ULL + nsec; 1155773bda96SJonathan Lemon 1156773bda96SJonathan Lemon ptp_clock_event(ext->bp->ptp, &ev); 1157773bda96SJonathan Lemon 1158773bda96SJonathan Lemon iowrite32(1, ®->intr); /* write 1 to ack */ 1159773bda96SJonathan Lemon 1160773bda96SJonathan Lemon return IRQ_HANDLED; 1161773bda96SJonathan Lemon } 1162773bda96SJonathan Lemon 1163773bda96SJonathan Lemon static int 1164773bda96SJonathan Lemon ptp_ocp_ts_enable(void *priv, bool enable) 1165773bda96SJonathan Lemon { 1166773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1167773bda96SJonathan Lemon struct ts_reg __iomem *reg = ext->mem; 1168773bda96SJonathan Lemon 1169773bda96SJonathan Lemon if (enable) { 1170773bda96SJonathan Lemon iowrite32(1, ®->enable); 1171773bda96SJonathan Lemon iowrite32(1, ®->intr_mask); 1172773bda96SJonathan Lemon iowrite32(1, ®->intr); 1173773bda96SJonathan Lemon } else { 1174773bda96SJonathan Lemon iowrite32(0, ®->intr_mask); 1175773bda96SJonathan Lemon iowrite32(0, ®->enable); 1176773bda96SJonathan Lemon } 1177773bda96SJonathan Lemon 1178773bda96SJonathan Lemon return 0; 1179773bda96SJonathan Lemon } 1180773bda96SJonathan Lemon 1181773bda96SJonathan Lemon static void 1182773bda96SJonathan Lemon ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext) 1183773bda96SJonathan Lemon { 1184773bda96SJonathan Lemon ext->info->enable(ext, false); 1185773bda96SJonathan Lemon pci_free_irq(ext->bp->pdev, ext->irq_vec, ext); 1186773bda96SJonathan Lemon kfree(ext); 1187773bda96SJonathan Lemon } 1188773bda96SJonathan Lemon 1189773bda96SJonathan Lemon static int 1190773bda96SJonathan Lemon ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r) 1191773bda96SJonathan Lemon { 1192773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1193773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext; 1194773bda96SJonathan Lemon int err; 1195773bda96SJonathan Lemon 1196773bda96SJonathan Lemon ext = kzalloc(sizeof(*ext), GFP_KERNEL); 1197773bda96SJonathan Lemon if (!ext) 1198773bda96SJonathan Lemon return -ENOMEM; 1199773bda96SJonathan Lemon 1200773bda96SJonathan Lemon err = -EINVAL; 1201773bda96SJonathan Lemon ext->mem = ptp_ocp_get_mem(bp, r); 1202773bda96SJonathan Lemon if (!ext->mem) 1203773bda96SJonathan Lemon goto out; 1204773bda96SJonathan Lemon 1205773bda96SJonathan Lemon ext->bp = bp; 1206773bda96SJonathan Lemon ext->info = r->extra; 1207773bda96SJonathan Lemon ext->irq_vec = r->irq_vec; 1208773bda96SJonathan Lemon 1209773bda96SJonathan Lemon err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL, 121056ec4403SJonathan Lemon ext, "ocp%d.%s", bp->id, r->name); 1211773bda96SJonathan Lemon if (err) { 1212773bda96SJonathan Lemon dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec); 1213773bda96SJonathan Lemon goto out; 1214773bda96SJonathan Lemon } 1215773bda96SJonathan Lemon 1216773bda96SJonathan Lemon bp_assign_entry(bp, r, ext); 1217773bda96SJonathan Lemon 1218773bda96SJonathan Lemon return 0; 1219773bda96SJonathan Lemon 1220773bda96SJonathan Lemon out: 1221773bda96SJonathan Lemon kfree(ext); 1222773bda96SJonathan Lemon return err; 1223773bda96SJonathan Lemon } 1224773bda96SJonathan Lemon 1225773bda96SJonathan Lemon static int 1226773bda96SJonathan Lemon ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r) 1227773bda96SJonathan Lemon { 1228773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1229773bda96SJonathan Lemon struct uart_8250_port uart; 1230773bda96SJonathan Lemon 1231773bda96SJonathan Lemon /* Setting UPF_IOREMAP and leaving port.membase unspecified lets 1232773bda96SJonathan Lemon * the serial port device claim and release the pci resource. 1233773bda96SJonathan Lemon */ 1234773bda96SJonathan Lemon memset(&uart, 0, sizeof(uart)); 1235773bda96SJonathan Lemon uart.port.dev = &pdev->dev; 1236773bda96SJonathan Lemon uart.port.iotype = UPIO_MEM; 1237773bda96SJonathan Lemon uart.port.regshift = 2; 1238773bda96SJonathan Lemon uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset; 1239773bda96SJonathan Lemon uart.port.irq = pci_irq_vector(pdev, r->irq_vec); 1240773bda96SJonathan Lemon uart.port.uartclk = 50000000; 1241773bda96SJonathan Lemon uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP; 1242773bda96SJonathan Lemon uart.port.type = PORT_16550A; 1243773bda96SJonathan Lemon 1244773bda96SJonathan Lemon return serial8250_register_8250_port(&uart); 1245773bda96SJonathan Lemon } 1246773bda96SJonathan Lemon 1247773bda96SJonathan Lemon static int 1248773bda96SJonathan Lemon ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r) 1249773bda96SJonathan Lemon { 1250773bda96SJonathan Lemon int port; 1251773bda96SJonathan Lemon 1252773bda96SJonathan Lemon port = ptp_ocp_serial_line(bp, r); 1253773bda96SJonathan Lemon if (port < 0) 1254773bda96SJonathan Lemon return port; 1255773bda96SJonathan Lemon 1256773bda96SJonathan Lemon bp_assign_entry(bp, r, port); 1257773bda96SJonathan Lemon 1258773bda96SJonathan Lemon return 0; 1259773bda96SJonathan Lemon } 1260773bda96SJonathan Lemon 1261773bda96SJonathan Lemon static int 1262773bda96SJonathan Lemon ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r) 1263773bda96SJonathan Lemon { 1264773bda96SJonathan Lemon void __iomem *mem; 1265773bda96SJonathan Lemon 1266773bda96SJonathan Lemon mem = ptp_ocp_get_mem(bp, r); 1267773bda96SJonathan Lemon if (!mem) 1268773bda96SJonathan Lemon return -EINVAL; 1269773bda96SJonathan Lemon 1270773bda96SJonathan Lemon bp_assign_entry(bp, r, mem); 1271773bda96SJonathan Lemon 1272773bda96SJonathan Lemon return 0; 1273773bda96SJonathan Lemon } 1274773bda96SJonathan Lemon 1275773bda96SJonathan Lemon /* FB specific board initializers; last "resource" registered. */ 1276773bda96SJonathan Lemon static int 1277773bda96SJonathan Lemon ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r) 1278773bda96SJonathan Lemon { 1279773bda96SJonathan Lemon bp->flash_start = 1024 * 4096; 1280773bda96SJonathan Lemon 1281065efcc5SJonathan Lemon ptp_ocp_tod_init(bp); 1282065efcc5SJonathan Lemon 1283773bda96SJonathan Lemon return ptp_ocp_init_clock(bp); 1284773bda96SJonathan Lemon } 1285773bda96SJonathan Lemon 128656ec4403SJonathan Lemon static bool 128756ec4403SJonathan Lemon ptp_ocp_allow_irq(struct ptp_ocp *bp, struct ocp_resource *r) 128856ec4403SJonathan Lemon { 128956ec4403SJonathan Lemon bool allow = !r->irq_vec || r->irq_vec < bp->n_irqs; 129056ec4403SJonathan Lemon 129156ec4403SJonathan Lemon if (!allow) 129256ec4403SJonathan Lemon dev_err(&bp->pdev->dev, "irq %d out of range, skipping %s\n", 129356ec4403SJonathan Lemon r->irq_vec, r->name); 129456ec4403SJonathan Lemon return allow; 129556ec4403SJonathan Lemon } 129656ec4403SJonathan Lemon 1297773bda96SJonathan Lemon static int 1298773bda96SJonathan Lemon ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data) 1299773bda96SJonathan Lemon { 1300773bda96SJonathan Lemon struct ocp_resource *r, *table; 1301773bda96SJonathan Lemon int err = 0; 1302773bda96SJonathan Lemon 1303773bda96SJonathan Lemon table = (struct ocp_resource *)driver_data; 1304773bda96SJonathan Lemon for (r = table; r->setup; r++) { 130556ec4403SJonathan Lemon if (!ptp_ocp_allow_irq(bp, r)) 130656ec4403SJonathan Lemon continue; 1307773bda96SJonathan Lemon err = r->setup(bp, r); 1308bceff290SJonathan Lemon if (err) { 1309bceff290SJonathan Lemon dev_err(&bp->pdev->dev, 1310bceff290SJonathan Lemon "Could not register %s: err %d\n", 1311bceff290SJonathan Lemon r->name, err); 1312773bda96SJonathan Lemon break; 1313773bda96SJonathan Lemon } 1314bceff290SJonathan Lemon } 1315773bda96SJonathan Lemon return err; 1316773bda96SJonathan Lemon } 1317773bda96SJonathan Lemon 13186baf2925SJonathan Lemon static void 13196baf2925SJonathan Lemon ptp_ocp_enable_fpga(u32 __iomem *reg, u32 bit, bool enable) 13206baf2925SJonathan Lemon { 13216baf2925SJonathan Lemon u32 ctrl; 13226baf2925SJonathan Lemon bool on; 13236baf2925SJonathan Lemon 13246baf2925SJonathan Lemon ctrl = ioread32(reg); 13256baf2925SJonathan Lemon on = ctrl & bit; 13266baf2925SJonathan Lemon if (on ^ enable) { 13276baf2925SJonathan Lemon ctrl &= ~bit; 13286baf2925SJonathan Lemon ctrl |= enable ? bit : 0; 13296baf2925SJonathan Lemon iowrite32(ctrl, reg); 13306baf2925SJonathan Lemon } 13316baf2925SJonathan Lemon } 13326baf2925SJonathan Lemon 13336baf2925SJonathan Lemon static void 13346baf2925SJonathan Lemon ptp_ocp_irig_out(struct ptp_ocp *bp, bool enable) 13356baf2925SJonathan Lemon { 13366baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->irig_out->ctrl, 13376baf2925SJonathan Lemon IRIG_M_CTRL_ENABLE, enable); 13386baf2925SJonathan Lemon } 13396baf2925SJonathan Lemon 13406baf2925SJonathan Lemon static void 13416baf2925SJonathan Lemon ptp_ocp_irig_in(struct ptp_ocp *bp, bool enable) 13426baf2925SJonathan Lemon { 13436baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->irig_in->ctrl, 13446baf2925SJonathan Lemon IRIG_S_CTRL_ENABLE, enable); 13456baf2925SJonathan Lemon } 13466baf2925SJonathan Lemon 13476baf2925SJonathan Lemon static void 13486baf2925SJonathan Lemon ptp_ocp_dcf_out(struct ptp_ocp *bp, bool enable) 13496baf2925SJonathan Lemon { 13506baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->dcf_out->ctrl, 13516baf2925SJonathan Lemon DCF_M_CTRL_ENABLE, enable); 13526baf2925SJonathan Lemon } 13536baf2925SJonathan Lemon 13546baf2925SJonathan Lemon static void 13556baf2925SJonathan Lemon ptp_ocp_dcf_in(struct ptp_ocp *bp, bool enable) 13566baf2925SJonathan Lemon { 13576baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->dcf_in->ctrl, 13586baf2925SJonathan Lemon DCF_S_CTRL_ENABLE, enable); 13596baf2925SJonathan Lemon } 13606baf2925SJonathan Lemon 13616baf2925SJonathan Lemon static void 13626baf2925SJonathan Lemon __handle_signal_outputs(struct ptp_ocp *bp, u32 val) 13636baf2925SJonathan Lemon { 13646baf2925SJonathan Lemon ptp_ocp_irig_out(bp, val & 0x00100010); 13656baf2925SJonathan Lemon ptp_ocp_dcf_out(bp, val & 0x00200020); 13666baf2925SJonathan Lemon } 13676baf2925SJonathan Lemon 13686baf2925SJonathan Lemon static void 13696baf2925SJonathan Lemon __handle_signal_inputs(struct ptp_ocp *bp, u32 val) 13706baf2925SJonathan Lemon { 13716baf2925SJonathan Lemon ptp_ocp_irig_in(bp, val & 0x00100010); 13726baf2925SJonathan Lemon ptp_ocp_dcf_in(bp, val & 0x00200020); 13736baf2925SJonathan Lemon } 13746baf2925SJonathan Lemon 1375e1daf0ecSJonathan Lemon /* 1376e1daf0ecSJonathan Lemon * ANT0 == gps (in) 1377e1daf0ecSJonathan Lemon * ANT1 == sma1 (in) 1378e1daf0ecSJonathan Lemon * ANT2 == sma2 (in) 1379e1daf0ecSJonathan Lemon * ANT3 == sma3 (out) 1380e1daf0ecSJonathan Lemon * ANT4 == sma4 (out) 1381e1daf0ecSJonathan Lemon */ 1382e1daf0ecSJonathan Lemon 1383e1daf0ecSJonathan Lemon enum ptp_ocp_sma_mode { 1384e1daf0ecSJonathan Lemon SMA_MODE_IN, 1385e1daf0ecSJonathan Lemon SMA_MODE_OUT, 1386e1daf0ecSJonathan Lemon }; 1387e1daf0ecSJonathan Lemon 1388e1daf0ecSJonathan Lemon static struct ptp_ocp_sma_connector { 1389e1daf0ecSJonathan Lemon enum ptp_ocp_sma_mode mode; 1390e1daf0ecSJonathan Lemon bool fixed_mode; 1391e1daf0ecSJonathan Lemon u16 default_out_idx; 1392e1daf0ecSJonathan Lemon } ptp_ocp_sma_map[4] = { 1393e1daf0ecSJonathan Lemon { 1394e1daf0ecSJonathan Lemon .mode = SMA_MODE_IN, 1395e1daf0ecSJonathan Lemon .fixed_mode = true, 1396e1daf0ecSJonathan Lemon }, 1397e1daf0ecSJonathan Lemon { 1398e1daf0ecSJonathan Lemon .mode = SMA_MODE_IN, 1399e1daf0ecSJonathan Lemon .fixed_mode = true, 1400e1daf0ecSJonathan Lemon }, 1401e1daf0ecSJonathan Lemon { 1402e1daf0ecSJonathan Lemon .mode = SMA_MODE_OUT, 1403e1daf0ecSJonathan Lemon .fixed_mode = true, 1404e1daf0ecSJonathan Lemon .default_out_idx = 0, /* 10Mhz */ 1405e1daf0ecSJonathan Lemon }, 1406e1daf0ecSJonathan Lemon { 1407e1daf0ecSJonathan Lemon .mode = SMA_MODE_OUT, 1408e1daf0ecSJonathan Lemon .fixed_mode = true, 1409e1daf0ecSJonathan Lemon .default_out_idx = 1, /* PHC */ 1410e1daf0ecSJonathan Lemon }, 1411e1daf0ecSJonathan Lemon }; 1412e1daf0ecSJonathan Lemon 1413e1daf0ecSJonathan Lemon static ssize_t 1414e1daf0ecSJonathan Lemon ptp_ocp_show_output(u32 val, char *buf, int default_idx) 1415e1daf0ecSJonathan Lemon { 1416e1daf0ecSJonathan Lemon const char *name; 1417e1daf0ecSJonathan Lemon ssize_t count; 1418e1daf0ecSJonathan Lemon 1419e1daf0ecSJonathan Lemon count = sysfs_emit(buf, "OUT: "); 1420e1daf0ecSJonathan Lemon name = ptp_ocp_select_name_from_val(ptp_ocp_sma_out, val); 1421e1daf0ecSJonathan Lemon if (!name) 1422e1daf0ecSJonathan Lemon name = ptp_ocp_sma_out[default_idx].name; 1423e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s\n", name); 1424e1daf0ecSJonathan Lemon return count; 1425e1daf0ecSJonathan Lemon } 1426e1daf0ecSJonathan Lemon 1427e1daf0ecSJonathan Lemon static ssize_t 1428e1daf0ecSJonathan Lemon ptp_ocp_show_inputs(u32 val, char *buf, const char *zero_in) 1429e1daf0ecSJonathan Lemon { 1430e1daf0ecSJonathan Lemon const char *name; 1431e1daf0ecSJonathan Lemon ssize_t count; 1432e1daf0ecSJonathan Lemon int i; 1433e1daf0ecSJonathan Lemon 1434e1daf0ecSJonathan Lemon count = sysfs_emit(buf, "IN: "); 1435e1daf0ecSJonathan Lemon for (i = 0; i < ARRAY_SIZE(ptp_ocp_sma_in); i++) { 1436e1daf0ecSJonathan Lemon if (val & ptp_ocp_sma_in[i].value) { 1437e1daf0ecSJonathan Lemon name = ptp_ocp_sma_in[i].name; 1438e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", name); 1439e1daf0ecSJonathan Lemon } 1440e1daf0ecSJonathan Lemon } 1441e1daf0ecSJonathan Lemon if (!val && zero_in) 1442e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", zero_in); 1443e1daf0ecSJonathan Lemon if (count) 1444e1daf0ecSJonathan Lemon count--; 1445e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "\n"); 1446e1daf0ecSJonathan Lemon return count; 1447e1daf0ecSJonathan Lemon } 1448e1daf0ecSJonathan Lemon 1449e1daf0ecSJonathan Lemon static int 1450e1daf0ecSJonathan Lemon sma_parse_inputs(const char *buf, enum ptp_ocp_sma_mode *mode) 1451e1daf0ecSJonathan Lemon { 1452e1daf0ecSJonathan Lemon struct ocp_selector *tbl[] = { ptp_ocp_sma_in, ptp_ocp_sma_out }; 1453e1daf0ecSJonathan Lemon int idx, count, dir; 1454e1daf0ecSJonathan Lemon char **argv; 1455e1daf0ecSJonathan Lemon int ret; 1456e1daf0ecSJonathan Lemon 1457e1daf0ecSJonathan Lemon argv = argv_split(GFP_KERNEL, buf, &count); 1458e1daf0ecSJonathan Lemon if (!argv) 1459e1daf0ecSJonathan Lemon return -ENOMEM; 1460e1daf0ecSJonathan Lemon 1461e1daf0ecSJonathan Lemon ret = -EINVAL; 1462e1daf0ecSJonathan Lemon if (!count) 1463e1daf0ecSJonathan Lemon goto out; 1464e1daf0ecSJonathan Lemon 1465e1daf0ecSJonathan Lemon idx = 0; 1466e1daf0ecSJonathan Lemon dir = *mode == SMA_MODE_IN ? 0 : 1; 1467e1daf0ecSJonathan Lemon if (!strcasecmp("IN:", argv[idx])) { 1468e1daf0ecSJonathan Lemon dir = 0; 1469e1daf0ecSJonathan Lemon idx++; 1470e1daf0ecSJonathan Lemon } 1471e1daf0ecSJonathan Lemon if (!strcasecmp("OUT:", argv[0])) { 1472e1daf0ecSJonathan Lemon dir = 1; 1473e1daf0ecSJonathan Lemon idx++; 1474e1daf0ecSJonathan Lemon } 1475e1daf0ecSJonathan Lemon *mode = dir == 0 ? SMA_MODE_IN : SMA_MODE_OUT; 1476e1daf0ecSJonathan Lemon 1477e1daf0ecSJonathan Lemon ret = 0; 1478e1daf0ecSJonathan Lemon for (; idx < count; idx++) 1479e1daf0ecSJonathan Lemon ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]); 1480e1daf0ecSJonathan Lemon if (ret < 0) 1481e1daf0ecSJonathan Lemon ret = -EINVAL; 1482e1daf0ecSJonathan Lemon 1483e1daf0ecSJonathan Lemon out: 1484e1daf0ecSJonathan Lemon argv_free(argv); 1485e1daf0ecSJonathan Lemon return ret; 1486e1daf0ecSJonathan Lemon } 1487e1daf0ecSJonathan Lemon 1488e1daf0ecSJonathan Lemon static ssize_t 1489e1daf0ecSJonathan Lemon ptp_ocp_sma_show(struct ptp_ocp *bp, int sma_nr, u32 val, char *buf, 1490e1daf0ecSJonathan Lemon const char *zero_in) 1491e1daf0ecSJonathan Lemon { 1492e1daf0ecSJonathan Lemon struct ptp_ocp_sma_connector *sma = &ptp_ocp_sma_map[sma_nr - 1]; 1493e1daf0ecSJonathan Lemon 1494e1daf0ecSJonathan Lemon if (sma->mode == SMA_MODE_IN) 1495e1daf0ecSJonathan Lemon return ptp_ocp_show_inputs(val, buf, zero_in); 1496e1daf0ecSJonathan Lemon 1497e1daf0ecSJonathan Lemon return ptp_ocp_show_output(val, buf, sma->default_out_idx); 1498e1daf0ecSJonathan Lemon } 1499e1daf0ecSJonathan Lemon 1500e1daf0ecSJonathan Lemon static ssize_t 1501e1daf0ecSJonathan Lemon sma1_show(struct device *dev, struct device_attribute *attr, char *buf) 1502e1daf0ecSJonathan Lemon { 1503e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1504e1daf0ecSJonathan Lemon u32 val; 1505e1daf0ecSJonathan Lemon 1506e1daf0ecSJonathan Lemon val = ioread32(&bp->sma->gpio1) & 0x3f; 1507e1daf0ecSJonathan Lemon return ptp_ocp_sma_show(bp, 1, val, buf, ptp_ocp_sma_in[0].name); 1508e1daf0ecSJonathan Lemon } 1509e1daf0ecSJonathan Lemon 1510e1daf0ecSJonathan Lemon static ssize_t 1511e1daf0ecSJonathan Lemon sma2_show(struct device *dev, struct device_attribute *attr, char *buf) 1512e1daf0ecSJonathan Lemon { 1513e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1514e1daf0ecSJonathan Lemon u32 val; 1515e1daf0ecSJonathan Lemon 1516e1daf0ecSJonathan Lemon val = (ioread32(&bp->sma->gpio1) >> 16) & 0x3f; 1517e1daf0ecSJonathan Lemon return ptp_ocp_sma_show(bp, 2, val, buf, NULL); 1518e1daf0ecSJonathan Lemon } 1519e1daf0ecSJonathan Lemon 1520e1daf0ecSJonathan Lemon static ssize_t 1521e1daf0ecSJonathan Lemon sma3_show(struct device *dev, struct device_attribute *attr, char *buf) 1522e1daf0ecSJonathan Lemon { 1523e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1524e1daf0ecSJonathan Lemon u32 val; 1525e1daf0ecSJonathan Lemon 1526e1daf0ecSJonathan Lemon val = ioread32(&bp->sma->gpio2) & 0x3f; 1527e1daf0ecSJonathan Lemon return ptp_ocp_sma_show(bp, 3, val, buf, NULL); 1528e1daf0ecSJonathan Lemon } 1529e1daf0ecSJonathan Lemon 1530e1daf0ecSJonathan Lemon static ssize_t 1531e1daf0ecSJonathan Lemon sma4_show(struct device *dev, struct device_attribute *attr, char *buf) 1532e1daf0ecSJonathan Lemon { 1533e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1534e1daf0ecSJonathan Lemon u32 val; 1535e1daf0ecSJonathan Lemon 1536e1daf0ecSJonathan Lemon val = (ioread32(&bp->sma->gpio2) >> 16) & 0x3f; 1537e1daf0ecSJonathan Lemon return ptp_ocp_sma_show(bp, 4, val, buf, NULL); 1538e1daf0ecSJonathan Lemon } 1539e1daf0ecSJonathan Lemon 1540e1daf0ecSJonathan Lemon static void 1541e1daf0ecSJonathan Lemon ptp_ocp_sma_store_output(struct ptp_ocp *bp, u32 val, u32 shift) 1542e1daf0ecSJonathan Lemon { 1543e1daf0ecSJonathan Lemon unsigned long flags; 1544e1daf0ecSJonathan Lemon u32 gpio, mask; 1545e1daf0ecSJonathan Lemon 1546e1daf0ecSJonathan Lemon mask = 0xffff << (16 - shift); 1547e1daf0ecSJonathan Lemon 1548e1daf0ecSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1549e1daf0ecSJonathan Lemon 1550e1daf0ecSJonathan Lemon gpio = ioread32(&bp->sma->gpio2); 1551e1daf0ecSJonathan Lemon gpio = (gpio & mask) | (val << shift); 15526baf2925SJonathan Lemon 15536baf2925SJonathan Lemon __handle_signal_outputs(bp, gpio); 15546baf2925SJonathan Lemon 1555e1daf0ecSJonathan Lemon iowrite32(gpio, &bp->sma->gpio2); 1556e1daf0ecSJonathan Lemon 1557e1daf0ecSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1558e1daf0ecSJonathan Lemon } 1559e1daf0ecSJonathan Lemon 1560e1daf0ecSJonathan Lemon static void 1561e1daf0ecSJonathan Lemon ptp_ocp_sma_store_inputs(struct ptp_ocp *bp, u32 val, u32 shift) 1562e1daf0ecSJonathan Lemon { 1563e1daf0ecSJonathan Lemon unsigned long flags; 1564e1daf0ecSJonathan Lemon u32 gpio, mask; 1565e1daf0ecSJonathan Lemon 1566e1daf0ecSJonathan Lemon mask = 0xffff << (16 - shift); 1567e1daf0ecSJonathan Lemon 1568e1daf0ecSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1569e1daf0ecSJonathan Lemon 1570e1daf0ecSJonathan Lemon gpio = ioread32(&bp->sma->gpio1); 1571e1daf0ecSJonathan Lemon gpio = (gpio & mask) | (val << shift); 15726baf2925SJonathan Lemon 15736baf2925SJonathan Lemon __handle_signal_inputs(bp, gpio); 15746baf2925SJonathan Lemon 1575e1daf0ecSJonathan Lemon iowrite32(gpio, &bp->sma->gpio1); 1576e1daf0ecSJonathan Lemon 1577e1daf0ecSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1578e1daf0ecSJonathan Lemon } 1579e1daf0ecSJonathan Lemon 1580e1daf0ecSJonathan Lemon static ssize_t 1581e1daf0ecSJonathan Lemon ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr, u32 shift) 1582e1daf0ecSJonathan Lemon { 1583e1daf0ecSJonathan Lemon struct ptp_ocp_sma_connector *sma = &ptp_ocp_sma_map[sma_nr - 1]; 1584e1daf0ecSJonathan Lemon enum ptp_ocp_sma_mode mode; 1585e1daf0ecSJonathan Lemon int val; 1586e1daf0ecSJonathan Lemon 1587e1daf0ecSJonathan Lemon mode = sma->mode; 1588e1daf0ecSJonathan Lemon val = sma_parse_inputs(buf, &mode); 1589e1daf0ecSJonathan Lemon if (val < 0) 1590e1daf0ecSJonathan Lemon return val; 1591e1daf0ecSJonathan Lemon 1592e1daf0ecSJonathan Lemon if (mode != sma->mode && sma->fixed_mode) 1593e1daf0ecSJonathan Lemon return -EOPNOTSUPP; 1594e1daf0ecSJonathan Lemon 1595e1daf0ecSJonathan Lemon if (mode != sma->mode) { 1596e1daf0ecSJonathan Lemon pr_err("Mode changes not supported yet.\n"); 1597e1daf0ecSJonathan Lemon return -EOPNOTSUPP; 1598e1daf0ecSJonathan Lemon } 1599e1daf0ecSJonathan Lemon 1600e1daf0ecSJonathan Lemon if (sma->mode == SMA_MODE_IN) 1601e1daf0ecSJonathan Lemon ptp_ocp_sma_store_inputs(bp, val, shift); 1602e1daf0ecSJonathan Lemon else 1603e1daf0ecSJonathan Lemon ptp_ocp_sma_store_output(bp, val, shift); 1604e1daf0ecSJonathan Lemon 1605e1daf0ecSJonathan Lemon return 0; 1606e1daf0ecSJonathan Lemon } 1607e1daf0ecSJonathan Lemon 1608e1daf0ecSJonathan Lemon static ssize_t 1609e1daf0ecSJonathan Lemon sma1_store(struct device *dev, struct device_attribute *attr, 1610e1daf0ecSJonathan Lemon const char *buf, size_t count) 1611e1daf0ecSJonathan Lemon { 1612e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1613e1daf0ecSJonathan Lemon int err; 1614e1daf0ecSJonathan Lemon 1615e1daf0ecSJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 1, 0); 1616e1daf0ecSJonathan Lemon return err ? err : count; 1617e1daf0ecSJonathan Lemon } 1618e1daf0ecSJonathan Lemon 1619e1daf0ecSJonathan Lemon static ssize_t 1620e1daf0ecSJonathan Lemon sma2_store(struct device *dev, struct device_attribute *attr, 1621e1daf0ecSJonathan Lemon const char *buf, size_t count) 1622e1daf0ecSJonathan Lemon { 1623e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1624e1daf0ecSJonathan Lemon int err; 1625e1daf0ecSJonathan Lemon 1626e1daf0ecSJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 2, 16); 1627e1daf0ecSJonathan Lemon return err ? err : count; 1628e1daf0ecSJonathan Lemon } 1629e1daf0ecSJonathan Lemon 1630e1daf0ecSJonathan Lemon static ssize_t 1631e1daf0ecSJonathan Lemon sma3_store(struct device *dev, struct device_attribute *attr, 1632e1daf0ecSJonathan Lemon const char *buf, size_t count) 1633e1daf0ecSJonathan Lemon { 1634e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1635e1daf0ecSJonathan Lemon int err; 1636e1daf0ecSJonathan Lemon 1637e1daf0ecSJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 3, 0); 1638e1daf0ecSJonathan Lemon return err ? err : count; 1639e1daf0ecSJonathan Lemon } 1640e1daf0ecSJonathan Lemon 1641e1daf0ecSJonathan Lemon static ssize_t 1642e1daf0ecSJonathan Lemon sma4_store(struct device *dev, struct device_attribute *attr, 1643e1daf0ecSJonathan Lemon const char *buf, size_t count) 1644e1daf0ecSJonathan Lemon { 1645e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1646e1daf0ecSJonathan Lemon int err; 1647e1daf0ecSJonathan Lemon 1648e1daf0ecSJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 4, 16); 1649e1daf0ecSJonathan Lemon return err ? err : count; 1650e1daf0ecSJonathan Lemon } 1651e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma1); 1652e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma2); 1653e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma3); 1654e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma4); 1655e1daf0ecSJonathan Lemon 1656e1daf0ecSJonathan Lemon static ssize_t 1657e1daf0ecSJonathan Lemon available_sma_inputs_show(struct device *dev, 1658e1daf0ecSJonathan Lemon struct device_attribute *attr, char *buf) 1659e1daf0ecSJonathan Lemon { 1660e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_sma_in, buf); 1661e1daf0ecSJonathan Lemon } 1662e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_inputs); 1663e1daf0ecSJonathan Lemon 1664e1daf0ecSJonathan Lemon static ssize_t 1665e1daf0ecSJonathan Lemon available_sma_outputs_show(struct device *dev, 1666e1daf0ecSJonathan Lemon struct device_attribute *attr, char *buf) 1667e1daf0ecSJonathan Lemon { 1668e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_sma_out, buf); 1669e1daf0ecSJonathan Lemon } 1670e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_outputs); 1671e1daf0ecSJonathan Lemon 1672773bda96SJonathan Lemon static ssize_t 1673773bda96SJonathan Lemon serialnum_show(struct device *dev, struct device_attribute *attr, char *buf) 1674773bda96SJonathan Lemon { 1675773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1676773bda96SJonathan Lemon 1677773bda96SJonathan Lemon if (!bp->has_serial) 1678773bda96SJonathan Lemon ptp_ocp_get_serial_number(bp); 1679773bda96SJonathan Lemon 1680773bda96SJonathan Lemon return sysfs_emit(buf, "%pM\n", bp->serial); 1681773bda96SJonathan Lemon } 1682773bda96SJonathan Lemon static DEVICE_ATTR_RO(serialnum); 1683773bda96SJonathan Lemon 1684773bda96SJonathan Lemon static ssize_t 1685ef0cfb34SJonathan Lemon gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf) 1686773bda96SJonathan Lemon { 1687773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1688773bda96SJonathan Lemon ssize_t ret; 1689773bda96SJonathan Lemon 1690ef0cfb34SJonathan Lemon if (bp->gnss_lost) 1691ef0cfb34SJonathan Lemon ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost); 1692773bda96SJonathan Lemon else 1693773bda96SJonathan Lemon ret = sysfs_emit(buf, "SYNC\n"); 1694773bda96SJonathan Lemon 1695773bda96SJonathan Lemon return ret; 1696773bda96SJonathan Lemon } 1697ef0cfb34SJonathan Lemon static DEVICE_ATTR_RO(gnss_sync); 1698773bda96SJonathan Lemon 1699773bda96SJonathan Lemon static ssize_t 170089260d87SJonathan Lemon utc_tai_offset_show(struct device *dev, 170189260d87SJonathan Lemon struct device_attribute *attr, char *buf) 170289260d87SJonathan Lemon { 170389260d87SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 170489260d87SJonathan Lemon 170589260d87SJonathan Lemon return sysfs_emit(buf, "%d\n", bp->utc_tai_offset); 170689260d87SJonathan Lemon } 170789260d87SJonathan Lemon 170889260d87SJonathan Lemon static ssize_t 170989260d87SJonathan Lemon utc_tai_offset_store(struct device *dev, 171089260d87SJonathan Lemon struct device_attribute *attr, 171189260d87SJonathan Lemon const char *buf, size_t count) 171289260d87SJonathan Lemon { 171389260d87SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 171489260d87SJonathan Lemon int err; 171589260d87SJonathan Lemon u32 val; 171689260d87SJonathan Lemon 171789260d87SJonathan Lemon err = kstrtou32(buf, 0, &val); 171889260d87SJonathan Lemon if (err) 171989260d87SJonathan Lemon return err; 172089260d87SJonathan Lemon 172189260d87SJonathan Lemon ptp_ocp_utc_distribute(bp, val); 172289260d87SJonathan Lemon 172389260d87SJonathan Lemon return count; 172489260d87SJonathan Lemon } 172589260d87SJonathan Lemon static DEVICE_ATTR_RW(utc_tai_offset); 172689260d87SJonathan Lemon 172789260d87SJonathan Lemon static ssize_t 1728d14ee252SJonathan Lemon irig_b_mode_show(struct device *dev, struct device_attribute *attr, char *buf) 1729d14ee252SJonathan Lemon { 1730d14ee252SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1731d14ee252SJonathan Lemon u32 val; 1732d14ee252SJonathan Lemon 1733d14ee252SJonathan Lemon val = ioread32(&bp->irig_out->ctrl); 1734d14ee252SJonathan Lemon val = (val >> 16) & 0x07; 1735d14ee252SJonathan Lemon return sysfs_emit(buf, "%d\n", val); 1736d14ee252SJonathan Lemon } 1737d14ee252SJonathan Lemon 1738d14ee252SJonathan Lemon static ssize_t 1739d14ee252SJonathan Lemon irig_b_mode_store(struct device *dev, 1740d14ee252SJonathan Lemon struct device_attribute *attr, 1741d14ee252SJonathan Lemon const char *buf, size_t count) 1742d14ee252SJonathan Lemon { 1743d14ee252SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1744d14ee252SJonathan Lemon unsigned long flags; 1745d14ee252SJonathan Lemon int err; 1746d14ee252SJonathan Lemon u32 reg; 1747d14ee252SJonathan Lemon u8 val; 1748d14ee252SJonathan Lemon 1749d14ee252SJonathan Lemon err = kstrtou8(buf, 0, &val); 1750d14ee252SJonathan Lemon if (err) 1751d14ee252SJonathan Lemon return err; 1752d14ee252SJonathan Lemon if (val > 7) 1753d14ee252SJonathan Lemon return -EINVAL; 1754d14ee252SJonathan Lemon 1755d14ee252SJonathan Lemon reg = ((val & 0x7) << 16); 1756d14ee252SJonathan Lemon 1757d14ee252SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1758d14ee252SJonathan Lemon iowrite32(0, &bp->irig_out->ctrl); /* disable */ 1759d14ee252SJonathan Lemon iowrite32(reg, &bp->irig_out->ctrl); /* change mode */ 1760d14ee252SJonathan Lemon iowrite32(reg | IRIG_M_CTRL_ENABLE, &bp->irig_out->ctrl); 1761d14ee252SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1762d14ee252SJonathan Lemon 1763d14ee252SJonathan Lemon return count; 1764d14ee252SJonathan Lemon } 1765d14ee252SJonathan Lemon static DEVICE_ATTR_RW(irig_b_mode); 1766d14ee252SJonathan Lemon 1767d14ee252SJonathan Lemon static ssize_t 1768773bda96SJonathan Lemon clock_source_show(struct device *dev, struct device_attribute *attr, char *buf) 1769773bda96SJonathan Lemon { 1770773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1771773bda96SJonathan Lemon const char *p; 1772773bda96SJonathan Lemon u32 select; 1773773bda96SJonathan Lemon 1774773bda96SJonathan Lemon select = ioread32(&bp->reg->select); 1775e1daf0ecSJonathan Lemon p = ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16); 1776773bda96SJonathan Lemon 1777773bda96SJonathan Lemon return sysfs_emit(buf, "%s\n", p); 1778773bda96SJonathan Lemon } 1779773bda96SJonathan Lemon 1780773bda96SJonathan Lemon static ssize_t 1781773bda96SJonathan Lemon clock_source_store(struct device *dev, struct device_attribute *attr, 1782773bda96SJonathan Lemon const char *buf, size_t count) 1783773bda96SJonathan Lemon { 1784773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1785773bda96SJonathan Lemon unsigned long flags; 1786773bda96SJonathan Lemon int val; 1787773bda96SJonathan Lemon 1788e1daf0ecSJonathan Lemon val = ptp_ocp_select_val_from_name(ptp_ocp_clock, buf); 1789773bda96SJonathan Lemon if (val < 0) 1790773bda96SJonathan Lemon return val; 1791773bda96SJonathan Lemon 1792773bda96SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1793773bda96SJonathan Lemon iowrite32(val, &bp->reg->select); 1794773bda96SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1795773bda96SJonathan Lemon 1796773bda96SJonathan Lemon return count; 1797773bda96SJonathan Lemon } 1798773bda96SJonathan Lemon static DEVICE_ATTR_RW(clock_source); 1799773bda96SJonathan Lemon 1800773bda96SJonathan Lemon static ssize_t 1801773bda96SJonathan Lemon available_clock_sources_show(struct device *dev, 1802773bda96SJonathan Lemon struct device_attribute *attr, char *buf) 1803773bda96SJonathan Lemon { 1804e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_clock, buf); 1805773bda96SJonathan Lemon } 1806773bda96SJonathan Lemon static DEVICE_ATTR_RO(available_clock_sources); 1807773bda96SJonathan Lemon 1808773bda96SJonathan Lemon static struct attribute *timecard_attrs[] = { 1809773bda96SJonathan Lemon &dev_attr_serialnum.attr, 1810ef0cfb34SJonathan Lemon &dev_attr_gnss_sync.attr, 1811773bda96SJonathan Lemon &dev_attr_clock_source.attr, 1812773bda96SJonathan Lemon &dev_attr_available_clock_sources.attr, 1813e1daf0ecSJonathan Lemon &dev_attr_sma1.attr, 1814e1daf0ecSJonathan Lemon &dev_attr_sma2.attr, 1815e1daf0ecSJonathan Lemon &dev_attr_sma3.attr, 1816e1daf0ecSJonathan Lemon &dev_attr_sma4.attr, 1817e1daf0ecSJonathan Lemon &dev_attr_available_sma_inputs.attr, 1818e1daf0ecSJonathan Lemon &dev_attr_available_sma_outputs.attr, 1819d14ee252SJonathan Lemon &dev_attr_irig_b_mode.attr, 182089260d87SJonathan Lemon &dev_attr_utc_tai_offset.attr, 1821773bda96SJonathan Lemon NULL, 1822773bda96SJonathan Lemon }; 1823773bda96SJonathan Lemon ATTRIBUTE_GROUPS(timecard); 1824773bda96SJonathan Lemon 1825*f67bf662SJonathan Lemon static const char * 1826*f67bf662SJonathan Lemon gpio_map(u32 gpio, u32 bit, const char *pri, const char *sec, const char *def) 1827*f67bf662SJonathan Lemon { 1828*f67bf662SJonathan Lemon const char *ans; 1829*f67bf662SJonathan Lemon 1830*f67bf662SJonathan Lemon if (gpio & (1 << bit)) 1831*f67bf662SJonathan Lemon ans = pri; 1832*f67bf662SJonathan Lemon else if (gpio & (1 << (bit + 16))) 1833*f67bf662SJonathan Lemon ans = sec; 1834*f67bf662SJonathan Lemon else 1835*f67bf662SJonathan Lemon ans = def; 1836*f67bf662SJonathan Lemon return ans; 1837*f67bf662SJonathan Lemon } 1838*f67bf662SJonathan Lemon 1839*f67bf662SJonathan Lemon static void 1840*f67bf662SJonathan Lemon gpio_multi_map(char *buf, u32 gpio, u32 bit, 1841*f67bf662SJonathan Lemon const char *pri, const char *sec, const char *def) 1842*f67bf662SJonathan Lemon { 1843*f67bf662SJonathan Lemon char *ans = buf; 1844*f67bf662SJonathan Lemon 1845*f67bf662SJonathan Lemon strcpy(ans, def); 1846*f67bf662SJonathan Lemon if (gpio & (1 << bit)) 1847*f67bf662SJonathan Lemon ans += sprintf(ans, "%s ", pri); 1848*f67bf662SJonathan Lemon if (gpio & (1 << (bit + 16))) 1849*f67bf662SJonathan Lemon ans += sprintf(ans, "%s ", sec); 1850*f67bf662SJonathan Lemon } 1851*f67bf662SJonathan Lemon 1852*f67bf662SJonathan Lemon static int 1853*f67bf662SJonathan Lemon ptp_ocp_summary_show(struct seq_file *s, void *data) 1854*f67bf662SJonathan Lemon { 1855*f67bf662SJonathan Lemon struct device *dev = s->private; 1856*f67bf662SJonathan Lemon struct ptp_system_timestamp sts; 1857*f67bf662SJonathan Lemon u32 sma_in, sma_out, ctrl, val; 1858*f67bf662SJonathan Lemon struct ts_reg __iomem *ts_reg; 1859*f67bf662SJonathan Lemon struct timespec64 ts; 1860*f67bf662SJonathan Lemon struct ptp_ocp *bp; 1861*f67bf662SJonathan Lemon const char *src; 1862*f67bf662SJonathan Lemon char *buf; 1863*f67bf662SJonathan Lemon bool on; 1864*f67bf662SJonathan Lemon 1865*f67bf662SJonathan Lemon buf = (char *)__get_free_page(GFP_KERNEL); 1866*f67bf662SJonathan Lemon if (!buf) 1867*f67bf662SJonathan Lemon return -ENOMEM; 1868*f67bf662SJonathan Lemon 1869*f67bf662SJonathan Lemon bp = dev_get_drvdata(dev); 1870*f67bf662SJonathan Lemon sma_in = ioread32(&bp->sma->gpio1); 1871*f67bf662SJonathan Lemon sma_out = ioread32(&bp->sma->gpio2); 1872*f67bf662SJonathan Lemon 1873*f67bf662SJonathan Lemon seq_printf(s, "%7s: /dev/ptp%d\n", "PTP", ptp_clock_index(bp->ptp)); 1874*f67bf662SJonathan Lemon 1875*f67bf662SJonathan Lemon sma1_show(dev, NULL, buf); 1876*f67bf662SJonathan Lemon seq_printf(s, " sma1: %s", buf); 1877*f67bf662SJonathan Lemon 1878*f67bf662SJonathan Lemon sma2_show(dev, NULL, buf); 1879*f67bf662SJonathan Lemon seq_printf(s, " sma2: %s", buf); 1880*f67bf662SJonathan Lemon 1881*f67bf662SJonathan Lemon sma3_show(dev, NULL, buf); 1882*f67bf662SJonathan Lemon seq_printf(s, " sma3: %s", buf); 1883*f67bf662SJonathan Lemon 1884*f67bf662SJonathan Lemon sma4_show(dev, NULL, buf); 1885*f67bf662SJonathan Lemon seq_printf(s, " sma4: %s", buf); 1886*f67bf662SJonathan Lemon 1887*f67bf662SJonathan Lemon if (bp->ts0) { 1888*f67bf662SJonathan Lemon ts_reg = bp->ts0->mem; 1889*f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 1890*f67bf662SJonathan Lemon src = "GNSS"; 1891*f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS0", 1892*f67bf662SJonathan Lemon on ? " ON" : "OFF", src); 1893*f67bf662SJonathan Lemon } 1894*f67bf662SJonathan Lemon 1895*f67bf662SJonathan Lemon if (bp->ts1) { 1896*f67bf662SJonathan Lemon ts_reg = bp->ts1->mem; 1897*f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 1898*f67bf662SJonathan Lemon src = gpio_map(sma_in, 2, "sma1", "sma2", "----"); 1899*f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS1", 1900*f67bf662SJonathan Lemon on ? " ON" : "OFF", src); 1901*f67bf662SJonathan Lemon } 1902*f67bf662SJonathan Lemon 1903*f67bf662SJonathan Lemon if (bp->ts2) { 1904*f67bf662SJonathan Lemon ts_reg = bp->ts2->mem; 1905*f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 1906*f67bf662SJonathan Lemon src = gpio_map(sma_in, 3, "sma1", "sma2", "----"); 1907*f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS2", 1908*f67bf662SJonathan Lemon on ? " ON" : "OFF", src); 1909*f67bf662SJonathan Lemon } 1910*f67bf662SJonathan Lemon 1911*f67bf662SJonathan Lemon if (bp->irig_out) { 1912*f67bf662SJonathan Lemon ctrl = ioread32(&bp->irig_out->ctrl); 1913*f67bf662SJonathan Lemon on = ctrl & IRIG_M_CTRL_ENABLE; 1914*f67bf662SJonathan Lemon val = ioread32(&bp->irig_out->status); 1915*f67bf662SJonathan Lemon gpio_multi_map(buf, sma_out, 4, "sma3", "sma4", "----"); 1916*f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, mode %d, out: %s\n", "IRIG", 1917*f67bf662SJonathan Lemon on ? " ON" : "OFF", val, (ctrl >> 16), buf); 1918*f67bf662SJonathan Lemon } 1919*f67bf662SJonathan Lemon 1920*f67bf662SJonathan Lemon if (bp->irig_in) { 1921*f67bf662SJonathan Lemon on = ioread32(&bp->irig_in->ctrl) & IRIG_S_CTRL_ENABLE; 1922*f67bf662SJonathan Lemon val = ioread32(&bp->irig_in->status); 1923*f67bf662SJonathan Lemon src = gpio_map(sma_in, 4, "sma1", "sma2", "----"); 1924*f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, src: %s\n", "IRIG in", 1925*f67bf662SJonathan Lemon on ? " ON" : "OFF", val, src); 1926*f67bf662SJonathan Lemon } 1927*f67bf662SJonathan Lemon 1928*f67bf662SJonathan Lemon if (bp->dcf_out) { 1929*f67bf662SJonathan Lemon on = ioread32(&bp->dcf_out->ctrl) & DCF_M_CTRL_ENABLE; 1930*f67bf662SJonathan Lemon val = ioread32(&bp->dcf_out->status); 1931*f67bf662SJonathan Lemon gpio_multi_map(buf, sma_out, 5, "sma3", "sma4", "----"); 1932*f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, out: %s\n", "DCF", 1933*f67bf662SJonathan Lemon on ? " ON" : "OFF", val, buf); 1934*f67bf662SJonathan Lemon } 1935*f67bf662SJonathan Lemon 1936*f67bf662SJonathan Lemon if (bp->dcf_in) { 1937*f67bf662SJonathan Lemon on = ioread32(&bp->dcf_in->ctrl) & DCF_S_CTRL_ENABLE; 1938*f67bf662SJonathan Lemon val = ioread32(&bp->dcf_in->status); 1939*f67bf662SJonathan Lemon src = gpio_map(sma_in, 5, "sma1", "sma2", "----"); 1940*f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, src: %s\n", "DCF in", 1941*f67bf662SJonathan Lemon on ? " ON" : "OFF", val, src); 1942*f67bf662SJonathan Lemon } 1943*f67bf662SJonathan Lemon 1944*f67bf662SJonathan Lemon /* compute src for PPS1, used below. */ 1945*f67bf662SJonathan Lemon if (bp->pps_select) { 1946*f67bf662SJonathan Lemon val = ioread32(&bp->pps_select->gpio1); 1947*f67bf662SJonathan Lemon if (val & 0x01) 1948*f67bf662SJonathan Lemon src = gpio_map(sma_in, 0, "sma1", "sma2", "----"); 1949*f67bf662SJonathan Lemon else if (val & 0x02) 1950*f67bf662SJonathan Lemon src = "MAC"; 1951*f67bf662SJonathan Lemon else if (val & 0x04) 1952*f67bf662SJonathan Lemon src = "GNSS"; 1953*f67bf662SJonathan Lemon else 1954*f67bf662SJonathan Lemon src = "----"; 1955*f67bf662SJonathan Lemon } else { 1956*f67bf662SJonathan Lemon src = "?"; 1957*f67bf662SJonathan Lemon } 1958*f67bf662SJonathan Lemon 1959*f67bf662SJonathan Lemon /* assumes automatic switchover/selection */ 1960*f67bf662SJonathan Lemon val = ioread32(&bp->reg->select); 1961*f67bf662SJonathan Lemon switch (val >> 16) { 1962*f67bf662SJonathan Lemon case 0: 1963*f67bf662SJonathan Lemon sprintf(buf, "----"); 1964*f67bf662SJonathan Lemon break; 1965*f67bf662SJonathan Lemon case 2: 1966*f67bf662SJonathan Lemon sprintf(buf, "IRIG"); 1967*f67bf662SJonathan Lemon break; 1968*f67bf662SJonathan Lemon case 3: 1969*f67bf662SJonathan Lemon sprintf(buf, "%s via PPS1", src); 1970*f67bf662SJonathan Lemon break; 1971*f67bf662SJonathan Lemon case 6: 1972*f67bf662SJonathan Lemon sprintf(buf, "DCF"); 1973*f67bf662SJonathan Lemon break; 1974*f67bf662SJonathan Lemon default: 1975*f67bf662SJonathan Lemon strcpy(buf, "unknown"); 1976*f67bf662SJonathan Lemon break; 1977*f67bf662SJonathan Lemon } 1978*f67bf662SJonathan Lemon val = ioread32(&bp->reg->status); 1979*f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, state: %s\n", "PHC src", buf, 1980*f67bf662SJonathan Lemon val & OCP_STATUS_IN_SYNC ? "sync" : "unsynced"); 1981*f67bf662SJonathan Lemon 1982*f67bf662SJonathan Lemon /* reuses PPS1 src from earlier */ 1983*f67bf662SJonathan Lemon seq_printf(s, "MAC PPS1 src: %s\n", src); 1984*f67bf662SJonathan Lemon 1985*f67bf662SJonathan Lemon src = gpio_map(sma_in, 1, "sma1", "sma2", "GNSS2"); 1986*f67bf662SJonathan Lemon seq_printf(s, "MAC PPS2 src: %s\n", src); 1987*f67bf662SJonathan Lemon 1988*f67bf662SJonathan Lemon if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts)) { 1989*f67bf662SJonathan Lemon struct timespec64 sys_ts; 1990*f67bf662SJonathan Lemon s64 pre_ns, post_ns, ns; 1991*f67bf662SJonathan Lemon 1992*f67bf662SJonathan Lemon pre_ns = timespec64_to_ns(&sts.pre_ts); 1993*f67bf662SJonathan Lemon post_ns = timespec64_to_ns(&sts.post_ts); 1994*f67bf662SJonathan Lemon ns = (pre_ns + post_ns) / 2; 1995*f67bf662SJonathan Lemon ns += (s64)bp->utc_tai_offset * NSEC_PER_SEC; 1996*f67bf662SJonathan Lemon sys_ts = ns_to_timespec64(ns); 1997*f67bf662SJonathan Lemon 1998*f67bf662SJonathan Lemon seq_printf(s, "%7s: %lld.%ld == %ptT TAI\n", "PHC", 1999*f67bf662SJonathan Lemon ts.tv_sec, ts.tv_nsec, &ts); 2000*f67bf662SJonathan Lemon seq_printf(s, "%7s: %lld.%ld == %ptT UTC offset %d\n", "SYS", 2001*f67bf662SJonathan Lemon sys_ts.tv_sec, sys_ts.tv_nsec, &sys_ts, 2002*f67bf662SJonathan Lemon bp->utc_tai_offset); 2003*f67bf662SJonathan Lemon seq_printf(s, "%7s: PHC:SYS offset: %lld window: %lld\n", "", 2004*f67bf662SJonathan Lemon timespec64_to_ns(&ts) - ns, 2005*f67bf662SJonathan Lemon post_ns - pre_ns); 2006*f67bf662SJonathan Lemon } 2007*f67bf662SJonathan Lemon 2008*f67bf662SJonathan Lemon free_page((unsigned long)buf); 2009*f67bf662SJonathan Lemon return 0; 2010*f67bf662SJonathan Lemon } 2011*f67bf662SJonathan Lemon DEFINE_SHOW_ATTRIBUTE(ptp_ocp_summary); 2012*f67bf662SJonathan Lemon 2013*f67bf662SJonathan Lemon static struct dentry *ptp_ocp_debugfs_root; 2014*f67bf662SJonathan Lemon 2015*f67bf662SJonathan Lemon static void 2016*f67bf662SJonathan Lemon ptp_ocp_debugfs_add_device(struct ptp_ocp *bp) 2017*f67bf662SJonathan Lemon { 2018*f67bf662SJonathan Lemon struct dentry *d; 2019*f67bf662SJonathan Lemon 2020*f67bf662SJonathan Lemon d = debugfs_create_dir(dev_name(&bp->dev), ptp_ocp_debugfs_root); 2021*f67bf662SJonathan Lemon bp->debug_root = d; 2022*f67bf662SJonathan Lemon debugfs_create_file("summary", 0444, bp->debug_root, 2023*f67bf662SJonathan Lemon &bp->dev, &ptp_ocp_summary_fops); 2024*f67bf662SJonathan Lemon } 2025*f67bf662SJonathan Lemon 2026*f67bf662SJonathan Lemon static void 2027*f67bf662SJonathan Lemon ptp_ocp_debugfs_remove_device(struct ptp_ocp *bp) 2028*f67bf662SJonathan Lemon { 2029*f67bf662SJonathan Lemon debugfs_remove_recursive(bp->debug_root); 2030*f67bf662SJonathan Lemon } 2031*f67bf662SJonathan Lemon 2032*f67bf662SJonathan Lemon static void 2033*f67bf662SJonathan Lemon ptp_ocp_debugfs_init(void) 2034*f67bf662SJonathan Lemon { 2035*f67bf662SJonathan Lemon ptp_ocp_debugfs_root = debugfs_create_dir("timecard", NULL); 2036*f67bf662SJonathan Lemon } 2037*f67bf662SJonathan Lemon 2038*f67bf662SJonathan Lemon static void 2039*f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(void) 2040*f67bf662SJonathan Lemon { 2041*f67bf662SJonathan Lemon debugfs_remove_recursive(ptp_ocp_debugfs_root); 2042*f67bf662SJonathan Lemon } 2043*f67bf662SJonathan Lemon 2044773bda96SJonathan Lemon static void 2045773bda96SJonathan Lemon ptp_ocp_dev_release(struct device *dev) 2046773bda96SJonathan Lemon { 2047773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2048773bda96SJonathan Lemon 2049773bda96SJonathan Lemon mutex_lock(&ptp_ocp_lock); 2050773bda96SJonathan Lemon idr_remove(&ptp_ocp_idr, bp->id); 2051773bda96SJonathan Lemon mutex_unlock(&ptp_ocp_lock); 2052773bda96SJonathan Lemon } 2053773bda96SJonathan Lemon 2054773bda96SJonathan Lemon static int 2055773bda96SJonathan Lemon ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev) 2056773bda96SJonathan Lemon { 2057773bda96SJonathan Lemon int err; 2058773bda96SJonathan Lemon 2059773bda96SJonathan Lemon mutex_lock(&ptp_ocp_lock); 2060773bda96SJonathan Lemon err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL); 2061773bda96SJonathan Lemon mutex_unlock(&ptp_ocp_lock); 2062773bda96SJonathan Lemon if (err < 0) { 2063773bda96SJonathan Lemon dev_err(&pdev->dev, "idr_alloc failed: %d\n", err); 2064773bda96SJonathan Lemon return err; 2065773bda96SJonathan Lemon } 2066773bda96SJonathan Lemon bp->id = err; 2067773bda96SJonathan Lemon 2068773bda96SJonathan Lemon bp->ptp_info = ptp_ocp_clock_info; 2069773bda96SJonathan Lemon spin_lock_init(&bp->lock); 2070ef0cfb34SJonathan Lemon bp->gnss_port = -1; 2071773bda96SJonathan Lemon bp->mac_port = -1; 2072773bda96SJonathan Lemon bp->pdev = pdev; 2073773bda96SJonathan Lemon 2074773bda96SJonathan Lemon device_initialize(&bp->dev); 2075773bda96SJonathan Lemon dev_set_name(&bp->dev, "ocp%d", bp->id); 2076773bda96SJonathan Lemon bp->dev.class = &timecard_class; 2077773bda96SJonathan Lemon bp->dev.parent = &pdev->dev; 2078773bda96SJonathan Lemon bp->dev.release = ptp_ocp_dev_release; 2079773bda96SJonathan Lemon dev_set_drvdata(&bp->dev, bp); 2080773bda96SJonathan Lemon 2081773bda96SJonathan Lemon err = device_add(&bp->dev); 2082773bda96SJonathan Lemon if (err) { 2083773bda96SJonathan Lemon dev_err(&bp->dev, "device add failed: %d\n", err); 2084773bda96SJonathan Lemon goto out; 2085773bda96SJonathan Lemon } 2086773bda96SJonathan Lemon 2087773bda96SJonathan Lemon pci_set_drvdata(pdev, bp); 2088773bda96SJonathan Lemon 2089773bda96SJonathan Lemon return 0; 2090773bda96SJonathan Lemon 2091773bda96SJonathan Lemon out: 2092773bda96SJonathan Lemon ptp_ocp_dev_release(&bp->dev); 2093d12f23faSJonathan Lemon put_device(&bp->dev); 2094773bda96SJonathan Lemon return err; 2095773bda96SJonathan Lemon } 2096773bda96SJonathan Lemon 2097773bda96SJonathan Lemon static void 2098773bda96SJonathan Lemon ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link) 2099773bda96SJonathan Lemon { 2100773bda96SJonathan Lemon struct device *dev = &bp->dev; 2101773bda96SJonathan Lemon 2102773bda96SJonathan Lemon if (sysfs_create_link(&dev->kobj, &child->kobj, link)) 2103773bda96SJonathan Lemon dev_err(dev, "%s symlink failed\n", link); 2104773bda96SJonathan Lemon } 2105773bda96SJonathan Lemon 2106773bda96SJonathan Lemon static void 2107773bda96SJonathan Lemon ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link) 2108773bda96SJonathan Lemon { 2109773bda96SJonathan Lemon struct device *dev, *child; 2110773bda96SJonathan Lemon 2111773bda96SJonathan Lemon dev = &bp->pdev->dev; 2112773bda96SJonathan Lemon 2113773bda96SJonathan Lemon child = device_find_child_by_name(dev, name); 2114773bda96SJonathan Lemon if (!child) { 2115773bda96SJonathan Lemon dev_err(dev, "Could not find device %s\n", name); 2116773bda96SJonathan Lemon return; 2117773bda96SJonathan Lemon } 2118773bda96SJonathan Lemon 2119773bda96SJonathan Lemon ptp_ocp_symlink(bp, child, link); 2120773bda96SJonathan Lemon put_device(child); 2121773bda96SJonathan Lemon } 2122773bda96SJonathan Lemon 2123773bda96SJonathan Lemon static int 2124773bda96SJonathan Lemon ptp_ocp_complete(struct ptp_ocp *bp) 2125773bda96SJonathan Lemon { 2126773bda96SJonathan Lemon struct pps_device *pps; 2127773bda96SJonathan Lemon char buf[32]; 2128773bda96SJonathan Lemon 2129ef0cfb34SJonathan Lemon if (bp->gnss_port != -1) { 2130ef0cfb34SJonathan Lemon sprintf(buf, "ttyS%d", bp->gnss_port); 2131ef0cfb34SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyGNSS"); 2132773bda96SJonathan Lemon } 2133773bda96SJonathan Lemon if (bp->mac_port != -1) { 2134773bda96SJonathan Lemon sprintf(buf, "ttyS%d", bp->mac_port); 2135773bda96SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyMAC"); 2136773bda96SJonathan Lemon } 2137773bda96SJonathan Lemon sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp)); 2138773bda96SJonathan Lemon ptp_ocp_link_child(bp, buf, "ptp"); 2139773bda96SJonathan Lemon 2140773bda96SJonathan Lemon pps = pps_lookup_dev(bp->ptp); 2141773bda96SJonathan Lemon if (pps) 2142773bda96SJonathan Lemon ptp_ocp_symlink(bp, pps->dev, "pps"); 2143773bda96SJonathan Lemon 2144773bda96SJonathan Lemon if (device_add_groups(&bp->dev, timecard_groups)) 2145773bda96SJonathan Lemon pr_err("device add groups failed\n"); 2146773bda96SJonathan Lemon 2147*f67bf662SJonathan Lemon ptp_ocp_debugfs_add_device(bp); 2148*f67bf662SJonathan Lemon 2149773bda96SJonathan Lemon return 0; 2150773bda96SJonathan Lemon } 2151773bda96SJonathan Lemon 2152773bda96SJonathan Lemon static void 2153065efcc5SJonathan Lemon ptp_ocp_phc_info(struct ptp_ocp *bp) 2154065efcc5SJonathan Lemon { 2155065efcc5SJonathan Lemon struct timespec64 ts; 2156065efcc5SJonathan Lemon u32 version, select; 2157065efcc5SJonathan Lemon bool sync; 2158065efcc5SJonathan Lemon 2159065efcc5SJonathan Lemon version = ioread32(&bp->reg->version); 2160065efcc5SJonathan Lemon select = ioread32(&bp->reg->select); 2161065efcc5SJonathan Lemon dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n", 2162065efcc5SJonathan Lemon version >> 24, (version >> 16) & 0xff, version & 0xffff, 2163065efcc5SJonathan Lemon ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16), 2164065efcc5SJonathan Lemon ptp_clock_index(bp->ptp)); 2165065efcc5SJonathan Lemon 2166065efcc5SJonathan Lemon sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC; 2167065efcc5SJonathan Lemon if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL)) 2168065efcc5SJonathan Lemon dev_info(&bp->pdev->dev, "Time: %lld.%ld, %s\n", 2169065efcc5SJonathan Lemon ts.tv_sec, ts.tv_nsec, 2170065efcc5SJonathan Lemon sync ? "in-sync" : "UNSYNCED"); 2171065efcc5SJonathan Lemon } 2172065efcc5SJonathan Lemon 2173065efcc5SJonathan Lemon static void 2174065efcc5SJonathan Lemon ptp_ocp_serial_info(struct device *dev, const char *name, int port, int baud) 2175065efcc5SJonathan Lemon { 2176065efcc5SJonathan Lemon if (port != -1) 2177065efcc5SJonathan Lemon dev_info(dev, "%5s: /dev/ttyS%-2d @ %6d\n", name, port, baud); 2178065efcc5SJonathan Lemon } 2179065efcc5SJonathan Lemon 2180065efcc5SJonathan Lemon static void 2181065efcc5SJonathan Lemon ptp_ocp_info(struct ptp_ocp *bp) 2182773bda96SJonathan Lemon { 2183773bda96SJonathan Lemon struct device *dev = &bp->pdev->dev; 2184773bda96SJonathan Lemon 2185065efcc5SJonathan Lemon ptp_ocp_phc_info(bp); 2186065efcc5SJonathan Lemon if (bp->tod) 2187065efcc5SJonathan Lemon ptp_ocp_tod_info(bp); 2188065efcc5SJonathan Lemon 2189773bda96SJonathan Lemon if (bp->image) { 2190773bda96SJonathan Lemon u32 ver = ioread32(&bp->image->version); 2191773bda96SJonathan Lemon 2192773bda96SJonathan Lemon dev_info(dev, "version %x\n", ver); 2193773bda96SJonathan Lemon if (ver & 0xffff) 2194773bda96SJonathan Lemon dev_info(dev, "regular image, version %d\n", 2195773bda96SJonathan Lemon ver & 0xffff); 2196773bda96SJonathan Lemon else 2197773bda96SJonathan Lemon dev_info(dev, "golden image, version %d\n", 2198773bda96SJonathan Lemon ver >> 16); 2199773bda96SJonathan Lemon } 2200065efcc5SJonathan Lemon ptp_ocp_serial_info(dev, "GNSS", bp->gnss_port, 115200); 2201065efcc5SJonathan Lemon ptp_ocp_serial_info(dev, "MAC", bp->mac_port, 57600); 2202773bda96SJonathan Lemon } 2203773bda96SJonathan Lemon 2204773bda96SJonathan Lemon static void 2205773bda96SJonathan Lemon ptp_ocp_detach_sysfs(struct ptp_ocp *bp) 2206773bda96SJonathan Lemon { 2207773bda96SJonathan Lemon struct device *dev = &bp->dev; 2208773bda96SJonathan Lemon 2209ef0cfb34SJonathan Lemon sysfs_remove_link(&dev->kobj, "ttyGNSS"); 2210773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "ttyMAC"); 2211773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "ptp"); 2212773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "pps"); 2213773bda96SJonathan Lemon device_remove_groups(dev, timecard_groups); 2214773bda96SJonathan Lemon } 2215773bda96SJonathan Lemon 2216773bda96SJonathan Lemon static void 2217773bda96SJonathan Lemon ptp_ocp_detach(struct ptp_ocp *bp) 2218773bda96SJonathan Lemon { 2219*f67bf662SJonathan Lemon ptp_ocp_debugfs_remove_device(bp); 2220773bda96SJonathan Lemon ptp_ocp_detach_sysfs(bp); 2221773bda96SJonathan Lemon if (timer_pending(&bp->watchdog)) 2222773bda96SJonathan Lemon del_timer_sync(&bp->watchdog); 2223773bda96SJonathan Lemon if (bp->ts0) 2224773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->ts0); 2225773bda96SJonathan Lemon if (bp->ts1) 2226773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->ts1); 2227dcf61469SJonathan Lemon if (bp->ts2) 2228dcf61469SJonathan Lemon ptp_ocp_unregister_ext(bp->ts2); 2229773bda96SJonathan Lemon if (bp->pps) 2230773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->pps); 2231ef0cfb34SJonathan Lemon if (bp->gnss_port != -1) 2232ef0cfb34SJonathan Lemon serial8250_unregister_port(bp->gnss_port); 2233773bda96SJonathan Lemon if (bp->mac_port != -1) 2234773bda96SJonathan Lemon serial8250_unregister_port(bp->mac_port); 2235773bda96SJonathan Lemon if (bp->spi_flash) 2236773bda96SJonathan Lemon platform_device_unregister(bp->spi_flash); 2237773bda96SJonathan Lemon if (bp->i2c_ctrl) 2238773bda96SJonathan Lemon platform_device_unregister(bp->i2c_ctrl); 2239773bda96SJonathan Lemon if (bp->i2c_clk) 2240773bda96SJonathan Lemon clk_hw_unregister_fixed_rate(bp->i2c_clk); 2241773bda96SJonathan Lemon if (bp->n_irqs) 2242773bda96SJonathan Lemon pci_free_irq_vectors(bp->pdev); 2243773bda96SJonathan Lemon if (bp->ptp) 2244773bda96SJonathan Lemon ptp_clock_unregister(bp->ptp); 2245773bda96SJonathan Lemon device_unregister(&bp->dev); 2246773bda96SJonathan Lemon } 2247773bda96SJonathan Lemon 2248a7e1abadSJonathan Lemon static int 2249a7e1abadSJonathan Lemon ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id) 2250a7e1abadSJonathan Lemon { 2251773bda96SJonathan Lemon struct devlink *devlink; 2252a7e1abadSJonathan Lemon struct ptp_ocp *bp; 2253a7e1abadSJonathan Lemon int err; 2254a7e1abadSJonathan Lemon 2255919d13a7SLeon Romanovsky devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp), &pdev->dev); 2256773bda96SJonathan Lemon if (!devlink) { 2257773bda96SJonathan Lemon dev_err(&pdev->dev, "devlink_alloc failed\n"); 2258a7e1abadSJonathan Lemon return -ENOMEM; 2259773bda96SJonathan Lemon } 2260773bda96SJonathan Lemon 2261919d13a7SLeon Romanovsky err = devlink_register(devlink); 2262773bda96SJonathan Lemon if (err) 2263773bda96SJonathan Lemon goto out_free; 2264a7e1abadSJonathan Lemon 2265a7e1abadSJonathan Lemon err = pci_enable_device(pdev); 2266a7e1abadSJonathan Lemon if (err) { 2267a7e1abadSJonathan Lemon dev_err(&pdev->dev, "pci_enable_device\n"); 2268773bda96SJonathan Lemon goto out_unregister; 2269a7e1abadSJonathan Lemon } 2270a7e1abadSJonathan Lemon 2271773bda96SJonathan Lemon bp = devlink_priv(devlink); 2272773bda96SJonathan Lemon err = ptp_ocp_device_init(bp, pdev); 2273773bda96SJonathan Lemon if (err) 2274d9fdbf13SJonathan Lemon goto out_disable; 2275a7e1abadSJonathan Lemon 2276773bda96SJonathan Lemon /* compat mode. 2277773bda96SJonathan Lemon * Older FPGA firmware only returns 2 irq's. 2278773bda96SJonathan Lemon * allow this - if not all of the IRQ's are returned, skip the 2279773bda96SJonathan Lemon * extra devices and just register the clock. 2280773bda96SJonathan Lemon */ 2281773bda96SJonathan Lemon err = pci_alloc_irq_vectors(pdev, 1, 10, PCI_IRQ_MSI | PCI_IRQ_MSIX); 2282773bda96SJonathan Lemon if (err < 0) { 2283773bda96SJonathan Lemon dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err); 2284773bda96SJonathan Lemon goto out; 2285a7e1abadSJonathan Lemon } 2286773bda96SJonathan Lemon bp->n_irqs = err; 2287773bda96SJonathan Lemon pci_set_master(pdev); 2288a7e1abadSJonathan Lemon 2289773bda96SJonathan Lemon err = ptp_ocp_register_resources(bp, id->driver_data); 2290a7e1abadSJonathan Lemon if (err) 2291a7e1abadSJonathan Lemon goto out; 2292a7e1abadSJonathan Lemon 2293a7e1abadSJonathan Lemon bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev); 2294a7e1abadSJonathan Lemon if (IS_ERR(bp->ptp)) { 2295a7e1abadSJonathan Lemon err = PTR_ERR(bp->ptp); 2296773bda96SJonathan Lemon dev_err(&pdev->dev, "ptp_clock_register: %d\n", err); 2297773bda96SJonathan Lemon bp->ptp = NULL; 2298a7e1abadSJonathan Lemon goto out; 2299a7e1abadSJonathan Lemon } 2300a7e1abadSJonathan Lemon 2301773bda96SJonathan Lemon err = ptp_ocp_complete(bp); 2302773bda96SJonathan Lemon if (err) 2303773bda96SJonathan Lemon goto out; 2304773bda96SJonathan Lemon 2305a7e1abadSJonathan Lemon ptp_ocp_info(bp); 2306a7e1abadSJonathan Lemon 2307a7e1abadSJonathan Lemon return 0; 2308a7e1abadSJonathan Lemon 2309a7e1abadSJonathan Lemon out: 2310773bda96SJonathan Lemon ptp_ocp_detach(bp); 2311773bda96SJonathan Lemon pci_set_drvdata(pdev, NULL); 2312d9fdbf13SJonathan Lemon out_disable: 2313d9fdbf13SJonathan Lemon pci_disable_device(pdev); 2314773bda96SJonathan Lemon out_unregister: 2315919d13a7SLeon Romanovsky devlink_unregister(devlink); 2316a7e1abadSJonathan Lemon out_free: 2317773bda96SJonathan Lemon devlink_free(devlink); 2318a7e1abadSJonathan Lemon 2319a7e1abadSJonathan Lemon return err; 2320a7e1abadSJonathan Lemon } 2321a7e1abadSJonathan Lemon 2322a7e1abadSJonathan Lemon static void 2323a7e1abadSJonathan Lemon ptp_ocp_remove(struct pci_dev *pdev) 2324a7e1abadSJonathan Lemon { 2325a7e1abadSJonathan Lemon struct ptp_ocp *bp = pci_get_drvdata(pdev); 2326773bda96SJonathan Lemon struct devlink *devlink = priv_to_devlink(bp); 2327a7e1abadSJonathan Lemon 2328773bda96SJonathan Lemon ptp_ocp_detach(bp); 2329a7e1abadSJonathan Lemon pci_set_drvdata(pdev, NULL); 2330d9fdbf13SJonathan Lemon pci_disable_device(pdev); 2331773bda96SJonathan Lemon 2332919d13a7SLeon Romanovsky devlink_unregister(devlink); 2333773bda96SJonathan Lemon devlink_free(devlink); 2334a7e1abadSJonathan Lemon } 2335a7e1abadSJonathan Lemon 2336a7e1abadSJonathan Lemon static struct pci_driver ptp_ocp_driver = { 2337a7e1abadSJonathan Lemon .name = KBUILD_MODNAME, 2338a7e1abadSJonathan Lemon .id_table = ptp_ocp_pcidev_id, 2339a7e1abadSJonathan Lemon .probe = ptp_ocp_probe, 2340a7e1abadSJonathan Lemon .remove = ptp_ocp_remove, 2341a7e1abadSJonathan Lemon }; 2342a7e1abadSJonathan Lemon 2343773bda96SJonathan Lemon static int 2344773bda96SJonathan Lemon ptp_ocp_i2c_notifier_call(struct notifier_block *nb, 2345773bda96SJonathan Lemon unsigned long action, void *data) 2346773bda96SJonathan Lemon { 2347773bda96SJonathan Lemon struct device *dev, *child = data; 2348773bda96SJonathan Lemon struct ptp_ocp *bp; 2349773bda96SJonathan Lemon bool add; 2350773bda96SJonathan Lemon 2351773bda96SJonathan Lemon switch (action) { 2352773bda96SJonathan Lemon case BUS_NOTIFY_ADD_DEVICE: 2353773bda96SJonathan Lemon case BUS_NOTIFY_DEL_DEVICE: 2354773bda96SJonathan Lemon add = action == BUS_NOTIFY_ADD_DEVICE; 2355773bda96SJonathan Lemon break; 2356773bda96SJonathan Lemon default: 2357773bda96SJonathan Lemon return 0; 2358773bda96SJonathan Lemon } 2359773bda96SJonathan Lemon 2360773bda96SJonathan Lemon if (!i2c_verify_adapter(child)) 2361773bda96SJonathan Lemon return 0; 2362773bda96SJonathan Lemon 2363773bda96SJonathan Lemon dev = child; 2364773bda96SJonathan Lemon while ((dev = dev->parent)) 2365773bda96SJonathan Lemon if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME)) 2366773bda96SJonathan Lemon goto found; 2367773bda96SJonathan Lemon return 0; 2368773bda96SJonathan Lemon 2369773bda96SJonathan Lemon found: 2370773bda96SJonathan Lemon bp = dev_get_drvdata(dev); 2371773bda96SJonathan Lemon if (add) 2372773bda96SJonathan Lemon ptp_ocp_symlink(bp, child, "i2c"); 2373773bda96SJonathan Lemon else 2374773bda96SJonathan Lemon sysfs_remove_link(&bp->dev.kobj, "i2c"); 2375773bda96SJonathan Lemon 2376773bda96SJonathan Lemon return 0; 2377773bda96SJonathan Lemon } 2378773bda96SJonathan Lemon 2379773bda96SJonathan Lemon static struct notifier_block ptp_ocp_i2c_notifier = { 2380773bda96SJonathan Lemon .notifier_call = ptp_ocp_i2c_notifier_call, 2381773bda96SJonathan Lemon }; 2382773bda96SJonathan Lemon 2383a7e1abadSJonathan Lemon static int __init 2384a7e1abadSJonathan Lemon ptp_ocp_init(void) 2385a7e1abadSJonathan Lemon { 2386773bda96SJonathan Lemon const char *what; 2387a7e1abadSJonathan Lemon int err; 2388a7e1abadSJonathan Lemon 2389*f67bf662SJonathan Lemon ptp_ocp_debugfs_init(); 2390*f67bf662SJonathan Lemon 2391773bda96SJonathan Lemon what = "timecard class"; 2392773bda96SJonathan Lemon err = class_register(&timecard_class); 2393773bda96SJonathan Lemon if (err) 2394773bda96SJonathan Lemon goto out; 2395773bda96SJonathan Lemon 2396773bda96SJonathan Lemon what = "i2c notifier"; 2397773bda96SJonathan Lemon err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2398773bda96SJonathan Lemon if (err) 2399773bda96SJonathan Lemon goto out_notifier; 2400773bda96SJonathan Lemon 2401773bda96SJonathan Lemon what = "ptp_ocp driver"; 2402a7e1abadSJonathan Lemon err = pci_register_driver(&ptp_ocp_driver); 2403773bda96SJonathan Lemon if (err) 2404773bda96SJonathan Lemon goto out_register; 2405773bda96SJonathan Lemon 2406773bda96SJonathan Lemon return 0; 2407773bda96SJonathan Lemon 2408773bda96SJonathan Lemon out_register: 2409773bda96SJonathan Lemon bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2410773bda96SJonathan Lemon out_notifier: 2411773bda96SJonathan Lemon class_unregister(&timecard_class); 2412773bda96SJonathan Lemon out: 2413*f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(); 2414773bda96SJonathan Lemon pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err); 2415a7e1abadSJonathan Lemon return err; 2416a7e1abadSJonathan Lemon } 2417a7e1abadSJonathan Lemon 2418a7e1abadSJonathan Lemon static void __exit 2419a7e1abadSJonathan Lemon ptp_ocp_fini(void) 2420a7e1abadSJonathan Lemon { 2421773bda96SJonathan Lemon bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2422a7e1abadSJonathan Lemon pci_unregister_driver(&ptp_ocp_driver); 2423773bda96SJonathan Lemon class_unregister(&timecard_class); 2424*f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(); 2425a7e1abadSJonathan Lemon } 2426a7e1abadSJonathan Lemon 2427a7e1abadSJonathan Lemon module_init(ptp_ocp_init); 2428a7e1abadSJonathan Lemon module_exit(ptp_ocp_fini); 2429a7e1abadSJonathan Lemon 2430a7e1abadSJonathan Lemon MODULE_DESCRIPTION("OpenCompute TimeCard driver"); 2431a7e1abadSJonathan Lemon MODULE_LICENSE("GPL v2"); 2432