1a7e1abadSJonathan Lemon // SPDX-License-Identifier: GPL-2.0-only 2a7e1abadSJonathan Lemon /* Copyright (c) 2020 Facebook */ 3a7e1abadSJonathan Lemon 4a7e1abadSJonathan Lemon #include <linux/err.h> 5a7e1abadSJonathan Lemon #include <linux/kernel.h> 6a7e1abadSJonathan Lemon #include <linux/module.h> 7f67bf662SJonathan Lemon #include <linux/debugfs.h> 8a7e1abadSJonathan Lemon #include <linux/init.h> 9a7e1abadSJonathan Lemon #include <linux/pci.h> 10773bda96SJonathan Lemon #include <linux/serial_8250.h> 11773bda96SJonathan Lemon #include <linux/clkdev.h> 12773bda96SJonathan Lemon #include <linux/clk-provider.h> 13773bda96SJonathan Lemon #include <linux/platform_device.h> 140cfcdd1eSJonathan Lemon #include <linux/platform_data/i2c-xiic.h> 15a7e1abadSJonathan Lemon #include <linux/ptp_clock_kernel.h> 16773bda96SJonathan Lemon #include <linux/spi/spi.h> 17773bda96SJonathan Lemon #include <linux/spi/xilinx_spi.h> 18773bda96SJonathan Lemon #include <net/devlink.h> 19773bda96SJonathan Lemon #include <linux/i2c.h> 20773bda96SJonathan Lemon #include <linux/mtd/mtd.h> 210cfcdd1eSJonathan Lemon #include <linux/nvmem-consumer.h> 22a7e1abadSJonathan Lemon 23773bda96SJonathan Lemon #ifndef PCI_VENDOR_ID_FACEBOOK 24773bda96SJonathan Lemon #define PCI_VENDOR_ID_FACEBOOK 0x1d9b 25773bda96SJonathan Lemon #endif 26773bda96SJonathan Lemon 27773bda96SJonathan Lemon #ifndef PCI_DEVICE_ID_FACEBOOK_TIMECARD 28773bda96SJonathan Lemon #define PCI_DEVICE_ID_FACEBOOK_TIMECARD 0x0400 29773bda96SJonathan Lemon #endif 30773bda96SJonathan Lemon 31773bda96SJonathan Lemon static struct class timecard_class = { 32773bda96SJonathan Lemon .owner = THIS_MODULE, 33773bda96SJonathan Lemon .name = "timecard", 34a7e1abadSJonathan Lemon }; 35a7e1abadSJonathan Lemon 36a7e1abadSJonathan Lemon struct ocp_reg { 37a7e1abadSJonathan Lemon u32 ctrl; 38a7e1abadSJonathan Lemon u32 status; 39a7e1abadSJonathan Lemon u32 select; 40a7e1abadSJonathan Lemon u32 version; 41a7e1abadSJonathan Lemon u32 time_ns; 42a7e1abadSJonathan Lemon u32 time_sec; 43a7e1abadSJonathan Lemon u32 __pad0[2]; 44a7e1abadSJonathan Lemon u32 adjust_ns; 45a7e1abadSJonathan Lemon u32 adjust_sec; 46a7e1abadSJonathan Lemon u32 __pad1[2]; 47a7e1abadSJonathan Lemon u32 offset_ns; 48a7e1abadSJonathan Lemon u32 offset_window_ns; 49773bda96SJonathan Lemon u32 __pad2[2]; 50773bda96SJonathan Lemon u32 drift_ns; 51773bda96SJonathan Lemon u32 drift_window_ns; 52773bda96SJonathan Lemon u32 __pad3[6]; 53773bda96SJonathan Lemon u32 servo_offset_p; 54773bda96SJonathan Lemon u32 servo_offset_i; 55773bda96SJonathan Lemon u32 servo_drift_p; 56773bda96SJonathan Lemon u32 servo_drift_i; 572f23f486SVadim Fedorenko u32 status_offset; 582f23f486SVadim Fedorenko u32 status_drift; 59a7e1abadSJonathan Lemon }; 60a7e1abadSJonathan Lemon 61a7e1abadSJonathan Lemon #define OCP_CTRL_ENABLE BIT(0) 62a7e1abadSJonathan Lemon #define OCP_CTRL_ADJUST_TIME BIT(1) 63a7e1abadSJonathan Lemon #define OCP_CTRL_ADJUST_OFFSET BIT(2) 64773bda96SJonathan Lemon #define OCP_CTRL_ADJUST_DRIFT BIT(3) 65773bda96SJonathan Lemon #define OCP_CTRL_ADJUST_SERVO BIT(8) 66a7e1abadSJonathan Lemon #define OCP_CTRL_READ_TIME_REQ BIT(30) 67a7e1abadSJonathan Lemon #define OCP_CTRL_READ_TIME_DONE BIT(31) 68a7e1abadSJonathan Lemon 69a7e1abadSJonathan Lemon #define OCP_STATUS_IN_SYNC BIT(0) 70773bda96SJonathan Lemon #define OCP_STATUS_IN_HOLDOVER BIT(1) 71a7e1abadSJonathan Lemon 72a7e1abadSJonathan Lemon #define OCP_SELECT_CLK_NONE 0 73773bda96SJonathan Lemon #define OCP_SELECT_CLK_REG 0xfe 74a7e1abadSJonathan Lemon 75a7e1abadSJonathan Lemon struct tod_reg { 76a7e1abadSJonathan Lemon u32 ctrl; 77a7e1abadSJonathan Lemon u32 status; 78a7e1abadSJonathan Lemon u32 uart_polarity; 79a7e1abadSJonathan Lemon u32 version; 80065efcc5SJonathan Lemon u32 adj_sec; 81a7e1abadSJonathan Lemon u32 __pad0[3]; 82a7e1abadSJonathan Lemon u32 uart_baud; 83a7e1abadSJonathan Lemon u32 __pad1[3]; 84a7e1abadSJonathan Lemon u32 utc_status; 85a7e1abadSJonathan Lemon u32 leap; 86a7e1abadSJonathan Lemon }; 87a7e1abadSJonathan Lemon 88a7e1abadSJonathan Lemon #define TOD_CTRL_PROTOCOL BIT(28) 89a7e1abadSJonathan Lemon #define TOD_CTRL_DISABLE_FMT_A BIT(17) 90a7e1abadSJonathan Lemon #define TOD_CTRL_DISABLE_FMT_B BIT(16) 91a7e1abadSJonathan Lemon #define TOD_CTRL_ENABLE BIT(0) 92a7e1abadSJonathan Lemon #define TOD_CTRL_GNSS_MASK ((1U << 4) - 1) 93a7e1abadSJonathan Lemon #define TOD_CTRL_GNSS_SHIFT 24 94a7e1abadSJonathan Lemon 95a7e1abadSJonathan Lemon #define TOD_STATUS_UTC_MASK 0xff 96a7e1abadSJonathan Lemon #define TOD_STATUS_UTC_VALID BIT(8) 979f492c4cSVadim Fedorenko #define TOD_STATUS_LEAP_ANNOUNCE BIT(12) 98a7e1abadSJonathan Lemon #define TOD_STATUS_LEAP_VALID BIT(16) 99a7e1abadSJonathan Lemon 100773bda96SJonathan Lemon struct ts_reg { 101773bda96SJonathan Lemon u32 enable; 102773bda96SJonathan Lemon u32 error; 103773bda96SJonathan Lemon u32 polarity; 104773bda96SJonathan Lemon u32 version; 105773bda96SJonathan Lemon u32 __pad0[4]; 106773bda96SJonathan Lemon u32 cable_delay; 107773bda96SJonathan Lemon u32 __pad1[3]; 108773bda96SJonathan Lemon u32 intr; 109773bda96SJonathan Lemon u32 intr_mask; 110773bda96SJonathan Lemon u32 event_count; 111773bda96SJonathan Lemon u32 __pad2[1]; 112773bda96SJonathan Lemon u32 ts_count; 113773bda96SJonathan Lemon u32 time_ns; 114773bda96SJonathan Lemon u32 time_sec; 115773bda96SJonathan Lemon u32 data_width; 116773bda96SJonathan Lemon u32 data; 117773bda96SJonathan Lemon }; 118773bda96SJonathan Lemon 119773bda96SJonathan Lemon struct pps_reg { 120773bda96SJonathan Lemon u32 ctrl; 121773bda96SJonathan Lemon u32 status; 1220d43d4f2SJonathan Lemon u32 __pad0[6]; 1230d43d4f2SJonathan Lemon u32 cable_delay; 124773bda96SJonathan Lemon }; 125773bda96SJonathan Lemon 126773bda96SJonathan Lemon #define PPS_STATUS_FILTER_ERR BIT(0) 127773bda96SJonathan Lemon #define PPS_STATUS_SUPERV_ERR BIT(1) 128773bda96SJonathan Lemon 129773bda96SJonathan Lemon struct img_reg { 130773bda96SJonathan Lemon u32 version; 131773bda96SJonathan Lemon }; 132773bda96SJonathan Lemon 133e1daf0ecSJonathan Lemon struct gpio_reg { 134e1daf0ecSJonathan Lemon u32 gpio1; 135e1daf0ecSJonathan Lemon u32 __pad0; 136e1daf0ecSJonathan Lemon u32 gpio2; 137e1daf0ecSJonathan Lemon u32 __pad1; 138e1daf0ecSJonathan Lemon }; 139e1daf0ecSJonathan Lemon 1406baf2925SJonathan Lemon struct irig_master_reg { 1416baf2925SJonathan Lemon u32 ctrl; 1426baf2925SJonathan Lemon u32 status; 1436baf2925SJonathan Lemon u32 __pad0; 1446baf2925SJonathan Lemon u32 version; 1456baf2925SJonathan Lemon u32 adj_sec; 1466baf2925SJonathan Lemon u32 mode_ctrl; 1476baf2925SJonathan Lemon }; 1486baf2925SJonathan Lemon 1496baf2925SJonathan Lemon #define IRIG_M_CTRL_ENABLE BIT(0) 1506baf2925SJonathan Lemon 1516baf2925SJonathan Lemon struct irig_slave_reg { 1526baf2925SJonathan Lemon u32 ctrl; 1536baf2925SJonathan Lemon u32 status; 1546baf2925SJonathan Lemon u32 __pad0; 1556baf2925SJonathan Lemon u32 version; 1566baf2925SJonathan Lemon u32 adj_sec; 1576baf2925SJonathan Lemon u32 mode_ctrl; 1586baf2925SJonathan Lemon }; 1596baf2925SJonathan Lemon 1606baf2925SJonathan Lemon #define IRIG_S_CTRL_ENABLE BIT(0) 1616baf2925SJonathan Lemon 1626baf2925SJonathan Lemon struct dcf_master_reg { 1636baf2925SJonathan Lemon u32 ctrl; 1646baf2925SJonathan Lemon u32 status; 1656baf2925SJonathan Lemon u32 __pad0; 1666baf2925SJonathan Lemon u32 version; 1676baf2925SJonathan Lemon u32 adj_sec; 1686baf2925SJonathan Lemon }; 1696baf2925SJonathan Lemon 1706baf2925SJonathan Lemon #define DCF_M_CTRL_ENABLE BIT(0) 1716baf2925SJonathan Lemon 1726baf2925SJonathan Lemon struct dcf_slave_reg { 1736baf2925SJonathan Lemon u32 ctrl; 1746baf2925SJonathan Lemon u32 status; 1756baf2925SJonathan Lemon u32 __pad0; 1766baf2925SJonathan Lemon u32 version; 1776baf2925SJonathan Lemon u32 adj_sec; 1786baf2925SJonathan Lemon }; 1796baf2925SJonathan Lemon 1806baf2925SJonathan Lemon #define DCF_S_CTRL_ENABLE BIT(0) 1816baf2925SJonathan Lemon 182773bda96SJonathan Lemon struct ptp_ocp_flash_info { 183773bda96SJonathan Lemon const char *name; 184773bda96SJonathan Lemon int pci_offset; 185773bda96SJonathan Lemon int data_size; 186773bda96SJonathan Lemon void *data; 187773bda96SJonathan Lemon }; 188773bda96SJonathan Lemon 1891618df6aSJonathan Lemon struct ptp_ocp_i2c_info { 1901618df6aSJonathan Lemon const char *name; 1911618df6aSJonathan Lemon unsigned long fixed_rate; 1921618df6aSJonathan Lemon size_t data_size; 1931618df6aSJonathan Lemon void *data; 1941618df6aSJonathan Lemon }; 1951618df6aSJonathan Lemon 196773bda96SJonathan Lemon struct ptp_ocp_ext_info { 197773bda96SJonathan Lemon int index; 198773bda96SJonathan Lemon irqreturn_t (*irq_fcn)(int irq, void *priv); 199a62a56d0SJonathan Lemon int (*enable)(void *priv, u32 req, bool enable); 200773bda96SJonathan Lemon }; 201773bda96SJonathan Lemon 202773bda96SJonathan Lemon struct ptp_ocp_ext_src { 203773bda96SJonathan Lemon void __iomem *mem; 204773bda96SJonathan Lemon struct ptp_ocp *bp; 205773bda96SJonathan Lemon struct ptp_ocp_ext_info *info; 206773bda96SJonathan Lemon int irq_vec; 207773bda96SJonathan Lemon }; 208773bda96SJonathan Lemon 2090cfcdd1eSJonathan Lemon #define OCP_BOARD_ID_LEN 13 2100cfcdd1eSJonathan Lemon #define OCP_SERIAL_LEN 6 2110cfcdd1eSJonathan Lemon 212a7e1abadSJonathan Lemon struct ptp_ocp { 213a7e1abadSJonathan Lemon struct pci_dev *pdev; 214773bda96SJonathan Lemon struct device dev; 215a7e1abadSJonathan Lemon spinlock_t lock; 216a7e1abadSJonathan Lemon struct ocp_reg __iomem *reg; 217a7e1abadSJonathan Lemon struct tod_reg __iomem *tod; 2180d43d4f2SJonathan Lemon struct pps_reg __iomem *pps_to_ext; 2190d43d4f2SJonathan Lemon struct pps_reg __iomem *pps_to_clk; 220f67bf662SJonathan Lemon struct gpio_reg __iomem *pps_select; 221e1daf0ecSJonathan Lemon struct gpio_reg __iomem *sma; 2226baf2925SJonathan Lemon struct irig_master_reg __iomem *irig_out; 2236baf2925SJonathan Lemon struct irig_slave_reg __iomem *irig_in; 2246baf2925SJonathan Lemon struct dcf_master_reg __iomem *dcf_out; 2256baf2925SJonathan Lemon struct dcf_slave_reg __iomem *dcf_in; 226e3516bb4SJonathan Lemon struct tod_reg __iomem *nmea_out; 227773bda96SJonathan Lemon struct ptp_ocp_ext_src *pps; 228773bda96SJonathan Lemon struct ptp_ocp_ext_src *ts0; 229773bda96SJonathan Lemon struct ptp_ocp_ext_src *ts1; 230dcf61469SJonathan Lemon struct ptp_ocp_ext_src *ts2; 231773bda96SJonathan Lemon struct img_reg __iomem *image; 232a7e1abadSJonathan Lemon struct ptp_clock *ptp; 233a7e1abadSJonathan Lemon struct ptp_clock_info ptp_info; 234773bda96SJonathan Lemon struct platform_device *i2c_ctrl; 235773bda96SJonathan Lemon struct platform_device *spi_flash; 236773bda96SJonathan Lemon struct clk_hw *i2c_clk; 237773bda96SJonathan Lemon struct timer_list watchdog; 2380cfcdd1eSJonathan Lemon const struct ptp_ocp_eeprom_map *eeprom_map; 239f67bf662SJonathan Lemon struct dentry *debug_root; 240ef0cfb34SJonathan Lemon time64_t gnss_lost; 241773bda96SJonathan Lemon int id; 242773bda96SJonathan Lemon int n_irqs; 243ef0cfb34SJonathan Lemon int gnss_port; 24471d7e085SJonathan Lemon int gnss2_port; 245773bda96SJonathan Lemon int mac_port; /* miniature atomic clock */ 246e3516bb4SJonathan Lemon int nmea_port; 247b0ca789aSJonathan Lemon u32 fw_version; 2480cfcdd1eSJonathan Lemon u8 board_id[OCP_BOARD_ID_LEN]; 2490cfcdd1eSJonathan Lemon u8 serial[OCP_SERIAL_LEN]; 2500cfcdd1eSJonathan Lemon bool has_eeprom_data; 251a62a56d0SJonathan Lemon u32 pps_req_map; 25289260d87SJonathan Lemon int flash_start; 25389260d87SJonathan Lemon u32 utc_tai_offset; 2541acffc6eSJonathan Lemon u32 ts_window_adjust; 255a7e1abadSJonathan Lemon }; 256a7e1abadSJonathan Lemon 257a62a56d0SJonathan Lemon #define OCP_REQ_TIMESTAMP BIT(0) 258a62a56d0SJonathan Lemon #define OCP_REQ_PPS BIT(1) 259a62a56d0SJonathan Lemon 260773bda96SJonathan Lemon struct ocp_resource { 261773bda96SJonathan Lemon unsigned long offset; 262773bda96SJonathan Lemon int size; 263773bda96SJonathan Lemon int irq_vec; 264773bda96SJonathan Lemon int (*setup)(struct ptp_ocp *bp, struct ocp_resource *r); 265773bda96SJonathan Lemon void *extra; 266773bda96SJonathan Lemon unsigned long bp_offset; 26756ec4403SJonathan Lemon const char * const name; 268773bda96SJonathan Lemon }; 269773bda96SJonathan Lemon 270773bda96SJonathan Lemon static int ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r); 271773bda96SJonathan Lemon static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r); 272773bda96SJonathan Lemon static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r); 273773bda96SJonathan Lemon static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r); 274773bda96SJonathan Lemon static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r); 275773bda96SJonathan Lemon static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r); 276773bda96SJonathan Lemon static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv); 277a62a56d0SJonathan Lemon static int ptp_ocp_ts_enable(void *priv, u32 req, bool enable); 278773bda96SJonathan Lemon 2790cfcdd1eSJonathan Lemon struct ptp_ocp_eeprom_map { 2800cfcdd1eSJonathan Lemon u16 off; 2810cfcdd1eSJonathan Lemon u16 len; 2820cfcdd1eSJonathan Lemon u32 bp_offset; 2830cfcdd1eSJonathan Lemon const void * const tag; 2840cfcdd1eSJonathan Lemon }; 2850cfcdd1eSJonathan Lemon 2860cfcdd1eSJonathan Lemon #define EEPROM_ENTRY(addr, member) \ 2870cfcdd1eSJonathan Lemon .off = addr, \ 2880cfcdd1eSJonathan Lemon .len = sizeof_field(struct ptp_ocp, member), \ 2890cfcdd1eSJonathan Lemon .bp_offset = offsetof(struct ptp_ocp, member) 2900cfcdd1eSJonathan Lemon 2910cfcdd1eSJonathan Lemon #define BP_MAP_ENTRY_ADDR(bp, map) ({ \ 2920cfcdd1eSJonathan Lemon (void *)((uintptr_t)(bp) + (map)->bp_offset); \ 2930cfcdd1eSJonathan Lemon }) 2940cfcdd1eSJonathan Lemon 2950cfcdd1eSJonathan Lemon static struct ptp_ocp_eeprom_map fb_eeprom_map[] = { 2960cfcdd1eSJonathan Lemon { EEPROM_ENTRY(0x43, board_id) }, 2970cfcdd1eSJonathan Lemon { EEPROM_ENTRY(0x00, serial), .tag = "mac" }, 2980cfcdd1eSJonathan Lemon { } 2990cfcdd1eSJonathan Lemon }; 3000cfcdd1eSJonathan Lemon 301773bda96SJonathan Lemon #define bp_assign_entry(bp, res, val) ({ \ 302773bda96SJonathan Lemon uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset; \ 303773bda96SJonathan Lemon *(typeof(val) *)addr = val; \ 304773bda96SJonathan Lemon }) 305773bda96SJonathan Lemon 306773bda96SJonathan Lemon #define OCP_RES_LOCATION(member) \ 30756ec4403SJonathan Lemon .name = #member, .bp_offset = offsetof(struct ptp_ocp, member) 308773bda96SJonathan Lemon 309773bda96SJonathan Lemon #define OCP_MEM_RESOURCE(member) \ 310773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_mem 311773bda96SJonathan Lemon 312773bda96SJonathan Lemon #define OCP_SERIAL_RESOURCE(member) \ 313773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_serial 314773bda96SJonathan Lemon 315773bda96SJonathan Lemon #define OCP_I2C_RESOURCE(member) \ 316773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_i2c 317773bda96SJonathan Lemon 318773bda96SJonathan Lemon #define OCP_SPI_RESOURCE(member) \ 319773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_spi 320773bda96SJonathan Lemon 321773bda96SJonathan Lemon #define OCP_EXT_RESOURCE(member) \ 322773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_ext 323773bda96SJonathan Lemon 324773bda96SJonathan Lemon /* This is the MSI vector mapping used. 325a62a56d0SJonathan Lemon * 0: TS3 (and PPS) 326773bda96SJonathan Lemon * 1: TS0 327773bda96SJonathan Lemon * 2: TS1 32871d7e085SJonathan Lemon * 3: GNSS 32971d7e085SJonathan Lemon * 4: GNSS2 330773bda96SJonathan Lemon * 5: MAC 331dcf61469SJonathan Lemon * 6: TS2 3321447149dSJonathan Lemon * 7: I2C controller 333e3516bb4SJonathan Lemon * 8: HWICAP (notused) 334773bda96SJonathan Lemon * 9: SPI Flash 335e3516bb4SJonathan Lemon * 10: NMEA 336773bda96SJonathan Lemon */ 337773bda96SJonathan Lemon 338773bda96SJonathan Lemon static struct ocp_resource ocp_fb_resource[] = { 339773bda96SJonathan Lemon { 340773bda96SJonathan Lemon OCP_MEM_RESOURCE(reg), 341773bda96SJonathan Lemon .offset = 0x01000000, .size = 0x10000, 342773bda96SJonathan Lemon }, 343773bda96SJonathan Lemon { 344773bda96SJonathan Lemon OCP_EXT_RESOURCE(ts0), 345773bda96SJonathan Lemon .offset = 0x01010000, .size = 0x10000, .irq_vec = 1, 346773bda96SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 34756ec4403SJonathan Lemon .index = 0, 348773bda96SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 349773bda96SJonathan Lemon .enable = ptp_ocp_ts_enable, 350773bda96SJonathan Lemon }, 351773bda96SJonathan Lemon }, 352773bda96SJonathan Lemon { 353773bda96SJonathan Lemon OCP_EXT_RESOURCE(ts1), 354773bda96SJonathan Lemon .offset = 0x01020000, .size = 0x10000, .irq_vec = 2, 355773bda96SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 35656ec4403SJonathan Lemon .index = 1, 357773bda96SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 358773bda96SJonathan Lemon .enable = ptp_ocp_ts_enable, 359773bda96SJonathan Lemon }, 360773bda96SJonathan Lemon }, 361773bda96SJonathan Lemon { 362dcf61469SJonathan Lemon OCP_EXT_RESOURCE(ts2), 363dcf61469SJonathan Lemon .offset = 0x01060000, .size = 0x10000, .irq_vec = 6, 364dcf61469SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 365dcf61469SJonathan Lemon .index = 2, 366dcf61469SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 367dcf61469SJonathan Lemon .enable = ptp_ocp_ts_enable, 368dcf61469SJonathan Lemon }, 369dcf61469SJonathan Lemon }, 370dcf61469SJonathan Lemon { 371a62a56d0SJonathan Lemon OCP_EXT_RESOURCE(pps), 372a62a56d0SJonathan Lemon .offset = 0x010C0000, .size = 0x10000, .irq_vec = 0, 373a62a56d0SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 374a62a56d0SJonathan Lemon .index = 3, 375a62a56d0SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 376a62a56d0SJonathan Lemon .enable = ptp_ocp_ts_enable, 377a62a56d0SJonathan Lemon }, 378a62a56d0SJonathan Lemon }, 379a62a56d0SJonathan Lemon { 3800d43d4f2SJonathan Lemon OCP_MEM_RESOURCE(pps_to_ext), 3810d43d4f2SJonathan Lemon .offset = 0x01030000, .size = 0x10000, 3820d43d4f2SJonathan Lemon }, 3830d43d4f2SJonathan Lemon { 3840d43d4f2SJonathan Lemon OCP_MEM_RESOURCE(pps_to_clk), 385773bda96SJonathan Lemon .offset = 0x01040000, .size = 0x10000, 386773bda96SJonathan Lemon }, 387773bda96SJonathan Lemon { 388773bda96SJonathan Lemon OCP_MEM_RESOURCE(tod), 389773bda96SJonathan Lemon .offset = 0x01050000, .size = 0x10000, 390773bda96SJonathan Lemon }, 391773bda96SJonathan Lemon { 3926baf2925SJonathan Lemon OCP_MEM_RESOURCE(irig_in), 3936baf2925SJonathan Lemon .offset = 0x01070000, .size = 0x10000, 3946baf2925SJonathan Lemon }, 3956baf2925SJonathan Lemon { 3966baf2925SJonathan Lemon OCP_MEM_RESOURCE(irig_out), 3976baf2925SJonathan Lemon .offset = 0x01080000, .size = 0x10000, 3986baf2925SJonathan Lemon }, 3996baf2925SJonathan Lemon { 4006baf2925SJonathan Lemon OCP_MEM_RESOURCE(dcf_in), 4016baf2925SJonathan Lemon .offset = 0x01090000, .size = 0x10000, 4026baf2925SJonathan Lemon }, 4036baf2925SJonathan Lemon { 4046baf2925SJonathan Lemon OCP_MEM_RESOURCE(dcf_out), 4056baf2925SJonathan Lemon .offset = 0x010A0000, .size = 0x10000, 4066baf2925SJonathan Lemon }, 4076baf2925SJonathan Lemon { 408e3516bb4SJonathan Lemon OCP_MEM_RESOURCE(nmea_out), 409e3516bb4SJonathan Lemon .offset = 0x010B0000, .size = 0x10000, 410e3516bb4SJonathan Lemon }, 411e3516bb4SJonathan Lemon { 412773bda96SJonathan Lemon OCP_MEM_RESOURCE(image), 413773bda96SJonathan Lemon .offset = 0x00020000, .size = 0x1000, 414773bda96SJonathan Lemon }, 415773bda96SJonathan Lemon { 416f67bf662SJonathan Lemon OCP_MEM_RESOURCE(pps_select), 417f67bf662SJonathan Lemon .offset = 0x00130000, .size = 0x1000, 418f67bf662SJonathan Lemon }, 419f67bf662SJonathan Lemon { 420e1daf0ecSJonathan Lemon OCP_MEM_RESOURCE(sma), 421e1daf0ecSJonathan Lemon .offset = 0x00140000, .size = 0x1000, 422e1daf0ecSJonathan Lemon }, 423e1daf0ecSJonathan Lemon { 424773bda96SJonathan Lemon OCP_I2C_RESOURCE(i2c_ctrl), 425773bda96SJonathan Lemon .offset = 0x00150000, .size = 0x10000, .irq_vec = 7, 4261618df6aSJonathan Lemon .extra = &(struct ptp_ocp_i2c_info) { 4271618df6aSJonathan Lemon .name = "xiic-i2c", 4281618df6aSJonathan Lemon .fixed_rate = 50000000, 4290cfcdd1eSJonathan Lemon .data_size = sizeof(struct xiic_i2c_platform_data), 4300cfcdd1eSJonathan Lemon .data = &(struct xiic_i2c_platform_data) { 4310cfcdd1eSJonathan Lemon .num_devices = 2, 4320cfcdd1eSJonathan Lemon .devices = (struct i2c_board_info[]) { 4330cfcdd1eSJonathan Lemon { I2C_BOARD_INFO("24c02", 0x50) }, 4340cfcdd1eSJonathan Lemon { I2C_BOARD_INFO("24mac402", 0x58), 4350cfcdd1eSJonathan Lemon .platform_data = "mac" }, 4360cfcdd1eSJonathan Lemon }, 4370cfcdd1eSJonathan Lemon }, 4381618df6aSJonathan Lemon }, 439773bda96SJonathan Lemon }, 440773bda96SJonathan Lemon { 441ef0cfb34SJonathan Lemon OCP_SERIAL_RESOURCE(gnss_port), 442773bda96SJonathan Lemon .offset = 0x00160000 + 0x1000, .irq_vec = 3, 443773bda96SJonathan Lemon }, 444773bda96SJonathan Lemon { 44571d7e085SJonathan Lemon OCP_SERIAL_RESOURCE(gnss2_port), 44671d7e085SJonathan Lemon .offset = 0x00170000 + 0x1000, .irq_vec = 4, 44771d7e085SJonathan Lemon }, 44871d7e085SJonathan Lemon { 449773bda96SJonathan Lemon OCP_SERIAL_RESOURCE(mac_port), 450773bda96SJonathan Lemon .offset = 0x00180000 + 0x1000, .irq_vec = 5, 451773bda96SJonathan Lemon }, 452773bda96SJonathan Lemon { 453e3516bb4SJonathan Lemon OCP_SERIAL_RESOURCE(nmea_port), 454e3516bb4SJonathan Lemon .offset = 0x00190000 + 0x1000, .irq_vec = 10, 455e3516bb4SJonathan Lemon }, 456e3516bb4SJonathan Lemon { 457773bda96SJonathan Lemon OCP_SPI_RESOURCE(spi_flash), 458773bda96SJonathan Lemon .offset = 0x00310000, .size = 0x10000, .irq_vec = 9, 459773bda96SJonathan Lemon .extra = &(struct ptp_ocp_flash_info) { 460773bda96SJonathan Lemon .name = "xilinx_spi", .pci_offset = 0, 461773bda96SJonathan Lemon .data_size = sizeof(struct xspi_platform_data), 462773bda96SJonathan Lemon .data = &(struct xspi_platform_data) { 463773bda96SJonathan Lemon .num_chipselect = 1, 464773bda96SJonathan Lemon .bits_per_word = 8, 465773bda96SJonathan Lemon .num_devices = 1, 466773bda96SJonathan Lemon .devices = &(struct spi_board_info) { 467773bda96SJonathan Lemon .modalias = "spi-nor", 468773bda96SJonathan Lemon }, 469773bda96SJonathan Lemon }, 470773bda96SJonathan Lemon }, 471773bda96SJonathan Lemon }, 472773bda96SJonathan Lemon { 473773bda96SJonathan Lemon .setup = ptp_ocp_fb_board_init, 474773bda96SJonathan Lemon }, 475773bda96SJonathan Lemon { } 476773bda96SJonathan Lemon }; 477773bda96SJonathan Lemon 478773bda96SJonathan Lemon static const struct pci_device_id ptp_ocp_pcidev_id[] = { 479773bda96SJonathan Lemon { PCI_DEVICE_DATA(FACEBOOK, TIMECARD, &ocp_fb_resource) }, 480773bda96SJonathan Lemon { 0 } 481773bda96SJonathan Lemon }; 482773bda96SJonathan Lemon MODULE_DEVICE_TABLE(pci, ptp_ocp_pcidev_id); 483773bda96SJonathan Lemon 484773bda96SJonathan Lemon static DEFINE_MUTEX(ptp_ocp_lock); 485773bda96SJonathan Lemon static DEFINE_IDR(ptp_ocp_idr); 486773bda96SJonathan Lemon 487e1daf0ecSJonathan Lemon struct ocp_selector { 488773bda96SJonathan Lemon const char *name; 489773bda96SJonathan Lemon int value; 490e1daf0ecSJonathan Lemon }; 491e1daf0ecSJonathan Lemon 492e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_clock[] = { 493773bda96SJonathan Lemon { .name = "NONE", .value = 0 }, 494773bda96SJonathan Lemon { .name = "TOD", .value = 1 }, 495773bda96SJonathan Lemon { .name = "IRIG", .value = 2 }, 496773bda96SJonathan Lemon { .name = "PPS", .value = 3 }, 497773bda96SJonathan Lemon { .name = "PTP", .value = 4 }, 498773bda96SJonathan Lemon { .name = "RTC", .value = 5 }, 499773bda96SJonathan Lemon { .name = "DCF", .value = 6 }, 500773bda96SJonathan Lemon { .name = "REGS", .value = 0xfe }, 501773bda96SJonathan Lemon { .name = "EXT", .value = 0xff }, 502e1daf0ecSJonathan Lemon { } 503e1daf0ecSJonathan Lemon }; 504e1daf0ecSJonathan Lemon 505e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_sma_in[] = { 506e1daf0ecSJonathan Lemon { .name = "10Mhz", .value = 0x00 }, 507e1daf0ecSJonathan Lemon { .name = "PPS1", .value = 0x01 }, 508e1daf0ecSJonathan Lemon { .name = "PPS2", .value = 0x02 }, 509e1daf0ecSJonathan Lemon { .name = "TS1", .value = 0x04 }, 510e1daf0ecSJonathan Lemon { .name = "TS2", .value = 0x08 }, 5116baf2925SJonathan Lemon { .name = "IRIG", .value = 0x10 }, 5126baf2925SJonathan Lemon { .name = "DCF", .value = 0x20 }, 513e1daf0ecSJonathan Lemon { } 514e1daf0ecSJonathan Lemon }; 515e1daf0ecSJonathan Lemon 516e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_sma_out[] = { 517e1daf0ecSJonathan Lemon { .name = "10Mhz", .value = 0x00 }, 518e1daf0ecSJonathan Lemon { .name = "PHC", .value = 0x01 }, 519e1daf0ecSJonathan Lemon { .name = "MAC", .value = 0x02 }, 520e1daf0ecSJonathan Lemon { .name = "GNSS", .value = 0x04 }, 521e1daf0ecSJonathan Lemon { .name = "GNSS2", .value = 0x08 }, 5226baf2925SJonathan Lemon { .name = "IRIG", .value = 0x10 }, 5236baf2925SJonathan Lemon { .name = "DCF", .value = 0x20 }, 524e1daf0ecSJonathan Lemon { } 525773bda96SJonathan Lemon }; 526773bda96SJonathan Lemon 527773bda96SJonathan Lemon static const char * 528e1daf0ecSJonathan Lemon ptp_ocp_select_name_from_val(struct ocp_selector *tbl, int val) 529773bda96SJonathan Lemon { 530773bda96SJonathan Lemon int i; 531773bda96SJonathan Lemon 532e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) 533e1daf0ecSJonathan Lemon if (tbl[i].value == val) 534e1daf0ecSJonathan Lemon return tbl[i].name; 535773bda96SJonathan Lemon return NULL; 536773bda96SJonathan Lemon } 537773bda96SJonathan Lemon 538773bda96SJonathan Lemon static int 539e1daf0ecSJonathan Lemon ptp_ocp_select_val_from_name(struct ocp_selector *tbl, const char *name) 540773bda96SJonathan Lemon { 541e1daf0ecSJonathan Lemon const char *select; 542773bda96SJonathan Lemon int i; 543773bda96SJonathan Lemon 544e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) { 545e1daf0ecSJonathan Lemon select = tbl[i].name; 546e1daf0ecSJonathan Lemon if (!strncasecmp(name, select, strlen(select))) 547e1daf0ecSJonathan Lemon return tbl[i].value; 548773bda96SJonathan Lemon } 549773bda96SJonathan Lemon return -EINVAL; 550773bda96SJonathan Lemon } 551773bda96SJonathan Lemon 552e1daf0ecSJonathan Lemon static ssize_t 553e1daf0ecSJonathan Lemon ptp_ocp_select_table_show(struct ocp_selector *tbl, char *buf) 554e1daf0ecSJonathan Lemon { 555e1daf0ecSJonathan Lemon ssize_t count; 556e1daf0ecSJonathan Lemon int i; 557e1daf0ecSJonathan Lemon 558e1daf0ecSJonathan Lemon count = 0; 559e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) 560e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", tbl[i].name); 561e1daf0ecSJonathan Lemon if (count) 562e1daf0ecSJonathan Lemon count--; 563e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "\n"); 564e1daf0ecSJonathan Lemon return count; 565e1daf0ecSJonathan Lemon } 566e1daf0ecSJonathan Lemon 567a7e1abadSJonathan Lemon static int 568a7e1abadSJonathan Lemon __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts, 569a7e1abadSJonathan Lemon struct ptp_system_timestamp *sts) 570a7e1abadSJonathan Lemon { 571a7e1abadSJonathan Lemon u32 ctrl, time_sec, time_ns; 572a7e1abadSJonathan Lemon int i; 573a7e1abadSJonathan Lemon 574a7e1abadSJonathan Lemon ptp_read_system_prets(sts); 5751acffc6eSJonathan Lemon 5761acffc6eSJonathan Lemon ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE; 577a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 578a7e1abadSJonathan Lemon 579a7e1abadSJonathan Lemon for (i = 0; i < 100; i++) { 580a7e1abadSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 581a7e1abadSJonathan Lemon if (ctrl & OCP_CTRL_READ_TIME_DONE) 582a7e1abadSJonathan Lemon break; 583a7e1abadSJonathan Lemon } 584a7e1abadSJonathan Lemon ptp_read_system_postts(sts); 585a7e1abadSJonathan Lemon 5861acffc6eSJonathan Lemon if (sts && bp->ts_window_adjust) { 5871acffc6eSJonathan Lemon s64 ns = timespec64_to_ns(&sts->post_ts); 5881acffc6eSJonathan Lemon 5891acffc6eSJonathan Lemon sts->post_ts = ns_to_timespec64(ns - bp->ts_window_adjust); 5901acffc6eSJonathan Lemon } 5911acffc6eSJonathan Lemon 592a7e1abadSJonathan Lemon time_ns = ioread32(&bp->reg->time_ns); 593a7e1abadSJonathan Lemon time_sec = ioread32(&bp->reg->time_sec); 594a7e1abadSJonathan Lemon 595a7e1abadSJonathan Lemon ts->tv_sec = time_sec; 596a7e1abadSJonathan Lemon ts->tv_nsec = time_ns; 597a7e1abadSJonathan Lemon 598a7e1abadSJonathan Lemon return ctrl & OCP_CTRL_READ_TIME_DONE ? 0 : -ETIMEDOUT; 599a7e1abadSJonathan Lemon } 600a7e1abadSJonathan Lemon 601a7e1abadSJonathan Lemon static int 602a7e1abadSJonathan Lemon ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts, 603a7e1abadSJonathan Lemon struct ptp_system_timestamp *sts) 604a7e1abadSJonathan Lemon { 605a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 606a7e1abadSJonathan Lemon unsigned long flags; 607a7e1abadSJonathan Lemon int err; 608a7e1abadSJonathan Lemon 609a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 610a7e1abadSJonathan Lemon err = __ptp_ocp_gettime_locked(bp, ts, sts); 611a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 612a7e1abadSJonathan Lemon 613a7e1abadSJonathan Lemon return err; 614a7e1abadSJonathan Lemon } 615a7e1abadSJonathan Lemon 616a7e1abadSJonathan Lemon static void 617a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts) 618a7e1abadSJonathan Lemon { 619a7e1abadSJonathan Lemon u32 ctrl, time_sec, time_ns; 620a7e1abadSJonathan Lemon u32 select; 621a7e1abadSJonathan Lemon 622a7e1abadSJonathan Lemon time_ns = ts->tv_nsec; 623a7e1abadSJonathan Lemon time_sec = ts->tv_sec; 624a7e1abadSJonathan Lemon 625a7e1abadSJonathan Lemon select = ioread32(&bp->reg->select); 626a7e1abadSJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 627a7e1abadSJonathan Lemon 628a7e1abadSJonathan Lemon iowrite32(time_ns, &bp->reg->adjust_ns); 629a7e1abadSJonathan Lemon iowrite32(time_sec, &bp->reg->adjust_sec); 630a7e1abadSJonathan Lemon 6311acffc6eSJonathan Lemon ctrl = OCP_CTRL_ADJUST_TIME | OCP_CTRL_ENABLE; 632a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 633a7e1abadSJonathan Lemon 634a7e1abadSJonathan Lemon /* restore clock selection */ 635a7e1abadSJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 636a7e1abadSJonathan Lemon } 637a7e1abadSJonathan Lemon 638a7e1abadSJonathan Lemon static int 639a7e1abadSJonathan Lemon ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts) 640a7e1abadSJonathan Lemon { 641a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 642a7e1abadSJonathan Lemon unsigned long flags; 643a7e1abadSJonathan Lemon 644a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 645a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(bp, ts); 646a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 647a7e1abadSJonathan Lemon 648a7e1abadSJonathan Lemon return 0; 649a7e1abadSJonathan Lemon } 650a7e1abadSJonathan Lemon 6516d59d4faSJonathan Lemon static void 65290f8f4c0SJonathan Lemon __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val) 6536d59d4faSJonathan Lemon { 6546d59d4faSJonathan Lemon u32 select, ctrl; 6556d59d4faSJonathan Lemon 6566d59d4faSJonathan Lemon select = ioread32(&bp->reg->select); 6576d59d4faSJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 6586d59d4faSJonathan Lemon 6596d59d4faSJonathan Lemon iowrite32(adj_val, &bp->reg->offset_ns); 66090f8f4c0SJonathan Lemon iowrite32(NSEC_PER_SEC, &bp->reg->offset_window_ns); 6616d59d4faSJonathan Lemon 6626d59d4faSJonathan Lemon ctrl = OCP_CTRL_ADJUST_OFFSET | OCP_CTRL_ENABLE; 6636d59d4faSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 6646d59d4faSJonathan Lemon 6656d59d4faSJonathan Lemon /* restore clock selection */ 6666d59d4faSJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 6676d59d4faSJonathan Lemon } 6686d59d4faSJonathan Lemon 66990f8f4c0SJonathan Lemon static void 67090f8f4c0SJonathan Lemon ptp_ocp_adjtime_coarse(struct ptp_ocp *bp, u64 delta_ns) 67190f8f4c0SJonathan Lemon { 67290f8f4c0SJonathan Lemon struct timespec64 ts; 67390f8f4c0SJonathan Lemon unsigned long flags; 67490f8f4c0SJonathan Lemon int err; 67590f8f4c0SJonathan Lemon 67690f8f4c0SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 67790f8f4c0SJonathan Lemon err = __ptp_ocp_gettime_locked(bp, &ts, NULL); 67890f8f4c0SJonathan Lemon if (likely(!err)) { 67990f8f4c0SJonathan Lemon timespec64_add_ns(&ts, delta_ns); 68090f8f4c0SJonathan Lemon __ptp_ocp_settime_locked(bp, &ts); 68190f8f4c0SJonathan Lemon } 68290f8f4c0SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 68390f8f4c0SJonathan Lemon } 68490f8f4c0SJonathan Lemon 685a7e1abadSJonathan Lemon static int 686a7e1abadSJonathan Lemon ptp_ocp_adjtime(struct ptp_clock_info *ptp_info, s64 delta_ns) 687a7e1abadSJonathan Lemon { 688a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 689a7e1abadSJonathan Lemon unsigned long flags; 6906d59d4faSJonathan Lemon u32 adj_ns, sign; 691a7e1abadSJonathan Lemon 69290f8f4c0SJonathan Lemon if (delta_ns > NSEC_PER_SEC || -delta_ns > NSEC_PER_SEC) { 69390f8f4c0SJonathan Lemon ptp_ocp_adjtime_coarse(bp, delta_ns); 69490f8f4c0SJonathan Lemon return 0; 69590f8f4c0SJonathan Lemon } 69690f8f4c0SJonathan Lemon 6976d59d4faSJonathan Lemon sign = delta_ns < 0 ? BIT(31) : 0; 6986d59d4faSJonathan Lemon adj_ns = sign ? -delta_ns : delta_ns; 699a7e1abadSJonathan Lemon 700a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 7016d59d4faSJonathan Lemon __ptp_ocp_adjtime_locked(bp, sign | adj_ns); 702a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 703a7e1abadSJonathan Lemon 7046d59d4faSJonathan Lemon return 0; 705a7e1abadSJonathan Lemon } 706a7e1abadSJonathan Lemon 707a7e1abadSJonathan Lemon static int 708a7e1abadSJonathan Lemon ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm) 709a7e1abadSJonathan Lemon { 710a7e1abadSJonathan Lemon if (scaled_ppm == 0) 711a7e1abadSJonathan Lemon return 0; 712a7e1abadSJonathan Lemon 713a7e1abadSJonathan Lemon return -EOPNOTSUPP; 714a7e1abadSJonathan Lemon } 715a7e1abadSJonathan Lemon 716773bda96SJonathan Lemon static int 7176d59d4faSJonathan Lemon ptp_ocp_null_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns) 718773bda96SJonathan Lemon { 719773bda96SJonathan Lemon return -EOPNOTSUPP; 720773bda96SJonathan Lemon } 721773bda96SJonathan Lemon 722773bda96SJonathan Lemon static int 723773bda96SJonathan Lemon ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq, 724773bda96SJonathan Lemon int on) 725773bda96SJonathan Lemon { 726773bda96SJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 727773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = NULL; 728a62a56d0SJonathan Lemon u32 req; 729773bda96SJonathan Lemon int err; 730773bda96SJonathan Lemon 731773bda96SJonathan Lemon switch (rq->type) { 732773bda96SJonathan Lemon case PTP_CLK_REQ_EXTTS: 733a62a56d0SJonathan Lemon req = OCP_REQ_TIMESTAMP; 734773bda96SJonathan Lemon switch (rq->extts.index) { 735773bda96SJonathan Lemon case 0: 736773bda96SJonathan Lemon ext = bp->ts0; 737773bda96SJonathan Lemon break; 738773bda96SJonathan Lemon case 1: 739773bda96SJonathan Lemon ext = bp->ts1; 740773bda96SJonathan Lemon break; 741dcf61469SJonathan Lemon case 2: 742dcf61469SJonathan Lemon ext = bp->ts2; 743dcf61469SJonathan Lemon break; 744a62a56d0SJonathan Lemon case 3: 745a62a56d0SJonathan Lemon ext = bp->pps; 746a62a56d0SJonathan Lemon break; 747773bda96SJonathan Lemon } 748773bda96SJonathan Lemon break; 749773bda96SJonathan Lemon case PTP_CLK_REQ_PPS: 750a62a56d0SJonathan Lemon req = OCP_REQ_PPS; 751773bda96SJonathan Lemon ext = bp->pps; 752773bda96SJonathan Lemon break; 753a62a56d0SJonathan Lemon case PTP_CLK_REQ_PEROUT: 754a62a56d0SJonathan Lemon if (on && 755a62a56d0SJonathan Lemon (rq->perout.period.sec != 1 || rq->perout.period.nsec != 0)) 756a62a56d0SJonathan Lemon return -EINVAL; 757a62a56d0SJonathan Lemon /* This is a request for 1PPS on an output SMA. 758a62a56d0SJonathan Lemon * Allow, but assume manual configuration. 759a62a56d0SJonathan Lemon */ 760a62a56d0SJonathan Lemon return 0; 761773bda96SJonathan Lemon default: 762773bda96SJonathan Lemon return -EOPNOTSUPP; 763773bda96SJonathan Lemon } 764773bda96SJonathan Lemon 765773bda96SJonathan Lemon err = -ENXIO; 766773bda96SJonathan Lemon if (ext) 767a62a56d0SJonathan Lemon err = ext->info->enable(ext, req, on); 768773bda96SJonathan Lemon 769773bda96SJonathan Lemon return err; 770773bda96SJonathan Lemon } 771773bda96SJonathan Lemon 772a7e1abadSJonathan Lemon static const struct ptp_clock_info ptp_ocp_clock_info = { 773a7e1abadSJonathan Lemon .owner = THIS_MODULE, 774a7e1abadSJonathan Lemon .name = KBUILD_MODNAME, 775a7e1abadSJonathan Lemon .max_adj = 100000000, 776a7e1abadSJonathan Lemon .gettimex64 = ptp_ocp_gettimex, 777a7e1abadSJonathan Lemon .settime64 = ptp_ocp_settime, 778a7e1abadSJonathan Lemon .adjtime = ptp_ocp_adjtime, 779a7e1abadSJonathan Lemon .adjfine = ptp_ocp_null_adjfine, 7806d59d4faSJonathan Lemon .adjphase = ptp_ocp_null_adjphase, 781773bda96SJonathan Lemon .enable = ptp_ocp_enable, 782773bda96SJonathan Lemon .pps = true, 783a62a56d0SJonathan Lemon .n_ext_ts = 4, 784a62a56d0SJonathan Lemon .n_per_out = 1, 785a7e1abadSJonathan Lemon }; 786a7e1abadSJonathan Lemon 787773bda96SJonathan Lemon static void 788773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp) 789773bda96SJonathan Lemon { 790773bda96SJonathan Lemon u32 ctrl, select; 791773bda96SJonathan Lemon 792773bda96SJonathan Lemon select = ioread32(&bp->reg->select); 793773bda96SJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 794773bda96SJonathan Lemon 795773bda96SJonathan Lemon iowrite32(0, &bp->reg->drift_ns); 796773bda96SJonathan Lemon 7971acffc6eSJonathan Lemon ctrl = OCP_CTRL_ADJUST_DRIFT | OCP_CTRL_ENABLE; 798773bda96SJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 799773bda96SJonathan Lemon 800773bda96SJonathan Lemon /* restore clock selection */ 801773bda96SJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 802773bda96SJonathan Lemon } 803773bda96SJonathan Lemon 804773bda96SJonathan Lemon static void 805e68462a0SVadim Fedorenko ptp_ocp_utc_distribute(struct ptp_ocp *bp, u32 val) 806e68462a0SVadim Fedorenko { 807e68462a0SVadim Fedorenko unsigned long flags; 808e68462a0SVadim Fedorenko 809e68462a0SVadim Fedorenko spin_lock_irqsave(&bp->lock, flags); 810e68462a0SVadim Fedorenko 811e68462a0SVadim Fedorenko bp->utc_tai_offset = val; 812e68462a0SVadim Fedorenko 813e68462a0SVadim Fedorenko if (bp->irig_out) 814e68462a0SVadim Fedorenko iowrite32(val, &bp->irig_out->adj_sec); 815e68462a0SVadim Fedorenko if (bp->dcf_out) 816e68462a0SVadim Fedorenko iowrite32(val, &bp->dcf_out->adj_sec); 817e68462a0SVadim Fedorenko if (bp->nmea_out) 818e68462a0SVadim Fedorenko iowrite32(val, &bp->nmea_out->adj_sec); 819e68462a0SVadim Fedorenko 820e68462a0SVadim Fedorenko spin_unlock_irqrestore(&bp->lock, flags); 821e68462a0SVadim Fedorenko } 822e68462a0SVadim Fedorenko 823e68462a0SVadim Fedorenko static void 824773bda96SJonathan Lemon ptp_ocp_watchdog(struct timer_list *t) 825773bda96SJonathan Lemon { 826773bda96SJonathan Lemon struct ptp_ocp *bp = from_timer(bp, t, watchdog); 827773bda96SJonathan Lemon unsigned long flags; 828e68462a0SVadim Fedorenko u32 status, utc_offset; 829773bda96SJonathan Lemon 8300d43d4f2SJonathan Lemon status = ioread32(&bp->pps_to_clk->status); 831773bda96SJonathan Lemon 832773bda96SJonathan Lemon if (status & PPS_STATUS_SUPERV_ERR) { 8330d43d4f2SJonathan Lemon iowrite32(status, &bp->pps_to_clk->status); 834ef0cfb34SJonathan Lemon if (!bp->gnss_lost) { 835773bda96SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 836773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(bp); 837773bda96SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 838ef0cfb34SJonathan Lemon bp->gnss_lost = ktime_get_real_seconds(); 839773bda96SJonathan Lemon } 840773bda96SJonathan Lemon 841ef0cfb34SJonathan Lemon } else if (bp->gnss_lost) { 842ef0cfb34SJonathan Lemon bp->gnss_lost = 0; 843773bda96SJonathan Lemon } 844773bda96SJonathan Lemon 845e68462a0SVadim Fedorenko /* if GNSS provides correct data we can rely on 846e68462a0SVadim Fedorenko * it to get leap second information 847e68462a0SVadim Fedorenko */ 848e68462a0SVadim Fedorenko if (bp->tod) { 849e68462a0SVadim Fedorenko status = ioread32(&bp->tod->utc_status); 850e68462a0SVadim Fedorenko utc_offset = status & TOD_STATUS_UTC_MASK; 851e68462a0SVadim Fedorenko if (status & TOD_STATUS_UTC_VALID && 852e68462a0SVadim Fedorenko utc_offset != bp->utc_tai_offset) 853e68462a0SVadim Fedorenko ptp_ocp_utc_distribute(bp, utc_offset); 854e68462a0SVadim Fedorenko } 855e68462a0SVadim Fedorenko 856773bda96SJonathan Lemon mod_timer(&bp->watchdog, jiffies + HZ); 857773bda96SJonathan Lemon } 858773bda96SJonathan Lemon 8591acffc6eSJonathan Lemon static void 8601acffc6eSJonathan Lemon ptp_ocp_estimate_pci_timing(struct ptp_ocp *bp) 8611acffc6eSJonathan Lemon { 8621acffc6eSJonathan Lemon ktime_t start, end; 8631acffc6eSJonathan Lemon ktime_t delay; 8641acffc6eSJonathan Lemon u32 ctrl; 8651acffc6eSJonathan Lemon 8661acffc6eSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 8671acffc6eSJonathan Lemon ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE; 8681acffc6eSJonathan Lemon 8691acffc6eSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 8701acffc6eSJonathan Lemon 8711acffc6eSJonathan Lemon start = ktime_get_ns(); 8721acffc6eSJonathan Lemon 8731acffc6eSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 8741acffc6eSJonathan Lemon 8751acffc6eSJonathan Lemon end = ktime_get_ns(); 8761acffc6eSJonathan Lemon 8771acffc6eSJonathan Lemon delay = end - start; 8781acffc6eSJonathan Lemon bp->ts_window_adjust = (delay >> 5) * 3; 8791acffc6eSJonathan Lemon } 8801acffc6eSJonathan Lemon 881a7e1abadSJonathan Lemon static int 882773bda96SJonathan Lemon ptp_ocp_init_clock(struct ptp_ocp *bp) 883a7e1abadSJonathan Lemon { 884a7e1abadSJonathan Lemon struct timespec64 ts; 885a7e1abadSJonathan Lemon bool sync; 886a7e1abadSJonathan Lemon u32 ctrl; 887a7e1abadSJonathan Lemon 8881acffc6eSJonathan Lemon ctrl = OCP_CTRL_ENABLE; 889a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 890a7e1abadSJonathan Lemon 891773bda96SJonathan Lemon /* NO DRIFT Correction */ 892773bda96SJonathan Lemon /* offset_p:i 1/8, offset_i: 1/16, drift_p: 0, drift_i: 0 */ 893773bda96SJonathan Lemon iowrite32(0x2000, &bp->reg->servo_offset_p); 894773bda96SJonathan Lemon iowrite32(0x1000, &bp->reg->servo_offset_i); 895773bda96SJonathan Lemon iowrite32(0, &bp->reg->servo_drift_p); 896773bda96SJonathan Lemon iowrite32(0, &bp->reg->servo_drift_i); 897773bda96SJonathan Lemon 898773bda96SJonathan Lemon /* latch servo values */ 899773bda96SJonathan Lemon ctrl |= OCP_CTRL_ADJUST_SERVO; 900773bda96SJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 901773bda96SJonathan Lemon 902a7e1abadSJonathan Lemon if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) { 903a7e1abadSJonathan Lemon dev_err(&bp->pdev->dev, "clock not enabled\n"); 904a7e1abadSJonathan Lemon return -ENODEV; 905a7e1abadSJonathan Lemon } 906a7e1abadSJonathan Lemon 9071acffc6eSJonathan Lemon ptp_ocp_estimate_pci_timing(bp); 9081acffc6eSJonathan Lemon 909a7e1abadSJonathan Lemon sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC; 910a7e1abadSJonathan Lemon if (!sync) { 911065efcc5SJonathan Lemon ktime_get_clocktai_ts64(&ts); 912a7e1abadSJonathan Lemon ptp_ocp_settime(&bp->ptp_info, &ts); 913a7e1abadSJonathan Lemon } 914a7e1abadSJonathan Lemon 915065efcc5SJonathan Lemon /* If there is a clock supervisor, then enable the watchdog */ 916065efcc5SJonathan Lemon if (bp->pps_to_clk) { 917773bda96SJonathan Lemon timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0); 918773bda96SJonathan Lemon mod_timer(&bp->watchdog, jiffies + HZ); 919065efcc5SJonathan Lemon } 920773bda96SJonathan Lemon 921a7e1abadSJonathan Lemon return 0; 922a7e1abadSJonathan Lemon } 923a7e1abadSJonathan Lemon 924a7e1abadSJonathan Lemon static void 925065efcc5SJonathan Lemon ptp_ocp_tod_init(struct ptp_ocp *bp) 926065efcc5SJonathan Lemon { 927065efcc5SJonathan Lemon u32 ctrl, reg; 928065efcc5SJonathan Lemon 929065efcc5SJonathan Lemon ctrl = ioread32(&bp->tod->ctrl); 930065efcc5SJonathan Lemon ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE; 931065efcc5SJonathan Lemon ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B); 932065efcc5SJonathan Lemon iowrite32(ctrl, &bp->tod->ctrl); 933065efcc5SJonathan Lemon 934065efcc5SJonathan Lemon reg = ioread32(&bp->tod->utc_status); 935065efcc5SJonathan Lemon if (reg & TOD_STATUS_UTC_VALID) 936065efcc5SJonathan Lemon ptp_ocp_utc_distribute(bp, reg & TOD_STATUS_UTC_MASK); 937065efcc5SJonathan Lemon } 938065efcc5SJonathan Lemon 9399f492c4cSVadim Fedorenko static const char * 9409f492c4cSVadim Fedorenko ptp_ocp_tod_proto_name(const int idx) 941a7e1abadSJonathan Lemon { 942a7e1abadSJonathan Lemon static const char * const proto_name[] = { 943a7e1abadSJonathan Lemon "NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none", 944a7e1abadSJonathan Lemon "UBX", "UBX_UTC", "UBX_LS", "UBX_none" 945a7e1abadSJonathan Lemon }; 9469f492c4cSVadim Fedorenko return proto_name[idx]; 9479f492c4cSVadim Fedorenko } 9489f492c4cSVadim Fedorenko 9499f492c4cSVadim Fedorenko static const char * 9509f492c4cSVadim Fedorenko ptp_ocp_tod_gnss_name(int idx) 9519f492c4cSVadim Fedorenko { 952a7e1abadSJonathan Lemon static const char * const gnss_name[] = { 953a7e1abadSJonathan Lemon "ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU", 9549f492c4cSVadim Fedorenko "Unknown" 955a7e1abadSJonathan Lemon }; 95672f00505SDan Carpenter if (idx >= ARRAY_SIZE(gnss_name)) 9579f492c4cSVadim Fedorenko idx = ARRAY_SIZE(gnss_name) - 1; 9589f492c4cSVadim Fedorenko return gnss_name[idx]; 959a7e1abadSJonathan Lemon } 960a7e1abadSJonathan Lemon 9610cfcdd1eSJonathan Lemon struct ptp_ocp_nvmem_match_info { 9620cfcdd1eSJonathan Lemon struct ptp_ocp *bp; 9630cfcdd1eSJonathan Lemon const void * const tag; 964773bda96SJonathan Lemon }; 965773bda96SJonathan Lemon 9660cfcdd1eSJonathan Lemon static int 9670cfcdd1eSJonathan Lemon ptp_ocp_nvmem_match(struct device *dev, const void *data) 9680cfcdd1eSJonathan Lemon { 9690cfcdd1eSJonathan Lemon const struct ptp_ocp_nvmem_match_info *info = data; 9700cfcdd1eSJonathan Lemon 9710cfcdd1eSJonathan Lemon dev = dev->parent; 9720cfcdd1eSJonathan Lemon if (!i2c_verify_client(dev) || info->tag != dev->platform_data) 9730cfcdd1eSJonathan Lemon return 0; 9740cfcdd1eSJonathan Lemon 9750cfcdd1eSJonathan Lemon while ((dev = dev->parent)) 9760cfcdd1eSJonathan Lemon if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME)) 9770cfcdd1eSJonathan Lemon return info->bp == dev_get_drvdata(dev); 978773bda96SJonathan Lemon return 0; 979773bda96SJonathan Lemon } 980773bda96SJonathan Lemon 9810cfcdd1eSJonathan Lemon static inline struct nvmem_device * 9820cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_get(struct ptp_ocp *bp, const void * const tag) 983773bda96SJonathan Lemon { 9840cfcdd1eSJonathan Lemon struct ptp_ocp_nvmem_match_info info = { .bp = bp, .tag = tag }; 9850cfcdd1eSJonathan Lemon 9860cfcdd1eSJonathan Lemon return nvmem_device_find(&info, ptp_ocp_nvmem_match); 9870cfcdd1eSJonathan Lemon } 9880cfcdd1eSJonathan Lemon 9890cfcdd1eSJonathan Lemon static inline void 9900cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_put(struct nvmem_device **nvmemp) 9910cfcdd1eSJonathan Lemon { 9920cfcdd1eSJonathan Lemon if (*nvmemp != NULL) { 9930cfcdd1eSJonathan Lemon nvmem_device_put(*nvmemp); 9940cfcdd1eSJonathan Lemon *nvmemp = NULL; 9950cfcdd1eSJonathan Lemon } 9960cfcdd1eSJonathan Lemon } 9970cfcdd1eSJonathan Lemon 9980cfcdd1eSJonathan Lemon static void 9990cfcdd1eSJonathan Lemon ptp_ocp_read_eeprom(struct ptp_ocp *bp) 10000cfcdd1eSJonathan Lemon { 10010cfcdd1eSJonathan Lemon const struct ptp_ocp_eeprom_map *map; 10020cfcdd1eSJonathan Lemon struct nvmem_device *nvmem; 10030cfcdd1eSJonathan Lemon const void *tag; 10040cfcdd1eSJonathan Lemon int ret; 1005773bda96SJonathan Lemon 10061447149dSJonathan Lemon if (!bp->i2c_ctrl) 10071447149dSJonathan Lemon return; 10081447149dSJonathan Lemon 10090cfcdd1eSJonathan Lemon tag = NULL; 10100cfcdd1eSJonathan Lemon nvmem = NULL; 1011773bda96SJonathan Lemon 10120cfcdd1eSJonathan Lemon for (map = bp->eeprom_map; map->len; map++) { 10130cfcdd1eSJonathan Lemon if (map->tag != tag) { 10140cfcdd1eSJonathan Lemon tag = map->tag; 10150cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_put(&nvmem); 10160cfcdd1eSJonathan Lemon } 10170cfcdd1eSJonathan Lemon if (!nvmem) { 10180cfcdd1eSJonathan Lemon nvmem = ptp_ocp_nvmem_device_get(bp, tag); 10190cfcdd1eSJonathan Lemon if (!nvmem) 1020773bda96SJonathan Lemon goto out; 1021773bda96SJonathan Lemon } 10220cfcdd1eSJonathan Lemon ret = nvmem_device_read(nvmem, map->off, map->len, 10230cfcdd1eSJonathan Lemon BP_MAP_ENTRY_ADDR(bp, map)); 10240cfcdd1eSJonathan Lemon if (ret != map->len) 10250cfcdd1eSJonathan Lemon goto read_fail; 1026773bda96SJonathan Lemon } 1027773bda96SJonathan Lemon 10280cfcdd1eSJonathan Lemon bp->has_eeprom_data = true; 1029773bda96SJonathan Lemon 1030773bda96SJonathan Lemon out: 10310cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_put(&nvmem); 10320cfcdd1eSJonathan Lemon return; 10330cfcdd1eSJonathan Lemon 10340cfcdd1eSJonathan Lemon read_fail: 10350cfcdd1eSJonathan Lemon dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", ret); 10360cfcdd1eSJonathan Lemon goto out; 10370cfcdd1eSJonathan Lemon } 10380cfcdd1eSJonathan Lemon 10390cfcdd1eSJonathan Lemon static int 10400cfcdd1eSJonathan Lemon ptp_ocp_firstchild(struct device *dev, void *data) 10410cfcdd1eSJonathan Lemon { 10420cfcdd1eSJonathan Lemon return 1; 1043773bda96SJonathan Lemon } 1044773bda96SJonathan Lemon 1045773bda96SJonathan Lemon static struct device * 1046773bda96SJonathan Lemon ptp_ocp_find_flash(struct ptp_ocp *bp) 1047773bda96SJonathan Lemon { 1048773bda96SJonathan Lemon struct device *dev, *last; 1049773bda96SJonathan Lemon 1050773bda96SJonathan Lemon last = NULL; 1051773bda96SJonathan Lemon dev = &bp->spi_flash->dev; 1052773bda96SJonathan Lemon 1053773bda96SJonathan Lemon while ((dev = device_find_child(dev, NULL, ptp_ocp_firstchild))) { 1054773bda96SJonathan Lemon if (!strcmp("mtd", dev_bus_name(dev))) 1055773bda96SJonathan Lemon break; 1056773bda96SJonathan Lemon put_device(last); 1057773bda96SJonathan Lemon last = dev; 1058773bda96SJonathan Lemon } 1059773bda96SJonathan Lemon put_device(last); 1060773bda96SJonathan Lemon 1061773bda96SJonathan Lemon return dev; 1062773bda96SJonathan Lemon } 1063773bda96SJonathan Lemon 1064773bda96SJonathan Lemon static int 1065773bda96SJonathan Lemon ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev, 1066773bda96SJonathan Lemon const struct firmware *fw) 1067773bda96SJonathan Lemon { 1068773bda96SJonathan Lemon struct mtd_info *mtd = dev_get_drvdata(dev); 1069773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 1070773bda96SJonathan Lemon size_t off, len, resid, wrote; 1071773bda96SJonathan Lemon struct erase_info erase; 1072773bda96SJonathan Lemon size_t base, blksz; 10737c807572SJonathan Lemon int err = 0; 1074773bda96SJonathan Lemon 1075773bda96SJonathan Lemon off = 0; 1076773bda96SJonathan Lemon base = bp->flash_start; 1077773bda96SJonathan Lemon blksz = 4096; 1078773bda96SJonathan Lemon resid = fw->size; 1079773bda96SJonathan Lemon 1080773bda96SJonathan Lemon while (resid) { 1081773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, "Flashing", 1082773bda96SJonathan Lemon NULL, off, fw->size); 1083773bda96SJonathan Lemon 1084773bda96SJonathan Lemon len = min_t(size_t, resid, blksz); 1085773bda96SJonathan Lemon erase.addr = base + off; 1086773bda96SJonathan Lemon erase.len = blksz; 1087773bda96SJonathan Lemon 1088773bda96SJonathan Lemon err = mtd_erase(mtd, &erase); 1089773bda96SJonathan Lemon if (err) 1090773bda96SJonathan Lemon goto out; 1091773bda96SJonathan Lemon 1092773bda96SJonathan Lemon err = mtd_write(mtd, base + off, len, &wrote, &fw->data[off]); 1093773bda96SJonathan Lemon if (err) 1094773bda96SJonathan Lemon goto out; 1095773bda96SJonathan Lemon 1096773bda96SJonathan Lemon off += blksz; 1097773bda96SJonathan Lemon resid -= len; 1098773bda96SJonathan Lemon } 1099773bda96SJonathan Lemon out: 1100773bda96SJonathan Lemon return err; 1101773bda96SJonathan Lemon } 1102773bda96SJonathan Lemon 1103773bda96SJonathan Lemon static int 1104773bda96SJonathan Lemon ptp_ocp_devlink_flash_update(struct devlink *devlink, 1105773bda96SJonathan Lemon struct devlink_flash_update_params *params, 1106773bda96SJonathan Lemon struct netlink_ext_ack *extack) 1107773bda96SJonathan Lemon { 1108773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 1109773bda96SJonathan Lemon struct device *dev; 1110773bda96SJonathan Lemon const char *msg; 1111773bda96SJonathan Lemon int err; 1112773bda96SJonathan Lemon 1113773bda96SJonathan Lemon dev = ptp_ocp_find_flash(bp); 1114773bda96SJonathan Lemon if (!dev) { 1115773bda96SJonathan Lemon dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n"); 1116773bda96SJonathan Lemon return -ENODEV; 1117773bda96SJonathan Lemon } 1118773bda96SJonathan Lemon 1119773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, "Preparing to flash", 1120773bda96SJonathan Lemon NULL, 0, 0); 1121773bda96SJonathan Lemon 1122773bda96SJonathan Lemon err = ptp_ocp_devlink_flash(devlink, dev, params->fw); 1123773bda96SJonathan Lemon 1124773bda96SJonathan Lemon msg = err ? "Flash error" : "Flash complete"; 1125773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0); 1126773bda96SJonathan Lemon 1127773bda96SJonathan Lemon put_device(dev); 1128773bda96SJonathan Lemon return err; 1129773bda96SJonathan Lemon } 1130773bda96SJonathan Lemon 1131773bda96SJonathan Lemon static int 1132773bda96SJonathan Lemon ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req, 1133773bda96SJonathan Lemon struct netlink_ext_ack *extack) 1134773bda96SJonathan Lemon { 1135773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 1136773bda96SJonathan Lemon char buf[32]; 1137773bda96SJonathan Lemon int err; 1138773bda96SJonathan Lemon 1139773bda96SJonathan Lemon err = devlink_info_driver_name_put(req, KBUILD_MODNAME); 1140773bda96SJonathan Lemon if (err) 1141773bda96SJonathan Lemon return err; 1142773bda96SJonathan Lemon 1143b0ca789aSJonathan Lemon if (bp->fw_version & 0xffff) { 1144b0ca789aSJonathan Lemon sprintf(buf, "%d", bp->fw_version); 1145b0ca789aSJonathan Lemon err = devlink_info_version_running_put(req, "fw", buf); 1146773bda96SJonathan Lemon } else { 1147b0ca789aSJonathan Lemon sprintf(buf, "%d", bp->fw_version >> 16); 1148b0ca789aSJonathan Lemon err = devlink_info_version_running_put(req, "loader", buf); 1149773bda96SJonathan Lemon } 1150773bda96SJonathan Lemon if (err) 1151773bda96SJonathan Lemon return err; 1152773bda96SJonathan Lemon 11530cfcdd1eSJonathan Lemon if (!bp->has_eeprom_data) { 11540cfcdd1eSJonathan Lemon ptp_ocp_read_eeprom(bp); 11550cfcdd1eSJonathan Lemon if (!bp->has_eeprom_data) 11560cfcdd1eSJonathan Lemon return 0; 11570cfcdd1eSJonathan Lemon } 1158773bda96SJonathan Lemon 1159773bda96SJonathan Lemon sprintf(buf, "%pM", bp->serial); 1160773bda96SJonathan Lemon err = devlink_info_serial_number_put(req, buf); 1161773bda96SJonathan Lemon if (err) 1162773bda96SJonathan Lemon return err; 11630cfcdd1eSJonathan Lemon 11640cfcdd1eSJonathan Lemon err = devlink_info_version_fixed_put(req, 11650cfcdd1eSJonathan Lemon DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, 11660cfcdd1eSJonathan Lemon bp->board_id); 11670cfcdd1eSJonathan Lemon if (err) 11680cfcdd1eSJonathan Lemon return err; 1169773bda96SJonathan Lemon 1170773bda96SJonathan Lemon return 0; 1171773bda96SJonathan Lemon } 1172773bda96SJonathan Lemon 1173773bda96SJonathan Lemon static const struct devlink_ops ptp_ocp_devlink_ops = { 1174773bda96SJonathan Lemon .flash_update = ptp_ocp_devlink_flash_update, 1175773bda96SJonathan Lemon .info_get = ptp_ocp_devlink_info_get, 1176773bda96SJonathan Lemon }; 1177773bda96SJonathan Lemon 1178773bda96SJonathan Lemon static void __iomem * 1179773bda96SJonathan Lemon __ptp_ocp_get_mem(struct ptp_ocp *bp, unsigned long start, int size) 1180773bda96SJonathan Lemon { 1181773bda96SJonathan Lemon struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp"); 1182773bda96SJonathan Lemon 1183773bda96SJonathan Lemon return devm_ioremap_resource(&bp->pdev->dev, &res); 1184773bda96SJonathan Lemon } 1185773bda96SJonathan Lemon 1186773bda96SJonathan Lemon static void __iomem * 1187773bda96SJonathan Lemon ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r) 1188773bda96SJonathan Lemon { 1189773bda96SJonathan Lemon unsigned long start; 1190773bda96SJonathan Lemon 1191773bda96SJonathan Lemon start = pci_resource_start(bp->pdev, 0) + r->offset; 1192773bda96SJonathan Lemon return __ptp_ocp_get_mem(bp, start, r->size); 1193773bda96SJonathan Lemon } 1194773bda96SJonathan Lemon 1195773bda96SJonathan Lemon static void 1196773bda96SJonathan Lemon ptp_ocp_set_irq_resource(struct resource *res, int irq) 1197773bda96SJonathan Lemon { 1198773bda96SJonathan Lemon struct resource r = DEFINE_RES_IRQ(irq); 1199773bda96SJonathan Lemon *res = r; 1200773bda96SJonathan Lemon } 1201773bda96SJonathan Lemon 1202773bda96SJonathan Lemon static void 1203773bda96SJonathan Lemon ptp_ocp_set_mem_resource(struct resource *res, unsigned long start, int size) 1204773bda96SJonathan Lemon { 1205773bda96SJonathan Lemon struct resource r = DEFINE_RES_MEM(start, size); 1206773bda96SJonathan Lemon *res = r; 1207773bda96SJonathan Lemon } 1208773bda96SJonathan Lemon 1209773bda96SJonathan Lemon static int 1210773bda96SJonathan Lemon ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r) 1211773bda96SJonathan Lemon { 1212773bda96SJonathan Lemon struct ptp_ocp_flash_info *info; 1213773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1214773bda96SJonathan Lemon struct platform_device *p; 1215773bda96SJonathan Lemon struct resource res[2]; 1216773bda96SJonathan Lemon unsigned long start; 1217773bda96SJonathan Lemon int id; 1218773bda96SJonathan Lemon 1219773bda96SJonathan Lemon start = pci_resource_start(pdev, 0) + r->offset; 1220773bda96SJonathan Lemon ptp_ocp_set_mem_resource(&res[0], start, r->size); 1221773bda96SJonathan Lemon ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1222773bda96SJonathan Lemon 1223773bda96SJonathan Lemon info = r->extra; 1224773bda96SJonathan Lemon id = pci_dev_id(pdev) << 1; 1225773bda96SJonathan Lemon id += info->pci_offset; 1226773bda96SJonathan Lemon 1227773bda96SJonathan Lemon p = platform_device_register_resndata(&pdev->dev, info->name, id, 1228773bda96SJonathan Lemon res, 2, info->data, 1229773bda96SJonathan Lemon info->data_size); 1230773bda96SJonathan Lemon if (IS_ERR(p)) 1231773bda96SJonathan Lemon return PTR_ERR(p); 1232773bda96SJonathan Lemon 1233773bda96SJonathan Lemon bp_assign_entry(bp, r, p); 1234773bda96SJonathan Lemon 1235773bda96SJonathan Lemon return 0; 1236773bda96SJonathan Lemon } 1237773bda96SJonathan Lemon 1238773bda96SJonathan Lemon static struct platform_device * 1239773bda96SJonathan Lemon ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id) 1240773bda96SJonathan Lemon { 12411618df6aSJonathan Lemon struct ptp_ocp_i2c_info *info; 1242773bda96SJonathan Lemon struct resource res[2]; 1243773bda96SJonathan Lemon unsigned long start; 1244773bda96SJonathan Lemon 12451618df6aSJonathan Lemon info = r->extra; 1246773bda96SJonathan Lemon start = pci_resource_start(pdev, 0) + r->offset; 1247773bda96SJonathan Lemon ptp_ocp_set_mem_resource(&res[0], start, r->size); 1248773bda96SJonathan Lemon ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1249773bda96SJonathan Lemon 12501618df6aSJonathan Lemon return platform_device_register_resndata(&pdev->dev, info->name, 12511618df6aSJonathan Lemon id, res, 2, 12521618df6aSJonathan Lemon info->data, info->data_size); 1253773bda96SJonathan Lemon } 1254773bda96SJonathan Lemon 1255773bda96SJonathan Lemon static int 1256773bda96SJonathan Lemon ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r) 1257773bda96SJonathan Lemon { 1258773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 12591618df6aSJonathan Lemon struct ptp_ocp_i2c_info *info; 1260773bda96SJonathan Lemon struct platform_device *p; 1261773bda96SJonathan Lemon struct clk_hw *clk; 1262773bda96SJonathan Lemon char buf[32]; 1263773bda96SJonathan Lemon int id; 1264773bda96SJonathan Lemon 12651618df6aSJonathan Lemon info = r->extra; 1266773bda96SJonathan Lemon id = pci_dev_id(bp->pdev); 1267773bda96SJonathan Lemon 1268773bda96SJonathan Lemon sprintf(buf, "AXI.%d", id); 12691618df6aSJonathan Lemon clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0, 12701618df6aSJonathan Lemon info->fixed_rate); 1271773bda96SJonathan Lemon if (IS_ERR(clk)) 1272773bda96SJonathan Lemon return PTR_ERR(clk); 1273773bda96SJonathan Lemon bp->i2c_clk = clk; 1274773bda96SJonathan Lemon 12751618df6aSJonathan Lemon sprintf(buf, "%s.%d", info->name, id); 1276773bda96SJonathan Lemon devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf); 1277773bda96SJonathan Lemon p = ptp_ocp_i2c_bus(bp->pdev, r, id); 1278773bda96SJonathan Lemon if (IS_ERR(p)) 1279773bda96SJonathan Lemon return PTR_ERR(p); 1280773bda96SJonathan Lemon 1281773bda96SJonathan Lemon bp_assign_entry(bp, r, p); 1282773bda96SJonathan Lemon 1283773bda96SJonathan Lemon return 0; 1284773bda96SJonathan Lemon } 1285773bda96SJonathan Lemon 1286773bda96SJonathan Lemon static irqreturn_t 1287773bda96SJonathan Lemon ptp_ocp_ts_irq(int irq, void *priv) 1288773bda96SJonathan Lemon { 1289773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1290773bda96SJonathan Lemon struct ts_reg __iomem *reg = ext->mem; 1291773bda96SJonathan Lemon struct ptp_clock_event ev; 1292773bda96SJonathan Lemon u32 sec, nsec; 1293773bda96SJonathan Lemon 1294a62a56d0SJonathan Lemon if (ext == ext->bp->pps) { 1295a62a56d0SJonathan Lemon if (ext->bp->pps_req_map & OCP_REQ_PPS) { 1296a62a56d0SJonathan Lemon ev.type = PTP_CLOCK_PPS; 1297a62a56d0SJonathan Lemon ptp_clock_event(ext->bp->ptp, &ev); 1298a62a56d0SJonathan Lemon } 1299a62a56d0SJonathan Lemon 1300a62a56d0SJonathan Lemon if ((ext->bp->pps_req_map & ~OCP_REQ_PPS) == 0) 1301a62a56d0SJonathan Lemon goto out; 1302a62a56d0SJonathan Lemon } 1303a62a56d0SJonathan Lemon 1304773bda96SJonathan Lemon /* XXX should fix API - this converts s/ns -> ts -> s/ns */ 1305773bda96SJonathan Lemon sec = ioread32(®->time_sec); 1306773bda96SJonathan Lemon nsec = ioread32(®->time_ns); 1307773bda96SJonathan Lemon 1308773bda96SJonathan Lemon ev.type = PTP_CLOCK_EXTTS; 1309773bda96SJonathan Lemon ev.index = ext->info->index; 13101acffc6eSJonathan Lemon ev.timestamp = sec * NSEC_PER_SEC + nsec; 1311773bda96SJonathan Lemon 1312773bda96SJonathan Lemon ptp_clock_event(ext->bp->ptp, &ev); 1313773bda96SJonathan Lemon 1314a62a56d0SJonathan Lemon out: 1315773bda96SJonathan Lemon iowrite32(1, ®->intr); /* write 1 to ack */ 1316773bda96SJonathan Lemon 1317773bda96SJonathan Lemon return IRQ_HANDLED; 1318773bda96SJonathan Lemon } 1319773bda96SJonathan Lemon 1320773bda96SJonathan Lemon static int 1321a62a56d0SJonathan Lemon ptp_ocp_ts_enable(void *priv, u32 req, bool enable) 1322773bda96SJonathan Lemon { 1323773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1324773bda96SJonathan Lemon struct ts_reg __iomem *reg = ext->mem; 1325a62a56d0SJonathan Lemon struct ptp_ocp *bp = ext->bp; 1326a62a56d0SJonathan Lemon 1327a62a56d0SJonathan Lemon if (ext == bp->pps) { 1328a62a56d0SJonathan Lemon u32 old_map = bp->pps_req_map; 1329a62a56d0SJonathan Lemon 1330a62a56d0SJonathan Lemon if (enable) 1331a62a56d0SJonathan Lemon bp->pps_req_map |= req; 1332a62a56d0SJonathan Lemon else 1333a62a56d0SJonathan Lemon bp->pps_req_map &= ~req; 1334a62a56d0SJonathan Lemon 1335a62a56d0SJonathan Lemon /* if no state change, just return */ 1336a62a56d0SJonathan Lemon if ((!!old_map ^ !!bp->pps_req_map) == 0) 1337a62a56d0SJonathan Lemon return 0; 1338a62a56d0SJonathan Lemon } 1339773bda96SJonathan Lemon 1340773bda96SJonathan Lemon if (enable) { 1341773bda96SJonathan Lemon iowrite32(1, ®->enable); 1342773bda96SJonathan Lemon iowrite32(1, ®->intr_mask); 1343773bda96SJonathan Lemon iowrite32(1, ®->intr); 1344773bda96SJonathan Lemon } else { 1345773bda96SJonathan Lemon iowrite32(0, ®->intr_mask); 1346773bda96SJonathan Lemon iowrite32(0, ®->enable); 1347773bda96SJonathan Lemon } 1348773bda96SJonathan Lemon 1349773bda96SJonathan Lemon return 0; 1350773bda96SJonathan Lemon } 1351773bda96SJonathan Lemon 1352773bda96SJonathan Lemon static void 1353773bda96SJonathan Lemon ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext) 1354773bda96SJonathan Lemon { 1355a62a56d0SJonathan Lemon ext->info->enable(ext, ~0, false); 1356773bda96SJonathan Lemon pci_free_irq(ext->bp->pdev, ext->irq_vec, ext); 1357773bda96SJonathan Lemon kfree(ext); 1358773bda96SJonathan Lemon } 1359773bda96SJonathan Lemon 1360773bda96SJonathan Lemon static int 1361773bda96SJonathan Lemon ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r) 1362773bda96SJonathan Lemon { 1363773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1364773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext; 1365773bda96SJonathan Lemon int err; 1366773bda96SJonathan Lemon 1367773bda96SJonathan Lemon ext = kzalloc(sizeof(*ext), GFP_KERNEL); 1368773bda96SJonathan Lemon if (!ext) 1369773bda96SJonathan Lemon return -ENOMEM; 1370773bda96SJonathan Lemon 1371773bda96SJonathan Lemon ext->mem = ptp_ocp_get_mem(bp, r); 1372c7521d3aSDan Carpenter if (IS_ERR(ext->mem)) { 1373c7521d3aSDan Carpenter err = PTR_ERR(ext->mem); 1374773bda96SJonathan Lemon goto out; 1375c7521d3aSDan Carpenter } 1376773bda96SJonathan Lemon 1377773bda96SJonathan Lemon ext->bp = bp; 1378773bda96SJonathan Lemon ext->info = r->extra; 1379773bda96SJonathan Lemon ext->irq_vec = r->irq_vec; 1380773bda96SJonathan Lemon 1381773bda96SJonathan Lemon err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL, 138256ec4403SJonathan Lemon ext, "ocp%d.%s", bp->id, r->name); 1383773bda96SJonathan Lemon if (err) { 1384773bda96SJonathan Lemon dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec); 1385773bda96SJonathan Lemon goto out; 1386773bda96SJonathan Lemon } 1387773bda96SJonathan Lemon 1388773bda96SJonathan Lemon bp_assign_entry(bp, r, ext); 1389773bda96SJonathan Lemon 1390773bda96SJonathan Lemon return 0; 1391773bda96SJonathan Lemon 1392773bda96SJonathan Lemon out: 1393773bda96SJonathan Lemon kfree(ext); 1394773bda96SJonathan Lemon return err; 1395773bda96SJonathan Lemon } 1396773bda96SJonathan Lemon 1397773bda96SJonathan Lemon static int 1398773bda96SJonathan Lemon ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r) 1399773bda96SJonathan Lemon { 1400773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1401773bda96SJonathan Lemon struct uart_8250_port uart; 1402773bda96SJonathan Lemon 1403773bda96SJonathan Lemon /* Setting UPF_IOREMAP and leaving port.membase unspecified lets 1404773bda96SJonathan Lemon * the serial port device claim and release the pci resource. 1405773bda96SJonathan Lemon */ 1406773bda96SJonathan Lemon memset(&uart, 0, sizeof(uart)); 1407773bda96SJonathan Lemon uart.port.dev = &pdev->dev; 1408773bda96SJonathan Lemon uart.port.iotype = UPIO_MEM; 1409773bda96SJonathan Lemon uart.port.regshift = 2; 1410773bda96SJonathan Lemon uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset; 1411773bda96SJonathan Lemon uart.port.irq = pci_irq_vector(pdev, r->irq_vec); 1412773bda96SJonathan Lemon uart.port.uartclk = 50000000; 1413*c17c4059SJonathan Lemon uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP | UPF_NO_THRE_TEST; 1414773bda96SJonathan Lemon uart.port.type = PORT_16550A; 1415773bda96SJonathan Lemon 1416773bda96SJonathan Lemon return serial8250_register_8250_port(&uart); 1417773bda96SJonathan Lemon } 1418773bda96SJonathan Lemon 1419773bda96SJonathan Lemon static int 1420773bda96SJonathan Lemon ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r) 1421773bda96SJonathan Lemon { 1422773bda96SJonathan Lemon int port; 1423773bda96SJonathan Lemon 1424773bda96SJonathan Lemon port = ptp_ocp_serial_line(bp, r); 1425773bda96SJonathan Lemon if (port < 0) 1426773bda96SJonathan Lemon return port; 1427773bda96SJonathan Lemon 1428773bda96SJonathan Lemon bp_assign_entry(bp, r, port); 1429773bda96SJonathan Lemon 1430773bda96SJonathan Lemon return 0; 1431773bda96SJonathan Lemon } 1432773bda96SJonathan Lemon 1433773bda96SJonathan Lemon static int 1434773bda96SJonathan Lemon ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r) 1435773bda96SJonathan Lemon { 1436773bda96SJonathan Lemon void __iomem *mem; 1437773bda96SJonathan Lemon 1438773bda96SJonathan Lemon mem = ptp_ocp_get_mem(bp, r); 1439c7521d3aSDan Carpenter if (IS_ERR(mem)) 1440c7521d3aSDan Carpenter return PTR_ERR(mem); 1441773bda96SJonathan Lemon 1442773bda96SJonathan Lemon bp_assign_entry(bp, r, mem); 1443773bda96SJonathan Lemon 1444773bda96SJonathan Lemon return 0; 1445773bda96SJonathan Lemon } 1446773bda96SJonathan Lemon 1447e3516bb4SJonathan Lemon static void 1448e3516bb4SJonathan Lemon ptp_ocp_nmea_out_init(struct ptp_ocp *bp) 1449e3516bb4SJonathan Lemon { 1450e3516bb4SJonathan Lemon if (!bp->nmea_out) 1451e3516bb4SJonathan Lemon return; 1452e3516bb4SJonathan Lemon 1453e3516bb4SJonathan Lemon iowrite32(0, &bp->nmea_out->ctrl); /* disable */ 1454e3516bb4SJonathan Lemon iowrite32(7, &bp->nmea_out->uart_baud); /* 115200 */ 1455e3516bb4SJonathan Lemon iowrite32(1, &bp->nmea_out->ctrl); /* enable */ 1456e3516bb4SJonathan Lemon } 1457e3516bb4SJonathan Lemon 1458773bda96SJonathan Lemon /* FB specific board initializers; last "resource" registered. */ 1459773bda96SJonathan Lemon static int 1460773bda96SJonathan Lemon ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r) 1461773bda96SJonathan Lemon { 1462773bda96SJonathan Lemon bp->flash_start = 1024 * 4096; 14630cfcdd1eSJonathan Lemon bp->eeprom_map = fb_eeprom_map; 1464b0ca789aSJonathan Lemon bp->fw_version = ioread32(&bp->image->version); 1465773bda96SJonathan Lemon 1466065efcc5SJonathan Lemon ptp_ocp_tod_init(bp); 1467e3516bb4SJonathan Lemon ptp_ocp_nmea_out_init(bp); 1468065efcc5SJonathan Lemon 1469773bda96SJonathan Lemon return ptp_ocp_init_clock(bp); 1470773bda96SJonathan Lemon } 1471773bda96SJonathan Lemon 147256ec4403SJonathan Lemon static bool 147356ec4403SJonathan Lemon ptp_ocp_allow_irq(struct ptp_ocp *bp, struct ocp_resource *r) 147456ec4403SJonathan Lemon { 147556ec4403SJonathan Lemon bool allow = !r->irq_vec || r->irq_vec < bp->n_irqs; 147656ec4403SJonathan Lemon 147756ec4403SJonathan Lemon if (!allow) 147856ec4403SJonathan Lemon dev_err(&bp->pdev->dev, "irq %d out of range, skipping %s\n", 147956ec4403SJonathan Lemon r->irq_vec, r->name); 148056ec4403SJonathan Lemon return allow; 148156ec4403SJonathan Lemon } 148256ec4403SJonathan Lemon 1483773bda96SJonathan Lemon static int 1484773bda96SJonathan Lemon ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data) 1485773bda96SJonathan Lemon { 1486773bda96SJonathan Lemon struct ocp_resource *r, *table; 1487773bda96SJonathan Lemon int err = 0; 1488773bda96SJonathan Lemon 1489773bda96SJonathan Lemon table = (struct ocp_resource *)driver_data; 1490773bda96SJonathan Lemon for (r = table; r->setup; r++) { 149156ec4403SJonathan Lemon if (!ptp_ocp_allow_irq(bp, r)) 149256ec4403SJonathan Lemon continue; 1493773bda96SJonathan Lemon err = r->setup(bp, r); 1494bceff290SJonathan Lemon if (err) { 1495bceff290SJonathan Lemon dev_err(&bp->pdev->dev, 1496bceff290SJonathan Lemon "Could not register %s: err %d\n", 1497bceff290SJonathan Lemon r->name, err); 1498773bda96SJonathan Lemon break; 1499773bda96SJonathan Lemon } 1500bceff290SJonathan Lemon } 1501773bda96SJonathan Lemon return err; 1502773bda96SJonathan Lemon } 1503773bda96SJonathan Lemon 15046baf2925SJonathan Lemon static void 15056baf2925SJonathan Lemon ptp_ocp_enable_fpga(u32 __iomem *reg, u32 bit, bool enable) 15066baf2925SJonathan Lemon { 15076baf2925SJonathan Lemon u32 ctrl; 15086baf2925SJonathan Lemon bool on; 15096baf2925SJonathan Lemon 15106baf2925SJonathan Lemon ctrl = ioread32(reg); 15116baf2925SJonathan Lemon on = ctrl & bit; 15126baf2925SJonathan Lemon if (on ^ enable) { 15136baf2925SJonathan Lemon ctrl &= ~bit; 15146baf2925SJonathan Lemon ctrl |= enable ? bit : 0; 15156baf2925SJonathan Lemon iowrite32(ctrl, reg); 15166baf2925SJonathan Lemon } 15176baf2925SJonathan Lemon } 15186baf2925SJonathan Lemon 15196baf2925SJonathan Lemon static void 15206baf2925SJonathan Lemon ptp_ocp_irig_out(struct ptp_ocp *bp, bool enable) 15216baf2925SJonathan Lemon { 15226baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->irig_out->ctrl, 15236baf2925SJonathan Lemon IRIG_M_CTRL_ENABLE, enable); 15246baf2925SJonathan Lemon } 15256baf2925SJonathan Lemon 15266baf2925SJonathan Lemon static void 15276baf2925SJonathan Lemon ptp_ocp_irig_in(struct ptp_ocp *bp, bool enable) 15286baf2925SJonathan Lemon { 15296baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->irig_in->ctrl, 15306baf2925SJonathan Lemon IRIG_S_CTRL_ENABLE, enable); 15316baf2925SJonathan Lemon } 15326baf2925SJonathan Lemon 15336baf2925SJonathan Lemon static void 15346baf2925SJonathan Lemon ptp_ocp_dcf_out(struct ptp_ocp *bp, bool enable) 15356baf2925SJonathan Lemon { 15366baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->dcf_out->ctrl, 15376baf2925SJonathan Lemon DCF_M_CTRL_ENABLE, enable); 15386baf2925SJonathan Lemon } 15396baf2925SJonathan Lemon 15406baf2925SJonathan Lemon static void 15416baf2925SJonathan Lemon ptp_ocp_dcf_in(struct ptp_ocp *bp, bool enable) 15426baf2925SJonathan Lemon { 15436baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->dcf_in->ctrl, 15446baf2925SJonathan Lemon DCF_S_CTRL_ENABLE, enable); 15456baf2925SJonathan Lemon } 15466baf2925SJonathan Lemon 15476baf2925SJonathan Lemon static void 15486baf2925SJonathan Lemon __handle_signal_outputs(struct ptp_ocp *bp, u32 val) 15496baf2925SJonathan Lemon { 15506baf2925SJonathan Lemon ptp_ocp_irig_out(bp, val & 0x00100010); 15516baf2925SJonathan Lemon ptp_ocp_dcf_out(bp, val & 0x00200020); 15526baf2925SJonathan Lemon } 15536baf2925SJonathan Lemon 15546baf2925SJonathan Lemon static void 15556baf2925SJonathan Lemon __handle_signal_inputs(struct ptp_ocp *bp, u32 val) 15566baf2925SJonathan Lemon { 15576baf2925SJonathan Lemon ptp_ocp_irig_in(bp, val & 0x00100010); 15586baf2925SJonathan Lemon ptp_ocp_dcf_in(bp, val & 0x00200020); 15596baf2925SJonathan Lemon } 15606baf2925SJonathan Lemon 1561e1daf0ecSJonathan Lemon /* 1562e1daf0ecSJonathan Lemon * ANT0 == gps (in) 1563e1daf0ecSJonathan Lemon * ANT1 == sma1 (in) 1564e1daf0ecSJonathan Lemon * ANT2 == sma2 (in) 1565e1daf0ecSJonathan Lemon * ANT3 == sma3 (out) 1566e1daf0ecSJonathan Lemon * ANT4 == sma4 (out) 1567e1daf0ecSJonathan Lemon */ 1568e1daf0ecSJonathan Lemon 1569e1daf0ecSJonathan Lemon enum ptp_ocp_sma_mode { 1570e1daf0ecSJonathan Lemon SMA_MODE_IN, 1571e1daf0ecSJonathan Lemon SMA_MODE_OUT, 1572e1daf0ecSJonathan Lemon }; 1573e1daf0ecSJonathan Lemon 1574e1daf0ecSJonathan Lemon static struct ptp_ocp_sma_connector { 1575e1daf0ecSJonathan Lemon enum ptp_ocp_sma_mode mode; 1576e1daf0ecSJonathan Lemon bool fixed_mode; 1577e1daf0ecSJonathan Lemon u16 default_out_idx; 1578e1daf0ecSJonathan Lemon } ptp_ocp_sma_map[4] = { 1579e1daf0ecSJonathan Lemon { 1580e1daf0ecSJonathan Lemon .mode = SMA_MODE_IN, 1581e1daf0ecSJonathan Lemon .fixed_mode = true, 1582e1daf0ecSJonathan Lemon }, 1583e1daf0ecSJonathan Lemon { 1584e1daf0ecSJonathan Lemon .mode = SMA_MODE_IN, 1585e1daf0ecSJonathan Lemon .fixed_mode = true, 1586e1daf0ecSJonathan Lemon }, 1587e1daf0ecSJonathan Lemon { 1588e1daf0ecSJonathan Lemon .mode = SMA_MODE_OUT, 1589e1daf0ecSJonathan Lemon .fixed_mode = true, 1590e1daf0ecSJonathan Lemon .default_out_idx = 0, /* 10Mhz */ 1591e1daf0ecSJonathan Lemon }, 1592e1daf0ecSJonathan Lemon { 1593e1daf0ecSJonathan Lemon .mode = SMA_MODE_OUT, 1594e1daf0ecSJonathan Lemon .fixed_mode = true, 1595e1daf0ecSJonathan Lemon .default_out_idx = 1, /* PHC */ 1596e1daf0ecSJonathan Lemon }, 1597e1daf0ecSJonathan Lemon }; 1598e1daf0ecSJonathan Lemon 1599e1daf0ecSJonathan Lemon static ssize_t 1600e1daf0ecSJonathan Lemon ptp_ocp_show_output(u32 val, char *buf, int default_idx) 1601e1daf0ecSJonathan Lemon { 1602e1daf0ecSJonathan Lemon const char *name; 1603e1daf0ecSJonathan Lemon ssize_t count; 1604e1daf0ecSJonathan Lemon 1605e1daf0ecSJonathan Lemon count = sysfs_emit(buf, "OUT: "); 1606e1daf0ecSJonathan Lemon name = ptp_ocp_select_name_from_val(ptp_ocp_sma_out, val); 1607e1daf0ecSJonathan Lemon if (!name) 1608e1daf0ecSJonathan Lemon name = ptp_ocp_sma_out[default_idx].name; 1609e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s\n", name); 1610e1daf0ecSJonathan Lemon return count; 1611e1daf0ecSJonathan Lemon } 1612e1daf0ecSJonathan Lemon 1613e1daf0ecSJonathan Lemon static ssize_t 1614e1daf0ecSJonathan Lemon ptp_ocp_show_inputs(u32 val, char *buf, const char *zero_in) 1615e1daf0ecSJonathan Lemon { 1616e1daf0ecSJonathan Lemon const char *name; 1617e1daf0ecSJonathan Lemon ssize_t count; 1618e1daf0ecSJonathan Lemon int i; 1619e1daf0ecSJonathan Lemon 1620e1daf0ecSJonathan Lemon count = sysfs_emit(buf, "IN: "); 1621e1daf0ecSJonathan Lemon for (i = 0; i < ARRAY_SIZE(ptp_ocp_sma_in); i++) { 1622e1daf0ecSJonathan Lemon if (val & ptp_ocp_sma_in[i].value) { 1623e1daf0ecSJonathan Lemon name = ptp_ocp_sma_in[i].name; 1624e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", name); 1625e1daf0ecSJonathan Lemon } 1626e1daf0ecSJonathan Lemon } 1627e1daf0ecSJonathan Lemon if (!val && zero_in) 1628e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", zero_in); 1629e1daf0ecSJonathan Lemon if (count) 1630e1daf0ecSJonathan Lemon count--; 1631e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "\n"); 1632e1daf0ecSJonathan Lemon return count; 1633e1daf0ecSJonathan Lemon } 1634e1daf0ecSJonathan Lemon 1635e1daf0ecSJonathan Lemon static int 1636e1daf0ecSJonathan Lemon sma_parse_inputs(const char *buf, enum ptp_ocp_sma_mode *mode) 1637e1daf0ecSJonathan Lemon { 1638e1daf0ecSJonathan Lemon struct ocp_selector *tbl[] = { ptp_ocp_sma_in, ptp_ocp_sma_out }; 1639e1daf0ecSJonathan Lemon int idx, count, dir; 1640e1daf0ecSJonathan Lemon char **argv; 1641e1daf0ecSJonathan Lemon int ret; 1642e1daf0ecSJonathan Lemon 1643e1daf0ecSJonathan Lemon argv = argv_split(GFP_KERNEL, buf, &count); 1644e1daf0ecSJonathan Lemon if (!argv) 1645e1daf0ecSJonathan Lemon return -ENOMEM; 1646e1daf0ecSJonathan Lemon 1647e1daf0ecSJonathan Lemon ret = -EINVAL; 1648e1daf0ecSJonathan Lemon if (!count) 1649e1daf0ecSJonathan Lemon goto out; 1650e1daf0ecSJonathan Lemon 1651e1daf0ecSJonathan Lemon idx = 0; 1652e1daf0ecSJonathan Lemon dir = *mode == SMA_MODE_IN ? 0 : 1; 1653e1daf0ecSJonathan Lemon if (!strcasecmp("IN:", argv[idx])) { 1654e1daf0ecSJonathan Lemon dir = 0; 1655e1daf0ecSJonathan Lemon idx++; 1656e1daf0ecSJonathan Lemon } 1657e1daf0ecSJonathan Lemon if (!strcasecmp("OUT:", argv[0])) { 1658e1daf0ecSJonathan Lemon dir = 1; 1659e1daf0ecSJonathan Lemon idx++; 1660e1daf0ecSJonathan Lemon } 1661e1daf0ecSJonathan Lemon *mode = dir == 0 ? SMA_MODE_IN : SMA_MODE_OUT; 1662e1daf0ecSJonathan Lemon 1663e1daf0ecSJonathan Lemon ret = 0; 1664e1daf0ecSJonathan Lemon for (; idx < count; idx++) 1665e1daf0ecSJonathan Lemon ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]); 1666e1daf0ecSJonathan Lemon if (ret < 0) 1667e1daf0ecSJonathan Lemon ret = -EINVAL; 1668e1daf0ecSJonathan Lemon 1669e1daf0ecSJonathan Lemon out: 1670e1daf0ecSJonathan Lemon argv_free(argv); 1671e1daf0ecSJonathan Lemon return ret; 1672e1daf0ecSJonathan Lemon } 1673e1daf0ecSJonathan Lemon 1674e1daf0ecSJonathan Lemon static ssize_t 1675e1daf0ecSJonathan Lemon ptp_ocp_sma_show(struct ptp_ocp *bp, int sma_nr, u32 val, char *buf, 1676e1daf0ecSJonathan Lemon const char *zero_in) 1677e1daf0ecSJonathan Lemon { 1678e1daf0ecSJonathan Lemon struct ptp_ocp_sma_connector *sma = &ptp_ocp_sma_map[sma_nr - 1]; 1679e1daf0ecSJonathan Lemon 1680e1daf0ecSJonathan Lemon if (sma->mode == SMA_MODE_IN) 1681e1daf0ecSJonathan Lemon return ptp_ocp_show_inputs(val, buf, zero_in); 1682e1daf0ecSJonathan Lemon 1683e1daf0ecSJonathan Lemon return ptp_ocp_show_output(val, buf, sma->default_out_idx); 1684e1daf0ecSJonathan Lemon } 1685e1daf0ecSJonathan Lemon 1686e1daf0ecSJonathan Lemon static ssize_t 1687e1daf0ecSJonathan Lemon sma1_show(struct device *dev, struct device_attribute *attr, char *buf) 1688e1daf0ecSJonathan Lemon { 1689e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1690e1daf0ecSJonathan Lemon u32 val; 1691e1daf0ecSJonathan Lemon 1692e1daf0ecSJonathan Lemon val = ioread32(&bp->sma->gpio1) & 0x3f; 1693e1daf0ecSJonathan Lemon return ptp_ocp_sma_show(bp, 1, val, buf, ptp_ocp_sma_in[0].name); 1694e1daf0ecSJonathan Lemon } 1695e1daf0ecSJonathan Lemon 1696e1daf0ecSJonathan Lemon static ssize_t 1697e1daf0ecSJonathan Lemon sma2_show(struct device *dev, struct device_attribute *attr, char *buf) 1698e1daf0ecSJonathan Lemon { 1699e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1700e1daf0ecSJonathan Lemon u32 val; 1701e1daf0ecSJonathan Lemon 1702e1daf0ecSJonathan Lemon val = (ioread32(&bp->sma->gpio1) >> 16) & 0x3f; 1703e1daf0ecSJonathan Lemon return ptp_ocp_sma_show(bp, 2, val, buf, NULL); 1704e1daf0ecSJonathan Lemon } 1705e1daf0ecSJonathan Lemon 1706e1daf0ecSJonathan Lemon static ssize_t 1707e1daf0ecSJonathan Lemon sma3_show(struct device *dev, struct device_attribute *attr, char *buf) 1708e1daf0ecSJonathan Lemon { 1709e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1710e1daf0ecSJonathan Lemon u32 val; 1711e1daf0ecSJonathan Lemon 1712e1daf0ecSJonathan Lemon val = ioread32(&bp->sma->gpio2) & 0x3f; 1713e1daf0ecSJonathan Lemon return ptp_ocp_sma_show(bp, 3, val, buf, NULL); 1714e1daf0ecSJonathan Lemon } 1715e1daf0ecSJonathan Lemon 1716e1daf0ecSJonathan Lemon static ssize_t 1717e1daf0ecSJonathan Lemon sma4_show(struct device *dev, struct device_attribute *attr, char *buf) 1718e1daf0ecSJonathan Lemon { 1719e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1720e1daf0ecSJonathan Lemon u32 val; 1721e1daf0ecSJonathan Lemon 1722e1daf0ecSJonathan Lemon val = (ioread32(&bp->sma->gpio2) >> 16) & 0x3f; 1723e1daf0ecSJonathan Lemon return ptp_ocp_sma_show(bp, 4, val, buf, NULL); 1724e1daf0ecSJonathan Lemon } 1725e1daf0ecSJonathan Lemon 1726e1daf0ecSJonathan Lemon static void 1727e1daf0ecSJonathan Lemon ptp_ocp_sma_store_output(struct ptp_ocp *bp, u32 val, u32 shift) 1728e1daf0ecSJonathan Lemon { 1729e1daf0ecSJonathan Lemon unsigned long flags; 1730e1daf0ecSJonathan Lemon u32 gpio, mask; 1731e1daf0ecSJonathan Lemon 1732e1daf0ecSJonathan Lemon mask = 0xffff << (16 - shift); 1733e1daf0ecSJonathan Lemon 1734e1daf0ecSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1735e1daf0ecSJonathan Lemon 1736e1daf0ecSJonathan Lemon gpio = ioread32(&bp->sma->gpio2); 1737e1daf0ecSJonathan Lemon gpio = (gpio & mask) | (val << shift); 17386baf2925SJonathan Lemon 17396baf2925SJonathan Lemon __handle_signal_outputs(bp, gpio); 17406baf2925SJonathan Lemon 1741e1daf0ecSJonathan Lemon iowrite32(gpio, &bp->sma->gpio2); 1742e1daf0ecSJonathan Lemon 1743e1daf0ecSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1744e1daf0ecSJonathan Lemon } 1745e1daf0ecSJonathan Lemon 1746e1daf0ecSJonathan Lemon static void 1747e1daf0ecSJonathan Lemon ptp_ocp_sma_store_inputs(struct ptp_ocp *bp, u32 val, u32 shift) 1748e1daf0ecSJonathan Lemon { 1749e1daf0ecSJonathan Lemon unsigned long flags; 1750e1daf0ecSJonathan Lemon u32 gpio, mask; 1751e1daf0ecSJonathan Lemon 1752e1daf0ecSJonathan Lemon mask = 0xffff << (16 - shift); 1753e1daf0ecSJonathan Lemon 1754e1daf0ecSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1755e1daf0ecSJonathan Lemon 1756e1daf0ecSJonathan Lemon gpio = ioread32(&bp->sma->gpio1); 1757e1daf0ecSJonathan Lemon gpio = (gpio & mask) | (val << shift); 17586baf2925SJonathan Lemon 17596baf2925SJonathan Lemon __handle_signal_inputs(bp, gpio); 17606baf2925SJonathan Lemon 1761e1daf0ecSJonathan Lemon iowrite32(gpio, &bp->sma->gpio1); 1762e1daf0ecSJonathan Lemon 1763e1daf0ecSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1764e1daf0ecSJonathan Lemon } 1765e1daf0ecSJonathan Lemon 1766e1daf0ecSJonathan Lemon static ssize_t 1767e1daf0ecSJonathan Lemon ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr, u32 shift) 1768e1daf0ecSJonathan Lemon { 1769e1daf0ecSJonathan Lemon struct ptp_ocp_sma_connector *sma = &ptp_ocp_sma_map[sma_nr - 1]; 1770e1daf0ecSJonathan Lemon enum ptp_ocp_sma_mode mode; 1771e1daf0ecSJonathan Lemon int val; 1772e1daf0ecSJonathan Lemon 1773e1daf0ecSJonathan Lemon mode = sma->mode; 1774e1daf0ecSJonathan Lemon val = sma_parse_inputs(buf, &mode); 1775e1daf0ecSJonathan Lemon if (val < 0) 1776e1daf0ecSJonathan Lemon return val; 1777e1daf0ecSJonathan Lemon 1778e1daf0ecSJonathan Lemon if (mode != sma->mode && sma->fixed_mode) 1779e1daf0ecSJonathan Lemon return -EOPNOTSUPP; 1780e1daf0ecSJonathan Lemon 1781e1daf0ecSJonathan Lemon if (mode != sma->mode) { 1782e1daf0ecSJonathan Lemon pr_err("Mode changes not supported yet.\n"); 1783e1daf0ecSJonathan Lemon return -EOPNOTSUPP; 1784e1daf0ecSJonathan Lemon } 1785e1daf0ecSJonathan Lemon 1786e1daf0ecSJonathan Lemon if (sma->mode == SMA_MODE_IN) 1787e1daf0ecSJonathan Lemon ptp_ocp_sma_store_inputs(bp, val, shift); 1788e1daf0ecSJonathan Lemon else 1789e1daf0ecSJonathan Lemon ptp_ocp_sma_store_output(bp, val, shift); 1790e1daf0ecSJonathan Lemon 1791e1daf0ecSJonathan Lemon return 0; 1792e1daf0ecSJonathan Lemon } 1793e1daf0ecSJonathan Lemon 1794e1daf0ecSJonathan Lemon static ssize_t 1795e1daf0ecSJonathan Lemon sma1_store(struct device *dev, struct device_attribute *attr, 1796e1daf0ecSJonathan Lemon const char *buf, size_t count) 1797e1daf0ecSJonathan Lemon { 1798e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1799e1daf0ecSJonathan Lemon int err; 1800e1daf0ecSJonathan Lemon 1801e1daf0ecSJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 1, 0); 1802e1daf0ecSJonathan Lemon return err ? err : count; 1803e1daf0ecSJonathan Lemon } 1804e1daf0ecSJonathan Lemon 1805e1daf0ecSJonathan Lemon static ssize_t 1806e1daf0ecSJonathan Lemon sma2_store(struct device *dev, struct device_attribute *attr, 1807e1daf0ecSJonathan Lemon const char *buf, size_t count) 1808e1daf0ecSJonathan Lemon { 1809e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1810e1daf0ecSJonathan Lemon int err; 1811e1daf0ecSJonathan Lemon 1812e1daf0ecSJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 2, 16); 1813e1daf0ecSJonathan Lemon return err ? err : count; 1814e1daf0ecSJonathan Lemon } 1815e1daf0ecSJonathan Lemon 1816e1daf0ecSJonathan Lemon static ssize_t 1817e1daf0ecSJonathan Lemon sma3_store(struct device *dev, struct device_attribute *attr, 1818e1daf0ecSJonathan Lemon const char *buf, size_t count) 1819e1daf0ecSJonathan Lemon { 1820e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1821e1daf0ecSJonathan Lemon int err; 1822e1daf0ecSJonathan Lemon 1823e1daf0ecSJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 3, 0); 1824e1daf0ecSJonathan Lemon return err ? err : count; 1825e1daf0ecSJonathan Lemon } 1826e1daf0ecSJonathan Lemon 1827e1daf0ecSJonathan Lemon static ssize_t 1828e1daf0ecSJonathan Lemon sma4_store(struct device *dev, struct device_attribute *attr, 1829e1daf0ecSJonathan Lemon const char *buf, size_t count) 1830e1daf0ecSJonathan Lemon { 1831e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1832e1daf0ecSJonathan Lemon int err; 1833e1daf0ecSJonathan Lemon 1834e1daf0ecSJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 4, 16); 1835e1daf0ecSJonathan Lemon return err ? err : count; 1836e1daf0ecSJonathan Lemon } 1837e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma1); 1838e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma2); 1839e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma3); 1840e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma4); 1841e1daf0ecSJonathan Lemon 1842e1daf0ecSJonathan Lemon static ssize_t 1843e1daf0ecSJonathan Lemon available_sma_inputs_show(struct device *dev, 1844e1daf0ecSJonathan Lemon struct device_attribute *attr, char *buf) 1845e1daf0ecSJonathan Lemon { 1846e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_sma_in, buf); 1847e1daf0ecSJonathan Lemon } 1848e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_inputs); 1849e1daf0ecSJonathan Lemon 1850e1daf0ecSJonathan Lemon static ssize_t 1851e1daf0ecSJonathan Lemon available_sma_outputs_show(struct device *dev, 1852e1daf0ecSJonathan Lemon struct device_attribute *attr, char *buf) 1853e1daf0ecSJonathan Lemon { 1854e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_sma_out, buf); 1855e1daf0ecSJonathan Lemon } 1856e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_outputs); 1857e1daf0ecSJonathan Lemon 1858773bda96SJonathan Lemon static ssize_t 1859773bda96SJonathan Lemon serialnum_show(struct device *dev, struct device_attribute *attr, char *buf) 1860773bda96SJonathan Lemon { 1861773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1862773bda96SJonathan Lemon 18630cfcdd1eSJonathan Lemon if (!bp->has_eeprom_data) 18640cfcdd1eSJonathan Lemon ptp_ocp_read_eeprom(bp); 1865773bda96SJonathan Lemon 1866773bda96SJonathan Lemon return sysfs_emit(buf, "%pM\n", bp->serial); 1867773bda96SJonathan Lemon } 1868773bda96SJonathan Lemon static DEVICE_ATTR_RO(serialnum); 1869773bda96SJonathan Lemon 1870773bda96SJonathan Lemon static ssize_t 1871ef0cfb34SJonathan Lemon gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf) 1872773bda96SJonathan Lemon { 1873773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1874773bda96SJonathan Lemon ssize_t ret; 1875773bda96SJonathan Lemon 1876ef0cfb34SJonathan Lemon if (bp->gnss_lost) 1877ef0cfb34SJonathan Lemon ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost); 1878773bda96SJonathan Lemon else 1879773bda96SJonathan Lemon ret = sysfs_emit(buf, "SYNC\n"); 1880773bda96SJonathan Lemon 1881773bda96SJonathan Lemon return ret; 1882773bda96SJonathan Lemon } 1883ef0cfb34SJonathan Lemon static DEVICE_ATTR_RO(gnss_sync); 1884773bda96SJonathan Lemon 1885773bda96SJonathan Lemon static ssize_t 188689260d87SJonathan Lemon utc_tai_offset_show(struct device *dev, 188789260d87SJonathan Lemon struct device_attribute *attr, char *buf) 188889260d87SJonathan Lemon { 188989260d87SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 189089260d87SJonathan Lemon 189189260d87SJonathan Lemon return sysfs_emit(buf, "%d\n", bp->utc_tai_offset); 189289260d87SJonathan Lemon } 189389260d87SJonathan Lemon 189489260d87SJonathan Lemon static ssize_t 189589260d87SJonathan Lemon utc_tai_offset_store(struct device *dev, 189689260d87SJonathan Lemon struct device_attribute *attr, 189789260d87SJonathan Lemon const char *buf, size_t count) 189889260d87SJonathan Lemon { 189989260d87SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 190089260d87SJonathan Lemon int err; 190189260d87SJonathan Lemon u32 val; 190289260d87SJonathan Lemon 190389260d87SJonathan Lemon err = kstrtou32(buf, 0, &val); 190489260d87SJonathan Lemon if (err) 190589260d87SJonathan Lemon return err; 190689260d87SJonathan Lemon 190789260d87SJonathan Lemon ptp_ocp_utc_distribute(bp, val); 190889260d87SJonathan Lemon 190989260d87SJonathan Lemon return count; 191089260d87SJonathan Lemon } 191189260d87SJonathan Lemon static DEVICE_ATTR_RW(utc_tai_offset); 191289260d87SJonathan Lemon 191389260d87SJonathan Lemon static ssize_t 19141acffc6eSJonathan Lemon ts_window_adjust_show(struct device *dev, 19151acffc6eSJonathan Lemon struct device_attribute *attr, char *buf) 19161acffc6eSJonathan Lemon { 19171acffc6eSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 19181acffc6eSJonathan Lemon 19191acffc6eSJonathan Lemon return sysfs_emit(buf, "%d\n", bp->ts_window_adjust); 19201acffc6eSJonathan Lemon } 19211acffc6eSJonathan Lemon 19221acffc6eSJonathan Lemon static ssize_t 19231acffc6eSJonathan Lemon ts_window_adjust_store(struct device *dev, 19241acffc6eSJonathan Lemon struct device_attribute *attr, 19251acffc6eSJonathan Lemon const char *buf, size_t count) 19261acffc6eSJonathan Lemon { 19271acffc6eSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 19281acffc6eSJonathan Lemon int err; 19291acffc6eSJonathan Lemon u32 val; 19301acffc6eSJonathan Lemon 19311acffc6eSJonathan Lemon err = kstrtou32(buf, 0, &val); 19321acffc6eSJonathan Lemon if (err) 19331acffc6eSJonathan Lemon return err; 19341acffc6eSJonathan Lemon 19351acffc6eSJonathan Lemon bp->ts_window_adjust = val; 19361acffc6eSJonathan Lemon 19371acffc6eSJonathan Lemon return count; 19381acffc6eSJonathan Lemon } 19391acffc6eSJonathan Lemon static DEVICE_ATTR_RW(ts_window_adjust); 19401acffc6eSJonathan Lemon 19411acffc6eSJonathan Lemon static ssize_t 1942d14ee252SJonathan Lemon irig_b_mode_show(struct device *dev, struct device_attribute *attr, char *buf) 1943d14ee252SJonathan Lemon { 1944d14ee252SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1945d14ee252SJonathan Lemon u32 val; 1946d14ee252SJonathan Lemon 1947d14ee252SJonathan Lemon val = ioread32(&bp->irig_out->ctrl); 1948d14ee252SJonathan Lemon val = (val >> 16) & 0x07; 1949d14ee252SJonathan Lemon return sysfs_emit(buf, "%d\n", val); 1950d14ee252SJonathan Lemon } 1951d14ee252SJonathan Lemon 1952d14ee252SJonathan Lemon static ssize_t 1953d14ee252SJonathan Lemon irig_b_mode_store(struct device *dev, 1954d14ee252SJonathan Lemon struct device_attribute *attr, 1955d14ee252SJonathan Lemon const char *buf, size_t count) 1956d14ee252SJonathan Lemon { 1957d14ee252SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1958d14ee252SJonathan Lemon unsigned long flags; 1959d14ee252SJonathan Lemon int err; 1960d14ee252SJonathan Lemon u32 reg; 1961d14ee252SJonathan Lemon u8 val; 1962d14ee252SJonathan Lemon 1963d14ee252SJonathan Lemon err = kstrtou8(buf, 0, &val); 1964d14ee252SJonathan Lemon if (err) 1965d14ee252SJonathan Lemon return err; 1966d14ee252SJonathan Lemon if (val > 7) 1967d14ee252SJonathan Lemon return -EINVAL; 1968d14ee252SJonathan Lemon 1969d14ee252SJonathan Lemon reg = ((val & 0x7) << 16); 1970d14ee252SJonathan Lemon 1971d14ee252SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1972d14ee252SJonathan Lemon iowrite32(0, &bp->irig_out->ctrl); /* disable */ 1973d14ee252SJonathan Lemon iowrite32(reg, &bp->irig_out->ctrl); /* change mode */ 1974d14ee252SJonathan Lemon iowrite32(reg | IRIG_M_CTRL_ENABLE, &bp->irig_out->ctrl); 1975d14ee252SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1976d14ee252SJonathan Lemon 1977d14ee252SJonathan Lemon return count; 1978d14ee252SJonathan Lemon } 1979d14ee252SJonathan Lemon static DEVICE_ATTR_RW(irig_b_mode); 1980d14ee252SJonathan Lemon 1981d14ee252SJonathan Lemon static ssize_t 1982773bda96SJonathan Lemon clock_source_show(struct device *dev, struct device_attribute *attr, char *buf) 1983773bda96SJonathan Lemon { 1984773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1985773bda96SJonathan Lemon const char *p; 1986773bda96SJonathan Lemon u32 select; 1987773bda96SJonathan Lemon 1988773bda96SJonathan Lemon select = ioread32(&bp->reg->select); 1989e1daf0ecSJonathan Lemon p = ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16); 1990773bda96SJonathan Lemon 1991773bda96SJonathan Lemon return sysfs_emit(buf, "%s\n", p); 1992773bda96SJonathan Lemon } 1993773bda96SJonathan Lemon 1994773bda96SJonathan Lemon static ssize_t 1995773bda96SJonathan Lemon clock_source_store(struct device *dev, struct device_attribute *attr, 1996773bda96SJonathan Lemon const char *buf, size_t count) 1997773bda96SJonathan Lemon { 1998773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1999773bda96SJonathan Lemon unsigned long flags; 2000773bda96SJonathan Lemon int val; 2001773bda96SJonathan Lemon 2002e1daf0ecSJonathan Lemon val = ptp_ocp_select_val_from_name(ptp_ocp_clock, buf); 2003773bda96SJonathan Lemon if (val < 0) 2004773bda96SJonathan Lemon return val; 2005773bda96SJonathan Lemon 2006773bda96SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 2007773bda96SJonathan Lemon iowrite32(val, &bp->reg->select); 2008773bda96SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 2009773bda96SJonathan Lemon 2010773bda96SJonathan Lemon return count; 2011773bda96SJonathan Lemon } 2012773bda96SJonathan Lemon static DEVICE_ATTR_RW(clock_source); 2013773bda96SJonathan Lemon 2014773bda96SJonathan Lemon static ssize_t 2015773bda96SJonathan Lemon available_clock_sources_show(struct device *dev, 2016773bda96SJonathan Lemon struct device_attribute *attr, char *buf) 2017773bda96SJonathan Lemon { 2018e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_clock, buf); 2019773bda96SJonathan Lemon } 2020773bda96SJonathan Lemon static DEVICE_ATTR_RO(available_clock_sources); 2021773bda96SJonathan Lemon 20222f23f486SVadim Fedorenko static ssize_t 20232f23f486SVadim Fedorenko clock_status_drift_show(struct device *dev, 20242f23f486SVadim Fedorenko struct device_attribute *attr, char *buf) 20252f23f486SVadim Fedorenko { 20262f23f486SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 20272f23f486SVadim Fedorenko u32 val; 20282f23f486SVadim Fedorenko int res; 20292f23f486SVadim Fedorenko 20302f23f486SVadim Fedorenko val = ioread32(&bp->reg->status_drift); 20312f23f486SVadim Fedorenko res = (val & ~INT_MAX) ? -1 : 1; 20322f23f486SVadim Fedorenko res *= (val & INT_MAX); 20332f23f486SVadim Fedorenko return sysfs_emit(buf, "%d\n", res); 20342f23f486SVadim Fedorenko } 20352f23f486SVadim Fedorenko static DEVICE_ATTR_RO(clock_status_drift); 20362f23f486SVadim Fedorenko 20372f23f486SVadim Fedorenko static ssize_t 20382f23f486SVadim Fedorenko clock_status_offset_show(struct device *dev, 20392f23f486SVadim Fedorenko struct device_attribute *attr, char *buf) 20402f23f486SVadim Fedorenko { 20412f23f486SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 20422f23f486SVadim Fedorenko u32 val; 20432f23f486SVadim Fedorenko int res; 20442f23f486SVadim Fedorenko 20452f23f486SVadim Fedorenko val = ioread32(&bp->reg->status_offset); 20462f23f486SVadim Fedorenko res = (val & ~INT_MAX) ? -1 : 1; 20472f23f486SVadim Fedorenko res *= (val & INT_MAX); 20482f23f486SVadim Fedorenko return sysfs_emit(buf, "%d\n", res); 20492f23f486SVadim Fedorenko } 20502f23f486SVadim Fedorenko static DEVICE_ATTR_RO(clock_status_offset); 20512f23f486SVadim Fedorenko 205244a412d1SVadim Fedorenko static ssize_t 205344a412d1SVadim Fedorenko tod_correction_show(struct device *dev, 205444a412d1SVadim Fedorenko struct device_attribute *attr, char *buf) 205544a412d1SVadim Fedorenko { 205644a412d1SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 205744a412d1SVadim Fedorenko u32 val; 205844a412d1SVadim Fedorenko int res; 205944a412d1SVadim Fedorenko 206044a412d1SVadim Fedorenko val = ioread32(&bp->tod->adj_sec); 206144a412d1SVadim Fedorenko res = (val & ~INT_MAX) ? -1 : 1; 206244a412d1SVadim Fedorenko res *= (val & INT_MAX); 206344a412d1SVadim Fedorenko return sysfs_emit(buf, "%d\n", res); 206444a412d1SVadim Fedorenko } 206544a412d1SVadim Fedorenko 206644a412d1SVadim Fedorenko static ssize_t 206744a412d1SVadim Fedorenko tod_correction_store(struct device *dev, struct device_attribute *attr, 206844a412d1SVadim Fedorenko const char *buf, size_t count) 206944a412d1SVadim Fedorenko { 207044a412d1SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 207144a412d1SVadim Fedorenko unsigned long flags; 207244a412d1SVadim Fedorenko int err, res; 207344a412d1SVadim Fedorenko u32 val = 0; 207444a412d1SVadim Fedorenko 207544a412d1SVadim Fedorenko err = kstrtos32(buf, 0, &res); 207644a412d1SVadim Fedorenko if (err) 207744a412d1SVadim Fedorenko return err; 207844a412d1SVadim Fedorenko if (res < 0) { 207944a412d1SVadim Fedorenko res *= -1; 208044a412d1SVadim Fedorenko val |= BIT(31); 208144a412d1SVadim Fedorenko } 208244a412d1SVadim Fedorenko val |= res; 208344a412d1SVadim Fedorenko 208444a412d1SVadim Fedorenko spin_lock_irqsave(&bp->lock, flags); 208544a412d1SVadim Fedorenko iowrite32(val, &bp->tod->adj_sec); 208644a412d1SVadim Fedorenko spin_unlock_irqrestore(&bp->lock, flags); 208744a412d1SVadim Fedorenko 208844a412d1SVadim Fedorenko return count; 208944a412d1SVadim Fedorenko } 209044a412d1SVadim Fedorenko static DEVICE_ATTR_RW(tod_correction); 209144a412d1SVadim Fedorenko 2092773bda96SJonathan Lemon static struct attribute *timecard_attrs[] = { 2093773bda96SJonathan Lemon &dev_attr_serialnum.attr, 2094ef0cfb34SJonathan Lemon &dev_attr_gnss_sync.attr, 2095773bda96SJonathan Lemon &dev_attr_clock_source.attr, 2096773bda96SJonathan Lemon &dev_attr_available_clock_sources.attr, 2097e1daf0ecSJonathan Lemon &dev_attr_sma1.attr, 2098e1daf0ecSJonathan Lemon &dev_attr_sma2.attr, 2099e1daf0ecSJonathan Lemon &dev_attr_sma3.attr, 2100e1daf0ecSJonathan Lemon &dev_attr_sma4.attr, 2101e1daf0ecSJonathan Lemon &dev_attr_available_sma_inputs.attr, 2102e1daf0ecSJonathan Lemon &dev_attr_available_sma_outputs.attr, 21032f23f486SVadim Fedorenko &dev_attr_clock_status_drift.attr, 21042f23f486SVadim Fedorenko &dev_attr_clock_status_offset.attr, 2105d14ee252SJonathan Lemon &dev_attr_irig_b_mode.attr, 210689260d87SJonathan Lemon &dev_attr_utc_tai_offset.attr, 21071acffc6eSJonathan Lemon &dev_attr_ts_window_adjust.attr, 210844a412d1SVadim Fedorenko &dev_attr_tod_correction.attr, 2109773bda96SJonathan Lemon NULL, 2110773bda96SJonathan Lemon }; 2111773bda96SJonathan Lemon ATTRIBUTE_GROUPS(timecard); 2112773bda96SJonathan Lemon 2113f67bf662SJonathan Lemon static const char * 2114f67bf662SJonathan Lemon gpio_map(u32 gpio, u32 bit, const char *pri, const char *sec, const char *def) 2115f67bf662SJonathan Lemon { 2116f67bf662SJonathan Lemon const char *ans; 2117f67bf662SJonathan Lemon 2118f67bf662SJonathan Lemon if (gpio & (1 << bit)) 2119f67bf662SJonathan Lemon ans = pri; 2120f67bf662SJonathan Lemon else if (gpio & (1 << (bit + 16))) 2121f67bf662SJonathan Lemon ans = sec; 2122f67bf662SJonathan Lemon else 2123f67bf662SJonathan Lemon ans = def; 2124f67bf662SJonathan Lemon return ans; 2125f67bf662SJonathan Lemon } 2126f67bf662SJonathan Lemon 2127f67bf662SJonathan Lemon static void 2128f67bf662SJonathan Lemon gpio_multi_map(char *buf, u32 gpio, u32 bit, 2129f67bf662SJonathan Lemon const char *pri, const char *sec, const char *def) 2130f67bf662SJonathan Lemon { 2131f67bf662SJonathan Lemon char *ans = buf; 2132f67bf662SJonathan Lemon 2133f67bf662SJonathan Lemon strcpy(ans, def); 2134f67bf662SJonathan Lemon if (gpio & (1 << bit)) 2135f67bf662SJonathan Lemon ans += sprintf(ans, "%s ", pri); 2136f67bf662SJonathan Lemon if (gpio & (1 << (bit + 16))) 2137f67bf662SJonathan Lemon ans += sprintf(ans, "%s ", sec); 2138f67bf662SJonathan Lemon } 2139f67bf662SJonathan Lemon 2140f67bf662SJonathan Lemon static int 2141f67bf662SJonathan Lemon ptp_ocp_summary_show(struct seq_file *s, void *data) 2142f67bf662SJonathan Lemon { 2143f67bf662SJonathan Lemon struct device *dev = s->private; 2144f67bf662SJonathan Lemon struct ptp_system_timestamp sts; 2145f67bf662SJonathan Lemon u32 sma_in, sma_out, ctrl, val; 2146f67bf662SJonathan Lemon struct ts_reg __iomem *ts_reg; 2147f67bf662SJonathan Lemon struct timespec64 ts; 2148f67bf662SJonathan Lemon struct ptp_ocp *bp; 2149f67bf662SJonathan Lemon const char *src; 2150a62a56d0SJonathan Lemon bool on, map; 2151f67bf662SJonathan Lemon char *buf; 2152f67bf662SJonathan Lemon 2153f67bf662SJonathan Lemon buf = (char *)__get_free_page(GFP_KERNEL); 2154f67bf662SJonathan Lemon if (!buf) 2155f67bf662SJonathan Lemon return -ENOMEM; 2156f67bf662SJonathan Lemon 2157f67bf662SJonathan Lemon bp = dev_get_drvdata(dev); 2158f67bf662SJonathan Lemon sma_in = ioread32(&bp->sma->gpio1); 2159f67bf662SJonathan Lemon sma_out = ioread32(&bp->sma->gpio2); 2160f67bf662SJonathan Lemon 2161f67bf662SJonathan Lemon seq_printf(s, "%7s: /dev/ptp%d\n", "PTP", ptp_clock_index(bp->ptp)); 216261fd7ac2SJonathan Lemon if (bp->gnss_port != -1) 216361fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS1", bp->gnss_port); 216461fd7ac2SJonathan Lemon if (bp->gnss2_port != -1) 216561fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS2", bp->gnss2_port); 216661fd7ac2SJonathan Lemon if (bp->mac_port != -1) 216761fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "MAC", bp->mac_port); 216861fd7ac2SJonathan Lemon if (bp->nmea_port != -1) 216961fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "NMEA", bp->nmea_port); 2170f67bf662SJonathan Lemon 2171f67bf662SJonathan Lemon sma1_show(dev, NULL, buf); 2172f67bf662SJonathan Lemon seq_printf(s, " sma1: %s", buf); 2173f67bf662SJonathan Lemon 2174f67bf662SJonathan Lemon sma2_show(dev, NULL, buf); 2175f67bf662SJonathan Lemon seq_printf(s, " sma2: %s", buf); 2176f67bf662SJonathan Lemon 2177f67bf662SJonathan Lemon sma3_show(dev, NULL, buf); 2178f67bf662SJonathan Lemon seq_printf(s, " sma3: %s", buf); 2179f67bf662SJonathan Lemon 2180f67bf662SJonathan Lemon sma4_show(dev, NULL, buf); 2181f67bf662SJonathan Lemon seq_printf(s, " sma4: %s", buf); 2182f67bf662SJonathan Lemon 2183f67bf662SJonathan Lemon if (bp->ts0) { 2184f67bf662SJonathan Lemon ts_reg = bp->ts0->mem; 2185f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 2186f67bf662SJonathan Lemon src = "GNSS"; 2187f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS0", 2188f67bf662SJonathan Lemon on ? " ON" : "OFF", src); 2189f67bf662SJonathan Lemon } 2190f67bf662SJonathan Lemon 2191f67bf662SJonathan Lemon if (bp->ts1) { 2192f67bf662SJonathan Lemon ts_reg = bp->ts1->mem; 2193f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 2194f67bf662SJonathan Lemon src = gpio_map(sma_in, 2, "sma1", "sma2", "----"); 2195f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS1", 2196f67bf662SJonathan Lemon on ? " ON" : "OFF", src); 2197f67bf662SJonathan Lemon } 2198f67bf662SJonathan Lemon 2199f67bf662SJonathan Lemon if (bp->ts2) { 2200f67bf662SJonathan Lemon ts_reg = bp->ts2->mem; 2201f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 2202f67bf662SJonathan Lemon src = gpio_map(sma_in, 3, "sma1", "sma2", "----"); 2203f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS2", 2204f67bf662SJonathan Lemon on ? " ON" : "OFF", src); 2205f67bf662SJonathan Lemon } 2206f67bf662SJonathan Lemon 2207a62a56d0SJonathan Lemon if (bp->pps) { 2208a62a56d0SJonathan Lemon ts_reg = bp->pps->mem; 2209a62a56d0SJonathan Lemon src = "PHC"; 2210a62a56d0SJonathan Lemon on = ioread32(&ts_reg->enable); 2211a62a56d0SJonathan Lemon map = !!(bp->pps_req_map & OCP_REQ_TIMESTAMP); 2212a62a56d0SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS3", 22131a575cdeSNathan Chancellor on && map ? " ON" : "OFF", src); 2214a62a56d0SJonathan Lemon 2215a62a56d0SJonathan Lemon map = !!(bp->pps_req_map & OCP_REQ_PPS); 2216a62a56d0SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "PPS", 22171a575cdeSNathan Chancellor on && map ? " ON" : "OFF", src); 2218a62a56d0SJonathan Lemon } 2219a62a56d0SJonathan Lemon 2220f67bf662SJonathan Lemon if (bp->irig_out) { 2221f67bf662SJonathan Lemon ctrl = ioread32(&bp->irig_out->ctrl); 2222f67bf662SJonathan Lemon on = ctrl & IRIG_M_CTRL_ENABLE; 2223f67bf662SJonathan Lemon val = ioread32(&bp->irig_out->status); 2224f67bf662SJonathan Lemon gpio_multi_map(buf, sma_out, 4, "sma3", "sma4", "----"); 2225f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, mode %d, out: %s\n", "IRIG", 2226f67bf662SJonathan Lemon on ? " ON" : "OFF", val, (ctrl >> 16), buf); 2227f67bf662SJonathan Lemon } 2228f67bf662SJonathan Lemon 2229f67bf662SJonathan Lemon if (bp->irig_in) { 2230f67bf662SJonathan Lemon on = ioread32(&bp->irig_in->ctrl) & IRIG_S_CTRL_ENABLE; 2231f67bf662SJonathan Lemon val = ioread32(&bp->irig_in->status); 2232f67bf662SJonathan Lemon src = gpio_map(sma_in, 4, "sma1", "sma2", "----"); 2233f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, src: %s\n", "IRIG in", 2234f67bf662SJonathan Lemon on ? " ON" : "OFF", val, src); 2235f67bf662SJonathan Lemon } 2236f67bf662SJonathan Lemon 2237f67bf662SJonathan Lemon if (bp->dcf_out) { 2238f67bf662SJonathan Lemon on = ioread32(&bp->dcf_out->ctrl) & DCF_M_CTRL_ENABLE; 2239f67bf662SJonathan Lemon val = ioread32(&bp->dcf_out->status); 2240f67bf662SJonathan Lemon gpio_multi_map(buf, sma_out, 5, "sma3", "sma4", "----"); 2241f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, out: %s\n", "DCF", 2242f67bf662SJonathan Lemon on ? " ON" : "OFF", val, buf); 2243f67bf662SJonathan Lemon } 2244f67bf662SJonathan Lemon 2245f67bf662SJonathan Lemon if (bp->dcf_in) { 2246f67bf662SJonathan Lemon on = ioread32(&bp->dcf_in->ctrl) & DCF_S_CTRL_ENABLE; 2247f67bf662SJonathan Lemon val = ioread32(&bp->dcf_in->status); 2248f67bf662SJonathan Lemon src = gpio_map(sma_in, 5, "sma1", "sma2", "----"); 2249f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, src: %s\n", "DCF in", 2250f67bf662SJonathan Lemon on ? " ON" : "OFF", val, src); 2251f67bf662SJonathan Lemon } 2252f67bf662SJonathan Lemon 2253e3516bb4SJonathan Lemon if (bp->nmea_out) { 2254e3516bb4SJonathan Lemon on = ioread32(&bp->nmea_out->ctrl) & 1; 2255e3516bb4SJonathan Lemon val = ioread32(&bp->nmea_out->status); 2256e3516bb4SJonathan Lemon seq_printf(s, "%7s: %s, error: %d\n", "NMEA", 2257e3516bb4SJonathan Lemon on ? " ON" : "OFF", val); 2258e3516bb4SJonathan Lemon } 2259e3516bb4SJonathan Lemon 2260f67bf662SJonathan Lemon /* compute src for PPS1, used below. */ 2261f67bf662SJonathan Lemon if (bp->pps_select) { 2262f67bf662SJonathan Lemon val = ioread32(&bp->pps_select->gpio1); 2263f67bf662SJonathan Lemon if (val & 0x01) 2264f67bf662SJonathan Lemon src = gpio_map(sma_in, 0, "sma1", "sma2", "----"); 2265f67bf662SJonathan Lemon else if (val & 0x02) 2266f67bf662SJonathan Lemon src = "MAC"; 2267f67bf662SJonathan Lemon else if (val & 0x04) 2268f67bf662SJonathan Lemon src = "GNSS"; 2269f67bf662SJonathan Lemon else 2270f67bf662SJonathan Lemon src = "----"; 2271f67bf662SJonathan Lemon } else { 2272f67bf662SJonathan Lemon src = "?"; 2273f67bf662SJonathan Lemon } 2274f67bf662SJonathan Lemon 2275f67bf662SJonathan Lemon /* assumes automatic switchover/selection */ 2276f67bf662SJonathan Lemon val = ioread32(&bp->reg->select); 2277f67bf662SJonathan Lemon switch (val >> 16) { 2278f67bf662SJonathan Lemon case 0: 2279f67bf662SJonathan Lemon sprintf(buf, "----"); 2280f67bf662SJonathan Lemon break; 2281f67bf662SJonathan Lemon case 2: 2282f67bf662SJonathan Lemon sprintf(buf, "IRIG"); 2283f67bf662SJonathan Lemon break; 2284f67bf662SJonathan Lemon case 3: 2285f67bf662SJonathan Lemon sprintf(buf, "%s via PPS1", src); 2286f67bf662SJonathan Lemon break; 2287f67bf662SJonathan Lemon case 6: 2288f67bf662SJonathan Lemon sprintf(buf, "DCF"); 2289f67bf662SJonathan Lemon break; 2290f67bf662SJonathan Lemon default: 2291f67bf662SJonathan Lemon strcpy(buf, "unknown"); 2292f67bf662SJonathan Lemon break; 2293f67bf662SJonathan Lemon } 2294f67bf662SJonathan Lemon val = ioread32(&bp->reg->status); 2295f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, state: %s\n", "PHC src", buf, 2296f67bf662SJonathan Lemon val & OCP_STATUS_IN_SYNC ? "sync" : "unsynced"); 2297f67bf662SJonathan Lemon 2298f67bf662SJonathan Lemon /* reuses PPS1 src from earlier */ 2299f67bf662SJonathan Lemon seq_printf(s, "MAC PPS1 src: %s\n", src); 2300f67bf662SJonathan Lemon 2301f67bf662SJonathan Lemon src = gpio_map(sma_in, 1, "sma1", "sma2", "GNSS2"); 2302f67bf662SJonathan Lemon seq_printf(s, "MAC PPS2 src: %s\n", src); 2303f67bf662SJonathan Lemon 2304f67bf662SJonathan Lemon if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts)) { 2305f67bf662SJonathan Lemon struct timespec64 sys_ts; 2306f67bf662SJonathan Lemon s64 pre_ns, post_ns, ns; 2307f67bf662SJonathan Lemon 2308f67bf662SJonathan Lemon pre_ns = timespec64_to_ns(&sts.pre_ts); 2309f67bf662SJonathan Lemon post_ns = timespec64_to_ns(&sts.post_ts); 2310f67bf662SJonathan Lemon ns = (pre_ns + post_ns) / 2; 2311f67bf662SJonathan Lemon ns += (s64)bp->utc_tai_offset * NSEC_PER_SEC; 2312f67bf662SJonathan Lemon sys_ts = ns_to_timespec64(ns); 2313f67bf662SJonathan Lemon 2314f67bf662SJonathan Lemon seq_printf(s, "%7s: %lld.%ld == %ptT TAI\n", "PHC", 2315f67bf662SJonathan Lemon ts.tv_sec, ts.tv_nsec, &ts); 2316f67bf662SJonathan Lemon seq_printf(s, "%7s: %lld.%ld == %ptT UTC offset %d\n", "SYS", 2317f67bf662SJonathan Lemon sys_ts.tv_sec, sys_ts.tv_nsec, &sys_ts, 2318f67bf662SJonathan Lemon bp->utc_tai_offset); 2319f67bf662SJonathan Lemon seq_printf(s, "%7s: PHC:SYS offset: %lld window: %lld\n", "", 2320f67bf662SJonathan Lemon timespec64_to_ns(&ts) - ns, 2321f67bf662SJonathan Lemon post_ns - pre_ns); 2322f67bf662SJonathan Lemon } 2323f67bf662SJonathan Lemon 2324f67bf662SJonathan Lemon free_page((unsigned long)buf); 2325f67bf662SJonathan Lemon return 0; 2326f67bf662SJonathan Lemon } 2327f67bf662SJonathan Lemon DEFINE_SHOW_ATTRIBUTE(ptp_ocp_summary); 2328f67bf662SJonathan Lemon 23299f492c4cSVadim Fedorenko static int 23309f492c4cSVadim Fedorenko ptp_ocp_tod_status_show(struct seq_file *s, void *data) 23319f492c4cSVadim Fedorenko { 23329f492c4cSVadim Fedorenko struct device *dev = s->private; 23339f492c4cSVadim Fedorenko struct ptp_ocp *bp; 23349f492c4cSVadim Fedorenko u32 val; 23359f492c4cSVadim Fedorenko int idx; 23369f492c4cSVadim Fedorenko 23379f492c4cSVadim Fedorenko bp = dev_get_drvdata(dev); 23389f492c4cSVadim Fedorenko 23399f492c4cSVadim Fedorenko val = ioread32(&bp->tod->ctrl); 23409f492c4cSVadim Fedorenko if (!(val & TOD_CTRL_ENABLE)) { 23419f492c4cSVadim Fedorenko seq_printf(s, "TOD Slave disabled\n"); 23429f492c4cSVadim Fedorenko return 0; 23439f492c4cSVadim Fedorenko } 23449f492c4cSVadim Fedorenko seq_printf(s, "TOD Slave enabled, Control Register 0x%08X\n", val); 23459f492c4cSVadim Fedorenko 23469f492c4cSVadim Fedorenko idx = val & TOD_CTRL_PROTOCOL ? 4 : 0; 23479f492c4cSVadim Fedorenko idx += (val >> 16) & 3; 23489f492c4cSVadim Fedorenko seq_printf(s, "Protocol %s\n", ptp_ocp_tod_proto_name(idx)); 23499f492c4cSVadim Fedorenko 23509f492c4cSVadim Fedorenko idx = (val >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK; 23519f492c4cSVadim Fedorenko seq_printf(s, "GNSS %s\n", ptp_ocp_tod_gnss_name(idx)); 23529f492c4cSVadim Fedorenko 23539f492c4cSVadim Fedorenko val = ioread32(&bp->tod->version); 23549f492c4cSVadim Fedorenko seq_printf(s, "TOD Version %d.%d.%d\n", 23559f492c4cSVadim Fedorenko val >> 24, (val >> 16) & 0xff, val & 0xffff); 23569f492c4cSVadim Fedorenko 23579f492c4cSVadim Fedorenko val = ioread32(&bp->tod->status); 23589f492c4cSVadim Fedorenko seq_printf(s, "Status register: 0x%08X\n", val); 23599f492c4cSVadim Fedorenko 23609f492c4cSVadim Fedorenko val = ioread32(&bp->tod->adj_sec); 23619f492c4cSVadim Fedorenko idx = (val & ~INT_MAX) ? -1 : 1; 23629f492c4cSVadim Fedorenko idx *= (val & INT_MAX); 23639f492c4cSVadim Fedorenko seq_printf(s, "Correction seconds: %d\n", idx); 23649f492c4cSVadim Fedorenko 23659f492c4cSVadim Fedorenko val = ioread32(&bp->tod->utc_status); 23669f492c4cSVadim Fedorenko seq_printf(s, "UTC status register: 0x%08X\n", val); 23679f492c4cSVadim Fedorenko seq_printf(s, "UTC offset: %d valid:%d\n", 23689f492c4cSVadim Fedorenko val & TOD_STATUS_UTC_MASK, val & TOD_STATUS_UTC_VALID ? 1 : 0); 23699f492c4cSVadim Fedorenko seq_printf(s, "Leap second info valid:%d, Leap second announce %d\n", 23709f492c4cSVadim Fedorenko val & TOD_STATUS_LEAP_VALID ? 1 : 0, 23719f492c4cSVadim Fedorenko val & TOD_STATUS_LEAP_ANNOUNCE ? 1 : 0); 23729f492c4cSVadim Fedorenko 23739f492c4cSVadim Fedorenko val = ioread32(&bp->tod->leap); 23749f492c4cSVadim Fedorenko seq_printf(s, "Time to next leap second (in sec): %d\n", (s32) val); 23759f492c4cSVadim Fedorenko 23769f492c4cSVadim Fedorenko return 0; 23779f492c4cSVadim Fedorenko } 23789f492c4cSVadim Fedorenko DEFINE_SHOW_ATTRIBUTE(ptp_ocp_tod_status); 23799f492c4cSVadim Fedorenko 2380f67bf662SJonathan Lemon static struct dentry *ptp_ocp_debugfs_root; 2381f67bf662SJonathan Lemon 2382f67bf662SJonathan Lemon static void 2383f67bf662SJonathan Lemon ptp_ocp_debugfs_add_device(struct ptp_ocp *bp) 2384f67bf662SJonathan Lemon { 2385f67bf662SJonathan Lemon struct dentry *d; 2386f67bf662SJonathan Lemon 2387f67bf662SJonathan Lemon d = debugfs_create_dir(dev_name(&bp->dev), ptp_ocp_debugfs_root); 2388f67bf662SJonathan Lemon bp->debug_root = d; 2389f67bf662SJonathan Lemon debugfs_create_file("summary", 0444, bp->debug_root, 2390f67bf662SJonathan Lemon &bp->dev, &ptp_ocp_summary_fops); 23919f492c4cSVadim Fedorenko if (bp->tod) 23929f492c4cSVadim Fedorenko debugfs_create_file("tod_status", 0444, bp->debug_root, 23939f492c4cSVadim Fedorenko &bp->dev, &ptp_ocp_tod_status_fops); 2394f67bf662SJonathan Lemon } 2395f67bf662SJonathan Lemon 2396f67bf662SJonathan Lemon static void 2397f67bf662SJonathan Lemon ptp_ocp_debugfs_remove_device(struct ptp_ocp *bp) 2398f67bf662SJonathan Lemon { 2399f67bf662SJonathan Lemon debugfs_remove_recursive(bp->debug_root); 2400f67bf662SJonathan Lemon } 2401f67bf662SJonathan Lemon 2402f67bf662SJonathan Lemon static void 2403f67bf662SJonathan Lemon ptp_ocp_debugfs_init(void) 2404f67bf662SJonathan Lemon { 2405f67bf662SJonathan Lemon ptp_ocp_debugfs_root = debugfs_create_dir("timecard", NULL); 2406f67bf662SJonathan Lemon } 2407f67bf662SJonathan Lemon 2408f67bf662SJonathan Lemon static void 2409f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(void) 2410f67bf662SJonathan Lemon { 2411f67bf662SJonathan Lemon debugfs_remove_recursive(ptp_ocp_debugfs_root); 2412f67bf662SJonathan Lemon } 2413f67bf662SJonathan Lemon 2414773bda96SJonathan Lemon static void 2415773bda96SJonathan Lemon ptp_ocp_dev_release(struct device *dev) 2416773bda96SJonathan Lemon { 2417773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2418773bda96SJonathan Lemon 2419773bda96SJonathan Lemon mutex_lock(&ptp_ocp_lock); 2420773bda96SJonathan Lemon idr_remove(&ptp_ocp_idr, bp->id); 2421773bda96SJonathan Lemon mutex_unlock(&ptp_ocp_lock); 2422773bda96SJonathan Lemon } 2423773bda96SJonathan Lemon 2424773bda96SJonathan Lemon static int 2425773bda96SJonathan Lemon ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev) 2426773bda96SJonathan Lemon { 2427773bda96SJonathan Lemon int err; 2428773bda96SJonathan Lemon 2429773bda96SJonathan Lemon mutex_lock(&ptp_ocp_lock); 2430773bda96SJonathan Lemon err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL); 2431773bda96SJonathan Lemon mutex_unlock(&ptp_ocp_lock); 2432773bda96SJonathan Lemon if (err < 0) { 2433773bda96SJonathan Lemon dev_err(&pdev->dev, "idr_alloc failed: %d\n", err); 2434773bda96SJonathan Lemon return err; 2435773bda96SJonathan Lemon } 2436773bda96SJonathan Lemon bp->id = err; 2437773bda96SJonathan Lemon 2438773bda96SJonathan Lemon bp->ptp_info = ptp_ocp_clock_info; 2439773bda96SJonathan Lemon spin_lock_init(&bp->lock); 2440ef0cfb34SJonathan Lemon bp->gnss_port = -1; 244171d7e085SJonathan Lemon bp->gnss2_port = -1; 2442773bda96SJonathan Lemon bp->mac_port = -1; 2443e3516bb4SJonathan Lemon bp->nmea_port = -1; 2444773bda96SJonathan Lemon bp->pdev = pdev; 2445773bda96SJonathan Lemon 2446773bda96SJonathan Lemon device_initialize(&bp->dev); 2447773bda96SJonathan Lemon dev_set_name(&bp->dev, "ocp%d", bp->id); 2448773bda96SJonathan Lemon bp->dev.class = &timecard_class; 2449773bda96SJonathan Lemon bp->dev.parent = &pdev->dev; 2450773bda96SJonathan Lemon bp->dev.release = ptp_ocp_dev_release; 2451773bda96SJonathan Lemon dev_set_drvdata(&bp->dev, bp); 2452773bda96SJonathan Lemon 2453773bda96SJonathan Lemon err = device_add(&bp->dev); 2454773bda96SJonathan Lemon if (err) { 2455773bda96SJonathan Lemon dev_err(&bp->dev, "device add failed: %d\n", err); 2456773bda96SJonathan Lemon goto out; 2457773bda96SJonathan Lemon } 2458773bda96SJonathan Lemon 2459773bda96SJonathan Lemon pci_set_drvdata(pdev, bp); 2460773bda96SJonathan Lemon 2461773bda96SJonathan Lemon return 0; 2462773bda96SJonathan Lemon 2463773bda96SJonathan Lemon out: 2464773bda96SJonathan Lemon ptp_ocp_dev_release(&bp->dev); 2465d12f23faSJonathan Lemon put_device(&bp->dev); 2466773bda96SJonathan Lemon return err; 2467773bda96SJonathan Lemon } 2468773bda96SJonathan Lemon 2469773bda96SJonathan Lemon static void 2470773bda96SJonathan Lemon ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link) 2471773bda96SJonathan Lemon { 2472773bda96SJonathan Lemon struct device *dev = &bp->dev; 2473773bda96SJonathan Lemon 2474773bda96SJonathan Lemon if (sysfs_create_link(&dev->kobj, &child->kobj, link)) 2475773bda96SJonathan Lemon dev_err(dev, "%s symlink failed\n", link); 2476773bda96SJonathan Lemon } 2477773bda96SJonathan Lemon 2478773bda96SJonathan Lemon static void 2479773bda96SJonathan Lemon ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link) 2480773bda96SJonathan Lemon { 2481773bda96SJonathan Lemon struct device *dev, *child; 2482773bda96SJonathan Lemon 2483773bda96SJonathan Lemon dev = &bp->pdev->dev; 2484773bda96SJonathan Lemon 2485773bda96SJonathan Lemon child = device_find_child_by_name(dev, name); 2486773bda96SJonathan Lemon if (!child) { 2487773bda96SJonathan Lemon dev_err(dev, "Could not find device %s\n", name); 2488773bda96SJonathan Lemon return; 2489773bda96SJonathan Lemon } 2490773bda96SJonathan Lemon 2491773bda96SJonathan Lemon ptp_ocp_symlink(bp, child, link); 2492773bda96SJonathan Lemon put_device(child); 2493773bda96SJonathan Lemon } 2494773bda96SJonathan Lemon 2495773bda96SJonathan Lemon static int 2496773bda96SJonathan Lemon ptp_ocp_complete(struct ptp_ocp *bp) 2497773bda96SJonathan Lemon { 2498773bda96SJonathan Lemon struct pps_device *pps; 2499773bda96SJonathan Lemon char buf[32]; 2500773bda96SJonathan Lemon 2501ef0cfb34SJonathan Lemon if (bp->gnss_port != -1) { 2502ef0cfb34SJonathan Lemon sprintf(buf, "ttyS%d", bp->gnss_port); 2503ef0cfb34SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyGNSS"); 2504773bda96SJonathan Lemon } 250571d7e085SJonathan Lemon if (bp->gnss2_port != -1) { 250671d7e085SJonathan Lemon sprintf(buf, "ttyS%d", bp->gnss2_port); 250771d7e085SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyGNSS2"); 250871d7e085SJonathan Lemon } 2509773bda96SJonathan Lemon if (bp->mac_port != -1) { 2510773bda96SJonathan Lemon sprintf(buf, "ttyS%d", bp->mac_port); 2511773bda96SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyMAC"); 2512773bda96SJonathan Lemon } 2513e3516bb4SJonathan Lemon if (bp->nmea_port != -1) { 2514e3516bb4SJonathan Lemon sprintf(buf, "ttyS%d", bp->nmea_port); 2515e3516bb4SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyNMEA"); 2516e3516bb4SJonathan Lemon } 2517773bda96SJonathan Lemon sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp)); 2518773bda96SJonathan Lemon ptp_ocp_link_child(bp, buf, "ptp"); 2519773bda96SJonathan Lemon 2520773bda96SJonathan Lemon pps = pps_lookup_dev(bp->ptp); 2521773bda96SJonathan Lemon if (pps) 2522773bda96SJonathan Lemon ptp_ocp_symlink(bp, pps->dev, "pps"); 2523773bda96SJonathan Lemon 2524773bda96SJonathan Lemon if (device_add_groups(&bp->dev, timecard_groups)) 2525773bda96SJonathan Lemon pr_err("device add groups failed\n"); 2526773bda96SJonathan Lemon 2527f67bf662SJonathan Lemon ptp_ocp_debugfs_add_device(bp); 2528f67bf662SJonathan Lemon 2529773bda96SJonathan Lemon return 0; 2530773bda96SJonathan Lemon } 2531773bda96SJonathan Lemon 2532773bda96SJonathan Lemon static void 2533065efcc5SJonathan Lemon ptp_ocp_phc_info(struct ptp_ocp *bp) 2534065efcc5SJonathan Lemon { 2535065efcc5SJonathan Lemon struct timespec64 ts; 2536065efcc5SJonathan Lemon u32 version, select; 2537065efcc5SJonathan Lemon bool sync; 2538065efcc5SJonathan Lemon 2539065efcc5SJonathan Lemon version = ioread32(&bp->reg->version); 2540065efcc5SJonathan Lemon select = ioread32(&bp->reg->select); 2541065efcc5SJonathan Lemon dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n", 2542065efcc5SJonathan Lemon version >> 24, (version >> 16) & 0xff, version & 0xffff, 2543065efcc5SJonathan Lemon ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16), 2544065efcc5SJonathan Lemon ptp_clock_index(bp->ptp)); 2545065efcc5SJonathan Lemon 2546065efcc5SJonathan Lemon sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC; 2547065efcc5SJonathan Lemon if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL)) 2548065efcc5SJonathan Lemon dev_info(&bp->pdev->dev, "Time: %lld.%ld, %s\n", 2549065efcc5SJonathan Lemon ts.tv_sec, ts.tv_nsec, 2550065efcc5SJonathan Lemon sync ? "in-sync" : "UNSYNCED"); 2551065efcc5SJonathan Lemon } 2552065efcc5SJonathan Lemon 2553065efcc5SJonathan Lemon static void 2554065efcc5SJonathan Lemon ptp_ocp_serial_info(struct device *dev, const char *name, int port, int baud) 2555065efcc5SJonathan Lemon { 2556065efcc5SJonathan Lemon if (port != -1) 2557065efcc5SJonathan Lemon dev_info(dev, "%5s: /dev/ttyS%-2d @ %6d\n", name, port, baud); 2558065efcc5SJonathan Lemon } 2559065efcc5SJonathan Lemon 2560065efcc5SJonathan Lemon static void 2561065efcc5SJonathan Lemon ptp_ocp_info(struct ptp_ocp *bp) 2562773bda96SJonathan Lemon { 2563e3516bb4SJonathan Lemon static int nmea_baud[] = { 2564e3516bb4SJonathan Lemon 1200, 2400, 4800, 9600, 19200, 38400, 2565e3516bb4SJonathan Lemon 57600, 115200, 230400, 460800, 921600, 2566e3516bb4SJonathan Lemon 1000000, 2000000 2567e3516bb4SJonathan Lemon }; 2568773bda96SJonathan Lemon struct device *dev = &bp->pdev->dev; 2569e3516bb4SJonathan Lemon u32 reg; 2570773bda96SJonathan Lemon 2571065efcc5SJonathan Lemon ptp_ocp_phc_info(bp); 2572065efcc5SJonathan Lemon 2573b0ca789aSJonathan Lemon dev_info(dev, "version %x\n", bp->fw_version); 2574b0ca789aSJonathan Lemon if (bp->fw_version & 0xffff) 2575773bda96SJonathan Lemon dev_info(dev, "regular image, version %d\n", 2576b0ca789aSJonathan Lemon bp->fw_version & 0xffff); 2577773bda96SJonathan Lemon else 2578773bda96SJonathan Lemon dev_info(dev, "golden image, version %d\n", 2579b0ca789aSJonathan Lemon bp->fw_version >> 16); 2580b0ca789aSJonathan Lemon 2581065efcc5SJonathan Lemon ptp_ocp_serial_info(dev, "GNSS", bp->gnss_port, 115200); 258271d7e085SJonathan Lemon ptp_ocp_serial_info(dev, "GNSS2", bp->gnss2_port, 115200); 2583065efcc5SJonathan Lemon ptp_ocp_serial_info(dev, "MAC", bp->mac_port, 57600); 2584e3516bb4SJonathan Lemon if (bp->nmea_out && bp->nmea_port != -1) { 2585e3516bb4SJonathan Lemon int baud = -1; 2586e3516bb4SJonathan Lemon 2587e3516bb4SJonathan Lemon reg = ioread32(&bp->nmea_out->uart_baud); 2588e3516bb4SJonathan Lemon if (reg < ARRAY_SIZE(nmea_baud)) 2589e3516bb4SJonathan Lemon baud = nmea_baud[reg]; 2590e3516bb4SJonathan Lemon ptp_ocp_serial_info(dev, "NMEA", bp->nmea_port, baud); 2591e3516bb4SJonathan Lemon } 2592773bda96SJonathan Lemon } 2593773bda96SJonathan Lemon 2594773bda96SJonathan Lemon static void 2595773bda96SJonathan Lemon ptp_ocp_detach_sysfs(struct ptp_ocp *bp) 2596773bda96SJonathan Lemon { 2597773bda96SJonathan Lemon struct device *dev = &bp->dev; 2598773bda96SJonathan Lemon 2599ef0cfb34SJonathan Lemon sysfs_remove_link(&dev->kobj, "ttyGNSS"); 2600773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "ttyMAC"); 2601773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "ptp"); 2602773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "pps"); 2603773bda96SJonathan Lemon device_remove_groups(dev, timecard_groups); 2604773bda96SJonathan Lemon } 2605773bda96SJonathan Lemon 2606773bda96SJonathan Lemon static void 2607773bda96SJonathan Lemon ptp_ocp_detach(struct ptp_ocp *bp) 2608773bda96SJonathan Lemon { 2609f67bf662SJonathan Lemon ptp_ocp_debugfs_remove_device(bp); 2610773bda96SJonathan Lemon ptp_ocp_detach_sysfs(bp); 2611773bda96SJonathan Lemon if (timer_pending(&bp->watchdog)) 2612773bda96SJonathan Lemon del_timer_sync(&bp->watchdog); 2613773bda96SJonathan Lemon if (bp->ts0) 2614773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->ts0); 2615773bda96SJonathan Lemon if (bp->ts1) 2616773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->ts1); 2617dcf61469SJonathan Lemon if (bp->ts2) 2618dcf61469SJonathan Lemon ptp_ocp_unregister_ext(bp->ts2); 2619773bda96SJonathan Lemon if (bp->pps) 2620773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->pps); 2621ef0cfb34SJonathan Lemon if (bp->gnss_port != -1) 2622ef0cfb34SJonathan Lemon serial8250_unregister_port(bp->gnss_port); 262371d7e085SJonathan Lemon if (bp->gnss2_port != -1) 262471d7e085SJonathan Lemon serial8250_unregister_port(bp->gnss2_port); 2625773bda96SJonathan Lemon if (bp->mac_port != -1) 2626773bda96SJonathan Lemon serial8250_unregister_port(bp->mac_port); 2627e3516bb4SJonathan Lemon if (bp->nmea_port != -1) 2628e3516bb4SJonathan Lemon serial8250_unregister_port(bp->nmea_port); 2629773bda96SJonathan Lemon if (bp->spi_flash) 2630773bda96SJonathan Lemon platform_device_unregister(bp->spi_flash); 2631773bda96SJonathan Lemon if (bp->i2c_ctrl) 2632773bda96SJonathan Lemon platform_device_unregister(bp->i2c_ctrl); 2633773bda96SJonathan Lemon if (bp->i2c_clk) 2634773bda96SJonathan Lemon clk_hw_unregister_fixed_rate(bp->i2c_clk); 2635773bda96SJonathan Lemon if (bp->n_irqs) 2636773bda96SJonathan Lemon pci_free_irq_vectors(bp->pdev); 2637773bda96SJonathan Lemon if (bp->ptp) 2638773bda96SJonathan Lemon ptp_clock_unregister(bp->ptp); 2639773bda96SJonathan Lemon device_unregister(&bp->dev); 2640773bda96SJonathan Lemon } 2641773bda96SJonathan Lemon 2642a7e1abadSJonathan Lemon static int 2643a7e1abadSJonathan Lemon ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id) 2644a7e1abadSJonathan Lemon { 2645773bda96SJonathan Lemon struct devlink *devlink; 2646a7e1abadSJonathan Lemon struct ptp_ocp *bp; 2647a7e1abadSJonathan Lemon int err; 2648a7e1abadSJonathan Lemon 2649919d13a7SLeon Romanovsky devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp), &pdev->dev); 2650773bda96SJonathan Lemon if (!devlink) { 2651773bda96SJonathan Lemon dev_err(&pdev->dev, "devlink_alloc failed\n"); 2652a7e1abadSJonathan Lemon return -ENOMEM; 2653773bda96SJonathan Lemon } 2654773bda96SJonathan Lemon 2655a7e1abadSJonathan Lemon err = pci_enable_device(pdev); 2656a7e1abadSJonathan Lemon if (err) { 2657a7e1abadSJonathan Lemon dev_err(&pdev->dev, "pci_enable_device\n"); 26584587369bSJonathan Lemon goto out_free; 2659a7e1abadSJonathan Lemon } 2660a7e1abadSJonathan Lemon 2661773bda96SJonathan Lemon bp = devlink_priv(devlink); 2662773bda96SJonathan Lemon err = ptp_ocp_device_init(bp, pdev); 2663773bda96SJonathan Lemon if (err) 2664d9fdbf13SJonathan Lemon goto out_disable; 2665a7e1abadSJonathan Lemon 2666773bda96SJonathan Lemon /* compat mode. 2667773bda96SJonathan Lemon * Older FPGA firmware only returns 2 irq's. 2668773bda96SJonathan Lemon * allow this - if not all of the IRQ's are returned, skip the 2669773bda96SJonathan Lemon * extra devices and just register the clock. 2670773bda96SJonathan Lemon */ 2671e3516bb4SJonathan Lemon err = pci_alloc_irq_vectors(pdev, 1, 11, PCI_IRQ_MSI | PCI_IRQ_MSIX); 2672773bda96SJonathan Lemon if (err < 0) { 2673773bda96SJonathan Lemon dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err); 2674773bda96SJonathan Lemon goto out; 2675a7e1abadSJonathan Lemon } 2676773bda96SJonathan Lemon bp->n_irqs = err; 2677773bda96SJonathan Lemon pci_set_master(pdev); 2678a7e1abadSJonathan Lemon 2679773bda96SJonathan Lemon err = ptp_ocp_register_resources(bp, id->driver_data); 2680a7e1abadSJonathan Lemon if (err) 2681a7e1abadSJonathan Lemon goto out; 2682a7e1abadSJonathan Lemon 2683a7e1abadSJonathan Lemon bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev); 2684a7e1abadSJonathan Lemon if (IS_ERR(bp->ptp)) { 2685a7e1abadSJonathan Lemon err = PTR_ERR(bp->ptp); 2686773bda96SJonathan Lemon dev_err(&pdev->dev, "ptp_clock_register: %d\n", err); 2687773bda96SJonathan Lemon bp->ptp = NULL; 2688a7e1abadSJonathan Lemon goto out; 2689a7e1abadSJonathan Lemon } 2690a7e1abadSJonathan Lemon 2691773bda96SJonathan Lemon err = ptp_ocp_complete(bp); 2692773bda96SJonathan Lemon if (err) 2693773bda96SJonathan Lemon goto out; 2694773bda96SJonathan Lemon 2695a7e1abadSJonathan Lemon ptp_ocp_info(bp); 2696c89f78e9SLeon Romanovsky devlink_register(devlink); 2697a7e1abadSJonathan Lemon return 0; 2698a7e1abadSJonathan Lemon 2699a7e1abadSJonathan Lemon out: 2700773bda96SJonathan Lemon ptp_ocp_detach(bp); 2701773bda96SJonathan Lemon pci_set_drvdata(pdev, NULL); 2702d9fdbf13SJonathan Lemon out_disable: 2703d9fdbf13SJonathan Lemon pci_disable_device(pdev); 27044587369bSJonathan Lemon out_free: 2705773bda96SJonathan Lemon devlink_free(devlink); 2706a7e1abadSJonathan Lemon return err; 2707a7e1abadSJonathan Lemon } 2708a7e1abadSJonathan Lemon 2709a7e1abadSJonathan Lemon static void 2710a7e1abadSJonathan Lemon ptp_ocp_remove(struct pci_dev *pdev) 2711a7e1abadSJonathan Lemon { 2712a7e1abadSJonathan Lemon struct ptp_ocp *bp = pci_get_drvdata(pdev); 2713773bda96SJonathan Lemon struct devlink *devlink = priv_to_devlink(bp); 2714a7e1abadSJonathan Lemon 2715c89f78e9SLeon Romanovsky devlink_unregister(devlink); 2716773bda96SJonathan Lemon ptp_ocp_detach(bp); 2717a7e1abadSJonathan Lemon pci_set_drvdata(pdev, NULL); 2718d9fdbf13SJonathan Lemon pci_disable_device(pdev); 2719773bda96SJonathan Lemon 2720773bda96SJonathan Lemon devlink_free(devlink); 2721a7e1abadSJonathan Lemon } 2722a7e1abadSJonathan Lemon 2723a7e1abadSJonathan Lemon static struct pci_driver ptp_ocp_driver = { 2724a7e1abadSJonathan Lemon .name = KBUILD_MODNAME, 2725a7e1abadSJonathan Lemon .id_table = ptp_ocp_pcidev_id, 2726a7e1abadSJonathan Lemon .probe = ptp_ocp_probe, 2727a7e1abadSJonathan Lemon .remove = ptp_ocp_remove, 2728a7e1abadSJonathan Lemon }; 2729a7e1abadSJonathan Lemon 2730773bda96SJonathan Lemon static int 2731773bda96SJonathan Lemon ptp_ocp_i2c_notifier_call(struct notifier_block *nb, 2732773bda96SJonathan Lemon unsigned long action, void *data) 2733773bda96SJonathan Lemon { 2734773bda96SJonathan Lemon struct device *dev, *child = data; 2735773bda96SJonathan Lemon struct ptp_ocp *bp; 2736773bda96SJonathan Lemon bool add; 2737773bda96SJonathan Lemon 2738773bda96SJonathan Lemon switch (action) { 2739773bda96SJonathan Lemon case BUS_NOTIFY_ADD_DEVICE: 2740773bda96SJonathan Lemon case BUS_NOTIFY_DEL_DEVICE: 2741773bda96SJonathan Lemon add = action == BUS_NOTIFY_ADD_DEVICE; 2742773bda96SJonathan Lemon break; 2743773bda96SJonathan Lemon default: 2744773bda96SJonathan Lemon return 0; 2745773bda96SJonathan Lemon } 2746773bda96SJonathan Lemon 2747773bda96SJonathan Lemon if (!i2c_verify_adapter(child)) 2748773bda96SJonathan Lemon return 0; 2749773bda96SJonathan Lemon 2750773bda96SJonathan Lemon dev = child; 2751773bda96SJonathan Lemon while ((dev = dev->parent)) 2752773bda96SJonathan Lemon if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME)) 2753773bda96SJonathan Lemon goto found; 2754773bda96SJonathan Lemon return 0; 2755773bda96SJonathan Lemon 2756773bda96SJonathan Lemon found: 2757773bda96SJonathan Lemon bp = dev_get_drvdata(dev); 2758773bda96SJonathan Lemon if (add) 2759773bda96SJonathan Lemon ptp_ocp_symlink(bp, child, "i2c"); 2760773bda96SJonathan Lemon else 2761773bda96SJonathan Lemon sysfs_remove_link(&bp->dev.kobj, "i2c"); 2762773bda96SJonathan Lemon 2763773bda96SJonathan Lemon return 0; 2764773bda96SJonathan Lemon } 2765773bda96SJonathan Lemon 2766773bda96SJonathan Lemon static struct notifier_block ptp_ocp_i2c_notifier = { 2767773bda96SJonathan Lemon .notifier_call = ptp_ocp_i2c_notifier_call, 2768773bda96SJonathan Lemon }; 2769773bda96SJonathan Lemon 2770a7e1abadSJonathan Lemon static int __init 2771a7e1abadSJonathan Lemon ptp_ocp_init(void) 2772a7e1abadSJonathan Lemon { 2773773bda96SJonathan Lemon const char *what; 2774a7e1abadSJonathan Lemon int err; 2775a7e1abadSJonathan Lemon 2776f67bf662SJonathan Lemon ptp_ocp_debugfs_init(); 2777f67bf662SJonathan Lemon 2778773bda96SJonathan Lemon what = "timecard class"; 2779773bda96SJonathan Lemon err = class_register(&timecard_class); 2780773bda96SJonathan Lemon if (err) 2781773bda96SJonathan Lemon goto out; 2782773bda96SJonathan Lemon 2783773bda96SJonathan Lemon what = "i2c notifier"; 2784773bda96SJonathan Lemon err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2785773bda96SJonathan Lemon if (err) 2786773bda96SJonathan Lemon goto out_notifier; 2787773bda96SJonathan Lemon 2788773bda96SJonathan Lemon what = "ptp_ocp driver"; 2789a7e1abadSJonathan Lemon err = pci_register_driver(&ptp_ocp_driver); 2790773bda96SJonathan Lemon if (err) 2791773bda96SJonathan Lemon goto out_register; 2792773bda96SJonathan Lemon 2793773bda96SJonathan Lemon return 0; 2794773bda96SJonathan Lemon 2795773bda96SJonathan Lemon out_register: 2796773bda96SJonathan Lemon bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2797773bda96SJonathan Lemon out_notifier: 2798773bda96SJonathan Lemon class_unregister(&timecard_class); 2799773bda96SJonathan Lemon out: 2800f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(); 2801773bda96SJonathan Lemon pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err); 2802a7e1abadSJonathan Lemon return err; 2803a7e1abadSJonathan Lemon } 2804a7e1abadSJonathan Lemon 2805a7e1abadSJonathan Lemon static void __exit 2806a7e1abadSJonathan Lemon ptp_ocp_fini(void) 2807a7e1abadSJonathan Lemon { 2808773bda96SJonathan Lemon bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2809a7e1abadSJonathan Lemon pci_unregister_driver(&ptp_ocp_driver); 2810773bda96SJonathan Lemon class_unregister(&timecard_class); 2811f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(); 2812a7e1abadSJonathan Lemon } 2813a7e1abadSJonathan Lemon 2814a7e1abadSJonathan Lemon module_init(ptp_ocp_init); 2815a7e1abadSJonathan Lemon module_exit(ptp_ocp_fini); 2816a7e1abadSJonathan Lemon 2817a7e1abadSJonathan Lemon MODULE_DESCRIPTION("OpenCompute TimeCard driver"); 2818a7e1abadSJonathan Lemon MODULE_LICENSE("GPL v2"); 2819