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 209*a509a7c6SJonathan Lemon enum ptp_ocp_sma_mode { 210*a509a7c6SJonathan Lemon SMA_MODE_IN, 211*a509a7c6SJonathan Lemon SMA_MODE_OUT, 212*a509a7c6SJonathan Lemon }; 213*a509a7c6SJonathan Lemon 214*a509a7c6SJonathan Lemon struct ptp_ocp_sma_connector { 215*a509a7c6SJonathan Lemon enum ptp_ocp_sma_mode mode; 216*a509a7c6SJonathan Lemon bool fixed_fcn; 217*a509a7c6SJonathan Lemon bool fixed_dir; 218*a509a7c6SJonathan Lemon }; 219*a509a7c6SJonathan Lemon 2200cfcdd1eSJonathan Lemon #define OCP_BOARD_ID_LEN 13 2210cfcdd1eSJonathan Lemon #define OCP_SERIAL_LEN 6 2220cfcdd1eSJonathan Lemon 223a7e1abadSJonathan Lemon struct ptp_ocp { 224a7e1abadSJonathan Lemon struct pci_dev *pdev; 225773bda96SJonathan Lemon struct device dev; 226a7e1abadSJonathan Lemon spinlock_t lock; 227a7e1abadSJonathan Lemon struct ocp_reg __iomem *reg; 228a7e1abadSJonathan Lemon struct tod_reg __iomem *tod; 2290d43d4f2SJonathan Lemon struct pps_reg __iomem *pps_to_ext; 2300d43d4f2SJonathan Lemon struct pps_reg __iomem *pps_to_clk; 231f67bf662SJonathan Lemon struct gpio_reg __iomem *pps_select; 232*a509a7c6SJonathan Lemon struct gpio_reg __iomem *sma_map1; 233*a509a7c6SJonathan Lemon struct gpio_reg __iomem *sma_map2; 2346baf2925SJonathan Lemon struct irig_master_reg __iomem *irig_out; 2356baf2925SJonathan Lemon struct irig_slave_reg __iomem *irig_in; 2366baf2925SJonathan Lemon struct dcf_master_reg __iomem *dcf_out; 2376baf2925SJonathan Lemon struct dcf_slave_reg __iomem *dcf_in; 238e3516bb4SJonathan Lemon struct tod_reg __iomem *nmea_out; 239773bda96SJonathan Lemon struct ptp_ocp_ext_src *pps; 240773bda96SJonathan Lemon struct ptp_ocp_ext_src *ts0; 241773bda96SJonathan Lemon struct ptp_ocp_ext_src *ts1; 242dcf61469SJonathan Lemon struct ptp_ocp_ext_src *ts2; 243773bda96SJonathan Lemon struct img_reg __iomem *image; 244a7e1abadSJonathan Lemon struct ptp_clock *ptp; 245a7e1abadSJonathan Lemon struct ptp_clock_info ptp_info; 246773bda96SJonathan Lemon struct platform_device *i2c_ctrl; 247773bda96SJonathan Lemon struct platform_device *spi_flash; 248773bda96SJonathan Lemon struct clk_hw *i2c_clk; 249773bda96SJonathan Lemon struct timer_list watchdog; 2500cfcdd1eSJonathan Lemon const struct ptp_ocp_eeprom_map *eeprom_map; 251f67bf662SJonathan Lemon struct dentry *debug_root; 252ef0cfb34SJonathan Lemon time64_t gnss_lost; 253773bda96SJonathan Lemon int id; 254773bda96SJonathan Lemon int n_irqs; 255ef0cfb34SJonathan Lemon int gnss_port; 25671d7e085SJonathan Lemon int gnss2_port; 257773bda96SJonathan Lemon int mac_port; /* miniature atomic clock */ 258e3516bb4SJonathan Lemon int nmea_port; 259b0ca789aSJonathan Lemon u32 fw_version; 2600cfcdd1eSJonathan Lemon u8 board_id[OCP_BOARD_ID_LEN]; 2610cfcdd1eSJonathan Lemon u8 serial[OCP_SERIAL_LEN]; 2620cfcdd1eSJonathan Lemon bool has_eeprom_data; 263a62a56d0SJonathan Lemon u32 pps_req_map; 26489260d87SJonathan Lemon int flash_start; 26589260d87SJonathan Lemon u32 utc_tai_offset; 2661acffc6eSJonathan Lemon u32 ts_window_adjust; 267*a509a7c6SJonathan Lemon struct ptp_ocp_sma_connector sma[4]; 268a7e1abadSJonathan Lemon }; 269a7e1abadSJonathan Lemon 270a62a56d0SJonathan Lemon #define OCP_REQ_TIMESTAMP BIT(0) 271a62a56d0SJonathan Lemon #define OCP_REQ_PPS BIT(1) 272a62a56d0SJonathan Lemon 273773bda96SJonathan Lemon struct ocp_resource { 274773bda96SJonathan Lemon unsigned long offset; 275773bda96SJonathan Lemon int size; 276773bda96SJonathan Lemon int irq_vec; 277773bda96SJonathan Lemon int (*setup)(struct ptp_ocp *bp, struct ocp_resource *r); 278773bda96SJonathan Lemon void *extra; 279773bda96SJonathan Lemon unsigned long bp_offset; 28056ec4403SJonathan Lemon const char * const name; 281773bda96SJonathan Lemon }; 282773bda96SJonathan Lemon 283773bda96SJonathan Lemon static int ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r); 284773bda96SJonathan Lemon static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r); 285773bda96SJonathan Lemon static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r); 286773bda96SJonathan Lemon static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r); 287773bda96SJonathan Lemon static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r); 288773bda96SJonathan Lemon static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r); 289773bda96SJonathan Lemon static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv); 290a62a56d0SJonathan Lemon static int ptp_ocp_ts_enable(void *priv, u32 req, bool enable); 291773bda96SJonathan Lemon 2920cfcdd1eSJonathan Lemon struct ptp_ocp_eeprom_map { 2930cfcdd1eSJonathan Lemon u16 off; 2940cfcdd1eSJonathan Lemon u16 len; 2950cfcdd1eSJonathan Lemon u32 bp_offset; 2960cfcdd1eSJonathan Lemon const void * const tag; 2970cfcdd1eSJonathan Lemon }; 2980cfcdd1eSJonathan Lemon 2990cfcdd1eSJonathan Lemon #define EEPROM_ENTRY(addr, member) \ 3000cfcdd1eSJonathan Lemon .off = addr, \ 3010cfcdd1eSJonathan Lemon .len = sizeof_field(struct ptp_ocp, member), \ 3020cfcdd1eSJonathan Lemon .bp_offset = offsetof(struct ptp_ocp, member) 3030cfcdd1eSJonathan Lemon 3040cfcdd1eSJonathan Lemon #define BP_MAP_ENTRY_ADDR(bp, map) ({ \ 3050cfcdd1eSJonathan Lemon (void *)((uintptr_t)(bp) + (map)->bp_offset); \ 3060cfcdd1eSJonathan Lemon }) 3070cfcdd1eSJonathan Lemon 3080cfcdd1eSJonathan Lemon static struct ptp_ocp_eeprom_map fb_eeprom_map[] = { 3090cfcdd1eSJonathan Lemon { EEPROM_ENTRY(0x43, board_id) }, 3100cfcdd1eSJonathan Lemon { EEPROM_ENTRY(0x00, serial), .tag = "mac" }, 3110cfcdd1eSJonathan Lemon { } 3120cfcdd1eSJonathan Lemon }; 3130cfcdd1eSJonathan Lemon 314773bda96SJonathan Lemon #define bp_assign_entry(bp, res, val) ({ \ 315773bda96SJonathan Lemon uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset; \ 316773bda96SJonathan Lemon *(typeof(val) *)addr = val; \ 317773bda96SJonathan Lemon }) 318773bda96SJonathan Lemon 319773bda96SJonathan Lemon #define OCP_RES_LOCATION(member) \ 32056ec4403SJonathan Lemon .name = #member, .bp_offset = offsetof(struct ptp_ocp, member) 321773bda96SJonathan Lemon 322773bda96SJonathan Lemon #define OCP_MEM_RESOURCE(member) \ 323773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_mem 324773bda96SJonathan Lemon 325773bda96SJonathan Lemon #define OCP_SERIAL_RESOURCE(member) \ 326773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_serial 327773bda96SJonathan Lemon 328773bda96SJonathan Lemon #define OCP_I2C_RESOURCE(member) \ 329773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_i2c 330773bda96SJonathan Lemon 331773bda96SJonathan Lemon #define OCP_SPI_RESOURCE(member) \ 332773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_spi 333773bda96SJonathan Lemon 334773bda96SJonathan Lemon #define OCP_EXT_RESOURCE(member) \ 335773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_ext 336773bda96SJonathan Lemon 337773bda96SJonathan Lemon /* This is the MSI vector mapping used. 338a62a56d0SJonathan Lemon * 0: TS3 (and PPS) 339773bda96SJonathan Lemon * 1: TS0 340773bda96SJonathan Lemon * 2: TS1 34171d7e085SJonathan Lemon * 3: GNSS 34271d7e085SJonathan Lemon * 4: GNSS2 343773bda96SJonathan Lemon * 5: MAC 344dcf61469SJonathan Lemon * 6: TS2 3451447149dSJonathan Lemon * 7: I2C controller 346e3516bb4SJonathan Lemon * 8: HWICAP (notused) 347773bda96SJonathan Lemon * 9: SPI Flash 348e3516bb4SJonathan Lemon * 10: NMEA 349773bda96SJonathan Lemon */ 350773bda96SJonathan Lemon 351773bda96SJonathan Lemon static struct ocp_resource ocp_fb_resource[] = { 352773bda96SJonathan Lemon { 353773bda96SJonathan Lemon OCP_MEM_RESOURCE(reg), 354773bda96SJonathan Lemon .offset = 0x01000000, .size = 0x10000, 355773bda96SJonathan Lemon }, 356773bda96SJonathan Lemon { 357773bda96SJonathan Lemon OCP_EXT_RESOURCE(ts0), 358773bda96SJonathan Lemon .offset = 0x01010000, .size = 0x10000, .irq_vec = 1, 359773bda96SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 36056ec4403SJonathan Lemon .index = 0, 361773bda96SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 362773bda96SJonathan Lemon .enable = ptp_ocp_ts_enable, 363773bda96SJonathan Lemon }, 364773bda96SJonathan Lemon }, 365773bda96SJonathan Lemon { 366773bda96SJonathan Lemon OCP_EXT_RESOURCE(ts1), 367773bda96SJonathan Lemon .offset = 0x01020000, .size = 0x10000, .irq_vec = 2, 368773bda96SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 36956ec4403SJonathan Lemon .index = 1, 370773bda96SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 371773bda96SJonathan Lemon .enable = ptp_ocp_ts_enable, 372773bda96SJonathan Lemon }, 373773bda96SJonathan Lemon }, 374773bda96SJonathan Lemon { 375dcf61469SJonathan Lemon OCP_EXT_RESOURCE(ts2), 376dcf61469SJonathan Lemon .offset = 0x01060000, .size = 0x10000, .irq_vec = 6, 377dcf61469SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 378dcf61469SJonathan Lemon .index = 2, 379dcf61469SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 380dcf61469SJonathan Lemon .enable = ptp_ocp_ts_enable, 381dcf61469SJonathan Lemon }, 382dcf61469SJonathan Lemon }, 383dcf61469SJonathan Lemon { 384a62a56d0SJonathan Lemon OCP_EXT_RESOURCE(pps), 385a62a56d0SJonathan Lemon .offset = 0x010C0000, .size = 0x10000, .irq_vec = 0, 386a62a56d0SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 387a62a56d0SJonathan Lemon .index = 3, 388a62a56d0SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 389a62a56d0SJonathan Lemon .enable = ptp_ocp_ts_enable, 390a62a56d0SJonathan Lemon }, 391a62a56d0SJonathan Lemon }, 392a62a56d0SJonathan Lemon { 3930d43d4f2SJonathan Lemon OCP_MEM_RESOURCE(pps_to_ext), 3940d43d4f2SJonathan Lemon .offset = 0x01030000, .size = 0x10000, 3950d43d4f2SJonathan Lemon }, 3960d43d4f2SJonathan Lemon { 3970d43d4f2SJonathan Lemon OCP_MEM_RESOURCE(pps_to_clk), 398773bda96SJonathan Lemon .offset = 0x01040000, .size = 0x10000, 399773bda96SJonathan Lemon }, 400773bda96SJonathan Lemon { 401773bda96SJonathan Lemon OCP_MEM_RESOURCE(tod), 402773bda96SJonathan Lemon .offset = 0x01050000, .size = 0x10000, 403773bda96SJonathan Lemon }, 404773bda96SJonathan Lemon { 4056baf2925SJonathan Lemon OCP_MEM_RESOURCE(irig_in), 4066baf2925SJonathan Lemon .offset = 0x01070000, .size = 0x10000, 4076baf2925SJonathan Lemon }, 4086baf2925SJonathan Lemon { 4096baf2925SJonathan Lemon OCP_MEM_RESOURCE(irig_out), 4106baf2925SJonathan Lemon .offset = 0x01080000, .size = 0x10000, 4116baf2925SJonathan Lemon }, 4126baf2925SJonathan Lemon { 4136baf2925SJonathan Lemon OCP_MEM_RESOURCE(dcf_in), 4146baf2925SJonathan Lemon .offset = 0x01090000, .size = 0x10000, 4156baf2925SJonathan Lemon }, 4166baf2925SJonathan Lemon { 4176baf2925SJonathan Lemon OCP_MEM_RESOURCE(dcf_out), 4186baf2925SJonathan Lemon .offset = 0x010A0000, .size = 0x10000, 4196baf2925SJonathan Lemon }, 4206baf2925SJonathan Lemon { 421e3516bb4SJonathan Lemon OCP_MEM_RESOURCE(nmea_out), 422e3516bb4SJonathan Lemon .offset = 0x010B0000, .size = 0x10000, 423e3516bb4SJonathan Lemon }, 424e3516bb4SJonathan Lemon { 425773bda96SJonathan Lemon OCP_MEM_RESOURCE(image), 426773bda96SJonathan Lemon .offset = 0x00020000, .size = 0x1000, 427773bda96SJonathan Lemon }, 428773bda96SJonathan Lemon { 429f67bf662SJonathan Lemon OCP_MEM_RESOURCE(pps_select), 430f67bf662SJonathan Lemon .offset = 0x00130000, .size = 0x1000, 431f67bf662SJonathan Lemon }, 432f67bf662SJonathan Lemon { 433*a509a7c6SJonathan Lemon OCP_MEM_RESOURCE(sma_map1), 434e1daf0ecSJonathan Lemon .offset = 0x00140000, .size = 0x1000, 435e1daf0ecSJonathan Lemon }, 436e1daf0ecSJonathan Lemon { 437*a509a7c6SJonathan Lemon OCP_MEM_RESOURCE(sma_map2), 438*a509a7c6SJonathan Lemon .offset = 0x00220000, .size = 0x1000, 439*a509a7c6SJonathan Lemon }, 440*a509a7c6SJonathan Lemon { 441773bda96SJonathan Lemon OCP_I2C_RESOURCE(i2c_ctrl), 442773bda96SJonathan Lemon .offset = 0x00150000, .size = 0x10000, .irq_vec = 7, 4431618df6aSJonathan Lemon .extra = &(struct ptp_ocp_i2c_info) { 4441618df6aSJonathan Lemon .name = "xiic-i2c", 4451618df6aSJonathan Lemon .fixed_rate = 50000000, 4460cfcdd1eSJonathan Lemon .data_size = sizeof(struct xiic_i2c_platform_data), 4470cfcdd1eSJonathan Lemon .data = &(struct xiic_i2c_platform_data) { 4480cfcdd1eSJonathan Lemon .num_devices = 2, 4490cfcdd1eSJonathan Lemon .devices = (struct i2c_board_info[]) { 4500cfcdd1eSJonathan Lemon { I2C_BOARD_INFO("24c02", 0x50) }, 4510cfcdd1eSJonathan Lemon { I2C_BOARD_INFO("24mac402", 0x58), 4520cfcdd1eSJonathan Lemon .platform_data = "mac" }, 4530cfcdd1eSJonathan Lemon }, 4540cfcdd1eSJonathan Lemon }, 4551618df6aSJonathan Lemon }, 456773bda96SJonathan Lemon }, 457773bda96SJonathan Lemon { 458ef0cfb34SJonathan Lemon OCP_SERIAL_RESOURCE(gnss_port), 459773bda96SJonathan Lemon .offset = 0x00160000 + 0x1000, .irq_vec = 3, 460773bda96SJonathan Lemon }, 461773bda96SJonathan Lemon { 46271d7e085SJonathan Lemon OCP_SERIAL_RESOURCE(gnss2_port), 46371d7e085SJonathan Lemon .offset = 0x00170000 + 0x1000, .irq_vec = 4, 46471d7e085SJonathan Lemon }, 46571d7e085SJonathan Lemon { 466773bda96SJonathan Lemon OCP_SERIAL_RESOURCE(mac_port), 467773bda96SJonathan Lemon .offset = 0x00180000 + 0x1000, .irq_vec = 5, 468773bda96SJonathan Lemon }, 469773bda96SJonathan Lemon { 470e3516bb4SJonathan Lemon OCP_SERIAL_RESOURCE(nmea_port), 471e3516bb4SJonathan Lemon .offset = 0x00190000 + 0x1000, .irq_vec = 10, 472e3516bb4SJonathan Lemon }, 473e3516bb4SJonathan Lemon { 474773bda96SJonathan Lemon OCP_SPI_RESOURCE(spi_flash), 475773bda96SJonathan Lemon .offset = 0x00310000, .size = 0x10000, .irq_vec = 9, 476773bda96SJonathan Lemon .extra = &(struct ptp_ocp_flash_info) { 477773bda96SJonathan Lemon .name = "xilinx_spi", .pci_offset = 0, 478773bda96SJonathan Lemon .data_size = sizeof(struct xspi_platform_data), 479773bda96SJonathan Lemon .data = &(struct xspi_platform_data) { 480773bda96SJonathan Lemon .num_chipselect = 1, 481773bda96SJonathan Lemon .bits_per_word = 8, 482773bda96SJonathan Lemon .num_devices = 1, 483773bda96SJonathan Lemon .devices = &(struct spi_board_info) { 484773bda96SJonathan Lemon .modalias = "spi-nor", 485773bda96SJonathan Lemon }, 486773bda96SJonathan Lemon }, 487773bda96SJonathan Lemon }, 488773bda96SJonathan Lemon }, 489773bda96SJonathan Lemon { 490773bda96SJonathan Lemon .setup = ptp_ocp_fb_board_init, 491773bda96SJonathan Lemon }, 492773bda96SJonathan Lemon { } 493773bda96SJonathan Lemon }; 494773bda96SJonathan Lemon 495773bda96SJonathan Lemon static const struct pci_device_id ptp_ocp_pcidev_id[] = { 496773bda96SJonathan Lemon { PCI_DEVICE_DATA(FACEBOOK, TIMECARD, &ocp_fb_resource) }, 497773bda96SJonathan Lemon { 0 } 498773bda96SJonathan Lemon }; 499773bda96SJonathan Lemon MODULE_DEVICE_TABLE(pci, ptp_ocp_pcidev_id); 500773bda96SJonathan Lemon 501773bda96SJonathan Lemon static DEFINE_MUTEX(ptp_ocp_lock); 502773bda96SJonathan Lemon static DEFINE_IDR(ptp_ocp_idr); 503773bda96SJonathan Lemon 504e1daf0ecSJonathan Lemon struct ocp_selector { 505773bda96SJonathan Lemon const char *name; 506773bda96SJonathan Lemon int value; 507e1daf0ecSJonathan Lemon }; 508e1daf0ecSJonathan Lemon 509e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_clock[] = { 510773bda96SJonathan Lemon { .name = "NONE", .value = 0 }, 511773bda96SJonathan Lemon { .name = "TOD", .value = 1 }, 512773bda96SJonathan Lemon { .name = "IRIG", .value = 2 }, 513773bda96SJonathan Lemon { .name = "PPS", .value = 3 }, 514773bda96SJonathan Lemon { .name = "PTP", .value = 4 }, 515773bda96SJonathan Lemon { .name = "RTC", .value = 5 }, 516773bda96SJonathan Lemon { .name = "DCF", .value = 6 }, 517773bda96SJonathan Lemon { .name = "REGS", .value = 0xfe }, 518773bda96SJonathan Lemon { .name = "EXT", .value = 0xff }, 519e1daf0ecSJonathan Lemon { } 520e1daf0ecSJonathan Lemon }; 521e1daf0ecSJonathan Lemon 522*a509a7c6SJonathan Lemon #define SMA_ENABLE BIT(15) 523*a509a7c6SJonathan Lemon #define SMA_SELECT_MASK ((1U << 15) - 1) 524*a509a7c6SJonathan Lemon 525e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_sma_in[] = { 526e1daf0ecSJonathan Lemon { .name = "10Mhz", .value = 0x00 }, 527e1daf0ecSJonathan Lemon { .name = "PPS1", .value = 0x01 }, 528e1daf0ecSJonathan Lemon { .name = "PPS2", .value = 0x02 }, 529e1daf0ecSJonathan Lemon { .name = "TS1", .value = 0x04 }, 530e1daf0ecSJonathan Lemon { .name = "TS2", .value = 0x08 }, 5316baf2925SJonathan Lemon { .name = "IRIG", .value = 0x10 }, 5326baf2925SJonathan Lemon { .name = "DCF", .value = 0x20 }, 533e1daf0ecSJonathan Lemon { } 534e1daf0ecSJonathan Lemon }; 535e1daf0ecSJonathan Lemon 536e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_sma_out[] = { 537e1daf0ecSJonathan Lemon { .name = "10Mhz", .value = 0x00 }, 538e1daf0ecSJonathan Lemon { .name = "PHC", .value = 0x01 }, 539e1daf0ecSJonathan Lemon { .name = "MAC", .value = 0x02 }, 540e1daf0ecSJonathan Lemon { .name = "GNSS", .value = 0x04 }, 541e1daf0ecSJonathan Lemon { .name = "GNSS2", .value = 0x08 }, 5426baf2925SJonathan Lemon { .name = "IRIG", .value = 0x10 }, 5436baf2925SJonathan Lemon { .name = "DCF", .value = 0x20 }, 544e1daf0ecSJonathan Lemon { } 545773bda96SJonathan Lemon }; 546773bda96SJonathan Lemon 547773bda96SJonathan Lemon static const char * 548e1daf0ecSJonathan Lemon ptp_ocp_select_name_from_val(struct ocp_selector *tbl, int val) 549773bda96SJonathan Lemon { 550773bda96SJonathan Lemon int i; 551773bda96SJonathan Lemon 552e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) 553e1daf0ecSJonathan Lemon if (tbl[i].value == val) 554e1daf0ecSJonathan Lemon return tbl[i].name; 555773bda96SJonathan Lemon return NULL; 556773bda96SJonathan Lemon } 557773bda96SJonathan Lemon 558773bda96SJonathan Lemon static int 559e1daf0ecSJonathan Lemon ptp_ocp_select_val_from_name(struct ocp_selector *tbl, const char *name) 560773bda96SJonathan Lemon { 561e1daf0ecSJonathan Lemon const char *select; 562773bda96SJonathan Lemon int i; 563773bda96SJonathan Lemon 564e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) { 565e1daf0ecSJonathan Lemon select = tbl[i].name; 566e1daf0ecSJonathan Lemon if (!strncasecmp(name, select, strlen(select))) 567e1daf0ecSJonathan Lemon return tbl[i].value; 568773bda96SJonathan Lemon } 569773bda96SJonathan Lemon return -EINVAL; 570773bda96SJonathan Lemon } 571773bda96SJonathan Lemon 572e1daf0ecSJonathan Lemon static ssize_t 573e1daf0ecSJonathan Lemon ptp_ocp_select_table_show(struct ocp_selector *tbl, char *buf) 574e1daf0ecSJonathan Lemon { 575e1daf0ecSJonathan Lemon ssize_t count; 576e1daf0ecSJonathan Lemon int i; 577e1daf0ecSJonathan Lemon 578e1daf0ecSJonathan Lemon count = 0; 579e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) 580e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", tbl[i].name); 581e1daf0ecSJonathan Lemon if (count) 582e1daf0ecSJonathan Lemon count--; 583e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "\n"); 584e1daf0ecSJonathan Lemon return count; 585e1daf0ecSJonathan Lemon } 586e1daf0ecSJonathan Lemon 587a7e1abadSJonathan Lemon static int 588a7e1abadSJonathan Lemon __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts, 589a7e1abadSJonathan Lemon struct ptp_system_timestamp *sts) 590a7e1abadSJonathan Lemon { 591a7e1abadSJonathan Lemon u32 ctrl, time_sec, time_ns; 592a7e1abadSJonathan Lemon int i; 593a7e1abadSJonathan Lemon 594a7e1abadSJonathan Lemon ptp_read_system_prets(sts); 5951acffc6eSJonathan Lemon 5961acffc6eSJonathan Lemon ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE; 597a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 598a7e1abadSJonathan Lemon 599a7e1abadSJonathan Lemon for (i = 0; i < 100; i++) { 600a7e1abadSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 601a7e1abadSJonathan Lemon if (ctrl & OCP_CTRL_READ_TIME_DONE) 602a7e1abadSJonathan Lemon break; 603a7e1abadSJonathan Lemon } 604a7e1abadSJonathan Lemon ptp_read_system_postts(sts); 605a7e1abadSJonathan Lemon 6061acffc6eSJonathan Lemon if (sts && bp->ts_window_adjust) { 6071acffc6eSJonathan Lemon s64 ns = timespec64_to_ns(&sts->post_ts); 6081acffc6eSJonathan Lemon 6091acffc6eSJonathan Lemon sts->post_ts = ns_to_timespec64(ns - bp->ts_window_adjust); 6101acffc6eSJonathan Lemon } 6111acffc6eSJonathan Lemon 612a7e1abadSJonathan Lemon time_ns = ioread32(&bp->reg->time_ns); 613a7e1abadSJonathan Lemon time_sec = ioread32(&bp->reg->time_sec); 614a7e1abadSJonathan Lemon 615a7e1abadSJonathan Lemon ts->tv_sec = time_sec; 616a7e1abadSJonathan Lemon ts->tv_nsec = time_ns; 617a7e1abadSJonathan Lemon 618a7e1abadSJonathan Lemon return ctrl & OCP_CTRL_READ_TIME_DONE ? 0 : -ETIMEDOUT; 619a7e1abadSJonathan Lemon } 620a7e1abadSJonathan Lemon 621a7e1abadSJonathan Lemon static int 622a7e1abadSJonathan Lemon ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts, 623a7e1abadSJonathan Lemon struct ptp_system_timestamp *sts) 624a7e1abadSJonathan Lemon { 625a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 626a7e1abadSJonathan Lemon unsigned long flags; 627a7e1abadSJonathan Lemon int err; 628a7e1abadSJonathan Lemon 629a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 630a7e1abadSJonathan Lemon err = __ptp_ocp_gettime_locked(bp, ts, sts); 631a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 632a7e1abadSJonathan Lemon 633a7e1abadSJonathan Lemon return err; 634a7e1abadSJonathan Lemon } 635a7e1abadSJonathan Lemon 636a7e1abadSJonathan Lemon static void 637a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts) 638a7e1abadSJonathan Lemon { 639a7e1abadSJonathan Lemon u32 ctrl, time_sec, time_ns; 640a7e1abadSJonathan Lemon u32 select; 641a7e1abadSJonathan Lemon 642a7e1abadSJonathan Lemon time_ns = ts->tv_nsec; 643a7e1abadSJonathan Lemon time_sec = ts->tv_sec; 644a7e1abadSJonathan Lemon 645a7e1abadSJonathan Lemon select = ioread32(&bp->reg->select); 646a7e1abadSJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 647a7e1abadSJonathan Lemon 648a7e1abadSJonathan Lemon iowrite32(time_ns, &bp->reg->adjust_ns); 649a7e1abadSJonathan Lemon iowrite32(time_sec, &bp->reg->adjust_sec); 650a7e1abadSJonathan Lemon 6511acffc6eSJonathan Lemon ctrl = OCP_CTRL_ADJUST_TIME | OCP_CTRL_ENABLE; 652a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 653a7e1abadSJonathan Lemon 654a7e1abadSJonathan Lemon /* restore clock selection */ 655a7e1abadSJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 656a7e1abadSJonathan Lemon } 657a7e1abadSJonathan Lemon 658a7e1abadSJonathan Lemon static int 659a7e1abadSJonathan Lemon ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts) 660a7e1abadSJonathan Lemon { 661a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 662a7e1abadSJonathan Lemon unsigned long flags; 663a7e1abadSJonathan Lemon 664a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 665a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(bp, ts); 666a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 667a7e1abadSJonathan Lemon 668a7e1abadSJonathan Lemon return 0; 669a7e1abadSJonathan Lemon } 670a7e1abadSJonathan Lemon 6716d59d4faSJonathan Lemon static void 67290f8f4c0SJonathan Lemon __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val) 6736d59d4faSJonathan Lemon { 6746d59d4faSJonathan Lemon u32 select, ctrl; 6756d59d4faSJonathan Lemon 6766d59d4faSJonathan Lemon select = ioread32(&bp->reg->select); 6776d59d4faSJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 6786d59d4faSJonathan Lemon 6796d59d4faSJonathan Lemon iowrite32(adj_val, &bp->reg->offset_ns); 68090f8f4c0SJonathan Lemon iowrite32(NSEC_PER_SEC, &bp->reg->offset_window_ns); 6816d59d4faSJonathan Lemon 6826d59d4faSJonathan Lemon ctrl = OCP_CTRL_ADJUST_OFFSET | OCP_CTRL_ENABLE; 6836d59d4faSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 6846d59d4faSJonathan Lemon 6856d59d4faSJonathan Lemon /* restore clock selection */ 6866d59d4faSJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 6876d59d4faSJonathan Lemon } 6886d59d4faSJonathan Lemon 68990f8f4c0SJonathan Lemon static void 69090f8f4c0SJonathan Lemon ptp_ocp_adjtime_coarse(struct ptp_ocp *bp, u64 delta_ns) 69190f8f4c0SJonathan Lemon { 69290f8f4c0SJonathan Lemon struct timespec64 ts; 69390f8f4c0SJonathan Lemon unsigned long flags; 69490f8f4c0SJonathan Lemon int err; 69590f8f4c0SJonathan Lemon 69690f8f4c0SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 69790f8f4c0SJonathan Lemon err = __ptp_ocp_gettime_locked(bp, &ts, NULL); 69890f8f4c0SJonathan Lemon if (likely(!err)) { 69990f8f4c0SJonathan Lemon timespec64_add_ns(&ts, delta_ns); 70090f8f4c0SJonathan Lemon __ptp_ocp_settime_locked(bp, &ts); 70190f8f4c0SJonathan Lemon } 70290f8f4c0SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 70390f8f4c0SJonathan Lemon } 70490f8f4c0SJonathan Lemon 705a7e1abadSJonathan Lemon static int 706a7e1abadSJonathan Lemon ptp_ocp_adjtime(struct ptp_clock_info *ptp_info, s64 delta_ns) 707a7e1abadSJonathan Lemon { 708a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 709a7e1abadSJonathan Lemon unsigned long flags; 7106d59d4faSJonathan Lemon u32 adj_ns, sign; 711a7e1abadSJonathan Lemon 71290f8f4c0SJonathan Lemon if (delta_ns > NSEC_PER_SEC || -delta_ns > NSEC_PER_SEC) { 71390f8f4c0SJonathan Lemon ptp_ocp_adjtime_coarse(bp, delta_ns); 71490f8f4c0SJonathan Lemon return 0; 71590f8f4c0SJonathan Lemon } 71690f8f4c0SJonathan Lemon 7176d59d4faSJonathan Lemon sign = delta_ns < 0 ? BIT(31) : 0; 7186d59d4faSJonathan Lemon adj_ns = sign ? -delta_ns : delta_ns; 719a7e1abadSJonathan Lemon 720a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 7216d59d4faSJonathan Lemon __ptp_ocp_adjtime_locked(bp, sign | adj_ns); 722a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 723a7e1abadSJonathan Lemon 7246d59d4faSJonathan Lemon return 0; 725a7e1abadSJonathan Lemon } 726a7e1abadSJonathan Lemon 727a7e1abadSJonathan Lemon static int 728a7e1abadSJonathan Lemon ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm) 729a7e1abadSJonathan Lemon { 730a7e1abadSJonathan Lemon if (scaled_ppm == 0) 731a7e1abadSJonathan Lemon return 0; 732a7e1abadSJonathan Lemon 733a7e1abadSJonathan Lemon return -EOPNOTSUPP; 734a7e1abadSJonathan Lemon } 735a7e1abadSJonathan Lemon 736773bda96SJonathan Lemon static int 7376d59d4faSJonathan Lemon ptp_ocp_null_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns) 738773bda96SJonathan Lemon { 739773bda96SJonathan Lemon return -EOPNOTSUPP; 740773bda96SJonathan Lemon } 741773bda96SJonathan Lemon 742773bda96SJonathan Lemon static int 743773bda96SJonathan Lemon ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq, 744773bda96SJonathan Lemon int on) 745773bda96SJonathan Lemon { 746773bda96SJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 747773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = NULL; 748a62a56d0SJonathan Lemon u32 req; 749773bda96SJonathan Lemon int err; 750773bda96SJonathan Lemon 751773bda96SJonathan Lemon switch (rq->type) { 752773bda96SJonathan Lemon case PTP_CLK_REQ_EXTTS: 753a62a56d0SJonathan Lemon req = OCP_REQ_TIMESTAMP; 754773bda96SJonathan Lemon switch (rq->extts.index) { 755773bda96SJonathan Lemon case 0: 756773bda96SJonathan Lemon ext = bp->ts0; 757773bda96SJonathan Lemon break; 758773bda96SJonathan Lemon case 1: 759773bda96SJonathan Lemon ext = bp->ts1; 760773bda96SJonathan Lemon break; 761dcf61469SJonathan Lemon case 2: 762dcf61469SJonathan Lemon ext = bp->ts2; 763dcf61469SJonathan Lemon break; 764a62a56d0SJonathan Lemon case 3: 765a62a56d0SJonathan Lemon ext = bp->pps; 766a62a56d0SJonathan Lemon break; 767773bda96SJonathan Lemon } 768773bda96SJonathan Lemon break; 769773bda96SJonathan Lemon case PTP_CLK_REQ_PPS: 770a62a56d0SJonathan Lemon req = OCP_REQ_PPS; 771773bda96SJonathan Lemon ext = bp->pps; 772773bda96SJonathan Lemon break; 773a62a56d0SJonathan Lemon case PTP_CLK_REQ_PEROUT: 774a62a56d0SJonathan Lemon if (on && 775a62a56d0SJonathan Lemon (rq->perout.period.sec != 1 || rq->perout.period.nsec != 0)) 776a62a56d0SJonathan Lemon return -EINVAL; 777a62a56d0SJonathan Lemon /* This is a request for 1PPS on an output SMA. 778a62a56d0SJonathan Lemon * Allow, but assume manual configuration. 779a62a56d0SJonathan Lemon */ 780a62a56d0SJonathan Lemon return 0; 781773bda96SJonathan Lemon default: 782773bda96SJonathan Lemon return -EOPNOTSUPP; 783773bda96SJonathan Lemon } 784773bda96SJonathan Lemon 785773bda96SJonathan Lemon err = -ENXIO; 786773bda96SJonathan Lemon if (ext) 787a62a56d0SJonathan Lemon err = ext->info->enable(ext, req, on); 788773bda96SJonathan Lemon 789773bda96SJonathan Lemon return err; 790773bda96SJonathan Lemon } 791773bda96SJonathan Lemon 792a7e1abadSJonathan Lemon static const struct ptp_clock_info ptp_ocp_clock_info = { 793a7e1abadSJonathan Lemon .owner = THIS_MODULE, 794a7e1abadSJonathan Lemon .name = KBUILD_MODNAME, 795a7e1abadSJonathan Lemon .max_adj = 100000000, 796a7e1abadSJonathan Lemon .gettimex64 = ptp_ocp_gettimex, 797a7e1abadSJonathan Lemon .settime64 = ptp_ocp_settime, 798a7e1abadSJonathan Lemon .adjtime = ptp_ocp_adjtime, 799a7e1abadSJonathan Lemon .adjfine = ptp_ocp_null_adjfine, 8006d59d4faSJonathan Lemon .adjphase = ptp_ocp_null_adjphase, 801773bda96SJonathan Lemon .enable = ptp_ocp_enable, 802773bda96SJonathan Lemon .pps = true, 803a62a56d0SJonathan Lemon .n_ext_ts = 4, 804a62a56d0SJonathan Lemon .n_per_out = 1, 805a7e1abadSJonathan Lemon }; 806a7e1abadSJonathan Lemon 807773bda96SJonathan Lemon static void 808773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp) 809773bda96SJonathan Lemon { 810773bda96SJonathan Lemon u32 ctrl, select; 811773bda96SJonathan Lemon 812773bda96SJonathan Lemon select = ioread32(&bp->reg->select); 813773bda96SJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 814773bda96SJonathan Lemon 815773bda96SJonathan Lemon iowrite32(0, &bp->reg->drift_ns); 816773bda96SJonathan Lemon 8171acffc6eSJonathan Lemon ctrl = OCP_CTRL_ADJUST_DRIFT | OCP_CTRL_ENABLE; 818773bda96SJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 819773bda96SJonathan Lemon 820773bda96SJonathan Lemon /* restore clock selection */ 821773bda96SJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 822773bda96SJonathan Lemon } 823773bda96SJonathan Lemon 824773bda96SJonathan Lemon static void 825e68462a0SVadim Fedorenko ptp_ocp_utc_distribute(struct ptp_ocp *bp, u32 val) 826e68462a0SVadim Fedorenko { 827e68462a0SVadim Fedorenko unsigned long flags; 828e68462a0SVadim Fedorenko 829e68462a0SVadim Fedorenko spin_lock_irqsave(&bp->lock, flags); 830e68462a0SVadim Fedorenko 831e68462a0SVadim Fedorenko bp->utc_tai_offset = val; 832e68462a0SVadim Fedorenko 833e68462a0SVadim Fedorenko if (bp->irig_out) 834e68462a0SVadim Fedorenko iowrite32(val, &bp->irig_out->adj_sec); 835e68462a0SVadim Fedorenko if (bp->dcf_out) 836e68462a0SVadim Fedorenko iowrite32(val, &bp->dcf_out->adj_sec); 837e68462a0SVadim Fedorenko if (bp->nmea_out) 838e68462a0SVadim Fedorenko iowrite32(val, &bp->nmea_out->adj_sec); 839e68462a0SVadim Fedorenko 840e68462a0SVadim Fedorenko spin_unlock_irqrestore(&bp->lock, flags); 841e68462a0SVadim Fedorenko } 842e68462a0SVadim Fedorenko 843e68462a0SVadim Fedorenko static void 844773bda96SJonathan Lemon ptp_ocp_watchdog(struct timer_list *t) 845773bda96SJonathan Lemon { 846773bda96SJonathan Lemon struct ptp_ocp *bp = from_timer(bp, t, watchdog); 847773bda96SJonathan Lemon unsigned long flags; 848e68462a0SVadim Fedorenko u32 status, utc_offset; 849773bda96SJonathan Lemon 8500d43d4f2SJonathan Lemon status = ioread32(&bp->pps_to_clk->status); 851773bda96SJonathan Lemon 852773bda96SJonathan Lemon if (status & PPS_STATUS_SUPERV_ERR) { 8530d43d4f2SJonathan Lemon iowrite32(status, &bp->pps_to_clk->status); 854ef0cfb34SJonathan Lemon if (!bp->gnss_lost) { 855773bda96SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 856773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(bp); 857773bda96SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 858ef0cfb34SJonathan Lemon bp->gnss_lost = ktime_get_real_seconds(); 859773bda96SJonathan Lemon } 860773bda96SJonathan Lemon 861ef0cfb34SJonathan Lemon } else if (bp->gnss_lost) { 862ef0cfb34SJonathan Lemon bp->gnss_lost = 0; 863773bda96SJonathan Lemon } 864773bda96SJonathan Lemon 865e68462a0SVadim Fedorenko /* if GNSS provides correct data we can rely on 866e68462a0SVadim Fedorenko * it to get leap second information 867e68462a0SVadim Fedorenko */ 868e68462a0SVadim Fedorenko if (bp->tod) { 869e68462a0SVadim Fedorenko status = ioread32(&bp->tod->utc_status); 870e68462a0SVadim Fedorenko utc_offset = status & TOD_STATUS_UTC_MASK; 871e68462a0SVadim Fedorenko if (status & TOD_STATUS_UTC_VALID && 872e68462a0SVadim Fedorenko utc_offset != bp->utc_tai_offset) 873e68462a0SVadim Fedorenko ptp_ocp_utc_distribute(bp, utc_offset); 874e68462a0SVadim Fedorenko } 875e68462a0SVadim Fedorenko 876773bda96SJonathan Lemon mod_timer(&bp->watchdog, jiffies + HZ); 877773bda96SJonathan Lemon } 878773bda96SJonathan Lemon 8791acffc6eSJonathan Lemon static void 8801acffc6eSJonathan Lemon ptp_ocp_estimate_pci_timing(struct ptp_ocp *bp) 8811acffc6eSJonathan Lemon { 8821acffc6eSJonathan Lemon ktime_t start, end; 8831acffc6eSJonathan Lemon ktime_t delay; 8841acffc6eSJonathan Lemon u32 ctrl; 8851acffc6eSJonathan Lemon 8861acffc6eSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 8871acffc6eSJonathan Lemon ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE; 8881acffc6eSJonathan Lemon 8891acffc6eSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 8901acffc6eSJonathan Lemon 8911acffc6eSJonathan Lemon start = ktime_get_ns(); 8921acffc6eSJonathan Lemon 8931acffc6eSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 8941acffc6eSJonathan Lemon 8951acffc6eSJonathan Lemon end = ktime_get_ns(); 8961acffc6eSJonathan Lemon 8971acffc6eSJonathan Lemon delay = end - start; 8981acffc6eSJonathan Lemon bp->ts_window_adjust = (delay >> 5) * 3; 8991acffc6eSJonathan Lemon } 9001acffc6eSJonathan Lemon 901a7e1abadSJonathan Lemon static int 902773bda96SJonathan Lemon ptp_ocp_init_clock(struct ptp_ocp *bp) 903a7e1abadSJonathan Lemon { 904a7e1abadSJonathan Lemon struct timespec64 ts; 905a7e1abadSJonathan Lemon bool sync; 906a7e1abadSJonathan Lemon u32 ctrl; 907a7e1abadSJonathan Lemon 9081acffc6eSJonathan Lemon ctrl = OCP_CTRL_ENABLE; 909a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 910a7e1abadSJonathan Lemon 911773bda96SJonathan Lemon /* NO DRIFT Correction */ 912773bda96SJonathan Lemon /* offset_p:i 1/8, offset_i: 1/16, drift_p: 0, drift_i: 0 */ 913773bda96SJonathan Lemon iowrite32(0x2000, &bp->reg->servo_offset_p); 914773bda96SJonathan Lemon iowrite32(0x1000, &bp->reg->servo_offset_i); 915773bda96SJonathan Lemon iowrite32(0, &bp->reg->servo_drift_p); 916773bda96SJonathan Lemon iowrite32(0, &bp->reg->servo_drift_i); 917773bda96SJonathan Lemon 918773bda96SJonathan Lemon /* latch servo values */ 919773bda96SJonathan Lemon ctrl |= OCP_CTRL_ADJUST_SERVO; 920773bda96SJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 921773bda96SJonathan Lemon 922a7e1abadSJonathan Lemon if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) { 923a7e1abadSJonathan Lemon dev_err(&bp->pdev->dev, "clock not enabled\n"); 924a7e1abadSJonathan Lemon return -ENODEV; 925a7e1abadSJonathan Lemon } 926a7e1abadSJonathan Lemon 9271acffc6eSJonathan Lemon ptp_ocp_estimate_pci_timing(bp); 9281acffc6eSJonathan Lemon 929a7e1abadSJonathan Lemon sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC; 930a7e1abadSJonathan Lemon if (!sync) { 931065efcc5SJonathan Lemon ktime_get_clocktai_ts64(&ts); 932a7e1abadSJonathan Lemon ptp_ocp_settime(&bp->ptp_info, &ts); 933a7e1abadSJonathan Lemon } 934a7e1abadSJonathan Lemon 935065efcc5SJonathan Lemon /* If there is a clock supervisor, then enable the watchdog */ 936065efcc5SJonathan Lemon if (bp->pps_to_clk) { 937773bda96SJonathan Lemon timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0); 938773bda96SJonathan Lemon mod_timer(&bp->watchdog, jiffies + HZ); 939065efcc5SJonathan Lemon } 940773bda96SJonathan Lemon 941a7e1abadSJonathan Lemon return 0; 942a7e1abadSJonathan Lemon } 943a7e1abadSJonathan Lemon 944a7e1abadSJonathan Lemon static void 945065efcc5SJonathan Lemon ptp_ocp_tod_init(struct ptp_ocp *bp) 946065efcc5SJonathan Lemon { 947065efcc5SJonathan Lemon u32 ctrl, reg; 948065efcc5SJonathan Lemon 949065efcc5SJonathan Lemon ctrl = ioread32(&bp->tod->ctrl); 950065efcc5SJonathan Lemon ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE; 951065efcc5SJonathan Lemon ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B); 952065efcc5SJonathan Lemon iowrite32(ctrl, &bp->tod->ctrl); 953065efcc5SJonathan Lemon 954065efcc5SJonathan Lemon reg = ioread32(&bp->tod->utc_status); 955065efcc5SJonathan Lemon if (reg & TOD_STATUS_UTC_VALID) 956065efcc5SJonathan Lemon ptp_ocp_utc_distribute(bp, reg & TOD_STATUS_UTC_MASK); 957065efcc5SJonathan Lemon } 958065efcc5SJonathan Lemon 9599f492c4cSVadim Fedorenko static const char * 9609f492c4cSVadim Fedorenko ptp_ocp_tod_proto_name(const int idx) 961a7e1abadSJonathan Lemon { 962a7e1abadSJonathan Lemon static const char * const proto_name[] = { 963a7e1abadSJonathan Lemon "NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none", 964a7e1abadSJonathan Lemon "UBX", "UBX_UTC", "UBX_LS", "UBX_none" 965a7e1abadSJonathan Lemon }; 9669f492c4cSVadim Fedorenko return proto_name[idx]; 9679f492c4cSVadim Fedorenko } 9689f492c4cSVadim Fedorenko 9699f492c4cSVadim Fedorenko static const char * 9709f492c4cSVadim Fedorenko ptp_ocp_tod_gnss_name(int idx) 9719f492c4cSVadim Fedorenko { 972a7e1abadSJonathan Lemon static const char * const gnss_name[] = { 973a7e1abadSJonathan Lemon "ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU", 9749f492c4cSVadim Fedorenko "Unknown" 975a7e1abadSJonathan Lemon }; 97672f00505SDan Carpenter if (idx >= ARRAY_SIZE(gnss_name)) 9779f492c4cSVadim Fedorenko idx = ARRAY_SIZE(gnss_name) - 1; 9789f492c4cSVadim Fedorenko return gnss_name[idx]; 979a7e1abadSJonathan Lemon } 980a7e1abadSJonathan Lemon 9810cfcdd1eSJonathan Lemon struct ptp_ocp_nvmem_match_info { 9820cfcdd1eSJonathan Lemon struct ptp_ocp *bp; 9830cfcdd1eSJonathan Lemon const void * const tag; 984773bda96SJonathan Lemon }; 985773bda96SJonathan Lemon 9860cfcdd1eSJonathan Lemon static int 9870cfcdd1eSJonathan Lemon ptp_ocp_nvmem_match(struct device *dev, const void *data) 9880cfcdd1eSJonathan Lemon { 9890cfcdd1eSJonathan Lemon const struct ptp_ocp_nvmem_match_info *info = data; 9900cfcdd1eSJonathan Lemon 9910cfcdd1eSJonathan Lemon dev = dev->parent; 9920cfcdd1eSJonathan Lemon if (!i2c_verify_client(dev) || info->tag != dev->platform_data) 9930cfcdd1eSJonathan Lemon return 0; 9940cfcdd1eSJonathan Lemon 9950cfcdd1eSJonathan Lemon while ((dev = dev->parent)) 9960cfcdd1eSJonathan Lemon if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME)) 9970cfcdd1eSJonathan Lemon return info->bp == dev_get_drvdata(dev); 998773bda96SJonathan Lemon return 0; 999773bda96SJonathan Lemon } 1000773bda96SJonathan Lemon 10010cfcdd1eSJonathan Lemon static inline struct nvmem_device * 10020cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_get(struct ptp_ocp *bp, const void * const tag) 1003773bda96SJonathan Lemon { 10040cfcdd1eSJonathan Lemon struct ptp_ocp_nvmem_match_info info = { .bp = bp, .tag = tag }; 10050cfcdd1eSJonathan Lemon 10060cfcdd1eSJonathan Lemon return nvmem_device_find(&info, ptp_ocp_nvmem_match); 10070cfcdd1eSJonathan Lemon } 10080cfcdd1eSJonathan Lemon 10090cfcdd1eSJonathan Lemon static inline void 10100cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_put(struct nvmem_device **nvmemp) 10110cfcdd1eSJonathan Lemon { 10120cfcdd1eSJonathan Lemon if (*nvmemp != NULL) { 10130cfcdd1eSJonathan Lemon nvmem_device_put(*nvmemp); 10140cfcdd1eSJonathan Lemon *nvmemp = NULL; 10150cfcdd1eSJonathan Lemon } 10160cfcdd1eSJonathan Lemon } 10170cfcdd1eSJonathan Lemon 10180cfcdd1eSJonathan Lemon static void 10190cfcdd1eSJonathan Lemon ptp_ocp_read_eeprom(struct ptp_ocp *bp) 10200cfcdd1eSJonathan Lemon { 10210cfcdd1eSJonathan Lemon const struct ptp_ocp_eeprom_map *map; 10220cfcdd1eSJonathan Lemon struct nvmem_device *nvmem; 10230cfcdd1eSJonathan Lemon const void *tag; 10240cfcdd1eSJonathan Lemon int ret; 1025773bda96SJonathan Lemon 10261447149dSJonathan Lemon if (!bp->i2c_ctrl) 10271447149dSJonathan Lemon return; 10281447149dSJonathan Lemon 10290cfcdd1eSJonathan Lemon tag = NULL; 10300cfcdd1eSJonathan Lemon nvmem = NULL; 1031773bda96SJonathan Lemon 10320cfcdd1eSJonathan Lemon for (map = bp->eeprom_map; map->len; map++) { 10330cfcdd1eSJonathan Lemon if (map->tag != tag) { 10340cfcdd1eSJonathan Lemon tag = map->tag; 10350cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_put(&nvmem); 10360cfcdd1eSJonathan Lemon } 10370cfcdd1eSJonathan Lemon if (!nvmem) { 10380cfcdd1eSJonathan Lemon nvmem = ptp_ocp_nvmem_device_get(bp, tag); 10390cfcdd1eSJonathan Lemon if (!nvmem) 1040773bda96SJonathan Lemon goto out; 1041773bda96SJonathan Lemon } 10420cfcdd1eSJonathan Lemon ret = nvmem_device_read(nvmem, map->off, map->len, 10430cfcdd1eSJonathan Lemon BP_MAP_ENTRY_ADDR(bp, map)); 10440cfcdd1eSJonathan Lemon if (ret != map->len) 10450cfcdd1eSJonathan Lemon goto read_fail; 1046773bda96SJonathan Lemon } 1047773bda96SJonathan Lemon 10480cfcdd1eSJonathan Lemon bp->has_eeprom_data = true; 1049773bda96SJonathan Lemon 1050773bda96SJonathan Lemon out: 10510cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_put(&nvmem); 10520cfcdd1eSJonathan Lemon return; 10530cfcdd1eSJonathan Lemon 10540cfcdd1eSJonathan Lemon read_fail: 10550cfcdd1eSJonathan Lemon dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", ret); 10560cfcdd1eSJonathan Lemon goto out; 10570cfcdd1eSJonathan Lemon } 10580cfcdd1eSJonathan Lemon 10590cfcdd1eSJonathan Lemon static int 10600cfcdd1eSJonathan Lemon ptp_ocp_firstchild(struct device *dev, void *data) 10610cfcdd1eSJonathan Lemon { 10620cfcdd1eSJonathan Lemon return 1; 1063773bda96SJonathan Lemon } 1064773bda96SJonathan Lemon 1065773bda96SJonathan Lemon static struct device * 1066773bda96SJonathan Lemon ptp_ocp_find_flash(struct ptp_ocp *bp) 1067773bda96SJonathan Lemon { 1068773bda96SJonathan Lemon struct device *dev, *last; 1069773bda96SJonathan Lemon 1070773bda96SJonathan Lemon last = NULL; 1071773bda96SJonathan Lemon dev = &bp->spi_flash->dev; 1072773bda96SJonathan Lemon 1073773bda96SJonathan Lemon while ((dev = device_find_child(dev, NULL, ptp_ocp_firstchild))) { 1074773bda96SJonathan Lemon if (!strcmp("mtd", dev_bus_name(dev))) 1075773bda96SJonathan Lemon break; 1076773bda96SJonathan Lemon put_device(last); 1077773bda96SJonathan Lemon last = dev; 1078773bda96SJonathan Lemon } 1079773bda96SJonathan Lemon put_device(last); 1080773bda96SJonathan Lemon 1081773bda96SJonathan Lemon return dev; 1082773bda96SJonathan Lemon } 1083773bda96SJonathan Lemon 1084773bda96SJonathan Lemon static int 1085773bda96SJonathan Lemon ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev, 1086773bda96SJonathan Lemon const struct firmware *fw) 1087773bda96SJonathan Lemon { 1088773bda96SJonathan Lemon struct mtd_info *mtd = dev_get_drvdata(dev); 1089773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 1090773bda96SJonathan Lemon size_t off, len, resid, wrote; 1091773bda96SJonathan Lemon struct erase_info erase; 1092773bda96SJonathan Lemon size_t base, blksz; 10937c807572SJonathan Lemon int err = 0; 1094773bda96SJonathan Lemon 1095773bda96SJonathan Lemon off = 0; 1096773bda96SJonathan Lemon base = bp->flash_start; 1097773bda96SJonathan Lemon blksz = 4096; 1098773bda96SJonathan Lemon resid = fw->size; 1099773bda96SJonathan Lemon 1100773bda96SJonathan Lemon while (resid) { 1101773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, "Flashing", 1102773bda96SJonathan Lemon NULL, off, fw->size); 1103773bda96SJonathan Lemon 1104773bda96SJonathan Lemon len = min_t(size_t, resid, blksz); 1105773bda96SJonathan Lemon erase.addr = base + off; 1106773bda96SJonathan Lemon erase.len = blksz; 1107773bda96SJonathan Lemon 1108773bda96SJonathan Lemon err = mtd_erase(mtd, &erase); 1109773bda96SJonathan Lemon if (err) 1110773bda96SJonathan Lemon goto out; 1111773bda96SJonathan Lemon 1112773bda96SJonathan Lemon err = mtd_write(mtd, base + off, len, &wrote, &fw->data[off]); 1113773bda96SJonathan Lemon if (err) 1114773bda96SJonathan Lemon goto out; 1115773bda96SJonathan Lemon 1116773bda96SJonathan Lemon off += blksz; 1117773bda96SJonathan Lemon resid -= len; 1118773bda96SJonathan Lemon } 1119773bda96SJonathan Lemon out: 1120773bda96SJonathan Lemon return err; 1121773bda96SJonathan Lemon } 1122773bda96SJonathan Lemon 1123773bda96SJonathan Lemon static int 1124773bda96SJonathan Lemon ptp_ocp_devlink_flash_update(struct devlink *devlink, 1125773bda96SJonathan Lemon struct devlink_flash_update_params *params, 1126773bda96SJonathan Lemon struct netlink_ext_ack *extack) 1127773bda96SJonathan Lemon { 1128773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 1129773bda96SJonathan Lemon struct device *dev; 1130773bda96SJonathan Lemon const char *msg; 1131773bda96SJonathan Lemon int err; 1132773bda96SJonathan Lemon 1133773bda96SJonathan Lemon dev = ptp_ocp_find_flash(bp); 1134773bda96SJonathan Lemon if (!dev) { 1135773bda96SJonathan Lemon dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n"); 1136773bda96SJonathan Lemon return -ENODEV; 1137773bda96SJonathan Lemon } 1138773bda96SJonathan Lemon 1139773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, "Preparing to flash", 1140773bda96SJonathan Lemon NULL, 0, 0); 1141773bda96SJonathan Lemon 1142773bda96SJonathan Lemon err = ptp_ocp_devlink_flash(devlink, dev, params->fw); 1143773bda96SJonathan Lemon 1144773bda96SJonathan Lemon msg = err ? "Flash error" : "Flash complete"; 1145773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0); 1146773bda96SJonathan Lemon 1147773bda96SJonathan Lemon put_device(dev); 1148773bda96SJonathan Lemon return err; 1149773bda96SJonathan Lemon } 1150773bda96SJonathan Lemon 1151773bda96SJonathan Lemon static int 1152773bda96SJonathan Lemon ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req, 1153773bda96SJonathan Lemon struct netlink_ext_ack *extack) 1154773bda96SJonathan Lemon { 1155773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 1156773bda96SJonathan Lemon char buf[32]; 1157773bda96SJonathan Lemon int err; 1158773bda96SJonathan Lemon 1159773bda96SJonathan Lemon err = devlink_info_driver_name_put(req, KBUILD_MODNAME); 1160773bda96SJonathan Lemon if (err) 1161773bda96SJonathan Lemon return err; 1162773bda96SJonathan Lemon 1163b0ca789aSJonathan Lemon if (bp->fw_version & 0xffff) { 1164b0ca789aSJonathan Lemon sprintf(buf, "%d", bp->fw_version); 1165b0ca789aSJonathan Lemon err = devlink_info_version_running_put(req, "fw", buf); 1166773bda96SJonathan Lemon } else { 1167b0ca789aSJonathan Lemon sprintf(buf, "%d", bp->fw_version >> 16); 1168b0ca789aSJonathan Lemon err = devlink_info_version_running_put(req, "loader", buf); 1169773bda96SJonathan Lemon } 1170773bda96SJonathan Lemon if (err) 1171773bda96SJonathan Lemon return err; 1172773bda96SJonathan Lemon 11730cfcdd1eSJonathan Lemon if (!bp->has_eeprom_data) { 11740cfcdd1eSJonathan Lemon ptp_ocp_read_eeprom(bp); 11750cfcdd1eSJonathan Lemon if (!bp->has_eeprom_data) 11760cfcdd1eSJonathan Lemon return 0; 11770cfcdd1eSJonathan Lemon } 1178773bda96SJonathan Lemon 1179773bda96SJonathan Lemon sprintf(buf, "%pM", bp->serial); 1180773bda96SJonathan Lemon err = devlink_info_serial_number_put(req, buf); 1181773bda96SJonathan Lemon if (err) 1182773bda96SJonathan Lemon return err; 11830cfcdd1eSJonathan Lemon 11840cfcdd1eSJonathan Lemon err = devlink_info_version_fixed_put(req, 11850cfcdd1eSJonathan Lemon DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, 11860cfcdd1eSJonathan Lemon bp->board_id); 11870cfcdd1eSJonathan Lemon if (err) 11880cfcdd1eSJonathan Lemon return err; 1189773bda96SJonathan Lemon 1190773bda96SJonathan Lemon return 0; 1191773bda96SJonathan Lemon } 1192773bda96SJonathan Lemon 1193773bda96SJonathan Lemon static const struct devlink_ops ptp_ocp_devlink_ops = { 1194773bda96SJonathan Lemon .flash_update = ptp_ocp_devlink_flash_update, 1195773bda96SJonathan Lemon .info_get = ptp_ocp_devlink_info_get, 1196773bda96SJonathan Lemon }; 1197773bda96SJonathan Lemon 1198773bda96SJonathan Lemon static void __iomem * 1199773bda96SJonathan Lemon __ptp_ocp_get_mem(struct ptp_ocp *bp, unsigned long start, int size) 1200773bda96SJonathan Lemon { 1201773bda96SJonathan Lemon struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp"); 1202773bda96SJonathan Lemon 1203773bda96SJonathan Lemon return devm_ioremap_resource(&bp->pdev->dev, &res); 1204773bda96SJonathan Lemon } 1205773bda96SJonathan Lemon 1206773bda96SJonathan Lemon static void __iomem * 1207773bda96SJonathan Lemon ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r) 1208773bda96SJonathan Lemon { 1209773bda96SJonathan Lemon unsigned long start; 1210773bda96SJonathan Lemon 1211773bda96SJonathan Lemon start = pci_resource_start(bp->pdev, 0) + r->offset; 1212773bda96SJonathan Lemon return __ptp_ocp_get_mem(bp, start, r->size); 1213773bda96SJonathan Lemon } 1214773bda96SJonathan Lemon 1215773bda96SJonathan Lemon static void 1216773bda96SJonathan Lemon ptp_ocp_set_irq_resource(struct resource *res, int irq) 1217773bda96SJonathan Lemon { 1218773bda96SJonathan Lemon struct resource r = DEFINE_RES_IRQ(irq); 1219773bda96SJonathan Lemon *res = r; 1220773bda96SJonathan Lemon } 1221773bda96SJonathan Lemon 1222773bda96SJonathan Lemon static void 1223773bda96SJonathan Lemon ptp_ocp_set_mem_resource(struct resource *res, unsigned long start, int size) 1224773bda96SJonathan Lemon { 1225773bda96SJonathan Lemon struct resource r = DEFINE_RES_MEM(start, size); 1226773bda96SJonathan Lemon *res = r; 1227773bda96SJonathan Lemon } 1228773bda96SJonathan Lemon 1229773bda96SJonathan Lemon static int 1230773bda96SJonathan Lemon ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r) 1231773bda96SJonathan Lemon { 1232773bda96SJonathan Lemon struct ptp_ocp_flash_info *info; 1233773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1234773bda96SJonathan Lemon struct platform_device *p; 1235773bda96SJonathan Lemon struct resource res[2]; 1236773bda96SJonathan Lemon unsigned long start; 1237773bda96SJonathan Lemon int id; 1238773bda96SJonathan Lemon 1239773bda96SJonathan Lemon start = pci_resource_start(pdev, 0) + r->offset; 1240773bda96SJonathan Lemon ptp_ocp_set_mem_resource(&res[0], start, r->size); 1241773bda96SJonathan Lemon ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1242773bda96SJonathan Lemon 1243773bda96SJonathan Lemon info = r->extra; 1244773bda96SJonathan Lemon id = pci_dev_id(pdev) << 1; 1245773bda96SJonathan Lemon id += info->pci_offset; 1246773bda96SJonathan Lemon 1247773bda96SJonathan Lemon p = platform_device_register_resndata(&pdev->dev, info->name, id, 1248773bda96SJonathan Lemon res, 2, info->data, 1249773bda96SJonathan Lemon info->data_size); 1250773bda96SJonathan Lemon if (IS_ERR(p)) 1251773bda96SJonathan Lemon return PTR_ERR(p); 1252773bda96SJonathan Lemon 1253773bda96SJonathan Lemon bp_assign_entry(bp, r, p); 1254773bda96SJonathan Lemon 1255773bda96SJonathan Lemon return 0; 1256773bda96SJonathan Lemon } 1257773bda96SJonathan Lemon 1258773bda96SJonathan Lemon static struct platform_device * 1259773bda96SJonathan Lemon ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id) 1260773bda96SJonathan Lemon { 12611618df6aSJonathan Lemon struct ptp_ocp_i2c_info *info; 1262773bda96SJonathan Lemon struct resource res[2]; 1263773bda96SJonathan Lemon unsigned long start; 1264773bda96SJonathan Lemon 12651618df6aSJonathan Lemon info = r->extra; 1266773bda96SJonathan Lemon start = pci_resource_start(pdev, 0) + r->offset; 1267773bda96SJonathan Lemon ptp_ocp_set_mem_resource(&res[0], start, r->size); 1268773bda96SJonathan Lemon ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1269773bda96SJonathan Lemon 12701618df6aSJonathan Lemon return platform_device_register_resndata(&pdev->dev, info->name, 12711618df6aSJonathan Lemon id, res, 2, 12721618df6aSJonathan Lemon info->data, info->data_size); 1273773bda96SJonathan Lemon } 1274773bda96SJonathan Lemon 1275773bda96SJonathan Lemon static int 1276773bda96SJonathan Lemon ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r) 1277773bda96SJonathan Lemon { 1278773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 12791618df6aSJonathan Lemon struct ptp_ocp_i2c_info *info; 1280773bda96SJonathan Lemon struct platform_device *p; 1281773bda96SJonathan Lemon struct clk_hw *clk; 1282773bda96SJonathan Lemon char buf[32]; 1283773bda96SJonathan Lemon int id; 1284773bda96SJonathan Lemon 12851618df6aSJonathan Lemon info = r->extra; 1286773bda96SJonathan Lemon id = pci_dev_id(bp->pdev); 1287773bda96SJonathan Lemon 1288773bda96SJonathan Lemon sprintf(buf, "AXI.%d", id); 12891618df6aSJonathan Lemon clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0, 12901618df6aSJonathan Lemon info->fixed_rate); 1291773bda96SJonathan Lemon if (IS_ERR(clk)) 1292773bda96SJonathan Lemon return PTR_ERR(clk); 1293773bda96SJonathan Lemon bp->i2c_clk = clk; 1294773bda96SJonathan Lemon 12951618df6aSJonathan Lemon sprintf(buf, "%s.%d", info->name, id); 1296773bda96SJonathan Lemon devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf); 1297773bda96SJonathan Lemon p = ptp_ocp_i2c_bus(bp->pdev, r, id); 1298773bda96SJonathan Lemon if (IS_ERR(p)) 1299773bda96SJonathan Lemon return PTR_ERR(p); 1300773bda96SJonathan Lemon 1301773bda96SJonathan Lemon bp_assign_entry(bp, r, p); 1302773bda96SJonathan Lemon 1303773bda96SJonathan Lemon return 0; 1304773bda96SJonathan Lemon } 1305773bda96SJonathan Lemon 1306773bda96SJonathan Lemon static irqreturn_t 1307773bda96SJonathan Lemon ptp_ocp_ts_irq(int irq, void *priv) 1308773bda96SJonathan Lemon { 1309773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1310773bda96SJonathan Lemon struct ts_reg __iomem *reg = ext->mem; 1311773bda96SJonathan Lemon struct ptp_clock_event ev; 1312773bda96SJonathan Lemon u32 sec, nsec; 1313773bda96SJonathan Lemon 1314a62a56d0SJonathan Lemon if (ext == ext->bp->pps) { 1315a62a56d0SJonathan Lemon if (ext->bp->pps_req_map & OCP_REQ_PPS) { 1316a62a56d0SJonathan Lemon ev.type = PTP_CLOCK_PPS; 1317a62a56d0SJonathan Lemon ptp_clock_event(ext->bp->ptp, &ev); 1318a62a56d0SJonathan Lemon } 1319a62a56d0SJonathan Lemon 1320a62a56d0SJonathan Lemon if ((ext->bp->pps_req_map & ~OCP_REQ_PPS) == 0) 1321a62a56d0SJonathan Lemon goto out; 1322a62a56d0SJonathan Lemon } 1323a62a56d0SJonathan Lemon 1324773bda96SJonathan Lemon /* XXX should fix API - this converts s/ns -> ts -> s/ns */ 1325773bda96SJonathan Lemon sec = ioread32(®->time_sec); 1326773bda96SJonathan Lemon nsec = ioread32(®->time_ns); 1327773bda96SJonathan Lemon 1328773bda96SJonathan Lemon ev.type = PTP_CLOCK_EXTTS; 1329773bda96SJonathan Lemon ev.index = ext->info->index; 13301acffc6eSJonathan Lemon ev.timestamp = sec * NSEC_PER_SEC + nsec; 1331773bda96SJonathan Lemon 1332773bda96SJonathan Lemon ptp_clock_event(ext->bp->ptp, &ev); 1333773bda96SJonathan Lemon 1334a62a56d0SJonathan Lemon out: 1335773bda96SJonathan Lemon iowrite32(1, ®->intr); /* write 1 to ack */ 1336773bda96SJonathan Lemon 1337773bda96SJonathan Lemon return IRQ_HANDLED; 1338773bda96SJonathan Lemon } 1339773bda96SJonathan Lemon 1340773bda96SJonathan Lemon static int 1341a62a56d0SJonathan Lemon ptp_ocp_ts_enable(void *priv, u32 req, bool enable) 1342773bda96SJonathan Lemon { 1343773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1344773bda96SJonathan Lemon struct ts_reg __iomem *reg = ext->mem; 1345a62a56d0SJonathan Lemon struct ptp_ocp *bp = ext->bp; 1346a62a56d0SJonathan Lemon 1347a62a56d0SJonathan Lemon if (ext == bp->pps) { 1348a62a56d0SJonathan Lemon u32 old_map = bp->pps_req_map; 1349a62a56d0SJonathan Lemon 1350a62a56d0SJonathan Lemon if (enable) 1351a62a56d0SJonathan Lemon bp->pps_req_map |= req; 1352a62a56d0SJonathan Lemon else 1353a62a56d0SJonathan Lemon bp->pps_req_map &= ~req; 1354a62a56d0SJonathan Lemon 1355a62a56d0SJonathan Lemon /* if no state change, just return */ 1356a62a56d0SJonathan Lemon if ((!!old_map ^ !!bp->pps_req_map) == 0) 1357a62a56d0SJonathan Lemon return 0; 1358a62a56d0SJonathan Lemon } 1359773bda96SJonathan Lemon 1360773bda96SJonathan Lemon if (enable) { 1361773bda96SJonathan Lemon iowrite32(1, ®->enable); 1362773bda96SJonathan Lemon iowrite32(1, ®->intr_mask); 1363773bda96SJonathan Lemon iowrite32(1, ®->intr); 1364773bda96SJonathan Lemon } else { 1365773bda96SJonathan Lemon iowrite32(0, ®->intr_mask); 1366773bda96SJonathan Lemon iowrite32(0, ®->enable); 1367773bda96SJonathan Lemon } 1368773bda96SJonathan Lemon 1369773bda96SJonathan Lemon return 0; 1370773bda96SJonathan Lemon } 1371773bda96SJonathan Lemon 1372773bda96SJonathan Lemon static void 1373773bda96SJonathan Lemon ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext) 1374773bda96SJonathan Lemon { 1375a62a56d0SJonathan Lemon ext->info->enable(ext, ~0, false); 1376773bda96SJonathan Lemon pci_free_irq(ext->bp->pdev, ext->irq_vec, ext); 1377773bda96SJonathan Lemon kfree(ext); 1378773bda96SJonathan Lemon } 1379773bda96SJonathan Lemon 1380773bda96SJonathan Lemon static int 1381773bda96SJonathan Lemon ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r) 1382773bda96SJonathan Lemon { 1383773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1384773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext; 1385773bda96SJonathan Lemon int err; 1386773bda96SJonathan Lemon 1387773bda96SJonathan Lemon ext = kzalloc(sizeof(*ext), GFP_KERNEL); 1388773bda96SJonathan Lemon if (!ext) 1389773bda96SJonathan Lemon return -ENOMEM; 1390773bda96SJonathan Lemon 1391773bda96SJonathan Lemon ext->mem = ptp_ocp_get_mem(bp, r); 1392c7521d3aSDan Carpenter if (IS_ERR(ext->mem)) { 1393c7521d3aSDan Carpenter err = PTR_ERR(ext->mem); 1394773bda96SJonathan Lemon goto out; 1395c7521d3aSDan Carpenter } 1396773bda96SJonathan Lemon 1397773bda96SJonathan Lemon ext->bp = bp; 1398773bda96SJonathan Lemon ext->info = r->extra; 1399773bda96SJonathan Lemon ext->irq_vec = r->irq_vec; 1400773bda96SJonathan Lemon 1401773bda96SJonathan Lemon err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL, 140256ec4403SJonathan Lemon ext, "ocp%d.%s", bp->id, r->name); 1403773bda96SJonathan Lemon if (err) { 1404773bda96SJonathan Lemon dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec); 1405773bda96SJonathan Lemon goto out; 1406773bda96SJonathan Lemon } 1407773bda96SJonathan Lemon 1408773bda96SJonathan Lemon bp_assign_entry(bp, r, ext); 1409773bda96SJonathan Lemon 1410773bda96SJonathan Lemon return 0; 1411773bda96SJonathan Lemon 1412773bda96SJonathan Lemon out: 1413773bda96SJonathan Lemon kfree(ext); 1414773bda96SJonathan Lemon return err; 1415773bda96SJonathan Lemon } 1416773bda96SJonathan Lemon 1417773bda96SJonathan Lemon static int 1418773bda96SJonathan Lemon ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r) 1419773bda96SJonathan Lemon { 1420773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1421773bda96SJonathan Lemon struct uart_8250_port uart; 1422773bda96SJonathan Lemon 1423773bda96SJonathan Lemon /* Setting UPF_IOREMAP and leaving port.membase unspecified lets 1424773bda96SJonathan Lemon * the serial port device claim and release the pci resource. 1425773bda96SJonathan Lemon */ 1426773bda96SJonathan Lemon memset(&uart, 0, sizeof(uart)); 1427773bda96SJonathan Lemon uart.port.dev = &pdev->dev; 1428773bda96SJonathan Lemon uart.port.iotype = UPIO_MEM; 1429773bda96SJonathan Lemon uart.port.regshift = 2; 1430773bda96SJonathan Lemon uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset; 1431773bda96SJonathan Lemon uart.port.irq = pci_irq_vector(pdev, r->irq_vec); 1432773bda96SJonathan Lemon uart.port.uartclk = 50000000; 1433c17c4059SJonathan Lemon uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP | UPF_NO_THRE_TEST; 1434773bda96SJonathan Lemon uart.port.type = PORT_16550A; 1435773bda96SJonathan Lemon 1436773bda96SJonathan Lemon return serial8250_register_8250_port(&uart); 1437773bda96SJonathan Lemon } 1438773bda96SJonathan Lemon 1439773bda96SJonathan Lemon static int 1440773bda96SJonathan Lemon ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r) 1441773bda96SJonathan Lemon { 1442773bda96SJonathan Lemon int port; 1443773bda96SJonathan Lemon 1444773bda96SJonathan Lemon port = ptp_ocp_serial_line(bp, r); 1445773bda96SJonathan Lemon if (port < 0) 1446773bda96SJonathan Lemon return port; 1447773bda96SJonathan Lemon 1448773bda96SJonathan Lemon bp_assign_entry(bp, r, port); 1449773bda96SJonathan Lemon 1450773bda96SJonathan Lemon return 0; 1451773bda96SJonathan Lemon } 1452773bda96SJonathan Lemon 1453773bda96SJonathan Lemon static int 1454773bda96SJonathan Lemon ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r) 1455773bda96SJonathan Lemon { 1456773bda96SJonathan Lemon void __iomem *mem; 1457773bda96SJonathan Lemon 1458773bda96SJonathan Lemon mem = ptp_ocp_get_mem(bp, r); 1459c7521d3aSDan Carpenter if (IS_ERR(mem)) 1460c7521d3aSDan Carpenter return PTR_ERR(mem); 1461773bda96SJonathan Lemon 1462773bda96SJonathan Lemon bp_assign_entry(bp, r, mem); 1463773bda96SJonathan Lemon 1464773bda96SJonathan Lemon return 0; 1465773bda96SJonathan Lemon } 1466773bda96SJonathan Lemon 1467e3516bb4SJonathan Lemon static void 1468e3516bb4SJonathan Lemon ptp_ocp_nmea_out_init(struct ptp_ocp *bp) 1469e3516bb4SJonathan Lemon { 1470e3516bb4SJonathan Lemon if (!bp->nmea_out) 1471e3516bb4SJonathan Lemon return; 1472e3516bb4SJonathan Lemon 1473e3516bb4SJonathan Lemon iowrite32(0, &bp->nmea_out->ctrl); /* disable */ 1474e3516bb4SJonathan Lemon iowrite32(7, &bp->nmea_out->uart_baud); /* 115200 */ 1475e3516bb4SJonathan Lemon iowrite32(1, &bp->nmea_out->ctrl); /* enable */ 1476e3516bb4SJonathan Lemon } 1477e3516bb4SJonathan Lemon 1478*a509a7c6SJonathan Lemon static void 1479*a509a7c6SJonathan Lemon ptp_ocp_sma_init(struct ptp_ocp *bp) 1480*a509a7c6SJonathan Lemon { 1481*a509a7c6SJonathan Lemon u32 reg; 1482*a509a7c6SJonathan Lemon int i; 1483*a509a7c6SJonathan Lemon 1484*a509a7c6SJonathan Lemon /* defaults */ 1485*a509a7c6SJonathan Lemon bp->sma[0].mode = SMA_MODE_IN; 1486*a509a7c6SJonathan Lemon bp->sma[1].mode = SMA_MODE_IN; 1487*a509a7c6SJonathan Lemon bp->sma[2].mode = SMA_MODE_OUT; 1488*a509a7c6SJonathan Lemon bp->sma[3].mode = SMA_MODE_OUT; 1489*a509a7c6SJonathan Lemon 1490*a509a7c6SJonathan Lemon /* If no SMA1 map, the pin functions and directions are fixed. */ 1491*a509a7c6SJonathan Lemon if (!bp->sma_map1) { 1492*a509a7c6SJonathan Lemon for (i = 0; i < 4; i++) { 1493*a509a7c6SJonathan Lemon bp->sma[i].fixed_fcn = true; 1494*a509a7c6SJonathan Lemon bp->sma[i].fixed_dir = true; 1495*a509a7c6SJonathan Lemon } 1496*a509a7c6SJonathan Lemon return; 1497*a509a7c6SJonathan Lemon } 1498*a509a7c6SJonathan Lemon 1499*a509a7c6SJonathan Lemon /* If SMA2 GPIO output map is all 1, it is not present. 1500*a509a7c6SJonathan Lemon * This indicates the firmware has fixed direction SMA pins. 1501*a509a7c6SJonathan Lemon */ 1502*a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map2->gpio2); 1503*a509a7c6SJonathan Lemon if (reg == 0xffffffff) { 1504*a509a7c6SJonathan Lemon for (i = 0; i < 4; i++) 1505*a509a7c6SJonathan Lemon bp->sma[i].fixed_dir = true; 1506*a509a7c6SJonathan Lemon } else { 1507*a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map1->gpio1); 1508*a509a7c6SJonathan Lemon bp->sma[0].mode = reg & BIT(15) ? SMA_MODE_IN : SMA_MODE_OUT; 1509*a509a7c6SJonathan Lemon bp->sma[1].mode = reg & BIT(31) ? SMA_MODE_IN : SMA_MODE_OUT; 1510*a509a7c6SJonathan Lemon 1511*a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map1->gpio2); 1512*a509a7c6SJonathan Lemon bp->sma[2].mode = reg & BIT(15) ? SMA_MODE_OUT : SMA_MODE_IN; 1513*a509a7c6SJonathan Lemon bp->sma[3].mode = reg & BIT(31) ? SMA_MODE_OUT : SMA_MODE_IN; 1514*a509a7c6SJonathan Lemon } 1515*a509a7c6SJonathan Lemon } 1516*a509a7c6SJonathan Lemon 1517773bda96SJonathan Lemon /* FB specific board initializers; last "resource" registered. */ 1518773bda96SJonathan Lemon static int 1519773bda96SJonathan Lemon ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r) 1520773bda96SJonathan Lemon { 1521773bda96SJonathan Lemon bp->flash_start = 1024 * 4096; 15220cfcdd1eSJonathan Lemon bp->eeprom_map = fb_eeprom_map; 1523b0ca789aSJonathan Lemon bp->fw_version = ioread32(&bp->image->version); 1524773bda96SJonathan Lemon 1525065efcc5SJonathan Lemon ptp_ocp_tod_init(bp); 1526e3516bb4SJonathan Lemon ptp_ocp_nmea_out_init(bp); 1527*a509a7c6SJonathan Lemon ptp_ocp_sma_init(bp); 1528065efcc5SJonathan Lemon 1529773bda96SJonathan Lemon return ptp_ocp_init_clock(bp); 1530773bda96SJonathan Lemon } 1531773bda96SJonathan Lemon 153256ec4403SJonathan Lemon static bool 153356ec4403SJonathan Lemon ptp_ocp_allow_irq(struct ptp_ocp *bp, struct ocp_resource *r) 153456ec4403SJonathan Lemon { 153556ec4403SJonathan Lemon bool allow = !r->irq_vec || r->irq_vec < bp->n_irqs; 153656ec4403SJonathan Lemon 153756ec4403SJonathan Lemon if (!allow) 153856ec4403SJonathan Lemon dev_err(&bp->pdev->dev, "irq %d out of range, skipping %s\n", 153956ec4403SJonathan Lemon r->irq_vec, r->name); 154056ec4403SJonathan Lemon return allow; 154156ec4403SJonathan Lemon } 154256ec4403SJonathan Lemon 1543773bda96SJonathan Lemon static int 1544773bda96SJonathan Lemon ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data) 1545773bda96SJonathan Lemon { 1546773bda96SJonathan Lemon struct ocp_resource *r, *table; 1547773bda96SJonathan Lemon int err = 0; 1548773bda96SJonathan Lemon 1549773bda96SJonathan Lemon table = (struct ocp_resource *)driver_data; 1550773bda96SJonathan Lemon for (r = table; r->setup; r++) { 155156ec4403SJonathan Lemon if (!ptp_ocp_allow_irq(bp, r)) 155256ec4403SJonathan Lemon continue; 1553773bda96SJonathan Lemon err = r->setup(bp, r); 1554bceff290SJonathan Lemon if (err) { 1555bceff290SJonathan Lemon dev_err(&bp->pdev->dev, 1556bceff290SJonathan Lemon "Could not register %s: err %d\n", 1557bceff290SJonathan Lemon r->name, err); 1558773bda96SJonathan Lemon break; 1559773bda96SJonathan Lemon } 1560bceff290SJonathan Lemon } 1561773bda96SJonathan Lemon return err; 1562773bda96SJonathan Lemon } 1563773bda96SJonathan Lemon 15646baf2925SJonathan Lemon static void 15656baf2925SJonathan Lemon ptp_ocp_enable_fpga(u32 __iomem *reg, u32 bit, bool enable) 15666baf2925SJonathan Lemon { 15676baf2925SJonathan Lemon u32 ctrl; 15686baf2925SJonathan Lemon bool on; 15696baf2925SJonathan Lemon 15706baf2925SJonathan Lemon ctrl = ioread32(reg); 15716baf2925SJonathan Lemon on = ctrl & bit; 15726baf2925SJonathan Lemon if (on ^ enable) { 15736baf2925SJonathan Lemon ctrl &= ~bit; 15746baf2925SJonathan Lemon ctrl |= enable ? bit : 0; 15756baf2925SJonathan Lemon iowrite32(ctrl, reg); 15766baf2925SJonathan Lemon } 15776baf2925SJonathan Lemon } 15786baf2925SJonathan Lemon 15796baf2925SJonathan Lemon static void 15806baf2925SJonathan Lemon ptp_ocp_irig_out(struct ptp_ocp *bp, bool enable) 15816baf2925SJonathan Lemon { 15826baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->irig_out->ctrl, 15836baf2925SJonathan Lemon IRIG_M_CTRL_ENABLE, enable); 15846baf2925SJonathan Lemon } 15856baf2925SJonathan Lemon 15866baf2925SJonathan Lemon static void 15876baf2925SJonathan Lemon ptp_ocp_irig_in(struct ptp_ocp *bp, bool enable) 15886baf2925SJonathan Lemon { 15896baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->irig_in->ctrl, 15906baf2925SJonathan Lemon IRIG_S_CTRL_ENABLE, enable); 15916baf2925SJonathan Lemon } 15926baf2925SJonathan Lemon 15936baf2925SJonathan Lemon static void 15946baf2925SJonathan Lemon ptp_ocp_dcf_out(struct ptp_ocp *bp, bool enable) 15956baf2925SJonathan Lemon { 15966baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->dcf_out->ctrl, 15976baf2925SJonathan Lemon DCF_M_CTRL_ENABLE, enable); 15986baf2925SJonathan Lemon } 15996baf2925SJonathan Lemon 16006baf2925SJonathan Lemon static void 16016baf2925SJonathan Lemon ptp_ocp_dcf_in(struct ptp_ocp *bp, bool enable) 16026baf2925SJonathan Lemon { 16036baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->dcf_in->ctrl, 16046baf2925SJonathan Lemon DCF_S_CTRL_ENABLE, enable); 16056baf2925SJonathan Lemon } 16066baf2925SJonathan Lemon 16076baf2925SJonathan Lemon static void 16086baf2925SJonathan Lemon __handle_signal_outputs(struct ptp_ocp *bp, u32 val) 16096baf2925SJonathan Lemon { 16106baf2925SJonathan Lemon ptp_ocp_irig_out(bp, val & 0x00100010); 16116baf2925SJonathan Lemon ptp_ocp_dcf_out(bp, val & 0x00200020); 16126baf2925SJonathan Lemon } 16136baf2925SJonathan Lemon 16146baf2925SJonathan Lemon static void 16156baf2925SJonathan Lemon __handle_signal_inputs(struct ptp_ocp *bp, u32 val) 16166baf2925SJonathan Lemon { 16176baf2925SJonathan Lemon ptp_ocp_irig_in(bp, val & 0x00100010); 16186baf2925SJonathan Lemon ptp_ocp_dcf_in(bp, val & 0x00200020); 16196baf2925SJonathan Lemon } 16206baf2925SJonathan Lemon 1621e1daf0ecSJonathan Lemon /* 1622e1daf0ecSJonathan Lemon * ANT0 == gps (in) 1623e1daf0ecSJonathan Lemon * ANT1 == sma1 (in) 1624e1daf0ecSJonathan Lemon * ANT2 == sma2 (in) 1625e1daf0ecSJonathan Lemon * ANT3 == sma3 (out) 1626e1daf0ecSJonathan Lemon * ANT4 == sma4 (out) 1627e1daf0ecSJonathan Lemon */ 1628e1daf0ecSJonathan Lemon 1629e1daf0ecSJonathan Lemon static ssize_t 1630e1daf0ecSJonathan Lemon ptp_ocp_show_output(u32 val, char *buf, int default_idx) 1631e1daf0ecSJonathan Lemon { 1632e1daf0ecSJonathan Lemon const char *name; 1633e1daf0ecSJonathan Lemon ssize_t count; 1634e1daf0ecSJonathan Lemon 1635e1daf0ecSJonathan Lemon count = sysfs_emit(buf, "OUT: "); 1636e1daf0ecSJonathan Lemon name = ptp_ocp_select_name_from_val(ptp_ocp_sma_out, val); 1637e1daf0ecSJonathan Lemon if (!name) 1638e1daf0ecSJonathan Lemon name = ptp_ocp_sma_out[default_idx].name; 1639e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s\n", name); 1640e1daf0ecSJonathan Lemon return count; 1641e1daf0ecSJonathan Lemon } 1642e1daf0ecSJonathan Lemon 1643e1daf0ecSJonathan Lemon static ssize_t 1644*a509a7c6SJonathan Lemon ptp_ocp_show_inputs(u32 val, char *buf, int default_idx) 1645e1daf0ecSJonathan Lemon { 1646e1daf0ecSJonathan Lemon const char *name; 1647e1daf0ecSJonathan Lemon ssize_t count; 1648e1daf0ecSJonathan Lemon int i; 1649e1daf0ecSJonathan Lemon 1650e1daf0ecSJonathan Lemon count = sysfs_emit(buf, "IN: "); 1651e1daf0ecSJonathan Lemon for (i = 0; i < ARRAY_SIZE(ptp_ocp_sma_in); i++) { 1652e1daf0ecSJonathan Lemon if (val & ptp_ocp_sma_in[i].value) { 1653e1daf0ecSJonathan Lemon name = ptp_ocp_sma_in[i].name; 1654e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", name); 1655e1daf0ecSJonathan Lemon } 1656e1daf0ecSJonathan Lemon } 1657*a509a7c6SJonathan Lemon if (!val && default_idx >= 0) 1658*a509a7c6SJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", 1659*a509a7c6SJonathan Lemon ptp_ocp_sma_in[default_idx].name); 1660e1daf0ecSJonathan Lemon if (count) 1661e1daf0ecSJonathan Lemon count--; 1662e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "\n"); 1663e1daf0ecSJonathan Lemon return count; 1664e1daf0ecSJonathan Lemon } 1665e1daf0ecSJonathan Lemon 1666e1daf0ecSJonathan Lemon static int 1667e1daf0ecSJonathan Lemon sma_parse_inputs(const char *buf, enum ptp_ocp_sma_mode *mode) 1668e1daf0ecSJonathan Lemon { 1669e1daf0ecSJonathan Lemon struct ocp_selector *tbl[] = { ptp_ocp_sma_in, ptp_ocp_sma_out }; 1670e1daf0ecSJonathan Lemon int idx, count, dir; 1671e1daf0ecSJonathan Lemon char **argv; 1672e1daf0ecSJonathan Lemon int ret; 1673e1daf0ecSJonathan Lemon 1674e1daf0ecSJonathan Lemon argv = argv_split(GFP_KERNEL, buf, &count); 1675e1daf0ecSJonathan Lemon if (!argv) 1676e1daf0ecSJonathan Lemon return -ENOMEM; 1677e1daf0ecSJonathan Lemon 1678e1daf0ecSJonathan Lemon ret = -EINVAL; 1679e1daf0ecSJonathan Lemon if (!count) 1680e1daf0ecSJonathan Lemon goto out; 1681e1daf0ecSJonathan Lemon 1682e1daf0ecSJonathan Lemon idx = 0; 1683e1daf0ecSJonathan Lemon dir = *mode == SMA_MODE_IN ? 0 : 1; 1684*a509a7c6SJonathan Lemon if (!strcasecmp("IN:", argv[0])) { 1685e1daf0ecSJonathan Lemon dir = 0; 1686e1daf0ecSJonathan Lemon idx++; 1687e1daf0ecSJonathan Lemon } 1688e1daf0ecSJonathan Lemon if (!strcasecmp("OUT:", argv[0])) { 1689e1daf0ecSJonathan Lemon dir = 1; 1690e1daf0ecSJonathan Lemon idx++; 1691e1daf0ecSJonathan Lemon } 1692e1daf0ecSJonathan Lemon *mode = dir == 0 ? SMA_MODE_IN : SMA_MODE_OUT; 1693e1daf0ecSJonathan Lemon 1694e1daf0ecSJonathan Lemon ret = 0; 1695e1daf0ecSJonathan Lemon for (; idx < count; idx++) 1696e1daf0ecSJonathan Lemon ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]); 1697e1daf0ecSJonathan Lemon if (ret < 0) 1698e1daf0ecSJonathan Lemon ret = -EINVAL; 1699e1daf0ecSJonathan Lemon 1700e1daf0ecSJonathan Lemon out: 1701e1daf0ecSJonathan Lemon argv_free(argv); 1702e1daf0ecSJonathan Lemon return ret; 1703e1daf0ecSJonathan Lemon } 1704e1daf0ecSJonathan Lemon 1705*a509a7c6SJonathan Lemon static u32 1706*a509a7c6SJonathan Lemon ptp_ocp_sma_get(struct ptp_ocp *bp, int sma_nr, enum ptp_ocp_sma_mode mode) 1707e1daf0ecSJonathan Lemon { 1708*a509a7c6SJonathan Lemon u32 __iomem *gpio; 1709*a509a7c6SJonathan Lemon u32 shift; 1710*a509a7c6SJonathan Lemon 1711*a509a7c6SJonathan Lemon if (bp->sma[sma_nr - 1].fixed_fcn) 1712*a509a7c6SJonathan Lemon return (sma_nr - 1) & 1; 1713*a509a7c6SJonathan Lemon 1714*a509a7c6SJonathan Lemon if (mode == SMA_MODE_IN) 1715*a509a7c6SJonathan Lemon gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1; 1716*a509a7c6SJonathan Lemon else 1717*a509a7c6SJonathan Lemon gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2; 1718*a509a7c6SJonathan Lemon shift = sma_nr & 1 ? 0 : 16; 1719*a509a7c6SJonathan Lemon 1720*a509a7c6SJonathan Lemon return (ioread32(gpio) >> shift) & 0xffff; 1721*a509a7c6SJonathan Lemon } 1722*a509a7c6SJonathan Lemon 1723*a509a7c6SJonathan Lemon static ssize_t 1724*a509a7c6SJonathan Lemon ptp_ocp_sma_show(struct ptp_ocp *bp, int sma_nr, char *buf, 1725*a509a7c6SJonathan Lemon int default_in_idx, int default_out_idx) 1726*a509a7c6SJonathan Lemon { 1727*a509a7c6SJonathan Lemon struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1]; 1728*a509a7c6SJonathan Lemon u32 val; 1729*a509a7c6SJonathan Lemon 1730*a509a7c6SJonathan Lemon val = ptp_ocp_sma_get(bp, sma_nr, sma->mode) & SMA_SELECT_MASK; 1731e1daf0ecSJonathan Lemon 1732e1daf0ecSJonathan Lemon if (sma->mode == SMA_MODE_IN) 1733*a509a7c6SJonathan Lemon return ptp_ocp_show_inputs(val, buf, default_in_idx); 1734e1daf0ecSJonathan Lemon 1735*a509a7c6SJonathan Lemon return ptp_ocp_show_output(val, buf, default_out_idx); 1736e1daf0ecSJonathan Lemon } 1737e1daf0ecSJonathan Lemon 1738e1daf0ecSJonathan Lemon static ssize_t 1739e1daf0ecSJonathan Lemon sma1_show(struct device *dev, struct device_attribute *attr, char *buf) 1740e1daf0ecSJonathan Lemon { 1741e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1742e1daf0ecSJonathan Lemon 1743*a509a7c6SJonathan Lemon return ptp_ocp_sma_show(bp, 1, buf, 0, 1); 1744e1daf0ecSJonathan Lemon } 1745e1daf0ecSJonathan Lemon 1746e1daf0ecSJonathan Lemon static ssize_t 1747e1daf0ecSJonathan Lemon sma2_show(struct device *dev, struct device_attribute *attr, char *buf) 1748e1daf0ecSJonathan Lemon { 1749e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1750e1daf0ecSJonathan Lemon 1751*a509a7c6SJonathan Lemon return ptp_ocp_sma_show(bp, 2, buf, -1, 1); 1752e1daf0ecSJonathan Lemon } 1753e1daf0ecSJonathan Lemon 1754e1daf0ecSJonathan Lemon static ssize_t 1755e1daf0ecSJonathan Lemon sma3_show(struct device *dev, struct device_attribute *attr, char *buf) 1756e1daf0ecSJonathan Lemon { 1757e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1758e1daf0ecSJonathan Lemon 1759*a509a7c6SJonathan Lemon return ptp_ocp_sma_show(bp, 3, buf, -1, 0); 1760e1daf0ecSJonathan Lemon } 1761e1daf0ecSJonathan Lemon 1762e1daf0ecSJonathan Lemon static ssize_t 1763e1daf0ecSJonathan Lemon sma4_show(struct device *dev, struct device_attribute *attr, char *buf) 1764e1daf0ecSJonathan Lemon { 1765e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1766e1daf0ecSJonathan Lemon 1767*a509a7c6SJonathan Lemon return ptp_ocp_sma_show(bp, 4, buf, -1, 1); 1768e1daf0ecSJonathan Lemon } 1769e1daf0ecSJonathan Lemon 1770e1daf0ecSJonathan Lemon static void 1771*a509a7c6SJonathan Lemon ptp_ocp_sma_store_output(struct ptp_ocp *bp, int sma_nr, u32 val) 1772e1daf0ecSJonathan Lemon { 1773*a509a7c6SJonathan Lemon u32 reg, mask, shift; 1774e1daf0ecSJonathan Lemon unsigned long flags; 1775*a509a7c6SJonathan Lemon u32 __iomem *gpio; 1776*a509a7c6SJonathan Lemon 1777*a509a7c6SJonathan Lemon gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2; 1778*a509a7c6SJonathan Lemon shift = sma_nr & 1 ? 0 : 16; 1779e1daf0ecSJonathan Lemon 1780e1daf0ecSJonathan Lemon mask = 0xffff << (16 - shift); 1781e1daf0ecSJonathan Lemon 1782e1daf0ecSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1783e1daf0ecSJonathan Lemon 1784*a509a7c6SJonathan Lemon reg = ioread32(gpio); 1785*a509a7c6SJonathan Lemon reg = (reg & mask) | (val << shift); 17866baf2925SJonathan Lemon 1787*a509a7c6SJonathan Lemon __handle_signal_outputs(bp, reg); 17886baf2925SJonathan Lemon 1789*a509a7c6SJonathan Lemon iowrite32(reg, gpio); 1790e1daf0ecSJonathan Lemon 1791e1daf0ecSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1792e1daf0ecSJonathan Lemon } 1793e1daf0ecSJonathan Lemon 1794e1daf0ecSJonathan Lemon static void 1795*a509a7c6SJonathan Lemon ptp_ocp_sma_store_inputs(struct ptp_ocp *bp, int sma_nr, u32 val) 1796e1daf0ecSJonathan Lemon { 1797*a509a7c6SJonathan Lemon u32 reg, mask, shift; 1798e1daf0ecSJonathan Lemon unsigned long flags; 1799*a509a7c6SJonathan Lemon u32 __iomem *gpio; 1800*a509a7c6SJonathan Lemon 1801*a509a7c6SJonathan Lemon gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1; 1802*a509a7c6SJonathan Lemon shift = sma_nr & 1 ? 0 : 16; 1803e1daf0ecSJonathan Lemon 1804e1daf0ecSJonathan Lemon mask = 0xffff << (16 - shift); 1805e1daf0ecSJonathan Lemon 1806e1daf0ecSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1807e1daf0ecSJonathan Lemon 1808*a509a7c6SJonathan Lemon reg = ioread32(gpio); 1809*a509a7c6SJonathan Lemon reg = (reg & mask) | (val << shift); 18106baf2925SJonathan Lemon 1811*a509a7c6SJonathan Lemon __handle_signal_inputs(bp, reg); 18126baf2925SJonathan Lemon 1813*a509a7c6SJonathan Lemon iowrite32(reg, gpio); 1814e1daf0ecSJonathan Lemon 1815e1daf0ecSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1816e1daf0ecSJonathan Lemon } 1817e1daf0ecSJonathan Lemon 1818*a509a7c6SJonathan Lemon static int 1819*a509a7c6SJonathan Lemon ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr) 1820e1daf0ecSJonathan Lemon { 1821*a509a7c6SJonathan Lemon struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1]; 1822e1daf0ecSJonathan Lemon enum ptp_ocp_sma_mode mode; 1823e1daf0ecSJonathan Lemon int val; 1824e1daf0ecSJonathan Lemon 1825e1daf0ecSJonathan Lemon mode = sma->mode; 1826e1daf0ecSJonathan Lemon val = sma_parse_inputs(buf, &mode); 1827e1daf0ecSJonathan Lemon if (val < 0) 1828e1daf0ecSJonathan Lemon return val; 1829e1daf0ecSJonathan Lemon 1830*a509a7c6SJonathan Lemon if (mode != sma->mode && sma->fixed_dir) 1831e1daf0ecSJonathan Lemon return -EOPNOTSUPP; 1832e1daf0ecSJonathan Lemon 1833*a509a7c6SJonathan Lemon if (sma->fixed_fcn) { 1834*a509a7c6SJonathan Lemon if (val != ((sma_nr - 1) & 1)) 1835e1daf0ecSJonathan Lemon return -EOPNOTSUPP; 1836*a509a7c6SJonathan Lemon return 0; 1837e1daf0ecSJonathan Lemon } 1838e1daf0ecSJonathan Lemon 1839*a509a7c6SJonathan Lemon if (mode != sma->mode) { 1840*a509a7c6SJonathan Lemon if (mode == SMA_MODE_IN) 1841*a509a7c6SJonathan Lemon ptp_ocp_sma_store_output(bp, sma_nr, 0); 1842e1daf0ecSJonathan Lemon else 1843*a509a7c6SJonathan Lemon ptp_ocp_sma_store_inputs(bp, sma_nr, 0); 1844*a509a7c6SJonathan Lemon sma->mode = mode; 1845*a509a7c6SJonathan Lemon } 1846*a509a7c6SJonathan Lemon 1847*a509a7c6SJonathan Lemon if (!sma->fixed_dir) 1848*a509a7c6SJonathan Lemon val |= SMA_ENABLE; /* add enable bit */ 1849*a509a7c6SJonathan Lemon 1850*a509a7c6SJonathan Lemon if (mode == SMA_MODE_IN) 1851*a509a7c6SJonathan Lemon ptp_ocp_sma_store_inputs(bp, sma_nr, val); 1852*a509a7c6SJonathan Lemon else 1853*a509a7c6SJonathan Lemon ptp_ocp_sma_store_output(bp, sma_nr, val); 1854e1daf0ecSJonathan Lemon 1855e1daf0ecSJonathan Lemon return 0; 1856e1daf0ecSJonathan Lemon } 1857e1daf0ecSJonathan Lemon 1858e1daf0ecSJonathan Lemon static ssize_t 1859e1daf0ecSJonathan Lemon sma1_store(struct device *dev, struct device_attribute *attr, 1860e1daf0ecSJonathan Lemon const char *buf, size_t count) 1861e1daf0ecSJonathan Lemon { 1862e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1863e1daf0ecSJonathan Lemon int err; 1864e1daf0ecSJonathan Lemon 1865*a509a7c6SJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 1); 1866e1daf0ecSJonathan Lemon return err ? err : count; 1867e1daf0ecSJonathan Lemon } 1868e1daf0ecSJonathan Lemon 1869e1daf0ecSJonathan Lemon static ssize_t 1870e1daf0ecSJonathan Lemon sma2_store(struct device *dev, struct device_attribute *attr, 1871e1daf0ecSJonathan Lemon const char *buf, size_t count) 1872e1daf0ecSJonathan Lemon { 1873e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1874e1daf0ecSJonathan Lemon int err; 1875e1daf0ecSJonathan Lemon 1876*a509a7c6SJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 2); 1877e1daf0ecSJonathan Lemon return err ? err : count; 1878e1daf0ecSJonathan Lemon } 1879e1daf0ecSJonathan Lemon 1880e1daf0ecSJonathan Lemon static ssize_t 1881e1daf0ecSJonathan Lemon sma3_store(struct device *dev, struct device_attribute *attr, 1882e1daf0ecSJonathan Lemon const char *buf, size_t count) 1883e1daf0ecSJonathan Lemon { 1884e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1885e1daf0ecSJonathan Lemon int err; 1886e1daf0ecSJonathan Lemon 1887*a509a7c6SJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 3); 1888e1daf0ecSJonathan Lemon return err ? err : count; 1889e1daf0ecSJonathan Lemon } 1890e1daf0ecSJonathan Lemon 1891e1daf0ecSJonathan Lemon static ssize_t 1892e1daf0ecSJonathan Lemon sma4_store(struct device *dev, struct device_attribute *attr, 1893e1daf0ecSJonathan Lemon const char *buf, size_t count) 1894e1daf0ecSJonathan Lemon { 1895e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1896e1daf0ecSJonathan Lemon int err; 1897e1daf0ecSJonathan Lemon 1898*a509a7c6SJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 4); 1899e1daf0ecSJonathan Lemon return err ? err : count; 1900e1daf0ecSJonathan Lemon } 1901e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma1); 1902e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma2); 1903e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma3); 1904e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma4); 1905e1daf0ecSJonathan Lemon 1906e1daf0ecSJonathan Lemon static ssize_t 1907e1daf0ecSJonathan Lemon available_sma_inputs_show(struct device *dev, 1908e1daf0ecSJonathan Lemon struct device_attribute *attr, char *buf) 1909e1daf0ecSJonathan Lemon { 1910e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_sma_in, buf); 1911e1daf0ecSJonathan Lemon } 1912e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_inputs); 1913e1daf0ecSJonathan Lemon 1914e1daf0ecSJonathan Lemon static ssize_t 1915e1daf0ecSJonathan Lemon available_sma_outputs_show(struct device *dev, 1916e1daf0ecSJonathan Lemon struct device_attribute *attr, char *buf) 1917e1daf0ecSJonathan Lemon { 1918e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_sma_out, buf); 1919e1daf0ecSJonathan Lemon } 1920e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_outputs); 1921e1daf0ecSJonathan Lemon 1922773bda96SJonathan Lemon static ssize_t 1923773bda96SJonathan Lemon serialnum_show(struct device *dev, struct device_attribute *attr, char *buf) 1924773bda96SJonathan Lemon { 1925773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1926773bda96SJonathan Lemon 19270cfcdd1eSJonathan Lemon if (!bp->has_eeprom_data) 19280cfcdd1eSJonathan Lemon ptp_ocp_read_eeprom(bp); 1929773bda96SJonathan Lemon 1930773bda96SJonathan Lemon return sysfs_emit(buf, "%pM\n", bp->serial); 1931773bda96SJonathan Lemon } 1932773bda96SJonathan Lemon static DEVICE_ATTR_RO(serialnum); 1933773bda96SJonathan Lemon 1934773bda96SJonathan Lemon static ssize_t 1935ef0cfb34SJonathan Lemon gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf) 1936773bda96SJonathan Lemon { 1937773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1938773bda96SJonathan Lemon ssize_t ret; 1939773bda96SJonathan Lemon 1940ef0cfb34SJonathan Lemon if (bp->gnss_lost) 1941ef0cfb34SJonathan Lemon ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost); 1942773bda96SJonathan Lemon else 1943773bda96SJonathan Lemon ret = sysfs_emit(buf, "SYNC\n"); 1944773bda96SJonathan Lemon 1945773bda96SJonathan Lemon return ret; 1946773bda96SJonathan Lemon } 1947ef0cfb34SJonathan Lemon static DEVICE_ATTR_RO(gnss_sync); 1948773bda96SJonathan Lemon 1949773bda96SJonathan Lemon static ssize_t 195089260d87SJonathan Lemon utc_tai_offset_show(struct device *dev, 195189260d87SJonathan Lemon struct device_attribute *attr, char *buf) 195289260d87SJonathan Lemon { 195389260d87SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 195489260d87SJonathan Lemon 195589260d87SJonathan Lemon return sysfs_emit(buf, "%d\n", bp->utc_tai_offset); 195689260d87SJonathan Lemon } 195789260d87SJonathan Lemon 195889260d87SJonathan Lemon static ssize_t 195989260d87SJonathan Lemon utc_tai_offset_store(struct device *dev, 196089260d87SJonathan Lemon struct device_attribute *attr, 196189260d87SJonathan Lemon const char *buf, size_t count) 196289260d87SJonathan Lemon { 196389260d87SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 196489260d87SJonathan Lemon int err; 196589260d87SJonathan Lemon u32 val; 196689260d87SJonathan Lemon 196789260d87SJonathan Lemon err = kstrtou32(buf, 0, &val); 196889260d87SJonathan Lemon if (err) 196989260d87SJonathan Lemon return err; 197089260d87SJonathan Lemon 197189260d87SJonathan Lemon ptp_ocp_utc_distribute(bp, val); 197289260d87SJonathan Lemon 197389260d87SJonathan Lemon return count; 197489260d87SJonathan Lemon } 197589260d87SJonathan Lemon static DEVICE_ATTR_RW(utc_tai_offset); 197689260d87SJonathan Lemon 197789260d87SJonathan Lemon static ssize_t 19781acffc6eSJonathan Lemon ts_window_adjust_show(struct device *dev, 19791acffc6eSJonathan Lemon struct device_attribute *attr, char *buf) 19801acffc6eSJonathan Lemon { 19811acffc6eSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 19821acffc6eSJonathan Lemon 19831acffc6eSJonathan Lemon return sysfs_emit(buf, "%d\n", bp->ts_window_adjust); 19841acffc6eSJonathan Lemon } 19851acffc6eSJonathan Lemon 19861acffc6eSJonathan Lemon static ssize_t 19871acffc6eSJonathan Lemon ts_window_adjust_store(struct device *dev, 19881acffc6eSJonathan Lemon struct device_attribute *attr, 19891acffc6eSJonathan Lemon const char *buf, size_t count) 19901acffc6eSJonathan Lemon { 19911acffc6eSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 19921acffc6eSJonathan Lemon int err; 19931acffc6eSJonathan Lemon u32 val; 19941acffc6eSJonathan Lemon 19951acffc6eSJonathan Lemon err = kstrtou32(buf, 0, &val); 19961acffc6eSJonathan Lemon if (err) 19971acffc6eSJonathan Lemon return err; 19981acffc6eSJonathan Lemon 19991acffc6eSJonathan Lemon bp->ts_window_adjust = val; 20001acffc6eSJonathan Lemon 20011acffc6eSJonathan Lemon return count; 20021acffc6eSJonathan Lemon } 20031acffc6eSJonathan Lemon static DEVICE_ATTR_RW(ts_window_adjust); 20041acffc6eSJonathan Lemon 20051acffc6eSJonathan Lemon static ssize_t 2006d14ee252SJonathan Lemon irig_b_mode_show(struct device *dev, struct device_attribute *attr, char *buf) 2007d14ee252SJonathan Lemon { 2008d14ee252SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2009d14ee252SJonathan Lemon u32 val; 2010d14ee252SJonathan Lemon 2011d14ee252SJonathan Lemon val = ioread32(&bp->irig_out->ctrl); 2012d14ee252SJonathan Lemon val = (val >> 16) & 0x07; 2013d14ee252SJonathan Lemon return sysfs_emit(buf, "%d\n", val); 2014d14ee252SJonathan Lemon } 2015d14ee252SJonathan Lemon 2016d14ee252SJonathan Lemon static ssize_t 2017d14ee252SJonathan Lemon irig_b_mode_store(struct device *dev, 2018d14ee252SJonathan Lemon struct device_attribute *attr, 2019d14ee252SJonathan Lemon const char *buf, size_t count) 2020d14ee252SJonathan Lemon { 2021d14ee252SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2022d14ee252SJonathan Lemon unsigned long flags; 2023d14ee252SJonathan Lemon int err; 2024d14ee252SJonathan Lemon u32 reg; 2025d14ee252SJonathan Lemon u8 val; 2026d14ee252SJonathan Lemon 2027d14ee252SJonathan Lemon err = kstrtou8(buf, 0, &val); 2028d14ee252SJonathan Lemon if (err) 2029d14ee252SJonathan Lemon return err; 2030d14ee252SJonathan Lemon if (val > 7) 2031d14ee252SJonathan Lemon return -EINVAL; 2032d14ee252SJonathan Lemon 2033d14ee252SJonathan Lemon reg = ((val & 0x7) << 16); 2034d14ee252SJonathan Lemon 2035d14ee252SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 2036d14ee252SJonathan Lemon iowrite32(0, &bp->irig_out->ctrl); /* disable */ 2037d14ee252SJonathan Lemon iowrite32(reg, &bp->irig_out->ctrl); /* change mode */ 2038d14ee252SJonathan Lemon iowrite32(reg | IRIG_M_CTRL_ENABLE, &bp->irig_out->ctrl); 2039d14ee252SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 2040d14ee252SJonathan Lemon 2041d14ee252SJonathan Lemon return count; 2042d14ee252SJonathan Lemon } 2043d14ee252SJonathan Lemon static DEVICE_ATTR_RW(irig_b_mode); 2044d14ee252SJonathan Lemon 2045d14ee252SJonathan Lemon static ssize_t 2046773bda96SJonathan Lemon clock_source_show(struct device *dev, struct device_attribute *attr, char *buf) 2047773bda96SJonathan Lemon { 2048773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2049773bda96SJonathan Lemon const char *p; 2050773bda96SJonathan Lemon u32 select; 2051773bda96SJonathan Lemon 2052773bda96SJonathan Lemon select = ioread32(&bp->reg->select); 2053e1daf0ecSJonathan Lemon p = ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16); 2054773bda96SJonathan Lemon 2055773bda96SJonathan Lemon return sysfs_emit(buf, "%s\n", p); 2056773bda96SJonathan Lemon } 2057773bda96SJonathan Lemon 2058773bda96SJonathan Lemon static ssize_t 2059773bda96SJonathan Lemon clock_source_store(struct device *dev, struct device_attribute *attr, 2060773bda96SJonathan Lemon const char *buf, size_t count) 2061773bda96SJonathan Lemon { 2062773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2063773bda96SJonathan Lemon unsigned long flags; 2064773bda96SJonathan Lemon int val; 2065773bda96SJonathan Lemon 2066e1daf0ecSJonathan Lemon val = ptp_ocp_select_val_from_name(ptp_ocp_clock, buf); 2067773bda96SJonathan Lemon if (val < 0) 2068773bda96SJonathan Lemon return val; 2069773bda96SJonathan Lemon 2070773bda96SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 2071773bda96SJonathan Lemon iowrite32(val, &bp->reg->select); 2072773bda96SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 2073773bda96SJonathan Lemon 2074773bda96SJonathan Lemon return count; 2075773bda96SJonathan Lemon } 2076773bda96SJonathan Lemon static DEVICE_ATTR_RW(clock_source); 2077773bda96SJonathan Lemon 2078773bda96SJonathan Lemon static ssize_t 2079773bda96SJonathan Lemon available_clock_sources_show(struct device *dev, 2080773bda96SJonathan Lemon struct device_attribute *attr, char *buf) 2081773bda96SJonathan Lemon { 2082e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_clock, buf); 2083773bda96SJonathan Lemon } 2084773bda96SJonathan Lemon static DEVICE_ATTR_RO(available_clock_sources); 2085773bda96SJonathan Lemon 20862f23f486SVadim Fedorenko static ssize_t 20872f23f486SVadim Fedorenko clock_status_drift_show(struct device *dev, 20882f23f486SVadim Fedorenko struct device_attribute *attr, char *buf) 20892f23f486SVadim Fedorenko { 20902f23f486SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 20912f23f486SVadim Fedorenko u32 val; 20922f23f486SVadim Fedorenko int res; 20932f23f486SVadim Fedorenko 20942f23f486SVadim Fedorenko val = ioread32(&bp->reg->status_drift); 20952f23f486SVadim Fedorenko res = (val & ~INT_MAX) ? -1 : 1; 20962f23f486SVadim Fedorenko res *= (val & INT_MAX); 20972f23f486SVadim Fedorenko return sysfs_emit(buf, "%d\n", res); 20982f23f486SVadim Fedorenko } 20992f23f486SVadim Fedorenko static DEVICE_ATTR_RO(clock_status_drift); 21002f23f486SVadim Fedorenko 21012f23f486SVadim Fedorenko static ssize_t 21022f23f486SVadim Fedorenko clock_status_offset_show(struct device *dev, 21032f23f486SVadim Fedorenko struct device_attribute *attr, char *buf) 21042f23f486SVadim Fedorenko { 21052f23f486SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 21062f23f486SVadim Fedorenko u32 val; 21072f23f486SVadim Fedorenko int res; 21082f23f486SVadim Fedorenko 21092f23f486SVadim Fedorenko val = ioread32(&bp->reg->status_offset); 21102f23f486SVadim Fedorenko res = (val & ~INT_MAX) ? -1 : 1; 21112f23f486SVadim Fedorenko res *= (val & INT_MAX); 21122f23f486SVadim Fedorenko return sysfs_emit(buf, "%d\n", res); 21132f23f486SVadim Fedorenko } 21142f23f486SVadim Fedorenko static DEVICE_ATTR_RO(clock_status_offset); 21152f23f486SVadim Fedorenko 211644a412d1SVadim Fedorenko static ssize_t 211744a412d1SVadim Fedorenko tod_correction_show(struct device *dev, 211844a412d1SVadim Fedorenko struct device_attribute *attr, char *buf) 211944a412d1SVadim Fedorenko { 212044a412d1SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 212144a412d1SVadim Fedorenko u32 val; 212244a412d1SVadim Fedorenko int res; 212344a412d1SVadim Fedorenko 212444a412d1SVadim Fedorenko val = ioread32(&bp->tod->adj_sec); 212544a412d1SVadim Fedorenko res = (val & ~INT_MAX) ? -1 : 1; 212644a412d1SVadim Fedorenko res *= (val & INT_MAX); 212744a412d1SVadim Fedorenko return sysfs_emit(buf, "%d\n", res); 212844a412d1SVadim Fedorenko } 212944a412d1SVadim Fedorenko 213044a412d1SVadim Fedorenko static ssize_t 213144a412d1SVadim Fedorenko tod_correction_store(struct device *dev, struct device_attribute *attr, 213244a412d1SVadim Fedorenko const char *buf, size_t count) 213344a412d1SVadim Fedorenko { 213444a412d1SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 213544a412d1SVadim Fedorenko unsigned long flags; 213644a412d1SVadim Fedorenko int err, res; 213744a412d1SVadim Fedorenko u32 val = 0; 213844a412d1SVadim Fedorenko 213944a412d1SVadim Fedorenko err = kstrtos32(buf, 0, &res); 214044a412d1SVadim Fedorenko if (err) 214144a412d1SVadim Fedorenko return err; 214244a412d1SVadim Fedorenko if (res < 0) { 214344a412d1SVadim Fedorenko res *= -1; 214444a412d1SVadim Fedorenko val |= BIT(31); 214544a412d1SVadim Fedorenko } 214644a412d1SVadim Fedorenko val |= res; 214744a412d1SVadim Fedorenko 214844a412d1SVadim Fedorenko spin_lock_irqsave(&bp->lock, flags); 214944a412d1SVadim Fedorenko iowrite32(val, &bp->tod->adj_sec); 215044a412d1SVadim Fedorenko spin_unlock_irqrestore(&bp->lock, flags); 215144a412d1SVadim Fedorenko 215244a412d1SVadim Fedorenko return count; 215344a412d1SVadim Fedorenko } 215444a412d1SVadim Fedorenko static DEVICE_ATTR_RW(tod_correction); 215544a412d1SVadim Fedorenko 2156773bda96SJonathan Lemon static struct attribute *timecard_attrs[] = { 2157773bda96SJonathan Lemon &dev_attr_serialnum.attr, 2158ef0cfb34SJonathan Lemon &dev_attr_gnss_sync.attr, 2159773bda96SJonathan Lemon &dev_attr_clock_source.attr, 2160773bda96SJonathan Lemon &dev_attr_available_clock_sources.attr, 2161e1daf0ecSJonathan Lemon &dev_attr_sma1.attr, 2162e1daf0ecSJonathan Lemon &dev_attr_sma2.attr, 2163e1daf0ecSJonathan Lemon &dev_attr_sma3.attr, 2164e1daf0ecSJonathan Lemon &dev_attr_sma4.attr, 2165e1daf0ecSJonathan Lemon &dev_attr_available_sma_inputs.attr, 2166e1daf0ecSJonathan Lemon &dev_attr_available_sma_outputs.attr, 21672f23f486SVadim Fedorenko &dev_attr_clock_status_drift.attr, 21682f23f486SVadim Fedorenko &dev_attr_clock_status_offset.attr, 2169d14ee252SJonathan Lemon &dev_attr_irig_b_mode.attr, 217089260d87SJonathan Lemon &dev_attr_utc_tai_offset.attr, 21711acffc6eSJonathan Lemon &dev_attr_ts_window_adjust.attr, 217244a412d1SVadim Fedorenko &dev_attr_tod_correction.attr, 2173773bda96SJonathan Lemon NULL, 2174773bda96SJonathan Lemon }; 2175773bda96SJonathan Lemon ATTRIBUTE_GROUPS(timecard); 2176773bda96SJonathan Lemon 2177*a509a7c6SJonathan Lemon static void 2178*a509a7c6SJonathan Lemon gpio_input_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit, 2179*a509a7c6SJonathan Lemon const char *def) 2180f67bf662SJonathan Lemon { 2181*a509a7c6SJonathan Lemon int i; 2182f67bf662SJonathan Lemon 2183*a509a7c6SJonathan Lemon for (i = 0; i < 4; i++) { 2184*a509a7c6SJonathan Lemon if (bp->sma[i].mode != SMA_MODE_IN) 2185*a509a7c6SJonathan Lemon continue; 2186*a509a7c6SJonathan Lemon if (map[i][0] & (1 << bit)) { 2187*a509a7c6SJonathan Lemon sprintf(buf, "sma%d", i + 1); 2188*a509a7c6SJonathan Lemon return; 2189*a509a7c6SJonathan Lemon } 2190*a509a7c6SJonathan Lemon } 2191*a509a7c6SJonathan Lemon if (!def) 2192*a509a7c6SJonathan Lemon def = "----"; 2193*a509a7c6SJonathan Lemon strcpy(buf, def); 2194f67bf662SJonathan Lemon } 2195f67bf662SJonathan Lemon 2196f67bf662SJonathan Lemon static void 2197*a509a7c6SJonathan Lemon gpio_output_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit) 2198f67bf662SJonathan Lemon { 2199f67bf662SJonathan Lemon char *ans = buf; 2200*a509a7c6SJonathan Lemon int i; 2201f67bf662SJonathan Lemon 2202*a509a7c6SJonathan Lemon strcpy(ans, "----"); 2203*a509a7c6SJonathan Lemon for (i = 0; i < 4; i++) { 2204*a509a7c6SJonathan Lemon if (bp->sma[i].mode != SMA_MODE_OUT) 2205*a509a7c6SJonathan Lemon continue; 2206*a509a7c6SJonathan Lemon if (map[i][1] & (1 << bit)) 2207*a509a7c6SJonathan Lemon ans += sprintf(ans, "sma%d ", i + 1); 2208*a509a7c6SJonathan Lemon } 2209f67bf662SJonathan Lemon } 2210f67bf662SJonathan Lemon 2211f67bf662SJonathan Lemon static int 2212f67bf662SJonathan Lemon ptp_ocp_summary_show(struct seq_file *s, void *data) 2213f67bf662SJonathan Lemon { 2214f67bf662SJonathan Lemon struct device *dev = s->private; 2215f67bf662SJonathan Lemon struct ptp_system_timestamp sts; 2216*a509a7c6SJonathan Lemon u16 sma_val[4][2], ctrl, val; 2217f67bf662SJonathan Lemon struct ts_reg __iomem *ts_reg; 2218f67bf662SJonathan Lemon struct timespec64 ts; 2219f67bf662SJonathan Lemon struct ptp_ocp *bp; 2220*a509a7c6SJonathan Lemon char *src, *buf; 2221a62a56d0SJonathan Lemon bool on, map; 2222f67bf662SJonathan Lemon 2223f67bf662SJonathan Lemon buf = (char *)__get_free_page(GFP_KERNEL); 2224f67bf662SJonathan Lemon if (!buf) 2225f67bf662SJonathan Lemon return -ENOMEM; 2226f67bf662SJonathan Lemon 2227f67bf662SJonathan Lemon bp = dev_get_drvdata(dev); 2228f67bf662SJonathan Lemon 2229f67bf662SJonathan Lemon seq_printf(s, "%7s: /dev/ptp%d\n", "PTP", ptp_clock_index(bp->ptp)); 223061fd7ac2SJonathan Lemon if (bp->gnss_port != -1) 223161fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS1", bp->gnss_port); 223261fd7ac2SJonathan Lemon if (bp->gnss2_port != -1) 223361fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS2", bp->gnss2_port); 223461fd7ac2SJonathan Lemon if (bp->mac_port != -1) 223561fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "MAC", bp->mac_port); 223661fd7ac2SJonathan Lemon if (bp->nmea_port != -1) 223761fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "NMEA", bp->nmea_port); 2238f67bf662SJonathan Lemon 2239*a509a7c6SJonathan Lemon memset(sma_val, 0xff, sizeof(sma_val)); 2240*a509a7c6SJonathan Lemon if (bp->sma_map1) { 2241*a509a7c6SJonathan Lemon u32 reg; 2242*a509a7c6SJonathan Lemon 2243*a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map1->gpio1); 2244*a509a7c6SJonathan Lemon sma_val[0][0] = reg & 0xffff; 2245*a509a7c6SJonathan Lemon sma_val[1][0] = reg >> 16; 2246*a509a7c6SJonathan Lemon 2247*a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map1->gpio2); 2248*a509a7c6SJonathan Lemon sma_val[2][1] = reg & 0xffff; 2249*a509a7c6SJonathan Lemon sma_val[3][1] = reg >> 16; 2250*a509a7c6SJonathan Lemon 2251*a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map2->gpio1); 2252*a509a7c6SJonathan Lemon sma_val[2][0] = reg & 0xffff; 2253*a509a7c6SJonathan Lemon sma_val[3][0] = reg >> 16; 2254*a509a7c6SJonathan Lemon 2255*a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map2->gpio2); 2256*a509a7c6SJonathan Lemon sma_val[0][1] = reg & 0xffff; 2257*a509a7c6SJonathan Lemon sma_val[1][1] = reg >> 16; 2258*a509a7c6SJonathan Lemon } 2259*a509a7c6SJonathan Lemon 2260f67bf662SJonathan Lemon sma1_show(dev, NULL, buf); 2261*a509a7c6SJonathan Lemon seq_printf(s, " sma1: %04x,%04x %s", 2262*a509a7c6SJonathan Lemon sma_val[0][0], sma_val[0][1], buf); 2263f67bf662SJonathan Lemon 2264f67bf662SJonathan Lemon sma2_show(dev, NULL, buf); 2265*a509a7c6SJonathan Lemon seq_printf(s, " sma2: %04x,%04x %s", 2266*a509a7c6SJonathan Lemon sma_val[1][0], sma_val[1][1], buf); 2267f67bf662SJonathan Lemon 2268f67bf662SJonathan Lemon sma3_show(dev, NULL, buf); 2269*a509a7c6SJonathan Lemon seq_printf(s, " sma3: %04x,%04x %s", 2270*a509a7c6SJonathan Lemon sma_val[2][0], sma_val[2][1], buf); 2271f67bf662SJonathan Lemon 2272f67bf662SJonathan Lemon sma4_show(dev, NULL, buf); 2273*a509a7c6SJonathan Lemon seq_printf(s, " sma4: %04x,%04x %s", 2274*a509a7c6SJonathan Lemon sma_val[3][0], sma_val[3][1], buf); 2275f67bf662SJonathan Lemon 2276f67bf662SJonathan Lemon if (bp->ts0) { 2277f67bf662SJonathan Lemon ts_reg = bp->ts0->mem; 2278f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 2279f67bf662SJonathan Lemon src = "GNSS"; 2280f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS0", 2281f67bf662SJonathan Lemon on ? " ON" : "OFF", src); 2282f67bf662SJonathan Lemon } 2283f67bf662SJonathan Lemon 2284f67bf662SJonathan Lemon if (bp->ts1) { 2285f67bf662SJonathan Lemon ts_reg = bp->ts1->mem; 2286f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 2287*a509a7c6SJonathan Lemon gpio_input_map(buf, bp, sma_val, 2, NULL); 2288f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS1", 2289*a509a7c6SJonathan Lemon on ? " ON" : "OFF", buf); 2290f67bf662SJonathan Lemon } 2291f67bf662SJonathan Lemon 2292f67bf662SJonathan Lemon if (bp->ts2) { 2293f67bf662SJonathan Lemon ts_reg = bp->ts2->mem; 2294f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 2295*a509a7c6SJonathan Lemon gpio_input_map(buf, bp, sma_val, 3, NULL); 2296f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS2", 2297*a509a7c6SJonathan Lemon on ? " ON" : "OFF", buf); 2298f67bf662SJonathan Lemon } 2299f67bf662SJonathan Lemon 2300a62a56d0SJonathan Lemon if (bp->pps) { 2301a62a56d0SJonathan Lemon ts_reg = bp->pps->mem; 2302a62a56d0SJonathan Lemon src = "PHC"; 2303a62a56d0SJonathan Lemon on = ioread32(&ts_reg->enable); 2304a62a56d0SJonathan Lemon map = !!(bp->pps_req_map & OCP_REQ_TIMESTAMP); 2305a62a56d0SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS3", 23061a575cdeSNathan Chancellor on && map ? " ON" : "OFF", src); 2307a62a56d0SJonathan Lemon 2308a62a56d0SJonathan Lemon map = !!(bp->pps_req_map & OCP_REQ_PPS); 2309a62a56d0SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "PPS", 23101a575cdeSNathan Chancellor on && map ? " ON" : "OFF", src); 2311a62a56d0SJonathan Lemon } 2312a62a56d0SJonathan Lemon 2313f67bf662SJonathan Lemon if (bp->irig_out) { 2314f67bf662SJonathan Lemon ctrl = ioread32(&bp->irig_out->ctrl); 2315f67bf662SJonathan Lemon on = ctrl & IRIG_M_CTRL_ENABLE; 2316f67bf662SJonathan Lemon val = ioread32(&bp->irig_out->status); 2317*a509a7c6SJonathan Lemon gpio_output_map(buf, bp, sma_val, 4); 2318f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, mode %d, out: %s\n", "IRIG", 2319f67bf662SJonathan Lemon on ? " ON" : "OFF", val, (ctrl >> 16), buf); 2320f67bf662SJonathan Lemon } 2321f67bf662SJonathan Lemon 2322f67bf662SJonathan Lemon if (bp->irig_in) { 2323f67bf662SJonathan Lemon on = ioread32(&bp->irig_in->ctrl) & IRIG_S_CTRL_ENABLE; 2324f67bf662SJonathan Lemon val = ioread32(&bp->irig_in->status); 2325*a509a7c6SJonathan Lemon gpio_input_map(buf, bp, sma_val, 4, NULL); 2326f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, src: %s\n", "IRIG in", 2327*a509a7c6SJonathan Lemon on ? " ON" : "OFF", val, buf); 2328f67bf662SJonathan Lemon } 2329f67bf662SJonathan Lemon 2330f67bf662SJonathan Lemon if (bp->dcf_out) { 2331f67bf662SJonathan Lemon on = ioread32(&bp->dcf_out->ctrl) & DCF_M_CTRL_ENABLE; 2332f67bf662SJonathan Lemon val = ioread32(&bp->dcf_out->status); 2333*a509a7c6SJonathan Lemon gpio_output_map(buf, bp, sma_val, 5); 2334f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, out: %s\n", "DCF", 2335f67bf662SJonathan Lemon on ? " ON" : "OFF", val, buf); 2336f67bf662SJonathan Lemon } 2337f67bf662SJonathan Lemon 2338f67bf662SJonathan Lemon if (bp->dcf_in) { 2339f67bf662SJonathan Lemon on = ioread32(&bp->dcf_in->ctrl) & DCF_S_CTRL_ENABLE; 2340f67bf662SJonathan Lemon val = ioread32(&bp->dcf_in->status); 2341*a509a7c6SJonathan Lemon gpio_input_map(buf, bp, sma_val, 5, NULL); 2342f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, src: %s\n", "DCF in", 2343*a509a7c6SJonathan Lemon on ? " ON" : "OFF", val, buf); 2344f67bf662SJonathan Lemon } 2345f67bf662SJonathan Lemon 2346e3516bb4SJonathan Lemon if (bp->nmea_out) { 2347e3516bb4SJonathan Lemon on = ioread32(&bp->nmea_out->ctrl) & 1; 2348e3516bb4SJonathan Lemon val = ioread32(&bp->nmea_out->status); 2349e3516bb4SJonathan Lemon seq_printf(s, "%7s: %s, error: %d\n", "NMEA", 2350e3516bb4SJonathan Lemon on ? " ON" : "OFF", val); 2351e3516bb4SJonathan Lemon } 2352e3516bb4SJonathan Lemon 2353f67bf662SJonathan Lemon /* compute src for PPS1, used below. */ 2354f67bf662SJonathan Lemon if (bp->pps_select) { 2355f67bf662SJonathan Lemon val = ioread32(&bp->pps_select->gpio1); 2356*a509a7c6SJonathan Lemon src = &buf[80]; 2357f67bf662SJonathan Lemon if (val & 0x01) 2358*a509a7c6SJonathan Lemon gpio_input_map(src, bp, sma_val, 0, NULL); 2359f67bf662SJonathan Lemon else if (val & 0x02) 2360f67bf662SJonathan Lemon src = "MAC"; 2361f67bf662SJonathan Lemon else if (val & 0x04) 2362f67bf662SJonathan Lemon src = "GNSS"; 2363f67bf662SJonathan Lemon else 2364f67bf662SJonathan Lemon src = "----"; 2365f67bf662SJonathan Lemon } else { 2366f67bf662SJonathan Lemon src = "?"; 2367f67bf662SJonathan Lemon } 2368f67bf662SJonathan Lemon 2369f67bf662SJonathan Lemon /* assumes automatic switchover/selection */ 2370f67bf662SJonathan Lemon val = ioread32(&bp->reg->select); 2371f67bf662SJonathan Lemon switch (val >> 16) { 2372f67bf662SJonathan Lemon case 0: 2373f67bf662SJonathan Lemon sprintf(buf, "----"); 2374f67bf662SJonathan Lemon break; 2375f67bf662SJonathan Lemon case 2: 2376f67bf662SJonathan Lemon sprintf(buf, "IRIG"); 2377f67bf662SJonathan Lemon break; 2378f67bf662SJonathan Lemon case 3: 2379f67bf662SJonathan Lemon sprintf(buf, "%s via PPS1", src); 2380f67bf662SJonathan Lemon break; 2381f67bf662SJonathan Lemon case 6: 2382f67bf662SJonathan Lemon sprintf(buf, "DCF"); 2383f67bf662SJonathan Lemon break; 2384f67bf662SJonathan Lemon default: 2385f67bf662SJonathan Lemon strcpy(buf, "unknown"); 2386f67bf662SJonathan Lemon break; 2387f67bf662SJonathan Lemon } 2388f67bf662SJonathan Lemon val = ioread32(&bp->reg->status); 2389f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, state: %s\n", "PHC src", buf, 2390f67bf662SJonathan Lemon val & OCP_STATUS_IN_SYNC ? "sync" : "unsynced"); 2391f67bf662SJonathan Lemon 2392f67bf662SJonathan Lemon /* reuses PPS1 src from earlier */ 2393f67bf662SJonathan Lemon seq_printf(s, "MAC PPS1 src: %s\n", src); 2394f67bf662SJonathan Lemon 2395*a509a7c6SJonathan Lemon gpio_input_map(buf, bp, sma_val, 1, "GNSS2"); 2396*a509a7c6SJonathan Lemon seq_printf(s, "MAC PPS2 src: %s\n", buf); 2397f67bf662SJonathan Lemon 2398f67bf662SJonathan Lemon if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts)) { 2399f67bf662SJonathan Lemon struct timespec64 sys_ts; 2400f67bf662SJonathan Lemon s64 pre_ns, post_ns, ns; 2401f67bf662SJonathan Lemon 2402f67bf662SJonathan Lemon pre_ns = timespec64_to_ns(&sts.pre_ts); 2403f67bf662SJonathan Lemon post_ns = timespec64_to_ns(&sts.post_ts); 2404f67bf662SJonathan Lemon ns = (pre_ns + post_ns) / 2; 2405f67bf662SJonathan Lemon ns += (s64)bp->utc_tai_offset * NSEC_PER_SEC; 2406f67bf662SJonathan Lemon sys_ts = ns_to_timespec64(ns); 2407f67bf662SJonathan Lemon 2408f67bf662SJonathan Lemon seq_printf(s, "%7s: %lld.%ld == %ptT TAI\n", "PHC", 2409f67bf662SJonathan Lemon ts.tv_sec, ts.tv_nsec, &ts); 2410f67bf662SJonathan Lemon seq_printf(s, "%7s: %lld.%ld == %ptT UTC offset %d\n", "SYS", 2411f67bf662SJonathan Lemon sys_ts.tv_sec, sys_ts.tv_nsec, &sys_ts, 2412f67bf662SJonathan Lemon bp->utc_tai_offset); 2413f67bf662SJonathan Lemon seq_printf(s, "%7s: PHC:SYS offset: %lld window: %lld\n", "", 2414f67bf662SJonathan Lemon timespec64_to_ns(&ts) - ns, 2415f67bf662SJonathan Lemon post_ns - pre_ns); 2416f67bf662SJonathan Lemon } 2417f67bf662SJonathan Lemon 2418f67bf662SJonathan Lemon free_page((unsigned long)buf); 2419f67bf662SJonathan Lemon return 0; 2420f67bf662SJonathan Lemon } 2421f67bf662SJonathan Lemon DEFINE_SHOW_ATTRIBUTE(ptp_ocp_summary); 2422f67bf662SJonathan Lemon 24239f492c4cSVadim Fedorenko static int 24249f492c4cSVadim Fedorenko ptp_ocp_tod_status_show(struct seq_file *s, void *data) 24259f492c4cSVadim Fedorenko { 24269f492c4cSVadim Fedorenko struct device *dev = s->private; 24279f492c4cSVadim Fedorenko struct ptp_ocp *bp; 24289f492c4cSVadim Fedorenko u32 val; 24299f492c4cSVadim Fedorenko int idx; 24309f492c4cSVadim Fedorenko 24319f492c4cSVadim Fedorenko bp = dev_get_drvdata(dev); 24329f492c4cSVadim Fedorenko 24339f492c4cSVadim Fedorenko val = ioread32(&bp->tod->ctrl); 24349f492c4cSVadim Fedorenko if (!(val & TOD_CTRL_ENABLE)) { 24359f492c4cSVadim Fedorenko seq_printf(s, "TOD Slave disabled\n"); 24369f492c4cSVadim Fedorenko return 0; 24379f492c4cSVadim Fedorenko } 24389f492c4cSVadim Fedorenko seq_printf(s, "TOD Slave enabled, Control Register 0x%08X\n", val); 24399f492c4cSVadim Fedorenko 24409f492c4cSVadim Fedorenko idx = val & TOD_CTRL_PROTOCOL ? 4 : 0; 24419f492c4cSVadim Fedorenko idx += (val >> 16) & 3; 24429f492c4cSVadim Fedorenko seq_printf(s, "Protocol %s\n", ptp_ocp_tod_proto_name(idx)); 24439f492c4cSVadim Fedorenko 24449f492c4cSVadim Fedorenko idx = (val >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK; 24459f492c4cSVadim Fedorenko seq_printf(s, "GNSS %s\n", ptp_ocp_tod_gnss_name(idx)); 24469f492c4cSVadim Fedorenko 24479f492c4cSVadim Fedorenko val = ioread32(&bp->tod->version); 24489f492c4cSVadim Fedorenko seq_printf(s, "TOD Version %d.%d.%d\n", 24499f492c4cSVadim Fedorenko val >> 24, (val >> 16) & 0xff, val & 0xffff); 24509f492c4cSVadim Fedorenko 24519f492c4cSVadim Fedorenko val = ioread32(&bp->tod->status); 24529f492c4cSVadim Fedorenko seq_printf(s, "Status register: 0x%08X\n", val); 24539f492c4cSVadim Fedorenko 24549f492c4cSVadim Fedorenko val = ioread32(&bp->tod->adj_sec); 24559f492c4cSVadim Fedorenko idx = (val & ~INT_MAX) ? -1 : 1; 24569f492c4cSVadim Fedorenko idx *= (val & INT_MAX); 24579f492c4cSVadim Fedorenko seq_printf(s, "Correction seconds: %d\n", idx); 24589f492c4cSVadim Fedorenko 24599f492c4cSVadim Fedorenko val = ioread32(&bp->tod->utc_status); 24609f492c4cSVadim Fedorenko seq_printf(s, "UTC status register: 0x%08X\n", val); 24619f492c4cSVadim Fedorenko seq_printf(s, "UTC offset: %d valid:%d\n", 24629f492c4cSVadim Fedorenko val & TOD_STATUS_UTC_MASK, val & TOD_STATUS_UTC_VALID ? 1 : 0); 24639f492c4cSVadim Fedorenko seq_printf(s, "Leap second info valid:%d, Leap second announce %d\n", 24649f492c4cSVadim Fedorenko val & TOD_STATUS_LEAP_VALID ? 1 : 0, 24659f492c4cSVadim Fedorenko val & TOD_STATUS_LEAP_ANNOUNCE ? 1 : 0); 24669f492c4cSVadim Fedorenko 24679f492c4cSVadim Fedorenko val = ioread32(&bp->tod->leap); 24689f492c4cSVadim Fedorenko seq_printf(s, "Time to next leap second (in sec): %d\n", (s32) val); 24699f492c4cSVadim Fedorenko 24709f492c4cSVadim Fedorenko return 0; 24719f492c4cSVadim Fedorenko } 24729f492c4cSVadim Fedorenko DEFINE_SHOW_ATTRIBUTE(ptp_ocp_tod_status); 24739f492c4cSVadim Fedorenko 2474f67bf662SJonathan Lemon static struct dentry *ptp_ocp_debugfs_root; 2475f67bf662SJonathan Lemon 2476f67bf662SJonathan Lemon static void 2477f67bf662SJonathan Lemon ptp_ocp_debugfs_add_device(struct ptp_ocp *bp) 2478f67bf662SJonathan Lemon { 2479f67bf662SJonathan Lemon struct dentry *d; 2480f67bf662SJonathan Lemon 2481f67bf662SJonathan Lemon d = debugfs_create_dir(dev_name(&bp->dev), ptp_ocp_debugfs_root); 2482f67bf662SJonathan Lemon bp->debug_root = d; 2483f67bf662SJonathan Lemon debugfs_create_file("summary", 0444, bp->debug_root, 2484f67bf662SJonathan Lemon &bp->dev, &ptp_ocp_summary_fops); 24859f492c4cSVadim Fedorenko if (bp->tod) 24869f492c4cSVadim Fedorenko debugfs_create_file("tod_status", 0444, bp->debug_root, 24879f492c4cSVadim Fedorenko &bp->dev, &ptp_ocp_tod_status_fops); 2488f67bf662SJonathan Lemon } 2489f67bf662SJonathan Lemon 2490f67bf662SJonathan Lemon static void 2491f67bf662SJonathan Lemon ptp_ocp_debugfs_remove_device(struct ptp_ocp *bp) 2492f67bf662SJonathan Lemon { 2493f67bf662SJonathan Lemon debugfs_remove_recursive(bp->debug_root); 2494f67bf662SJonathan Lemon } 2495f67bf662SJonathan Lemon 2496f67bf662SJonathan Lemon static void 2497f67bf662SJonathan Lemon ptp_ocp_debugfs_init(void) 2498f67bf662SJonathan Lemon { 2499f67bf662SJonathan Lemon ptp_ocp_debugfs_root = debugfs_create_dir("timecard", NULL); 2500f67bf662SJonathan Lemon } 2501f67bf662SJonathan Lemon 2502f67bf662SJonathan Lemon static void 2503f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(void) 2504f67bf662SJonathan Lemon { 2505f67bf662SJonathan Lemon debugfs_remove_recursive(ptp_ocp_debugfs_root); 2506f67bf662SJonathan Lemon } 2507f67bf662SJonathan Lemon 2508773bda96SJonathan Lemon static void 2509773bda96SJonathan Lemon ptp_ocp_dev_release(struct device *dev) 2510773bda96SJonathan Lemon { 2511773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2512773bda96SJonathan Lemon 2513773bda96SJonathan Lemon mutex_lock(&ptp_ocp_lock); 2514773bda96SJonathan Lemon idr_remove(&ptp_ocp_idr, bp->id); 2515773bda96SJonathan Lemon mutex_unlock(&ptp_ocp_lock); 2516773bda96SJonathan Lemon } 2517773bda96SJonathan Lemon 2518773bda96SJonathan Lemon static int 2519773bda96SJonathan Lemon ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev) 2520773bda96SJonathan Lemon { 2521773bda96SJonathan Lemon int err; 2522773bda96SJonathan Lemon 2523773bda96SJonathan Lemon mutex_lock(&ptp_ocp_lock); 2524773bda96SJonathan Lemon err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL); 2525773bda96SJonathan Lemon mutex_unlock(&ptp_ocp_lock); 2526773bda96SJonathan Lemon if (err < 0) { 2527773bda96SJonathan Lemon dev_err(&pdev->dev, "idr_alloc failed: %d\n", err); 2528773bda96SJonathan Lemon return err; 2529773bda96SJonathan Lemon } 2530773bda96SJonathan Lemon bp->id = err; 2531773bda96SJonathan Lemon 2532773bda96SJonathan Lemon bp->ptp_info = ptp_ocp_clock_info; 2533773bda96SJonathan Lemon spin_lock_init(&bp->lock); 2534ef0cfb34SJonathan Lemon bp->gnss_port = -1; 253571d7e085SJonathan Lemon bp->gnss2_port = -1; 2536773bda96SJonathan Lemon bp->mac_port = -1; 2537e3516bb4SJonathan Lemon bp->nmea_port = -1; 2538773bda96SJonathan Lemon bp->pdev = pdev; 2539773bda96SJonathan Lemon 2540773bda96SJonathan Lemon device_initialize(&bp->dev); 2541773bda96SJonathan Lemon dev_set_name(&bp->dev, "ocp%d", bp->id); 2542773bda96SJonathan Lemon bp->dev.class = &timecard_class; 2543773bda96SJonathan Lemon bp->dev.parent = &pdev->dev; 2544773bda96SJonathan Lemon bp->dev.release = ptp_ocp_dev_release; 2545773bda96SJonathan Lemon dev_set_drvdata(&bp->dev, bp); 2546773bda96SJonathan Lemon 2547773bda96SJonathan Lemon err = device_add(&bp->dev); 2548773bda96SJonathan Lemon if (err) { 2549773bda96SJonathan Lemon dev_err(&bp->dev, "device add failed: %d\n", err); 2550773bda96SJonathan Lemon goto out; 2551773bda96SJonathan Lemon } 2552773bda96SJonathan Lemon 2553773bda96SJonathan Lemon pci_set_drvdata(pdev, bp); 2554773bda96SJonathan Lemon 2555773bda96SJonathan Lemon return 0; 2556773bda96SJonathan Lemon 2557773bda96SJonathan Lemon out: 2558773bda96SJonathan Lemon ptp_ocp_dev_release(&bp->dev); 2559d12f23faSJonathan Lemon put_device(&bp->dev); 2560773bda96SJonathan Lemon return err; 2561773bda96SJonathan Lemon } 2562773bda96SJonathan Lemon 2563773bda96SJonathan Lemon static void 2564773bda96SJonathan Lemon ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link) 2565773bda96SJonathan Lemon { 2566773bda96SJonathan Lemon struct device *dev = &bp->dev; 2567773bda96SJonathan Lemon 2568773bda96SJonathan Lemon if (sysfs_create_link(&dev->kobj, &child->kobj, link)) 2569773bda96SJonathan Lemon dev_err(dev, "%s symlink failed\n", link); 2570773bda96SJonathan Lemon } 2571773bda96SJonathan Lemon 2572773bda96SJonathan Lemon static void 2573773bda96SJonathan Lemon ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link) 2574773bda96SJonathan Lemon { 2575773bda96SJonathan Lemon struct device *dev, *child; 2576773bda96SJonathan Lemon 2577773bda96SJonathan Lemon dev = &bp->pdev->dev; 2578773bda96SJonathan Lemon 2579773bda96SJonathan Lemon child = device_find_child_by_name(dev, name); 2580773bda96SJonathan Lemon if (!child) { 2581773bda96SJonathan Lemon dev_err(dev, "Could not find device %s\n", name); 2582773bda96SJonathan Lemon return; 2583773bda96SJonathan Lemon } 2584773bda96SJonathan Lemon 2585773bda96SJonathan Lemon ptp_ocp_symlink(bp, child, link); 2586773bda96SJonathan Lemon put_device(child); 2587773bda96SJonathan Lemon } 2588773bda96SJonathan Lemon 2589773bda96SJonathan Lemon static int 2590773bda96SJonathan Lemon ptp_ocp_complete(struct ptp_ocp *bp) 2591773bda96SJonathan Lemon { 2592773bda96SJonathan Lemon struct pps_device *pps; 2593773bda96SJonathan Lemon char buf[32]; 2594773bda96SJonathan Lemon 2595ef0cfb34SJonathan Lemon if (bp->gnss_port != -1) { 2596ef0cfb34SJonathan Lemon sprintf(buf, "ttyS%d", bp->gnss_port); 2597ef0cfb34SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyGNSS"); 2598773bda96SJonathan Lemon } 259971d7e085SJonathan Lemon if (bp->gnss2_port != -1) { 260071d7e085SJonathan Lemon sprintf(buf, "ttyS%d", bp->gnss2_port); 260171d7e085SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyGNSS2"); 260271d7e085SJonathan Lemon } 2603773bda96SJonathan Lemon if (bp->mac_port != -1) { 2604773bda96SJonathan Lemon sprintf(buf, "ttyS%d", bp->mac_port); 2605773bda96SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyMAC"); 2606773bda96SJonathan Lemon } 2607e3516bb4SJonathan Lemon if (bp->nmea_port != -1) { 2608e3516bb4SJonathan Lemon sprintf(buf, "ttyS%d", bp->nmea_port); 2609e3516bb4SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyNMEA"); 2610e3516bb4SJonathan Lemon } 2611773bda96SJonathan Lemon sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp)); 2612773bda96SJonathan Lemon ptp_ocp_link_child(bp, buf, "ptp"); 2613773bda96SJonathan Lemon 2614773bda96SJonathan Lemon pps = pps_lookup_dev(bp->ptp); 2615773bda96SJonathan Lemon if (pps) 2616773bda96SJonathan Lemon ptp_ocp_symlink(bp, pps->dev, "pps"); 2617773bda96SJonathan Lemon 2618773bda96SJonathan Lemon if (device_add_groups(&bp->dev, timecard_groups)) 2619773bda96SJonathan Lemon pr_err("device add groups failed\n"); 2620773bda96SJonathan Lemon 2621f67bf662SJonathan Lemon ptp_ocp_debugfs_add_device(bp); 2622f67bf662SJonathan Lemon 2623773bda96SJonathan Lemon return 0; 2624773bda96SJonathan Lemon } 2625773bda96SJonathan Lemon 2626773bda96SJonathan Lemon static void 2627065efcc5SJonathan Lemon ptp_ocp_phc_info(struct ptp_ocp *bp) 2628065efcc5SJonathan Lemon { 2629065efcc5SJonathan Lemon struct timespec64 ts; 2630065efcc5SJonathan Lemon u32 version, select; 2631065efcc5SJonathan Lemon bool sync; 2632065efcc5SJonathan Lemon 2633065efcc5SJonathan Lemon version = ioread32(&bp->reg->version); 2634065efcc5SJonathan Lemon select = ioread32(&bp->reg->select); 2635065efcc5SJonathan Lemon dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n", 2636065efcc5SJonathan Lemon version >> 24, (version >> 16) & 0xff, version & 0xffff, 2637065efcc5SJonathan Lemon ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16), 2638065efcc5SJonathan Lemon ptp_clock_index(bp->ptp)); 2639065efcc5SJonathan Lemon 2640065efcc5SJonathan Lemon sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC; 2641065efcc5SJonathan Lemon if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL)) 2642065efcc5SJonathan Lemon dev_info(&bp->pdev->dev, "Time: %lld.%ld, %s\n", 2643065efcc5SJonathan Lemon ts.tv_sec, ts.tv_nsec, 2644065efcc5SJonathan Lemon sync ? "in-sync" : "UNSYNCED"); 2645065efcc5SJonathan Lemon } 2646065efcc5SJonathan Lemon 2647065efcc5SJonathan Lemon static void 2648065efcc5SJonathan Lemon ptp_ocp_serial_info(struct device *dev, const char *name, int port, int baud) 2649065efcc5SJonathan Lemon { 2650065efcc5SJonathan Lemon if (port != -1) 2651065efcc5SJonathan Lemon dev_info(dev, "%5s: /dev/ttyS%-2d @ %6d\n", name, port, baud); 2652065efcc5SJonathan Lemon } 2653065efcc5SJonathan Lemon 2654065efcc5SJonathan Lemon static void 2655065efcc5SJonathan Lemon ptp_ocp_info(struct ptp_ocp *bp) 2656773bda96SJonathan Lemon { 2657e3516bb4SJonathan Lemon static int nmea_baud[] = { 2658e3516bb4SJonathan Lemon 1200, 2400, 4800, 9600, 19200, 38400, 2659e3516bb4SJonathan Lemon 57600, 115200, 230400, 460800, 921600, 2660e3516bb4SJonathan Lemon 1000000, 2000000 2661e3516bb4SJonathan Lemon }; 2662773bda96SJonathan Lemon struct device *dev = &bp->pdev->dev; 2663e3516bb4SJonathan Lemon u32 reg; 2664773bda96SJonathan Lemon 2665065efcc5SJonathan Lemon ptp_ocp_phc_info(bp); 2666065efcc5SJonathan Lemon 2667b0ca789aSJonathan Lemon dev_info(dev, "version %x\n", bp->fw_version); 2668b0ca789aSJonathan Lemon if (bp->fw_version & 0xffff) 2669773bda96SJonathan Lemon dev_info(dev, "regular image, version %d\n", 2670b0ca789aSJonathan Lemon bp->fw_version & 0xffff); 2671773bda96SJonathan Lemon else 2672773bda96SJonathan Lemon dev_info(dev, "golden image, version %d\n", 2673b0ca789aSJonathan Lemon bp->fw_version >> 16); 2674b0ca789aSJonathan Lemon 2675065efcc5SJonathan Lemon ptp_ocp_serial_info(dev, "GNSS", bp->gnss_port, 115200); 267671d7e085SJonathan Lemon ptp_ocp_serial_info(dev, "GNSS2", bp->gnss2_port, 115200); 2677065efcc5SJonathan Lemon ptp_ocp_serial_info(dev, "MAC", bp->mac_port, 57600); 2678e3516bb4SJonathan Lemon if (bp->nmea_out && bp->nmea_port != -1) { 2679e3516bb4SJonathan Lemon int baud = -1; 2680e3516bb4SJonathan Lemon 2681e3516bb4SJonathan Lemon reg = ioread32(&bp->nmea_out->uart_baud); 2682e3516bb4SJonathan Lemon if (reg < ARRAY_SIZE(nmea_baud)) 2683e3516bb4SJonathan Lemon baud = nmea_baud[reg]; 2684e3516bb4SJonathan Lemon ptp_ocp_serial_info(dev, "NMEA", bp->nmea_port, baud); 2685e3516bb4SJonathan Lemon } 2686773bda96SJonathan Lemon } 2687773bda96SJonathan Lemon 2688773bda96SJonathan Lemon static void 2689773bda96SJonathan Lemon ptp_ocp_detach_sysfs(struct ptp_ocp *bp) 2690773bda96SJonathan Lemon { 2691773bda96SJonathan Lemon struct device *dev = &bp->dev; 2692773bda96SJonathan Lemon 2693ef0cfb34SJonathan Lemon sysfs_remove_link(&dev->kobj, "ttyGNSS"); 2694773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "ttyMAC"); 2695773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "ptp"); 2696773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "pps"); 2697773bda96SJonathan Lemon device_remove_groups(dev, timecard_groups); 2698773bda96SJonathan Lemon } 2699773bda96SJonathan Lemon 2700773bda96SJonathan Lemon static void 2701773bda96SJonathan Lemon ptp_ocp_detach(struct ptp_ocp *bp) 2702773bda96SJonathan Lemon { 2703f67bf662SJonathan Lemon ptp_ocp_debugfs_remove_device(bp); 2704773bda96SJonathan Lemon ptp_ocp_detach_sysfs(bp); 2705773bda96SJonathan Lemon if (timer_pending(&bp->watchdog)) 2706773bda96SJonathan Lemon del_timer_sync(&bp->watchdog); 2707773bda96SJonathan Lemon if (bp->ts0) 2708773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->ts0); 2709773bda96SJonathan Lemon if (bp->ts1) 2710773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->ts1); 2711dcf61469SJonathan Lemon if (bp->ts2) 2712dcf61469SJonathan Lemon ptp_ocp_unregister_ext(bp->ts2); 2713773bda96SJonathan Lemon if (bp->pps) 2714773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->pps); 2715ef0cfb34SJonathan Lemon if (bp->gnss_port != -1) 2716ef0cfb34SJonathan Lemon serial8250_unregister_port(bp->gnss_port); 271771d7e085SJonathan Lemon if (bp->gnss2_port != -1) 271871d7e085SJonathan Lemon serial8250_unregister_port(bp->gnss2_port); 2719773bda96SJonathan Lemon if (bp->mac_port != -1) 2720773bda96SJonathan Lemon serial8250_unregister_port(bp->mac_port); 2721e3516bb4SJonathan Lemon if (bp->nmea_port != -1) 2722e3516bb4SJonathan Lemon serial8250_unregister_port(bp->nmea_port); 2723773bda96SJonathan Lemon if (bp->spi_flash) 2724773bda96SJonathan Lemon platform_device_unregister(bp->spi_flash); 2725773bda96SJonathan Lemon if (bp->i2c_ctrl) 2726773bda96SJonathan Lemon platform_device_unregister(bp->i2c_ctrl); 2727773bda96SJonathan Lemon if (bp->i2c_clk) 2728773bda96SJonathan Lemon clk_hw_unregister_fixed_rate(bp->i2c_clk); 2729773bda96SJonathan Lemon if (bp->n_irqs) 2730773bda96SJonathan Lemon pci_free_irq_vectors(bp->pdev); 2731773bda96SJonathan Lemon if (bp->ptp) 2732773bda96SJonathan Lemon ptp_clock_unregister(bp->ptp); 2733773bda96SJonathan Lemon device_unregister(&bp->dev); 2734773bda96SJonathan Lemon } 2735773bda96SJonathan Lemon 2736a7e1abadSJonathan Lemon static int 2737a7e1abadSJonathan Lemon ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id) 2738a7e1abadSJonathan Lemon { 2739773bda96SJonathan Lemon struct devlink *devlink; 2740a7e1abadSJonathan Lemon struct ptp_ocp *bp; 2741a7e1abadSJonathan Lemon int err; 2742a7e1abadSJonathan Lemon 2743919d13a7SLeon Romanovsky devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp), &pdev->dev); 2744773bda96SJonathan Lemon if (!devlink) { 2745773bda96SJonathan Lemon dev_err(&pdev->dev, "devlink_alloc failed\n"); 2746a7e1abadSJonathan Lemon return -ENOMEM; 2747773bda96SJonathan Lemon } 2748773bda96SJonathan Lemon 2749a7e1abadSJonathan Lemon err = pci_enable_device(pdev); 2750a7e1abadSJonathan Lemon if (err) { 2751a7e1abadSJonathan Lemon dev_err(&pdev->dev, "pci_enable_device\n"); 27524587369bSJonathan Lemon goto out_free; 2753a7e1abadSJonathan Lemon } 2754a7e1abadSJonathan Lemon 2755773bda96SJonathan Lemon bp = devlink_priv(devlink); 2756773bda96SJonathan Lemon err = ptp_ocp_device_init(bp, pdev); 2757773bda96SJonathan Lemon if (err) 2758d9fdbf13SJonathan Lemon goto out_disable; 2759a7e1abadSJonathan Lemon 2760773bda96SJonathan Lemon /* compat mode. 2761773bda96SJonathan Lemon * Older FPGA firmware only returns 2 irq's. 2762773bda96SJonathan Lemon * allow this - if not all of the IRQ's are returned, skip the 2763773bda96SJonathan Lemon * extra devices and just register the clock. 2764773bda96SJonathan Lemon */ 2765e3516bb4SJonathan Lemon err = pci_alloc_irq_vectors(pdev, 1, 11, PCI_IRQ_MSI | PCI_IRQ_MSIX); 2766773bda96SJonathan Lemon if (err < 0) { 2767773bda96SJonathan Lemon dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err); 2768773bda96SJonathan Lemon goto out; 2769a7e1abadSJonathan Lemon } 2770773bda96SJonathan Lemon bp->n_irqs = err; 2771773bda96SJonathan Lemon pci_set_master(pdev); 2772a7e1abadSJonathan Lemon 2773773bda96SJonathan Lemon err = ptp_ocp_register_resources(bp, id->driver_data); 2774a7e1abadSJonathan Lemon if (err) 2775a7e1abadSJonathan Lemon goto out; 2776a7e1abadSJonathan Lemon 2777a7e1abadSJonathan Lemon bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev); 2778a7e1abadSJonathan Lemon if (IS_ERR(bp->ptp)) { 2779a7e1abadSJonathan Lemon err = PTR_ERR(bp->ptp); 2780773bda96SJonathan Lemon dev_err(&pdev->dev, "ptp_clock_register: %d\n", err); 2781773bda96SJonathan Lemon bp->ptp = NULL; 2782a7e1abadSJonathan Lemon goto out; 2783a7e1abadSJonathan Lemon } 2784a7e1abadSJonathan Lemon 2785773bda96SJonathan Lemon err = ptp_ocp_complete(bp); 2786773bda96SJonathan Lemon if (err) 2787773bda96SJonathan Lemon goto out; 2788773bda96SJonathan Lemon 2789a7e1abadSJonathan Lemon ptp_ocp_info(bp); 2790c89f78e9SLeon Romanovsky devlink_register(devlink); 2791a7e1abadSJonathan Lemon return 0; 2792a7e1abadSJonathan Lemon 2793a7e1abadSJonathan Lemon out: 2794773bda96SJonathan Lemon ptp_ocp_detach(bp); 2795773bda96SJonathan Lemon pci_set_drvdata(pdev, NULL); 2796d9fdbf13SJonathan Lemon out_disable: 2797d9fdbf13SJonathan Lemon pci_disable_device(pdev); 27984587369bSJonathan Lemon out_free: 2799773bda96SJonathan Lemon devlink_free(devlink); 2800a7e1abadSJonathan Lemon return err; 2801a7e1abadSJonathan Lemon } 2802a7e1abadSJonathan Lemon 2803a7e1abadSJonathan Lemon static void 2804a7e1abadSJonathan Lemon ptp_ocp_remove(struct pci_dev *pdev) 2805a7e1abadSJonathan Lemon { 2806a7e1abadSJonathan Lemon struct ptp_ocp *bp = pci_get_drvdata(pdev); 2807773bda96SJonathan Lemon struct devlink *devlink = priv_to_devlink(bp); 2808a7e1abadSJonathan Lemon 2809c89f78e9SLeon Romanovsky devlink_unregister(devlink); 2810773bda96SJonathan Lemon ptp_ocp_detach(bp); 2811a7e1abadSJonathan Lemon pci_set_drvdata(pdev, NULL); 2812d9fdbf13SJonathan Lemon pci_disable_device(pdev); 2813773bda96SJonathan Lemon 2814773bda96SJonathan Lemon devlink_free(devlink); 2815a7e1abadSJonathan Lemon } 2816a7e1abadSJonathan Lemon 2817a7e1abadSJonathan Lemon static struct pci_driver ptp_ocp_driver = { 2818a7e1abadSJonathan Lemon .name = KBUILD_MODNAME, 2819a7e1abadSJonathan Lemon .id_table = ptp_ocp_pcidev_id, 2820a7e1abadSJonathan Lemon .probe = ptp_ocp_probe, 2821a7e1abadSJonathan Lemon .remove = ptp_ocp_remove, 2822a7e1abadSJonathan Lemon }; 2823a7e1abadSJonathan Lemon 2824773bda96SJonathan Lemon static int 2825773bda96SJonathan Lemon ptp_ocp_i2c_notifier_call(struct notifier_block *nb, 2826773bda96SJonathan Lemon unsigned long action, void *data) 2827773bda96SJonathan Lemon { 2828773bda96SJonathan Lemon struct device *dev, *child = data; 2829773bda96SJonathan Lemon struct ptp_ocp *bp; 2830773bda96SJonathan Lemon bool add; 2831773bda96SJonathan Lemon 2832773bda96SJonathan Lemon switch (action) { 2833773bda96SJonathan Lemon case BUS_NOTIFY_ADD_DEVICE: 2834773bda96SJonathan Lemon case BUS_NOTIFY_DEL_DEVICE: 2835773bda96SJonathan Lemon add = action == BUS_NOTIFY_ADD_DEVICE; 2836773bda96SJonathan Lemon break; 2837773bda96SJonathan Lemon default: 2838773bda96SJonathan Lemon return 0; 2839773bda96SJonathan Lemon } 2840773bda96SJonathan Lemon 2841773bda96SJonathan Lemon if (!i2c_verify_adapter(child)) 2842773bda96SJonathan Lemon return 0; 2843773bda96SJonathan Lemon 2844773bda96SJonathan Lemon dev = child; 2845773bda96SJonathan Lemon while ((dev = dev->parent)) 2846773bda96SJonathan Lemon if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME)) 2847773bda96SJonathan Lemon goto found; 2848773bda96SJonathan Lemon return 0; 2849773bda96SJonathan Lemon 2850773bda96SJonathan Lemon found: 2851773bda96SJonathan Lemon bp = dev_get_drvdata(dev); 2852773bda96SJonathan Lemon if (add) 2853773bda96SJonathan Lemon ptp_ocp_symlink(bp, child, "i2c"); 2854773bda96SJonathan Lemon else 2855773bda96SJonathan Lemon sysfs_remove_link(&bp->dev.kobj, "i2c"); 2856773bda96SJonathan Lemon 2857773bda96SJonathan Lemon return 0; 2858773bda96SJonathan Lemon } 2859773bda96SJonathan Lemon 2860773bda96SJonathan Lemon static struct notifier_block ptp_ocp_i2c_notifier = { 2861773bda96SJonathan Lemon .notifier_call = ptp_ocp_i2c_notifier_call, 2862773bda96SJonathan Lemon }; 2863773bda96SJonathan Lemon 2864a7e1abadSJonathan Lemon static int __init 2865a7e1abadSJonathan Lemon ptp_ocp_init(void) 2866a7e1abadSJonathan Lemon { 2867773bda96SJonathan Lemon const char *what; 2868a7e1abadSJonathan Lemon int err; 2869a7e1abadSJonathan Lemon 2870f67bf662SJonathan Lemon ptp_ocp_debugfs_init(); 2871f67bf662SJonathan Lemon 2872773bda96SJonathan Lemon what = "timecard class"; 2873773bda96SJonathan Lemon err = class_register(&timecard_class); 2874773bda96SJonathan Lemon if (err) 2875773bda96SJonathan Lemon goto out; 2876773bda96SJonathan Lemon 2877773bda96SJonathan Lemon what = "i2c notifier"; 2878773bda96SJonathan Lemon err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2879773bda96SJonathan Lemon if (err) 2880773bda96SJonathan Lemon goto out_notifier; 2881773bda96SJonathan Lemon 2882773bda96SJonathan Lemon what = "ptp_ocp driver"; 2883a7e1abadSJonathan Lemon err = pci_register_driver(&ptp_ocp_driver); 2884773bda96SJonathan Lemon if (err) 2885773bda96SJonathan Lemon goto out_register; 2886773bda96SJonathan Lemon 2887773bda96SJonathan Lemon return 0; 2888773bda96SJonathan Lemon 2889773bda96SJonathan Lemon out_register: 2890773bda96SJonathan Lemon bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2891773bda96SJonathan Lemon out_notifier: 2892773bda96SJonathan Lemon class_unregister(&timecard_class); 2893773bda96SJonathan Lemon out: 2894f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(); 2895773bda96SJonathan Lemon pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err); 2896a7e1abadSJonathan Lemon return err; 2897a7e1abadSJonathan Lemon } 2898a7e1abadSJonathan Lemon 2899a7e1abadSJonathan Lemon static void __exit 2900a7e1abadSJonathan Lemon ptp_ocp_fini(void) 2901a7e1abadSJonathan Lemon { 2902773bda96SJonathan Lemon bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2903a7e1abadSJonathan Lemon pci_unregister_driver(&ptp_ocp_driver); 2904773bda96SJonathan Lemon class_unregister(&timecard_class); 2905f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(); 2906a7e1abadSJonathan Lemon } 2907a7e1abadSJonathan Lemon 2908a7e1abadSJonathan Lemon module_init(ptp_ocp_init); 2909a7e1abadSJonathan Lemon module_exit(ptp_ocp_fini); 2910a7e1abadSJonathan Lemon 2911a7e1abadSJonathan Lemon MODULE_DESCRIPTION("OpenCompute TimeCard driver"); 2912a7e1abadSJonathan Lemon MODULE_LICENSE("GPL v2"); 2913