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 209a509a7c6SJonathan Lemon enum ptp_ocp_sma_mode { 210a509a7c6SJonathan Lemon SMA_MODE_IN, 211a509a7c6SJonathan Lemon SMA_MODE_OUT, 212a509a7c6SJonathan Lemon }; 213a509a7c6SJonathan Lemon 214a509a7c6SJonathan Lemon struct ptp_ocp_sma_connector { 215a509a7c6SJonathan Lemon enum ptp_ocp_sma_mode mode; 216a509a7c6SJonathan Lemon bool fixed_fcn; 217a509a7c6SJonathan Lemon bool fixed_dir; 218b2c4f0acSJonathan Lemon bool disabled; 219a509a7c6SJonathan Lemon }; 220a509a7c6SJonathan Lemon 221*c205d53cSJonathan Lemon struct ocp_attr_group { 222*c205d53cSJonathan Lemon u64 cap; 223*c205d53cSJonathan Lemon const struct attribute_group *group; 224*c205d53cSJonathan Lemon }; 225*c205d53cSJonathan Lemon 226*c205d53cSJonathan Lemon #define OCP_CAP_BASIC BIT(0) 227*c205d53cSJonathan Lemon 2280cfcdd1eSJonathan Lemon #define OCP_BOARD_ID_LEN 13 2290cfcdd1eSJonathan Lemon #define OCP_SERIAL_LEN 6 2300cfcdd1eSJonathan Lemon 231a7e1abadSJonathan Lemon struct ptp_ocp { 232a7e1abadSJonathan Lemon struct pci_dev *pdev; 233773bda96SJonathan Lemon struct device dev; 234a7e1abadSJonathan Lemon spinlock_t lock; 235a7e1abadSJonathan Lemon struct ocp_reg __iomem *reg; 236a7e1abadSJonathan Lemon struct tod_reg __iomem *tod; 2370d43d4f2SJonathan Lemon struct pps_reg __iomem *pps_to_ext; 2380d43d4f2SJonathan Lemon struct pps_reg __iomem *pps_to_clk; 239f67bf662SJonathan Lemon struct gpio_reg __iomem *pps_select; 240a509a7c6SJonathan Lemon struct gpio_reg __iomem *sma_map1; 241a509a7c6SJonathan Lemon struct gpio_reg __iomem *sma_map2; 2426baf2925SJonathan Lemon struct irig_master_reg __iomem *irig_out; 2436baf2925SJonathan Lemon struct irig_slave_reg __iomem *irig_in; 2446baf2925SJonathan Lemon struct dcf_master_reg __iomem *dcf_out; 2456baf2925SJonathan Lemon struct dcf_slave_reg __iomem *dcf_in; 246e3516bb4SJonathan Lemon struct tod_reg __iomem *nmea_out; 247773bda96SJonathan Lemon struct ptp_ocp_ext_src *pps; 248773bda96SJonathan Lemon struct ptp_ocp_ext_src *ts0; 249773bda96SJonathan Lemon struct ptp_ocp_ext_src *ts1; 250dcf61469SJonathan Lemon struct ptp_ocp_ext_src *ts2; 251773bda96SJonathan Lemon struct img_reg __iomem *image; 252a7e1abadSJonathan Lemon struct ptp_clock *ptp; 253a7e1abadSJonathan Lemon struct ptp_clock_info ptp_info; 254773bda96SJonathan Lemon struct platform_device *i2c_ctrl; 255773bda96SJonathan Lemon struct platform_device *spi_flash; 256773bda96SJonathan Lemon struct clk_hw *i2c_clk; 257773bda96SJonathan Lemon struct timer_list watchdog; 258*c205d53cSJonathan Lemon const struct ocp_attr_group *attr_tbl; 2590cfcdd1eSJonathan Lemon const struct ptp_ocp_eeprom_map *eeprom_map; 260f67bf662SJonathan Lemon struct dentry *debug_root; 261ef0cfb34SJonathan Lemon time64_t gnss_lost; 262773bda96SJonathan Lemon int id; 263773bda96SJonathan Lemon int n_irqs; 264ef0cfb34SJonathan Lemon int gnss_port; 26571d7e085SJonathan Lemon int gnss2_port; 266773bda96SJonathan Lemon int mac_port; /* miniature atomic clock */ 267e3516bb4SJonathan Lemon int nmea_port; 268b0ca789aSJonathan Lemon u32 fw_version; 2690cfcdd1eSJonathan Lemon u8 board_id[OCP_BOARD_ID_LEN]; 2700cfcdd1eSJonathan Lemon u8 serial[OCP_SERIAL_LEN]; 2710cfcdd1eSJonathan Lemon bool has_eeprom_data; 272a62a56d0SJonathan Lemon u32 pps_req_map; 27389260d87SJonathan Lemon int flash_start; 27489260d87SJonathan Lemon u32 utc_tai_offset; 2751acffc6eSJonathan Lemon u32 ts_window_adjust; 276*c205d53cSJonathan Lemon u64 fw_cap; 277a509a7c6SJonathan Lemon struct ptp_ocp_sma_connector sma[4]; 278a7e1abadSJonathan Lemon }; 279a7e1abadSJonathan Lemon 280a62a56d0SJonathan Lemon #define OCP_REQ_TIMESTAMP BIT(0) 281a62a56d0SJonathan Lemon #define OCP_REQ_PPS BIT(1) 282a62a56d0SJonathan Lemon 283773bda96SJonathan Lemon struct ocp_resource { 284773bda96SJonathan Lemon unsigned long offset; 285773bda96SJonathan Lemon int size; 286773bda96SJonathan Lemon int irq_vec; 287773bda96SJonathan Lemon int (*setup)(struct ptp_ocp *bp, struct ocp_resource *r); 288773bda96SJonathan Lemon void *extra; 289773bda96SJonathan Lemon unsigned long bp_offset; 29056ec4403SJonathan Lemon const char * const name; 291773bda96SJonathan Lemon }; 292773bda96SJonathan Lemon 293773bda96SJonathan Lemon static int ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r); 294773bda96SJonathan Lemon static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r); 295773bda96SJonathan Lemon static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r); 296773bda96SJonathan Lemon static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r); 297773bda96SJonathan Lemon static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r); 298773bda96SJonathan Lemon static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r); 299773bda96SJonathan Lemon static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv); 300a62a56d0SJonathan Lemon static int ptp_ocp_ts_enable(void *priv, u32 req, bool enable); 301773bda96SJonathan Lemon 302*c205d53cSJonathan Lemon static const struct ocp_attr_group fb_timecard_groups[]; 303*c205d53cSJonathan Lemon 3040cfcdd1eSJonathan Lemon struct ptp_ocp_eeprom_map { 3050cfcdd1eSJonathan Lemon u16 off; 3060cfcdd1eSJonathan Lemon u16 len; 3070cfcdd1eSJonathan Lemon u32 bp_offset; 3080cfcdd1eSJonathan Lemon const void * const tag; 3090cfcdd1eSJonathan Lemon }; 3100cfcdd1eSJonathan Lemon 3110cfcdd1eSJonathan Lemon #define EEPROM_ENTRY(addr, member) \ 3120cfcdd1eSJonathan Lemon .off = addr, \ 3130cfcdd1eSJonathan Lemon .len = sizeof_field(struct ptp_ocp, member), \ 3140cfcdd1eSJonathan Lemon .bp_offset = offsetof(struct ptp_ocp, member) 3150cfcdd1eSJonathan Lemon 3160cfcdd1eSJonathan Lemon #define BP_MAP_ENTRY_ADDR(bp, map) ({ \ 3170cfcdd1eSJonathan Lemon (void *)((uintptr_t)(bp) + (map)->bp_offset); \ 3180cfcdd1eSJonathan Lemon }) 3190cfcdd1eSJonathan Lemon 3200cfcdd1eSJonathan Lemon static struct ptp_ocp_eeprom_map fb_eeprom_map[] = { 3210cfcdd1eSJonathan Lemon { EEPROM_ENTRY(0x43, board_id) }, 3220cfcdd1eSJonathan Lemon { EEPROM_ENTRY(0x00, serial), .tag = "mac" }, 3230cfcdd1eSJonathan Lemon { } 3240cfcdd1eSJonathan Lemon }; 3250cfcdd1eSJonathan Lemon 326773bda96SJonathan Lemon #define bp_assign_entry(bp, res, val) ({ \ 327773bda96SJonathan Lemon uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset; \ 328773bda96SJonathan Lemon *(typeof(val) *)addr = val; \ 329773bda96SJonathan Lemon }) 330773bda96SJonathan Lemon 331773bda96SJonathan Lemon #define OCP_RES_LOCATION(member) \ 33256ec4403SJonathan Lemon .name = #member, .bp_offset = offsetof(struct ptp_ocp, member) 333773bda96SJonathan Lemon 334773bda96SJonathan Lemon #define OCP_MEM_RESOURCE(member) \ 335773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_mem 336773bda96SJonathan Lemon 337773bda96SJonathan Lemon #define OCP_SERIAL_RESOURCE(member) \ 338773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_serial 339773bda96SJonathan Lemon 340773bda96SJonathan Lemon #define OCP_I2C_RESOURCE(member) \ 341773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_i2c 342773bda96SJonathan Lemon 343773bda96SJonathan Lemon #define OCP_SPI_RESOURCE(member) \ 344773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_spi 345773bda96SJonathan Lemon 346773bda96SJonathan Lemon #define OCP_EXT_RESOURCE(member) \ 347773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_ext 348773bda96SJonathan Lemon 349773bda96SJonathan Lemon /* This is the MSI vector mapping used. 350a62a56d0SJonathan Lemon * 0: TS3 (and PPS) 351773bda96SJonathan Lemon * 1: TS0 352773bda96SJonathan Lemon * 2: TS1 353be69087cSJonathan Lemon * 3: GNSS1 35471d7e085SJonathan Lemon * 4: GNSS2 355773bda96SJonathan Lemon * 5: MAC 356dcf61469SJonathan Lemon * 6: TS2 3571447149dSJonathan Lemon * 7: I2C controller 358e3516bb4SJonathan Lemon * 8: HWICAP (notused) 359773bda96SJonathan Lemon * 9: SPI Flash 360e3516bb4SJonathan Lemon * 10: NMEA 361773bda96SJonathan Lemon */ 362773bda96SJonathan Lemon 363773bda96SJonathan Lemon static struct ocp_resource ocp_fb_resource[] = { 364773bda96SJonathan Lemon { 365773bda96SJonathan Lemon OCP_MEM_RESOURCE(reg), 366773bda96SJonathan Lemon .offset = 0x01000000, .size = 0x10000, 367773bda96SJonathan Lemon }, 368773bda96SJonathan Lemon { 369773bda96SJonathan Lemon OCP_EXT_RESOURCE(ts0), 370773bda96SJonathan Lemon .offset = 0x01010000, .size = 0x10000, .irq_vec = 1, 371773bda96SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 37256ec4403SJonathan Lemon .index = 0, 373773bda96SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 374773bda96SJonathan Lemon .enable = ptp_ocp_ts_enable, 375773bda96SJonathan Lemon }, 376773bda96SJonathan Lemon }, 377773bda96SJonathan Lemon { 378773bda96SJonathan Lemon OCP_EXT_RESOURCE(ts1), 379773bda96SJonathan Lemon .offset = 0x01020000, .size = 0x10000, .irq_vec = 2, 380773bda96SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 38156ec4403SJonathan Lemon .index = 1, 382773bda96SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 383773bda96SJonathan Lemon .enable = ptp_ocp_ts_enable, 384773bda96SJonathan Lemon }, 385773bda96SJonathan Lemon }, 386773bda96SJonathan Lemon { 387dcf61469SJonathan Lemon OCP_EXT_RESOURCE(ts2), 388dcf61469SJonathan Lemon .offset = 0x01060000, .size = 0x10000, .irq_vec = 6, 389dcf61469SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 390dcf61469SJonathan Lemon .index = 2, 391dcf61469SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 392dcf61469SJonathan Lemon .enable = ptp_ocp_ts_enable, 393dcf61469SJonathan Lemon }, 394dcf61469SJonathan Lemon }, 395dcf61469SJonathan Lemon { 396a62a56d0SJonathan Lemon OCP_EXT_RESOURCE(pps), 397a62a56d0SJonathan Lemon .offset = 0x010C0000, .size = 0x10000, .irq_vec = 0, 398a62a56d0SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 399a62a56d0SJonathan Lemon .index = 3, 400a62a56d0SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 401a62a56d0SJonathan Lemon .enable = ptp_ocp_ts_enable, 402a62a56d0SJonathan Lemon }, 403a62a56d0SJonathan Lemon }, 404a62a56d0SJonathan Lemon { 4050d43d4f2SJonathan Lemon OCP_MEM_RESOURCE(pps_to_ext), 4060d43d4f2SJonathan Lemon .offset = 0x01030000, .size = 0x10000, 4070d43d4f2SJonathan Lemon }, 4080d43d4f2SJonathan Lemon { 4090d43d4f2SJonathan Lemon OCP_MEM_RESOURCE(pps_to_clk), 410773bda96SJonathan Lemon .offset = 0x01040000, .size = 0x10000, 411773bda96SJonathan Lemon }, 412773bda96SJonathan Lemon { 413773bda96SJonathan Lemon OCP_MEM_RESOURCE(tod), 414773bda96SJonathan Lemon .offset = 0x01050000, .size = 0x10000, 415773bda96SJonathan Lemon }, 416773bda96SJonathan Lemon { 4176baf2925SJonathan Lemon OCP_MEM_RESOURCE(irig_in), 4186baf2925SJonathan Lemon .offset = 0x01070000, .size = 0x10000, 4196baf2925SJonathan Lemon }, 4206baf2925SJonathan Lemon { 4216baf2925SJonathan Lemon OCP_MEM_RESOURCE(irig_out), 4226baf2925SJonathan Lemon .offset = 0x01080000, .size = 0x10000, 4236baf2925SJonathan Lemon }, 4246baf2925SJonathan Lemon { 4256baf2925SJonathan Lemon OCP_MEM_RESOURCE(dcf_in), 4266baf2925SJonathan Lemon .offset = 0x01090000, .size = 0x10000, 4276baf2925SJonathan Lemon }, 4286baf2925SJonathan Lemon { 4296baf2925SJonathan Lemon OCP_MEM_RESOURCE(dcf_out), 4306baf2925SJonathan Lemon .offset = 0x010A0000, .size = 0x10000, 4316baf2925SJonathan Lemon }, 4326baf2925SJonathan Lemon { 433e3516bb4SJonathan Lemon OCP_MEM_RESOURCE(nmea_out), 434e3516bb4SJonathan Lemon .offset = 0x010B0000, .size = 0x10000, 435e3516bb4SJonathan Lemon }, 436e3516bb4SJonathan Lemon { 437773bda96SJonathan Lemon OCP_MEM_RESOURCE(image), 438773bda96SJonathan Lemon .offset = 0x00020000, .size = 0x1000, 439773bda96SJonathan Lemon }, 440773bda96SJonathan Lemon { 441f67bf662SJonathan Lemon OCP_MEM_RESOURCE(pps_select), 442f67bf662SJonathan Lemon .offset = 0x00130000, .size = 0x1000, 443f67bf662SJonathan Lemon }, 444f67bf662SJonathan Lemon { 445a509a7c6SJonathan Lemon OCP_MEM_RESOURCE(sma_map1), 446e1daf0ecSJonathan Lemon .offset = 0x00140000, .size = 0x1000, 447e1daf0ecSJonathan Lemon }, 448e1daf0ecSJonathan Lemon { 449a509a7c6SJonathan Lemon OCP_MEM_RESOURCE(sma_map2), 450a509a7c6SJonathan Lemon .offset = 0x00220000, .size = 0x1000, 451a509a7c6SJonathan Lemon }, 452a509a7c6SJonathan Lemon { 453773bda96SJonathan Lemon OCP_I2C_RESOURCE(i2c_ctrl), 454773bda96SJonathan Lemon .offset = 0x00150000, .size = 0x10000, .irq_vec = 7, 4551618df6aSJonathan Lemon .extra = &(struct ptp_ocp_i2c_info) { 4561618df6aSJonathan Lemon .name = "xiic-i2c", 4571618df6aSJonathan Lemon .fixed_rate = 50000000, 4580cfcdd1eSJonathan Lemon .data_size = sizeof(struct xiic_i2c_platform_data), 4590cfcdd1eSJonathan Lemon .data = &(struct xiic_i2c_platform_data) { 4600cfcdd1eSJonathan Lemon .num_devices = 2, 4610cfcdd1eSJonathan Lemon .devices = (struct i2c_board_info[]) { 4620cfcdd1eSJonathan Lemon { I2C_BOARD_INFO("24c02", 0x50) }, 4630cfcdd1eSJonathan Lemon { I2C_BOARD_INFO("24mac402", 0x58), 4640cfcdd1eSJonathan Lemon .platform_data = "mac" }, 4650cfcdd1eSJonathan Lemon }, 4660cfcdd1eSJonathan Lemon }, 4671618df6aSJonathan Lemon }, 468773bda96SJonathan Lemon }, 469773bda96SJonathan Lemon { 470ef0cfb34SJonathan Lemon OCP_SERIAL_RESOURCE(gnss_port), 471773bda96SJonathan Lemon .offset = 0x00160000 + 0x1000, .irq_vec = 3, 472773bda96SJonathan Lemon }, 473773bda96SJonathan Lemon { 47471d7e085SJonathan Lemon OCP_SERIAL_RESOURCE(gnss2_port), 47571d7e085SJonathan Lemon .offset = 0x00170000 + 0x1000, .irq_vec = 4, 47671d7e085SJonathan Lemon }, 47771d7e085SJonathan Lemon { 478773bda96SJonathan Lemon OCP_SERIAL_RESOURCE(mac_port), 479773bda96SJonathan Lemon .offset = 0x00180000 + 0x1000, .irq_vec = 5, 480773bda96SJonathan Lemon }, 481773bda96SJonathan Lemon { 482e3516bb4SJonathan Lemon OCP_SERIAL_RESOURCE(nmea_port), 483e3516bb4SJonathan Lemon .offset = 0x00190000 + 0x1000, .irq_vec = 10, 484e3516bb4SJonathan Lemon }, 485e3516bb4SJonathan Lemon { 486773bda96SJonathan Lemon OCP_SPI_RESOURCE(spi_flash), 487773bda96SJonathan Lemon .offset = 0x00310000, .size = 0x10000, .irq_vec = 9, 488773bda96SJonathan Lemon .extra = &(struct ptp_ocp_flash_info) { 489773bda96SJonathan Lemon .name = "xilinx_spi", .pci_offset = 0, 490773bda96SJonathan Lemon .data_size = sizeof(struct xspi_platform_data), 491773bda96SJonathan Lemon .data = &(struct xspi_platform_data) { 492773bda96SJonathan Lemon .num_chipselect = 1, 493773bda96SJonathan Lemon .bits_per_word = 8, 494773bda96SJonathan Lemon .num_devices = 1, 495773bda96SJonathan Lemon .devices = &(struct spi_board_info) { 496773bda96SJonathan Lemon .modalias = "spi-nor", 497773bda96SJonathan Lemon }, 498773bda96SJonathan Lemon }, 499773bda96SJonathan Lemon }, 500773bda96SJonathan Lemon }, 501773bda96SJonathan Lemon { 502773bda96SJonathan Lemon .setup = ptp_ocp_fb_board_init, 503773bda96SJonathan Lemon }, 504773bda96SJonathan Lemon { } 505773bda96SJonathan Lemon }; 506773bda96SJonathan Lemon 507773bda96SJonathan Lemon static const struct pci_device_id ptp_ocp_pcidev_id[] = { 508773bda96SJonathan Lemon { PCI_DEVICE_DATA(FACEBOOK, TIMECARD, &ocp_fb_resource) }, 509773bda96SJonathan Lemon { 0 } 510773bda96SJonathan Lemon }; 511773bda96SJonathan Lemon MODULE_DEVICE_TABLE(pci, ptp_ocp_pcidev_id); 512773bda96SJonathan Lemon 513773bda96SJonathan Lemon static DEFINE_MUTEX(ptp_ocp_lock); 514773bda96SJonathan Lemon static DEFINE_IDR(ptp_ocp_idr); 515773bda96SJonathan Lemon 516e1daf0ecSJonathan Lemon struct ocp_selector { 517773bda96SJonathan Lemon const char *name; 518773bda96SJonathan Lemon int value; 519e1daf0ecSJonathan Lemon }; 520e1daf0ecSJonathan Lemon 521e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_clock[] = { 522773bda96SJonathan Lemon { .name = "NONE", .value = 0 }, 523773bda96SJonathan Lemon { .name = "TOD", .value = 1 }, 524773bda96SJonathan Lemon { .name = "IRIG", .value = 2 }, 525773bda96SJonathan Lemon { .name = "PPS", .value = 3 }, 526773bda96SJonathan Lemon { .name = "PTP", .value = 4 }, 527773bda96SJonathan Lemon { .name = "RTC", .value = 5 }, 528773bda96SJonathan Lemon { .name = "DCF", .value = 6 }, 529773bda96SJonathan Lemon { .name = "REGS", .value = 0xfe }, 530773bda96SJonathan Lemon { .name = "EXT", .value = 0xff }, 531e1daf0ecSJonathan Lemon { } 532e1daf0ecSJonathan Lemon }; 533e1daf0ecSJonathan Lemon 534a509a7c6SJonathan Lemon #define SMA_ENABLE BIT(15) 535a509a7c6SJonathan Lemon #define SMA_SELECT_MASK ((1U << 15) - 1) 536b2c4f0acSJonathan Lemon #define SMA_DISABLE 0x10000 537a509a7c6SJonathan Lemon 538e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_sma_in[] = { 539e1daf0ecSJonathan Lemon { .name = "10Mhz", .value = 0x00 }, 540e1daf0ecSJonathan Lemon { .name = "PPS1", .value = 0x01 }, 541e1daf0ecSJonathan Lemon { .name = "PPS2", .value = 0x02 }, 542e1daf0ecSJonathan Lemon { .name = "TS1", .value = 0x04 }, 543e1daf0ecSJonathan Lemon { .name = "TS2", .value = 0x08 }, 5446baf2925SJonathan Lemon { .name = "IRIG", .value = 0x10 }, 5456baf2925SJonathan Lemon { .name = "DCF", .value = 0x20 }, 546b2c4f0acSJonathan Lemon { .name = "None", .value = SMA_DISABLE }, 547e1daf0ecSJonathan Lemon { } 548e1daf0ecSJonathan Lemon }; 549e1daf0ecSJonathan Lemon 550e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_sma_out[] = { 551e1daf0ecSJonathan Lemon { .name = "10Mhz", .value = 0x00 }, 552e1daf0ecSJonathan Lemon { .name = "PHC", .value = 0x01 }, 553e1daf0ecSJonathan Lemon { .name = "MAC", .value = 0x02 }, 554be69087cSJonathan Lemon { .name = "GNSS1", .value = 0x04 }, 555e1daf0ecSJonathan Lemon { .name = "GNSS2", .value = 0x08 }, 5566baf2925SJonathan Lemon { .name = "IRIG", .value = 0x10 }, 5576baf2925SJonathan Lemon { .name = "DCF", .value = 0x20 }, 558cd09193fSJonathan Lemon { .name = "GND", .value = 0x2000 }, 559cd09193fSJonathan Lemon { .name = "VCC", .value = 0x4000 }, 560e1daf0ecSJonathan Lemon { } 561773bda96SJonathan Lemon }; 562773bda96SJonathan Lemon 563773bda96SJonathan Lemon static const char * 564e1daf0ecSJonathan Lemon ptp_ocp_select_name_from_val(struct ocp_selector *tbl, int val) 565773bda96SJonathan Lemon { 566773bda96SJonathan Lemon int i; 567773bda96SJonathan Lemon 568e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) 569e1daf0ecSJonathan Lemon if (tbl[i].value == val) 570e1daf0ecSJonathan Lemon return tbl[i].name; 571773bda96SJonathan Lemon return NULL; 572773bda96SJonathan Lemon } 573773bda96SJonathan Lemon 574773bda96SJonathan Lemon static int 575e1daf0ecSJonathan Lemon ptp_ocp_select_val_from_name(struct ocp_selector *tbl, const char *name) 576773bda96SJonathan Lemon { 577e1daf0ecSJonathan Lemon const char *select; 578773bda96SJonathan Lemon int i; 579773bda96SJonathan Lemon 580e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) { 581e1daf0ecSJonathan Lemon select = tbl[i].name; 582e1daf0ecSJonathan Lemon if (!strncasecmp(name, select, strlen(select))) 583e1daf0ecSJonathan Lemon return tbl[i].value; 584773bda96SJonathan Lemon } 585773bda96SJonathan Lemon return -EINVAL; 586773bda96SJonathan Lemon } 587773bda96SJonathan Lemon 588e1daf0ecSJonathan Lemon static ssize_t 589e1daf0ecSJonathan Lemon ptp_ocp_select_table_show(struct ocp_selector *tbl, char *buf) 590e1daf0ecSJonathan Lemon { 591e1daf0ecSJonathan Lemon ssize_t count; 592e1daf0ecSJonathan Lemon int i; 593e1daf0ecSJonathan Lemon 594e1daf0ecSJonathan Lemon count = 0; 595e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) 596e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", tbl[i].name); 597e1daf0ecSJonathan Lemon if (count) 598e1daf0ecSJonathan Lemon count--; 599e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "\n"); 600e1daf0ecSJonathan Lemon return count; 601e1daf0ecSJonathan Lemon } 602e1daf0ecSJonathan Lemon 603a7e1abadSJonathan Lemon static int 604a7e1abadSJonathan Lemon __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts, 605a7e1abadSJonathan Lemon struct ptp_system_timestamp *sts) 606a7e1abadSJonathan Lemon { 607a7e1abadSJonathan Lemon u32 ctrl, time_sec, time_ns; 608a7e1abadSJonathan Lemon int i; 609a7e1abadSJonathan Lemon 610a7e1abadSJonathan Lemon ptp_read_system_prets(sts); 6111acffc6eSJonathan Lemon 6121acffc6eSJonathan Lemon ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE; 613a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 614a7e1abadSJonathan Lemon 615a7e1abadSJonathan Lemon for (i = 0; i < 100; i++) { 616a7e1abadSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 617a7e1abadSJonathan Lemon if (ctrl & OCP_CTRL_READ_TIME_DONE) 618a7e1abadSJonathan Lemon break; 619a7e1abadSJonathan Lemon } 620a7e1abadSJonathan Lemon ptp_read_system_postts(sts); 621a7e1abadSJonathan Lemon 6221acffc6eSJonathan Lemon if (sts && bp->ts_window_adjust) { 6231acffc6eSJonathan Lemon s64 ns = timespec64_to_ns(&sts->post_ts); 6241acffc6eSJonathan Lemon 6251acffc6eSJonathan Lemon sts->post_ts = ns_to_timespec64(ns - bp->ts_window_adjust); 6261acffc6eSJonathan Lemon } 6271acffc6eSJonathan Lemon 628a7e1abadSJonathan Lemon time_ns = ioread32(&bp->reg->time_ns); 629a7e1abadSJonathan Lemon time_sec = ioread32(&bp->reg->time_sec); 630a7e1abadSJonathan Lemon 631a7e1abadSJonathan Lemon ts->tv_sec = time_sec; 632a7e1abadSJonathan Lemon ts->tv_nsec = time_ns; 633a7e1abadSJonathan Lemon 634a7e1abadSJonathan Lemon return ctrl & OCP_CTRL_READ_TIME_DONE ? 0 : -ETIMEDOUT; 635a7e1abadSJonathan Lemon } 636a7e1abadSJonathan Lemon 637a7e1abadSJonathan Lemon static int 638a7e1abadSJonathan Lemon ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts, 639a7e1abadSJonathan Lemon struct ptp_system_timestamp *sts) 640a7e1abadSJonathan Lemon { 641a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 642a7e1abadSJonathan Lemon unsigned long flags; 643a7e1abadSJonathan Lemon int err; 644a7e1abadSJonathan Lemon 645a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 646a7e1abadSJonathan Lemon err = __ptp_ocp_gettime_locked(bp, ts, sts); 647a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 648a7e1abadSJonathan Lemon 649a7e1abadSJonathan Lemon return err; 650a7e1abadSJonathan Lemon } 651a7e1abadSJonathan Lemon 652a7e1abadSJonathan Lemon static void 653a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts) 654a7e1abadSJonathan Lemon { 655a7e1abadSJonathan Lemon u32 ctrl, time_sec, time_ns; 656a7e1abadSJonathan Lemon u32 select; 657a7e1abadSJonathan Lemon 658a7e1abadSJonathan Lemon time_ns = ts->tv_nsec; 659a7e1abadSJonathan Lemon time_sec = ts->tv_sec; 660a7e1abadSJonathan Lemon 661a7e1abadSJonathan Lemon select = ioread32(&bp->reg->select); 662a7e1abadSJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 663a7e1abadSJonathan Lemon 664a7e1abadSJonathan Lemon iowrite32(time_ns, &bp->reg->adjust_ns); 665a7e1abadSJonathan Lemon iowrite32(time_sec, &bp->reg->adjust_sec); 666a7e1abadSJonathan Lemon 6671acffc6eSJonathan Lemon ctrl = OCP_CTRL_ADJUST_TIME | OCP_CTRL_ENABLE; 668a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 669a7e1abadSJonathan Lemon 670a7e1abadSJonathan Lemon /* restore clock selection */ 671a7e1abadSJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 672a7e1abadSJonathan Lemon } 673a7e1abadSJonathan Lemon 674a7e1abadSJonathan Lemon static int 675a7e1abadSJonathan Lemon ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts) 676a7e1abadSJonathan Lemon { 677a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 678a7e1abadSJonathan Lemon unsigned long flags; 679a7e1abadSJonathan Lemon 680a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 681a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(bp, ts); 682a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 683a7e1abadSJonathan Lemon 684a7e1abadSJonathan Lemon return 0; 685a7e1abadSJonathan Lemon } 686a7e1abadSJonathan Lemon 6876d59d4faSJonathan Lemon static void 68890f8f4c0SJonathan Lemon __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val) 6896d59d4faSJonathan Lemon { 6906d59d4faSJonathan Lemon u32 select, ctrl; 6916d59d4faSJonathan Lemon 6926d59d4faSJonathan Lemon select = ioread32(&bp->reg->select); 6936d59d4faSJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 6946d59d4faSJonathan Lemon 6956d59d4faSJonathan Lemon iowrite32(adj_val, &bp->reg->offset_ns); 69690f8f4c0SJonathan Lemon iowrite32(NSEC_PER_SEC, &bp->reg->offset_window_ns); 6976d59d4faSJonathan Lemon 6986d59d4faSJonathan Lemon ctrl = OCP_CTRL_ADJUST_OFFSET | OCP_CTRL_ENABLE; 6996d59d4faSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 7006d59d4faSJonathan Lemon 7016d59d4faSJonathan Lemon /* restore clock selection */ 7026d59d4faSJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 7036d59d4faSJonathan Lemon } 7046d59d4faSJonathan Lemon 70590f8f4c0SJonathan Lemon static void 70690f8f4c0SJonathan Lemon ptp_ocp_adjtime_coarse(struct ptp_ocp *bp, u64 delta_ns) 70790f8f4c0SJonathan Lemon { 70890f8f4c0SJonathan Lemon struct timespec64 ts; 70990f8f4c0SJonathan Lemon unsigned long flags; 71090f8f4c0SJonathan Lemon int err; 71190f8f4c0SJonathan Lemon 71290f8f4c0SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 71390f8f4c0SJonathan Lemon err = __ptp_ocp_gettime_locked(bp, &ts, NULL); 71490f8f4c0SJonathan Lemon if (likely(!err)) { 71590f8f4c0SJonathan Lemon timespec64_add_ns(&ts, delta_ns); 71690f8f4c0SJonathan Lemon __ptp_ocp_settime_locked(bp, &ts); 71790f8f4c0SJonathan Lemon } 71890f8f4c0SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 71990f8f4c0SJonathan Lemon } 72090f8f4c0SJonathan Lemon 721a7e1abadSJonathan Lemon static int 722a7e1abadSJonathan Lemon ptp_ocp_adjtime(struct ptp_clock_info *ptp_info, s64 delta_ns) 723a7e1abadSJonathan Lemon { 724a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 725a7e1abadSJonathan Lemon unsigned long flags; 7266d59d4faSJonathan Lemon u32 adj_ns, sign; 727a7e1abadSJonathan Lemon 72890f8f4c0SJonathan Lemon if (delta_ns > NSEC_PER_SEC || -delta_ns > NSEC_PER_SEC) { 72990f8f4c0SJonathan Lemon ptp_ocp_adjtime_coarse(bp, delta_ns); 73090f8f4c0SJonathan Lemon return 0; 73190f8f4c0SJonathan Lemon } 73290f8f4c0SJonathan Lemon 7336d59d4faSJonathan Lemon sign = delta_ns < 0 ? BIT(31) : 0; 7346d59d4faSJonathan Lemon adj_ns = sign ? -delta_ns : delta_ns; 735a7e1abadSJonathan Lemon 736a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 7376d59d4faSJonathan Lemon __ptp_ocp_adjtime_locked(bp, sign | adj_ns); 738a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 739a7e1abadSJonathan Lemon 7406d59d4faSJonathan Lemon return 0; 741a7e1abadSJonathan Lemon } 742a7e1abadSJonathan Lemon 743a7e1abadSJonathan Lemon static int 744a7e1abadSJonathan Lemon ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm) 745a7e1abadSJonathan Lemon { 746a7e1abadSJonathan Lemon if (scaled_ppm == 0) 747a7e1abadSJonathan Lemon return 0; 748a7e1abadSJonathan Lemon 749a7e1abadSJonathan Lemon return -EOPNOTSUPP; 750a7e1abadSJonathan Lemon } 751a7e1abadSJonathan Lemon 752773bda96SJonathan Lemon static int 7536d59d4faSJonathan Lemon ptp_ocp_null_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns) 754773bda96SJonathan Lemon { 755773bda96SJonathan Lemon return -EOPNOTSUPP; 756773bda96SJonathan Lemon } 757773bda96SJonathan Lemon 758773bda96SJonathan Lemon static int 759773bda96SJonathan Lemon ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq, 760773bda96SJonathan Lemon int on) 761773bda96SJonathan Lemon { 762773bda96SJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 763773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = NULL; 764a62a56d0SJonathan Lemon u32 req; 765773bda96SJonathan Lemon int err; 766773bda96SJonathan Lemon 767773bda96SJonathan Lemon switch (rq->type) { 768773bda96SJonathan Lemon case PTP_CLK_REQ_EXTTS: 769a62a56d0SJonathan Lemon req = OCP_REQ_TIMESTAMP; 770773bda96SJonathan Lemon switch (rq->extts.index) { 771773bda96SJonathan Lemon case 0: 772773bda96SJonathan Lemon ext = bp->ts0; 773773bda96SJonathan Lemon break; 774773bda96SJonathan Lemon case 1: 775773bda96SJonathan Lemon ext = bp->ts1; 776773bda96SJonathan Lemon break; 777dcf61469SJonathan Lemon case 2: 778dcf61469SJonathan Lemon ext = bp->ts2; 779dcf61469SJonathan Lemon break; 780a62a56d0SJonathan Lemon case 3: 781a62a56d0SJonathan Lemon ext = bp->pps; 782a62a56d0SJonathan Lemon break; 783773bda96SJonathan Lemon } 784773bda96SJonathan Lemon break; 785773bda96SJonathan Lemon case PTP_CLK_REQ_PPS: 786a62a56d0SJonathan Lemon req = OCP_REQ_PPS; 787773bda96SJonathan Lemon ext = bp->pps; 788773bda96SJonathan Lemon break; 789a62a56d0SJonathan Lemon case PTP_CLK_REQ_PEROUT: 790a62a56d0SJonathan Lemon if (on && 791a62a56d0SJonathan Lemon (rq->perout.period.sec != 1 || rq->perout.period.nsec != 0)) 792a62a56d0SJonathan Lemon return -EINVAL; 793a62a56d0SJonathan Lemon /* This is a request for 1PPS on an output SMA. 794a62a56d0SJonathan Lemon * Allow, but assume manual configuration. 795a62a56d0SJonathan Lemon */ 796a62a56d0SJonathan Lemon return 0; 797773bda96SJonathan Lemon default: 798773bda96SJonathan Lemon return -EOPNOTSUPP; 799773bda96SJonathan Lemon } 800773bda96SJonathan Lemon 801773bda96SJonathan Lemon err = -ENXIO; 802773bda96SJonathan Lemon if (ext) 803a62a56d0SJonathan Lemon err = ext->info->enable(ext, req, on); 804773bda96SJonathan Lemon 805773bda96SJonathan Lemon return err; 806773bda96SJonathan Lemon } 807773bda96SJonathan Lemon 808a7e1abadSJonathan Lemon static const struct ptp_clock_info ptp_ocp_clock_info = { 809a7e1abadSJonathan Lemon .owner = THIS_MODULE, 810a7e1abadSJonathan Lemon .name = KBUILD_MODNAME, 811a7e1abadSJonathan Lemon .max_adj = 100000000, 812a7e1abadSJonathan Lemon .gettimex64 = ptp_ocp_gettimex, 813a7e1abadSJonathan Lemon .settime64 = ptp_ocp_settime, 814a7e1abadSJonathan Lemon .adjtime = ptp_ocp_adjtime, 815a7e1abadSJonathan Lemon .adjfine = ptp_ocp_null_adjfine, 8166d59d4faSJonathan Lemon .adjphase = ptp_ocp_null_adjphase, 817773bda96SJonathan Lemon .enable = ptp_ocp_enable, 818773bda96SJonathan Lemon .pps = true, 819a62a56d0SJonathan Lemon .n_ext_ts = 4, 820a62a56d0SJonathan Lemon .n_per_out = 1, 821a7e1abadSJonathan Lemon }; 822a7e1abadSJonathan Lemon 823773bda96SJonathan Lemon static void 824773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp) 825773bda96SJonathan Lemon { 826773bda96SJonathan Lemon u32 ctrl, select; 827773bda96SJonathan Lemon 828773bda96SJonathan Lemon select = ioread32(&bp->reg->select); 829773bda96SJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 830773bda96SJonathan Lemon 831773bda96SJonathan Lemon iowrite32(0, &bp->reg->drift_ns); 832773bda96SJonathan Lemon 8331acffc6eSJonathan Lemon ctrl = OCP_CTRL_ADJUST_DRIFT | OCP_CTRL_ENABLE; 834773bda96SJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 835773bda96SJonathan Lemon 836773bda96SJonathan Lemon /* restore clock selection */ 837773bda96SJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 838773bda96SJonathan Lemon } 839773bda96SJonathan Lemon 840773bda96SJonathan Lemon static void 841e68462a0SVadim Fedorenko ptp_ocp_utc_distribute(struct ptp_ocp *bp, u32 val) 842e68462a0SVadim Fedorenko { 843e68462a0SVadim Fedorenko unsigned long flags; 844e68462a0SVadim Fedorenko 845e68462a0SVadim Fedorenko spin_lock_irqsave(&bp->lock, flags); 846e68462a0SVadim Fedorenko 847e68462a0SVadim Fedorenko bp->utc_tai_offset = val; 848e68462a0SVadim Fedorenko 849e68462a0SVadim Fedorenko if (bp->irig_out) 850e68462a0SVadim Fedorenko iowrite32(val, &bp->irig_out->adj_sec); 851e68462a0SVadim Fedorenko if (bp->dcf_out) 852e68462a0SVadim Fedorenko iowrite32(val, &bp->dcf_out->adj_sec); 853e68462a0SVadim Fedorenko if (bp->nmea_out) 854e68462a0SVadim Fedorenko iowrite32(val, &bp->nmea_out->adj_sec); 855e68462a0SVadim Fedorenko 856e68462a0SVadim Fedorenko spin_unlock_irqrestore(&bp->lock, flags); 857e68462a0SVadim Fedorenko } 858e68462a0SVadim Fedorenko 859e68462a0SVadim Fedorenko static void 860773bda96SJonathan Lemon ptp_ocp_watchdog(struct timer_list *t) 861773bda96SJonathan Lemon { 862773bda96SJonathan Lemon struct ptp_ocp *bp = from_timer(bp, t, watchdog); 863773bda96SJonathan Lemon unsigned long flags; 864e68462a0SVadim Fedorenko u32 status, utc_offset; 865773bda96SJonathan Lemon 8660d43d4f2SJonathan Lemon status = ioread32(&bp->pps_to_clk->status); 867773bda96SJonathan Lemon 868773bda96SJonathan Lemon if (status & PPS_STATUS_SUPERV_ERR) { 8690d43d4f2SJonathan Lemon iowrite32(status, &bp->pps_to_clk->status); 870ef0cfb34SJonathan Lemon if (!bp->gnss_lost) { 871773bda96SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 872773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(bp); 873773bda96SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 874ef0cfb34SJonathan Lemon bp->gnss_lost = ktime_get_real_seconds(); 875773bda96SJonathan Lemon } 876773bda96SJonathan Lemon 877ef0cfb34SJonathan Lemon } else if (bp->gnss_lost) { 878ef0cfb34SJonathan Lemon bp->gnss_lost = 0; 879773bda96SJonathan Lemon } 880773bda96SJonathan Lemon 881e68462a0SVadim Fedorenko /* if GNSS provides correct data we can rely on 882e68462a0SVadim Fedorenko * it to get leap second information 883e68462a0SVadim Fedorenko */ 884e68462a0SVadim Fedorenko if (bp->tod) { 885e68462a0SVadim Fedorenko status = ioread32(&bp->tod->utc_status); 886e68462a0SVadim Fedorenko utc_offset = status & TOD_STATUS_UTC_MASK; 887e68462a0SVadim Fedorenko if (status & TOD_STATUS_UTC_VALID && 888e68462a0SVadim Fedorenko utc_offset != bp->utc_tai_offset) 889e68462a0SVadim Fedorenko ptp_ocp_utc_distribute(bp, utc_offset); 890e68462a0SVadim Fedorenko } 891e68462a0SVadim Fedorenko 892773bda96SJonathan Lemon mod_timer(&bp->watchdog, jiffies + HZ); 893773bda96SJonathan Lemon } 894773bda96SJonathan Lemon 8951acffc6eSJonathan Lemon static void 8961acffc6eSJonathan Lemon ptp_ocp_estimate_pci_timing(struct ptp_ocp *bp) 8971acffc6eSJonathan Lemon { 8981acffc6eSJonathan Lemon ktime_t start, end; 8991acffc6eSJonathan Lemon ktime_t delay; 9001acffc6eSJonathan Lemon u32 ctrl; 9011acffc6eSJonathan Lemon 9021acffc6eSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 9031acffc6eSJonathan Lemon ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE; 9041acffc6eSJonathan Lemon 9051acffc6eSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 9061acffc6eSJonathan Lemon 9071acffc6eSJonathan Lemon start = ktime_get_ns(); 9081acffc6eSJonathan Lemon 9091acffc6eSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 9101acffc6eSJonathan Lemon 9111acffc6eSJonathan Lemon end = ktime_get_ns(); 9121acffc6eSJonathan Lemon 9131acffc6eSJonathan Lemon delay = end - start; 9141acffc6eSJonathan Lemon bp->ts_window_adjust = (delay >> 5) * 3; 9151acffc6eSJonathan Lemon } 9161acffc6eSJonathan Lemon 917a7e1abadSJonathan Lemon static int 918773bda96SJonathan Lemon ptp_ocp_init_clock(struct ptp_ocp *bp) 919a7e1abadSJonathan Lemon { 920a7e1abadSJonathan Lemon struct timespec64 ts; 921a7e1abadSJonathan Lemon bool sync; 922a7e1abadSJonathan Lemon u32 ctrl; 923a7e1abadSJonathan Lemon 9241acffc6eSJonathan Lemon ctrl = OCP_CTRL_ENABLE; 925a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 926a7e1abadSJonathan Lemon 927773bda96SJonathan Lemon /* NO DRIFT Correction */ 928773bda96SJonathan Lemon /* offset_p:i 1/8, offset_i: 1/16, drift_p: 0, drift_i: 0 */ 929773bda96SJonathan Lemon iowrite32(0x2000, &bp->reg->servo_offset_p); 930773bda96SJonathan Lemon iowrite32(0x1000, &bp->reg->servo_offset_i); 931773bda96SJonathan Lemon iowrite32(0, &bp->reg->servo_drift_p); 932773bda96SJonathan Lemon iowrite32(0, &bp->reg->servo_drift_i); 933773bda96SJonathan Lemon 934773bda96SJonathan Lemon /* latch servo values */ 935773bda96SJonathan Lemon ctrl |= OCP_CTRL_ADJUST_SERVO; 936773bda96SJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 937773bda96SJonathan Lemon 938a7e1abadSJonathan Lemon if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) { 939a7e1abadSJonathan Lemon dev_err(&bp->pdev->dev, "clock not enabled\n"); 940a7e1abadSJonathan Lemon return -ENODEV; 941a7e1abadSJonathan Lemon } 942a7e1abadSJonathan Lemon 9431acffc6eSJonathan Lemon ptp_ocp_estimate_pci_timing(bp); 9441acffc6eSJonathan Lemon 945a7e1abadSJonathan Lemon sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC; 946a7e1abadSJonathan Lemon if (!sync) { 947065efcc5SJonathan Lemon ktime_get_clocktai_ts64(&ts); 948a7e1abadSJonathan Lemon ptp_ocp_settime(&bp->ptp_info, &ts); 949a7e1abadSJonathan Lemon } 950a7e1abadSJonathan Lemon 951065efcc5SJonathan Lemon /* If there is a clock supervisor, then enable the watchdog */ 952065efcc5SJonathan Lemon if (bp->pps_to_clk) { 953773bda96SJonathan Lemon timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0); 954773bda96SJonathan Lemon mod_timer(&bp->watchdog, jiffies + HZ); 955065efcc5SJonathan Lemon } 956773bda96SJonathan Lemon 957a7e1abadSJonathan Lemon return 0; 958a7e1abadSJonathan Lemon } 959a7e1abadSJonathan Lemon 960a7e1abadSJonathan Lemon static void 961065efcc5SJonathan Lemon ptp_ocp_tod_init(struct ptp_ocp *bp) 962065efcc5SJonathan Lemon { 963065efcc5SJonathan Lemon u32 ctrl, reg; 964065efcc5SJonathan Lemon 965065efcc5SJonathan Lemon ctrl = ioread32(&bp->tod->ctrl); 966065efcc5SJonathan Lemon ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE; 967065efcc5SJonathan Lemon ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B); 968065efcc5SJonathan Lemon iowrite32(ctrl, &bp->tod->ctrl); 969065efcc5SJonathan Lemon 970065efcc5SJonathan Lemon reg = ioread32(&bp->tod->utc_status); 971065efcc5SJonathan Lemon if (reg & TOD_STATUS_UTC_VALID) 972065efcc5SJonathan Lemon ptp_ocp_utc_distribute(bp, reg & TOD_STATUS_UTC_MASK); 973065efcc5SJonathan Lemon } 974065efcc5SJonathan Lemon 9759f492c4cSVadim Fedorenko static const char * 9769f492c4cSVadim Fedorenko ptp_ocp_tod_proto_name(const int idx) 977a7e1abadSJonathan Lemon { 978a7e1abadSJonathan Lemon static const char * const proto_name[] = { 979a7e1abadSJonathan Lemon "NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none", 980a7e1abadSJonathan Lemon "UBX", "UBX_UTC", "UBX_LS", "UBX_none" 981a7e1abadSJonathan Lemon }; 9829f492c4cSVadim Fedorenko return proto_name[idx]; 9839f492c4cSVadim Fedorenko } 9849f492c4cSVadim Fedorenko 9859f492c4cSVadim Fedorenko static const char * 9869f492c4cSVadim Fedorenko ptp_ocp_tod_gnss_name(int idx) 9879f492c4cSVadim Fedorenko { 988a7e1abadSJonathan Lemon static const char * const gnss_name[] = { 989a7e1abadSJonathan Lemon "ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU", 9909f492c4cSVadim Fedorenko "Unknown" 991a7e1abadSJonathan Lemon }; 99272f00505SDan Carpenter if (idx >= ARRAY_SIZE(gnss_name)) 9939f492c4cSVadim Fedorenko idx = ARRAY_SIZE(gnss_name) - 1; 9949f492c4cSVadim Fedorenko return gnss_name[idx]; 995a7e1abadSJonathan Lemon } 996a7e1abadSJonathan Lemon 9970cfcdd1eSJonathan Lemon struct ptp_ocp_nvmem_match_info { 9980cfcdd1eSJonathan Lemon struct ptp_ocp *bp; 9990cfcdd1eSJonathan Lemon const void * const tag; 1000773bda96SJonathan Lemon }; 1001773bda96SJonathan Lemon 10020cfcdd1eSJonathan Lemon static int 10030cfcdd1eSJonathan Lemon ptp_ocp_nvmem_match(struct device *dev, const void *data) 10040cfcdd1eSJonathan Lemon { 10050cfcdd1eSJonathan Lemon const struct ptp_ocp_nvmem_match_info *info = data; 10060cfcdd1eSJonathan Lemon 10070cfcdd1eSJonathan Lemon dev = dev->parent; 10080cfcdd1eSJonathan Lemon if (!i2c_verify_client(dev) || info->tag != dev->platform_data) 10090cfcdd1eSJonathan Lemon return 0; 10100cfcdd1eSJonathan Lemon 10110cfcdd1eSJonathan Lemon while ((dev = dev->parent)) 10120cfcdd1eSJonathan Lemon if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME)) 10130cfcdd1eSJonathan Lemon return info->bp == dev_get_drvdata(dev); 1014773bda96SJonathan Lemon return 0; 1015773bda96SJonathan Lemon } 1016773bda96SJonathan Lemon 10170cfcdd1eSJonathan Lemon static inline struct nvmem_device * 10180cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_get(struct ptp_ocp *bp, const void * const tag) 1019773bda96SJonathan Lemon { 10200cfcdd1eSJonathan Lemon struct ptp_ocp_nvmem_match_info info = { .bp = bp, .tag = tag }; 10210cfcdd1eSJonathan Lemon 10220cfcdd1eSJonathan Lemon return nvmem_device_find(&info, ptp_ocp_nvmem_match); 10230cfcdd1eSJonathan Lemon } 10240cfcdd1eSJonathan Lemon 10250cfcdd1eSJonathan Lemon static inline void 10260cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_put(struct nvmem_device **nvmemp) 10270cfcdd1eSJonathan Lemon { 10280cfcdd1eSJonathan Lemon if (*nvmemp != NULL) { 10290cfcdd1eSJonathan Lemon nvmem_device_put(*nvmemp); 10300cfcdd1eSJonathan Lemon *nvmemp = NULL; 10310cfcdd1eSJonathan Lemon } 10320cfcdd1eSJonathan Lemon } 10330cfcdd1eSJonathan Lemon 10340cfcdd1eSJonathan Lemon static void 10350cfcdd1eSJonathan Lemon ptp_ocp_read_eeprom(struct ptp_ocp *bp) 10360cfcdd1eSJonathan Lemon { 10370cfcdd1eSJonathan Lemon const struct ptp_ocp_eeprom_map *map; 10380cfcdd1eSJonathan Lemon struct nvmem_device *nvmem; 10390cfcdd1eSJonathan Lemon const void *tag; 10400cfcdd1eSJonathan Lemon int ret; 1041773bda96SJonathan Lemon 10421447149dSJonathan Lemon if (!bp->i2c_ctrl) 10431447149dSJonathan Lemon return; 10441447149dSJonathan Lemon 10450cfcdd1eSJonathan Lemon tag = NULL; 10460cfcdd1eSJonathan Lemon nvmem = NULL; 1047773bda96SJonathan Lemon 10480cfcdd1eSJonathan Lemon for (map = bp->eeprom_map; map->len; map++) { 10490cfcdd1eSJonathan Lemon if (map->tag != tag) { 10500cfcdd1eSJonathan Lemon tag = map->tag; 10510cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_put(&nvmem); 10520cfcdd1eSJonathan Lemon } 10530cfcdd1eSJonathan Lemon if (!nvmem) { 10540cfcdd1eSJonathan Lemon nvmem = ptp_ocp_nvmem_device_get(bp, tag); 10550cfcdd1eSJonathan Lemon if (!nvmem) 1056773bda96SJonathan Lemon goto out; 1057773bda96SJonathan Lemon } 10580cfcdd1eSJonathan Lemon ret = nvmem_device_read(nvmem, map->off, map->len, 10590cfcdd1eSJonathan Lemon BP_MAP_ENTRY_ADDR(bp, map)); 10600cfcdd1eSJonathan Lemon if (ret != map->len) 10610cfcdd1eSJonathan Lemon goto read_fail; 1062773bda96SJonathan Lemon } 1063773bda96SJonathan Lemon 10640cfcdd1eSJonathan Lemon bp->has_eeprom_data = true; 1065773bda96SJonathan Lemon 1066773bda96SJonathan Lemon out: 10670cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_put(&nvmem); 10680cfcdd1eSJonathan Lemon return; 10690cfcdd1eSJonathan Lemon 10700cfcdd1eSJonathan Lemon read_fail: 10710cfcdd1eSJonathan Lemon dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", ret); 10720cfcdd1eSJonathan Lemon goto out; 10730cfcdd1eSJonathan Lemon } 10740cfcdd1eSJonathan Lemon 10750cfcdd1eSJonathan Lemon static int 10760cfcdd1eSJonathan Lemon ptp_ocp_firstchild(struct device *dev, void *data) 10770cfcdd1eSJonathan Lemon { 10780cfcdd1eSJonathan Lemon return 1; 1079773bda96SJonathan Lemon } 1080773bda96SJonathan Lemon 1081773bda96SJonathan Lemon static struct device * 1082773bda96SJonathan Lemon ptp_ocp_find_flash(struct ptp_ocp *bp) 1083773bda96SJonathan Lemon { 1084773bda96SJonathan Lemon struct device *dev, *last; 1085773bda96SJonathan Lemon 1086773bda96SJonathan Lemon last = NULL; 1087773bda96SJonathan Lemon dev = &bp->spi_flash->dev; 1088773bda96SJonathan Lemon 1089773bda96SJonathan Lemon while ((dev = device_find_child(dev, NULL, ptp_ocp_firstchild))) { 1090773bda96SJonathan Lemon if (!strcmp("mtd", dev_bus_name(dev))) 1091773bda96SJonathan Lemon break; 1092773bda96SJonathan Lemon put_device(last); 1093773bda96SJonathan Lemon last = dev; 1094773bda96SJonathan Lemon } 1095773bda96SJonathan Lemon put_device(last); 1096773bda96SJonathan Lemon 1097773bda96SJonathan Lemon return dev; 1098773bda96SJonathan Lemon } 1099773bda96SJonathan Lemon 1100773bda96SJonathan Lemon static int 1101773bda96SJonathan Lemon ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev, 1102773bda96SJonathan Lemon const struct firmware *fw) 1103773bda96SJonathan Lemon { 1104773bda96SJonathan Lemon struct mtd_info *mtd = dev_get_drvdata(dev); 1105773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 1106773bda96SJonathan Lemon size_t off, len, resid, wrote; 1107773bda96SJonathan Lemon struct erase_info erase; 1108773bda96SJonathan Lemon size_t base, blksz; 11097c807572SJonathan Lemon int err = 0; 1110773bda96SJonathan Lemon 1111773bda96SJonathan Lemon off = 0; 1112773bda96SJonathan Lemon base = bp->flash_start; 1113773bda96SJonathan Lemon blksz = 4096; 1114773bda96SJonathan Lemon resid = fw->size; 1115773bda96SJonathan Lemon 1116773bda96SJonathan Lemon while (resid) { 1117773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, "Flashing", 1118773bda96SJonathan Lemon NULL, off, fw->size); 1119773bda96SJonathan Lemon 1120773bda96SJonathan Lemon len = min_t(size_t, resid, blksz); 1121773bda96SJonathan Lemon erase.addr = base + off; 1122773bda96SJonathan Lemon erase.len = blksz; 1123773bda96SJonathan Lemon 1124773bda96SJonathan Lemon err = mtd_erase(mtd, &erase); 1125773bda96SJonathan Lemon if (err) 1126773bda96SJonathan Lemon goto out; 1127773bda96SJonathan Lemon 1128773bda96SJonathan Lemon err = mtd_write(mtd, base + off, len, &wrote, &fw->data[off]); 1129773bda96SJonathan Lemon if (err) 1130773bda96SJonathan Lemon goto out; 1131773bda96SJonathan Lemon 1132773bda96SJonathan Lemon off += blksz; 1133773bda96SJonathan Lemon resid -= len; 1134773bda96SJonathan Lemon } 1135773bda96SJonathan Lemon out: 1136773bda96SJonathan Lemon return err; 1137773bda96SJonathan Lemon } 1138773bda96SJonathan Lemon 1139773bda96SJonathan Lemon static int 1140773bda96SJonathan Lemon ptp_ocp_devlink_flash_update(struct devlink *devlink, 1141773bda96SJonathan Lemon struct devlink_flash_update_params *params, 1142773bda96SJonathan Lemon struct netlink_ext_ack *extack) 1143773bda96SJonathan Lemon { 1144773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 1145773bda96SJonathan Lemon struct device *dev; 1146773bda96SJonathan Lemon const char *msg; 1147773bda96SJonathan Lemon int err; 1148773bda96SJonathan Lemon 1149773bda96SJonathan Lemon dev = ptp_ocp_find_flash(bp); 1150773bda96SJonathan Lemon if (!dev) { 1151773bda96SJonathan Lemon dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n"); 1152773bda96SJonathan Lemon return -ENODEV; 1153773bda96SJonathan Lemon } 1154773bda96SJonathan Lemon 1155773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, "Preparing to flash", 1156773bda96SJonathan Lemon NULL, 0, 0); 1157773bda96SJonathan Lemon 1158773bda96SJonathan Lemon err = ptp_ocp_devlink_flash(devlink, dev, params->fw); 1159773bda96SJonathan Lemon 1160773bda96SJonathan Lemon msg = err ? "Flash error" : "Flash complete"; 1161773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0); 1162773bda96SJonathan Lemon 1163773bda96SJonathan Lemon put_device(dev); 1164773bda96SJonathan Lemon return err; 1165773bda96SJonathan Lemon } 1166773bda96SJonathan Lemon 1167773bda96SJonathan Lemon static int 1168773bda96SJonathan Lemon ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req, 1169773bda96SJonathan Lemon struct netlink_ext_ack *extack) 1170773bda96SJonathan Lemon { 1171773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 1172773bda96SJonathan Lemon char buf[32]; 1173773bda96SJonathan Lemon int err; 1174773bda96SJonathan Lemon 1175773bda96SJonathan Lemon err = devlink_info_driver_name_put(req, KBUILD_MODNAME); 1176773bda96SJonathan Lemon if (err) 1177773bda96SJonathan Lemon return err; 1178773bda96SJonathan Lemon 1179b0ca789aSJonathan Lemon if (bp->fw_version & 0xffff) { 1180b0ca789aSJonathan Lemon sprintf(buf, "%d", bp->fw_version); 1181b0ca789aSJonathan Lemon err = devlink_info_version_running_put(req, "fw", buf); 1182773bda96SJonathan Lemon } else { 1183b0ca789aSJonathan Lemon sprintf(buf, "%d", bp->fw_version >> 16); 1184b0ca789aSJonathan Lemon err = devlink_info_version_running_put(req, "loader", buf); 1185773bda96SJonathan Lemon } 1186773bda96SJonathan Lemon if (err) 1187773bda96SJonathan Lemon return err; 1188773bda96SJonathan Lemon 11890cfcdd1eSJonathan Lemon if (!bp->has_eeprom_data) { 11900cfcdd1eSJonathan Lemon ptp_ocp_read_eeprom(bp); 11910cfcdd1eSJonathan Lemon if (!bp->has_eeprom_data) 11920cfcdd1eSJonathan Lemon return 0; 11930cfcdd1eSJonathan Lemon } 1194773bda96SJonathan Lemon 1195773bda96SJonathan Lemon sprintf(buf, "%pM", bp->serial); 1196773bda96SJonathan Lemon err = devlink_info_serial_number_put(req, buf); 1197773bda96SJonathan Lemon if (err) 1198773bda96SJonathan Lemon return err; 11990cfcdd1eSJonathan Lemon 12000cfcdd1eSJonathan Lemon err = devlink_info_version_fixed_put(req, 12010cfcdd1eSJonathan Lemon DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, 12020cfcdd1eSJonathan Lemon bp->board_id); 12030cfcdd1eSJonathan Lemon if (err) 12040cfcdd1eSJonathan Lemon return err; 1205773bda96SJonathan Lemon 1206773bda96SJonathan Lemon return 0; 1207773bda96SJonathan Lemon } 1208773bda96SJonathan Lemon 1209773bda96SJonathan Lemon static const struct devlink_ops ptp_ocp_devlink_ops = { 1210773bda96SJonathan Lemon .flash_update = ptp_ocp_devlink_flash_update, 1211773bda96SJonathan Lemon .info_get = ptp_ocp_devlink_info_get, 1212773bda96SJonathan Lemon }; 1213773bda96SJonathan Lemon 1214773bda96SJonathan Lemon static void __iomem * 1215773bda96SJonathan Lemon __ptp_ocp_get_mem(struct ptp_ocp *bp, unsigned long start, int size) 1216773bda96SJonathan Lemon { 1217773bda96SJonathan Lemon struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp"); 1218773bda96SJonathan Lemon 1219773bda96SJonathan Lemon return devm_ioremap_resource(&bp->pdev->dev, &res); 1220773bda96SJonathan Lemon } 1221773bda96SJonathan Lemon 1222773bda96SJonathan Lemon static void __iomem * 1223773bda96SJonathan Lemon ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r) 1224773bda96SJonathan Lemon { 1225773bda96SJonathan Lemon unsigned long start; 1226773bda96SJonathan Lemon 1227773bda96SJonathan Lemon start = pci_resource_start(bp->pdev, 0) + r->offset; 1228773bda96SJonathan Lemon return __ptp_ocp_get_mem(bp, start, r->size); 1229773bda96SJonathan Lemon } 1230773bda96SJonathan Lemon 1231773bda96SJonathan Lemon static void 1232773bda96SJonathan Lemon ptp_ocp_set_irq_resource(struct resource *res, int irq) 1233773bda96SJonathan Lemon { 1234773bda96SJonathan Lemon struct resource r = DEFINE_RES_IRQ(irq); 1235773bda96SJonathan Lemon *res = r; 1236773bda96SJonathan Lemon } 1237773bda96SJonathan Lemon 1238773bda96SJonathan Lemon static void 1239773bda96SJonathan Lemon ptp_ocp_set_mem_resource(struct resource *res, unsigned long start, int size) 1240773bda96SJonathan Lemon { 1241773bda96SJonathan Lemon struct resource r = DEFINE_RES_MEM(start, size); 1242773bda96SJonathan Lemon *res = r; 1243773bda96SJonathan Lemon } 1244773bda96SJonathan Lemon 1245773bda96SJonathan Lemon static int 1246773bda96SJonathan Lemon ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r) 1247773bda96SJonathan Lemon { 1248773bda96SJonathan Lemon struct ptp_ocp_flash_info *info; 1249773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1250773bda96SJonathan Lemon struct platform_device *p; 1251773bda96SJonathan Lemon struct resource res[2]; 1252773bda96SJonathan Lemon unsigned long start; 1253773bda96SJonathan Lemon int id; 1254773bda96SJonathan Lemon 1255773bda96SJonathan Lemon start = pci_resource_start(pdev, 0) + r->offset; 1256773bda96SJonathan Lemon ptp_ocp_set_mem_resource(&res[0], start, r->size); 1257773bda96SJonathan Lemon ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1258773bda96SJonathan Lemon 1259773bda96SJonathan Lemon info = r->extra; 1260773bda96SJonathan Lemon id = pci_dev_id(pdev) << 1; 1261773bda96SJonathan Lemon id += info->pci_offset; 1262773bda96SJonathan Lemon 1263773bda96SJonathan Lemon p = platform_device_register_resndata(&pdev->dev, info->name, id, 1264773bda96SJonathan Lemon res, 2, info->data, 1265773bda96SJonathan Lemon info->data_size); 1266773bda96SJonathan Lemon if (IS_ERR(p)) 1267773bda96SJonathan Lemon return PTR_ERR(p); 1268773bda96SJonathan Lemon 1269773bda96SJonathan Lemon bp_assign_entry(bp, r, p); 1270773bda96SJonathan Lemon 1271773bda96SJonathan Lemon return 0; 1272773bda96SJonathan Lemon } 1273773bda96SJonathan Lemon 1274773bda96SJonathan Lemon static struct platform_device * 1275773bda96SJonathan Lemon ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id) 1276773bda96SJonathan Lemon { 12771618df6aSJonathan Lemon struct ptp_ocp_i2c_info *info; 1278773bda96SJonathan Lemon struct resource res[2]; 1279773bda96SJonathan Lemon unsigned long start; 1280773bda96SJonathan Lemon 12811618df6aSJonathan Lemon info = r->extra; 1282773bda96SJonathan Lemon start = pci_resource_start(pdev, 0) + r->offset; 1283773bda96SJonathan Lemon ptp_ocp_set_mem_resource(&res[0], start, r->size); 1284773bda96SJonathan Lemon ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1285773bda96SJonathan Lemon 12861618df6aSJonathan Lemon return platform_device_register_resndata(&pdev->dev, info->name, 12871618df6aSJonathan Lemon id, res, 2, 12881618df6aSJonathan Lemon info->data, info->data_size); 1289773bda96SJonathan Lemon } 1290773bda96SJonathan Lemon 1291773bda96SJonathan Lemon static int 1292773bda96SJonathan Lemon ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r) 1293773bda96SJonathan Lemon { 1294773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 12951618df6aSJonathan Lemon struct ptp_ocp_i2c_info *info; 1296773bda96SJonathan Lemon struct platform_device *p; 1297773bda96SJonathan Lemon struct clk_hw *clk; 1298773bda96SJonathan Lemon char buf[32]; 1299773bda96SJonathan Lemon int id; 1300773bda96SJonathan Lemon 13011618df6aSJonathan Lemon info = r->extra; 1302773bda96SJonathan Lemon id = pci_dev_id(bp->pdev); 1303773bda96SJonathan Lemon 1304773bda96SJonathan Lemon sprintf(buf, "AXI.%d", id); 13051618df6aSJonathan Lemon clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0, 13061618df6aSJonathan Lemon info->fixed_rate); 1307773bda96SJonathan Lemon if (IS_ERR(clk)) 1308773bda96SJonathan Lemon return PTR_ERR(clk); 1309773bda96SJonathan Lemon bp->i2c_clk = clk; 1310773bda96SJonathan Lemon 13111618df6aSJonathan Lemon sprintf(buf, "%s.%d", info->name, id); 1312773bda96SJonathan Lemon devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf); 1313773bda96SJonathan Lemon p = ptp_ocp_i2c_bus(bp->pdev, r, id); 1314773bda96SJonathan Lemon if (IS_ERR(p)) 1315773bda96SJonathan Lemon return PTR_ERR(p); 1316773bda96SJonathan Lemon 1317773bda96SJonathan Lemon bp_assign_entry(bp, r, p); 1318773bda96SJonathan Lemon 1319773bda96SJonathan Lemon return 0; 1320773bda96SJonathan Lemon } 1321773bda96SJonathan Lemon 1322773bda96SJonathan Lemon static irqreturn_t 1323773bda96SJonathan Lemon ptp_ocp_ts_irq(int irq, void *priv) 1324773bda96SJonathan Lemon { 1325773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1326773bda96SJonathan Lemon struct ts_reg __iomem *reg = ext->mem; 1327773bda96SJonathan Lemon struct ptp_clock_event ev; 1328773bda96SJonathan Lemon u32 sec, nsec; 1329773bda96SJonathan Lemon 1330a62a56d0SJonathan Lemon if (ext == ext->bp->pps) { 1331a62a56d0SJonathan Lemon if (ext->bp->pps_req_map & OCP_REQ_PPS) { 1332a62a56d0SJonathan Lemon ev.type = PTP_CLOCK_PPS; 1333a62a56d0SJonathan Lemon ptp_clock_event(ext->bp->ptp, &ev); 1334a62a56d0SJonathan Lemon } 1335a62a56d0SJonathan Lemon 1336a62a56d0SJonathan Lemon if ((ext->bp->pps_req_map & ~OCP_REQ_PPS) == 0) 1337a62a56d0SJonathan Lemon goto out; 1338a62a56d0SJonathan Lemon } 1339a62a56d0SJonathan Lemon 1340773bda96SJonathan Lemon /* XXX should fix API - this converts s/ns -> ts -> s/ns */ 1341773bda96SJonathan Lemon sec = ioread32(®->time_sec); 1342773bda96SJonathan Lemon nsec = ioread32(®->time_ns); 1343773bda96SJonathan Lemon 1344773bda96SJonathan Lemon ev.type = PTP_CLOCK_EXTTS; 1345773bda96SJonathan Lemon ev.index = ext->info->index; 13461acffc6eSJonathan Lemon ev.timestamp = sec * NSEC_PER_SEC + nsec; 1347773bda96SJonathan Lemon 1348773bda96SJonathan Lemon ptp_clock_event(ext->bp->ptp, &ev); 1349773bda96SJonathan Lemon 1350a62a56d0SJonathan Lemon out: 1351773bda96SJonathan Lemon iowrite32(1, ®->intr); /* write 1 to ack */ 1352773bda96SJonathan Lemon 1353773bda96SJonathan Lemon return IRQ_HANDLED; 1354773bda96SJonathan Lemon } 1355773bda96SJonathan Lemon 1356773bda96SJonathan Lemon static int 1357a62a56d0SJonathan Lemon ptp_ocp_ts_enable(void *priv, u32 req, bool enable) 1358773bda96SJonathan Lemon { 1359773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1360773bda96SJonathan Lemon struct ts_reg __iomem *reg = ext->mem; 1361a62a56d0SJonathan Lemon struct ptp_ocp *bp = ext->bp; 1362a62a56d0SJonathan Lemon 1363a62a56d0SJonathan Lemon if (ext == bp->pps) { 1364a62a56d0SJonathan Lemon u32 old_map = bp->pps_req_map; 1365a62a56d0SJonathan Lemon 1366a62a56d0SJonathan Lemon if (enable) 1367a62a56d0SJonathan Lemon bp->pps_req_map |= req; 1368a62a56d0SJonathan Lemon else 1369a62a56d0SJonathan Lemon bp->pps_req_map &= ~req; 1370a62a56d0SJonathan Lemon 1371a62a56d0SJonathan Lemon /* if no state change, just return */ 1372a62a56d0SJonathan Lemon if ((!!old_map ^ !!bp->pps_req_map) == 0) 1373a62a56d0SJonathan Lemon return 0; 1374a62a56d0SJonathan Lemon } 1375773bda96SJonathan Lemon 1376773bda96SJonathan Lemon if (enable) { 1377773bda96SJonathan Lemon iowrite32(1, ®->enable); 1378773bda96SJonathan Lemon iowrite32(1, ®->intr_mask); 1379773bda96SJonathan Lemon iowrite32(1, ®->intr); 1380773bda96SJonathan Lemon } else { 1381773bda96SJonathan Lemon iowrite32(0, ®->intr_mask); 1382773bda96SJonathan Lemon iowrite32(0, ®->enable); 1383773bda96SJonathan Lemon } 1384773bda96SJonathan Lemon 1385773bda96SJonathan Lemon return 0; 1386773bda96SJonathan Lemon } 1387773bda96SJonathan Lemon 1388773bda96SJonathan Lemon static void 1389773bda96SJonathan Lemon ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext) 1390773bda96SJonathan Lemon { 1391a62a56d0SJonathan Lemon ext->info->enable(ext, ~0, false); 1392773bda96SJonathan Lemon pci_free_irq(ext->bp->pdev, ext->irq_vec, ext); 1393773bda96SJonathan Lemon kfree(ext); 1394773bda96SJonathan Lemon } 1395773bda96SJonathan Lemon 1396773bda96SJonathan Lemon static int 1397773bda96SJonathan Lemon ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r) 1398773bda96SJonathan Lemon { 1399773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1400773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext; 1401773bda96SJonathan Lemon int err; 1402773bda96SJonathan Lemon 1403773bda96SJonathan Lemon ext = kzalloc(sizeof(*ext), GFP_KERNEL); 1404773bda96SJonathan Lemon if (!ext) 1405773bda96SJonathan Lemon return -ENOMEM; 1406773bda96SJonathan Lemon 1407773bda96SJonathan Lemon ext->mem = ptp_ocp_get_mem(bp, r); 1408c7521d3aSDan Carpenter if (IS_ERR(ext->mem)) { 1409c7521d3aSDan Carpenter err = PTR_ERR(ext->mem); 1410773bda96SJonathan Lemon goto out; 1411c7521d3aSDan Carpenter } 1412773bda96SJonathan Lemon 1413773bda96SJonathan Lemon ext->bp = bp; 1414773bda96SJonathan Lemon ext->info = r->extra; 1415773bda96SJonathan Lemon ext->irq_vec = r->irq_vec; 1416773bda96SJonathan Lemon 1417773bda96SJonathan Lemon err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL, 141856ec4403SJonathan Lemon ext, "ocp%d.%s", bp->id, r->name); 1419773bda96SJonathan Lemon if (err) { 1420773bda96SJonathan Lemon dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec); 1421773bda96SJonathan Lemon goto out; 1422773bda96SJonathan Lemon } 1423773bda96SJonathan Lemon 1424773bda96SJonathan Lemon bp_assign_entry(bp, r, ext); 1425773bda96SJonathan Lemon 1426773bda96SJonathan Lemon return 0; 1427773bda96SJonathan Lemon 1428773bda96SJonathan Lemon out: 1429773bda96SJonathan Lemon kfree(ext); 1430773bda96SJonathan Lemon return err; 1431773bda96SJonathan Lemon } 1432773bda96SJonathan Lemon 1433773bda96SJonathan Lemon static int 1434773bda96SJonathan Lemon ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r) 1435773bda96SJonathan Lemon { 1436773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1437773bda96SJonathan Lemon struct uart_8250_port uart; 1438773bda96SJonathan Lemon 1439773bda96SJonathan Lemon /* Setting UPF_IOREMAP and leaving port.membase unspecified lets 1440773bda96SJonathan Lemon * the serial port device claim and release the pci resource. 1441773bda96SJonathan Lemon */ 1442773bda96SJonathan Lemon memset(&uart, 0, sizeof(uart)); 1443773bda96SJonathan Lemon uart.port.dev = &pdev->dev; 1444773bda96SJonathan Lemon uart.port.iotype = UPIO_MEM; 1445773bda96SJonathan Lemon uart.port.regshift = 2; 1446773bda96SJonathan Lemon uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset; 1447773bda96SJonathan Lemon uart.port.irq = pci_irq_vector(pdev, r->irq_vec); 1448773bda96SJonathan Lemon uart.port.uartclk = 50000000; 1449c17c4059SJonathan Lemon uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP | UPF_NO_THRE_TEST; 1450773bda96SJonathan Lemon uart.port.type = PORT_16550A; 1451773bda96SJonathan Lemon 1452773bda96SJonathan Lemon return serial8250_register_8250_port(&uart); 1453773bda96SJonathan Lemon } 1454773bda96SJonathan Lemon 1455773bda96SJonathan Lemon static int 1456773bda96SJonathan Lemon ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r) 1457773bda96SJonathan Lemon { 1458773bda96SJonathan Lemon int port; 1459773bda96SJonathan Lemon 1460773bda96SJonathan Lemon port = ptp_ocp_serial_line(bp, r); 1461773bda96SJonathan Lemon if (port < 0) 1462773bda96SJonathan Lemon return port; 1463773bda96SJonathan Lemon 1464773bda96SJonathan Lemon bp_assign_entry(bp, r, port); 1465773bda96SJonathan Lemon 1466773bda96SJonathan Lemon return 0; 1467773bda96SJonathan Lemon } 1468773bda96SJonathan Lemon 1469773bda96SJonathan Lemon static int 1470773bda96SJonathan Lemon ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r) 1471773bda96SJonathan Lemon { 1472773bda96SJonathan Lemon void __iomem *mem; 1473773bda96SJonathan Lemon 1474773bda96SJonathan Lemon mem = ptp_ocp_get_mem(bp, r); 1475c7521d3aSDan Carpenter if (IS_ERR(mem)) 1476c7521d3aSDan Carpenter return PTR_ERR(mem); 1477773bda96SJonathan Lemon 1478773bda96SJonathan Lemon bp_assign_entry(bp, r, mem); 1479773bda96SJonathan Lemon 1480773bda96SJonathan Lemon return 0; 1481773bda96SJonathan Lemon } 1482773bda96SJonathan Lemon 1483e3516bb4SJonathan Lemon static void 1484e3516bb4SJonathan Lemon ptp_ocp_nmea_out_init(struct ptp_ocp *bp) 1485e3516bb4SJonathan Lemon { 1486e3516bb4SJonathan Lemon if (!bp->nmea_out) 1487e3516bb4SJonathan Lemon return; 1488e3516bb4SJonathan Lemon 1489e3516bb4SJonathan Lemon iowrite32(0, &bp->nmea_out->ctrl); /* disable */ 1490e3516bb4SJonathan Lemon iowrite32(7, &bp->nmea_out->uart_baud); /* 115200 */ 1491e3516bb4SJonathan Lemon iowrite32(1, &bp->nmea_out->ctrl); /* enable */ 1492e3516bb4SJonathan Lemon } 1493e3516bb4SJonathan Lemon 1494a509a7c6SJonathan Lemon static void 1495a509a7c6SJonathan Lemon ptp_ocp_sma_init(struct ptp_ocp *bp) 1496a509a7c6SJonathan Lemon { 1497a509a7c6SJonathan Lemon u32 reg; 1498a509a7c6SJonathan Lemon int i; 1499a509a7c6SJonathan Lemon 1500a509a7c6SJonathan Lemon /* defaults */ 1501a509a7c6SJonathan Lemon bp->sma[0].mode = SMA_MODE_IN; 1502a509a7c6SJonathan Lemon bp->sma[1].mode = SMA_MODE_IN; 1503a509a7c6SJonathan Lemon bp->sma[2].mode = SMA_MODE_OUT; 1504a509a7c6SJonathan Lemon bp->sma[3].mode = SMA_MODE_OUT; 1505a509a7c6SJonathan Lemon 1506a509a7c6SJonathan Lemon /* If no SMA1 map, the pin functions and directions are fixed. */ 1507a509a7c6SJonathan Lemon if (!bp->sma_map1) { 1508a509a7c6SJonathan Lemon for (i = 0; i < 4; i++) { 1509a509a7c6SJonathan Lemon bp->sma[i].fixed_fcn = true; 1510a509a7c6SJonathan Lemon bp->sma[i].fixed_dir = true; 1511a509a7c6SJonathan Lemon } 1512a509a7c6SJonathan Lemon return; 1513a509a7c6SJonathan Lemon } 1514a509a7c6SJonathan Lemon 1515a509a7c6SJonathan Lemon /* If SMA2 GPIO output map is all 1, it is not present. 1516a509a7c6SJonathan Lemon * This indicates the firmware has fixed direction SMA pins. 1517a509a7c6SJonathan Lemon */ 1518a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map2->gpio2); 1519a509a7c6SJonathan Lemon if (reg == 0xffffffff) { 1520a509a7c6SJonathan Lemon for (i = 0; i < 4; i++) 1521a509a7c6SJonathan Lemon bp->sma[i].fixed_dir = true; 1522a509a7c6SJonathan Lemon } else { 1523a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map1->gpio1); 1524a509a7c6SJonathan Lemon bp->sma[0].mode = reg & BIT(15) ? SMA_MODE_IN : SMA_MODE_OUT; 1525a509a7c6SJonathan Lemon bp->sma[1].mode = reg & BIT(31) ? SMA_MODE_IN : SMA_MODE_OUT; 1526a509a7c6SJonathan Lemon 1527a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map1->gpio2); 1528a509a7c6SJonathan Lemon bp->sma[2].mode = reg & BIT(15) ? SMA_MODE_OUT : SMA_MODE_IN; 1529a509a7c6SJonathan Lemon bp->sma[3].mode = reg & BIT(31) ? SMA_MODE_OUT : SMA_MODE_IN; 1530a509a7c6SJonathan Lemon } 1531a509a7c6SJonathan Lemon } 1532a509a7c6SJonathan Lemon 1533773bda96SJonathan Lemon /* FB specific board initializers; last "resource" registered. */ 1534773bda96SJonathan Lemon static int 1535773bda96SJonathan Lemon ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r) 1536773bda96SJonathan Lemon { 1537773bda96SJonathan Lemon bp->flash_start = 1024 * 4096; 15380cfcdd1eSJonathan Lemon bp->eeprom_map = fb_eeprom_map; 1539b0ca789aSJonathan Lemon bp->fw_version = ioread32(&bp->image->version); 1540*c205d53cSJonathan Lemon bp->attr_tbl = fb_timecard_groups; 1541*c205d53cSJonathan Lemon bp->fw_cap = OCP_CAP_BASIC; 1542773bda96SJonathan Lemon 1543065efcc5SJonathan Lemon ptp_ocp_tod_init(bp); 1544e3516bb4SJonathan Lemon ptp_ocp_nmea_out_init(bp); 1545a509a7c6SJonathan Lemon ptp_ocp_sma_init(bp); 1546065efcc5SJonathan Lemon 1547773bda96SJonathan Lemon return ptp_ocp_init_clock(bp); 1548773bda96SJonathan Lemon } 1549773bda96SJonathan Lemon 155056ec4403SJonathan Lemon static bool 155156ec4403SJonathan Lemon ptp_ocp_allow_irq(struct ptp_ocp *bp, struct ocp_resource *r) 155256ec4403SJonathan Lemon { 155356ec4403SJonathan Lemon bool allow = !r->irq_vec || r->irq_vec < bp->n_irqs; 155456ec4403SJonathan Lemon 155556ec4403SJonathan Lemon if (!allow) 155656ec4403SJonathan Lemon dev_err(&bp->pdev->dev, "irq %d out of range, skipping %s\n", 155756ec4403SJonathan Lemon r->irq_vec, r->name); 155856ec4403SJonathan Lemon return allow; 155956ec4403SJonathan Lemon } 156056ec4403SJonathan Lemon 1561773bda96SJonathan Lemon static int 1562773bda96SJonathan Lemon ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data) 1563773bda96SJonathan Lemon { 1564773bda96SJonathan Lemon struct ocp_resource *r, *table; 1565773bda96SJonathan Lemon int err = 0; 1566773bda96SJonathan Lemon 1567773bda96SJonathan Lemon table = (struct ocp_resource *)driver_data; 1568773bda96SJonathan Lemon for (r = table; r->setup; r++) { 156956ec4403SJonathan Lemon if (!ptp_ocp_allow_irq(bp, r)) 157056ec4403SJonathan Lemon continue; 1571773bda96SJonathan Lemon err = r->setup(bp, r); 1572bceff290SJonathan Lemon if (err) { 1573bceff290SJonathan Lemon dev_err(&bp->pdev->dev, 1574bceff290SJonathan Lemon "Could not register %s: err %d\n", 1575bceff290SJonathan Lemon r->name, err); 1576773bda96SJonathan Lemon break; 1577773bda96SJonathan Lemon } 1578bceff290SJonathan Lemon } 1579773bda96SJonathan Lemon return err; 1580773bda96SJonathan Lemon } 1581773bda96SJonathan Lemon 15826baf2925SJonathan Lemon static void 15836baf2925SJonathan Lemon ptp_ocp_enable_fpga(u32 __iomem *reg, u32 bit, bool enable) 15846baf2925SJonathan Lemon { 15856baf2925SJonathan Lemon u32 ctrl; 15866baf2925SJonathan Lemon bool on; 15876baf2925SJonathan Lemon 15886baf2925SJonathan Lemon ctrl = ioread32(reg); 15896baf2925SJonathan Lemon on = ctrl & bit; 15906baf2925SJonathan Lemon if (on ^ enable) { 15916baf2925SJonathan Lemon ctrl &= ~bit; 15926baf2925SJonathan Lemon ctrl |= enable ? bit : 0; 15936baf2925SJonathan Lemon iowrite32(ctrl, reg); 15946baf2925SJonathan Lemon } 15956baf2925SJonathan Lemon } 15966baf2925SJonathan Lemon 15976baf2925SJonathan Lemon static void 15986baf2925SJonathan Lemon ptp_ocp_irig_out(struct ptp_ocp *bp, bool enable) 15996baf2925SJonathan Lemon { 16006baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->irig_out->ctrl, 16016baf2925SJonathan Lemon IRIG_M_CTRL_ENABLE, enable); 16026baf2925SJonathan Lemon } 16036baf2925SJonathan Lemon 16046baf2925SJonathan Lemon static void 16056baf2925SJonathan Lemon ptp_ocp_irig_in(struct ptp_ocp *bp, bool enable) 16066baf2925SJonathan Lemon { 16076baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->irig_in->ctrl, 16086baf2925SJonathan Lemon IRIG_S_CTRL_ENABLE, enable); 16096baf2925SJonathan Lemon } 16106baf2925SJonathan Lemon 16116baf2925SJonathan Lemon static void 16126baf2925SJonathan Lemon ptp_ocp_dcf_out(struct ptp_ocp *bp, bool enable) 16136baf2925SJonathan Lemon { 16146baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->dcf_out->ctrl, 16156baf2925SJonathan Lemon DCF_M_CTRL_ENABLE, enable); 16166baf2925SJonathan Lemon } 16176baf2925SJonathan Lemon 16186baf2925SJonathan Lemon static void 16196baf2925SJonathan Lemon ptp_ocp_dcf_in(struct ptp_ocp *bp, bool enable) 16206baf2925SJonathan Lemon { 16216baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->dcf_in->ctrl, 16226baf2925SJonathan Lemon DCF_S_CTRL_ENABLE, enable); 16236baf2925SJonathan Lemon } 16246baf2925SJonathan Lemon 16256baf2925SJonathan Lemon static void 16266baf2925SJonathan Lemon __handle_signal_outputs(struct ptp_ocp *bp, u32 val) 16276baf2925SJonathan Lemon { 16286baf2925SJonathan Lemon ptp_ocp_irig_out(bp, val & 0x00100010); 16296baf2925SJonathan Lemon ptp_ocp_dcf_out(bp, val & 0x00200020); 16306baf2925SJonathan Lemon } 16316baf2925SJonathan Lemon 16326baf2925SJonathan Lemon static void 16336baf2925SJonathan Lemon __handle_signal_inputs(struct ptp_ocp *bp, u32 val) 16346baf2925SJonathan Lemon { 16356baf2925SJonathan Lemon ptp_ocp_irig_in(bp, val & 0x00100010); 16366baf2925SJonathan Lemon ptp_ocp_dcf_in(bp, val & 0x00200020); 16376baf2925SJonathan Lemon } 16386baf2925SJonathan Lemon 1639e1daf0ecSJonathan Lemon /* 1640e1daf0ecSJonathan Lemon * ANT0 == gps (in) 1641e1daf0ecSJonathan Lemon * ANT1 == sma1 (in) 1642e1daf0ecSJonathan Lemon * ANT2 == sma2 (in) 1643e1daf0ecSJonathan Lemon * ANT3 == sma3 (out) 1644e1daf0ecSJonathan Lemon * ANT4 == sma4 (out) 1645e1daf0ecSJonathan Lemon */ 1646e1daf0ecSJonathan Lemon 1647e1daf0ecSJonathan Lemon static ssize_t 1648b2c4f0acSJonathan Lemon ptp_ocp_show_output(u32 val, char *buf, int def_val) 1649e1daf0ecSJonathan Lemon { 1650e1daf0ecSJonathan Lemon const char *name; 1651e1daf0ecSJonathan Lemon ssize_t count; 1652e1daf0ecSJonathan Lemon 1653e1daf0ecSJonathan Lemon count = sysfs_emit(buf, "OUT: "); 1654e1daf0ecSJonathan Lemon name = ptp_ocp_select_name_from_val(ptp_ocp_sma_out, val); 1655e1daf0ecSJonathan Lemon if (!name) 1656b2c4f0acSJonathan Lemon name = ptp_ocp_select_name_from_val(ptp_ocp_sma_out, def_val); 1657e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s\n", name); 1658e1daf0ecSJonathan Lemon return count; 1659e1daf0ecSJonathan Lemon } 1660e1daf0ecSJonathan Lemon 1661e1daf0ecSJonathan Lemon static ssize_t 1662b2c4f0acSJonathan Lemon ptp_ocp_show_inputs(u32 val, char *buf, int def_val) 1663e1daf0ecSJonathan Lemon { 1664e1daf0ecSJonathan Lemon const char *name; 1665e1daf0ecSJonathan Lemon ssize_t count; 1666e1daf0ecSJonathan Lemon int i; 1667e1daf0ecSJonathan Lemon 1668e1daf0ecSJonathan Lemon count = sysfs_emit(buf, "IN: "); 1669e1daf0ecSJonathan Lemon for (i = 0; i < ARRAY_SIZE(ptp_ocp_sma_in); i++) { 1670e1daf0ecSJonathan Lemon if (val & ptp_ocp_sma_in[i].value) { 1671e1daf0ecSJonathan Lemon name = ptp_ocp_sma_in[i].name; 1672e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", name); 1673e1daf0ecSJonathan Lemon } 1674e1daf0ecSJonathan Lemon } 1675b2c4f0acSJonathan Lemon if (!val && def_val >= 0) { 1676b2c4f0acSJonathan Lemon name = ptp_ocp_select_name_from_val(ptp_ocp_sma_in, def_val); 1677b2c4f0acSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", name); 1678b2c4f0acSJonathan Lemon } 1679e1daf0ecSJonathan Lemon if (count) 1680e1daf0ecSJonathan Lemon count--; 1681e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "\n"); 1682e1daf0ecSJonathan Lemon return count; 1683e1daf0ecSJonathan Lemon } 1684e1daf0ecSJonathan Lemon 1685e1daf0ecSJonathan Lemon static int 1686e1daf0ecSJonathan Lemon sma_parse_inputs(const char *buf, enum ptp_ocp_sma_mode *mode) 1687e1daf0ecSJonathan Lemon { 1688e1daf0ecSJonathan Lemon struct ocp_selector *tbl[] = { ptp_ocp_sma_in, ptp_ocp_sma_out }; 1689e1daf0ecSJonathan Lemon int idx, count, dir; 1690e1daf0ecSJonathan Lemon char **argv; 1691e1daf0ecSJonathan Lemon int ret; 1692e1daf0ecSJonathan Lemon 1693e1daf0ecSJonathan Lemon argv = argv_split(GFP_KERNEL, buf, &count); 1694e1daf0ecSJonathan Lemon if (!argv) 1695e1daf0ecSJonathan Lemon return -ENOMEM; 1696e1daf0ecSJonathan Lemon 1697e1daf0ecSJonathan Lemon ret = -EINVAL; 1698e1daf0ecSJonathan Lemon if (!count) 1699e1daf0ecSJonathan Lemon goto out; 1700e1daf0ecSJonathan Lemon 1701e1daf0ecSJonathan Lemon idx = 0; 1702e1daf0ecSJonathan Lemon dir = *mode == SMA_MODE_IN ? 0 : 1; 1703a509a7c6SJonathan Lemon if (!strcasecmp("IN:", argv[0])) { 1704e1daf0ecSJonathan Lemon dir = 0; 1705e1daf0ecSJonathan Lemon idx++; 1706e1daf0ecSJonathan Lemon } 1707e1daf0ecSJonathan Lemon if (!strcasecmp("OUT:", argv[0])) { 1708e1daf0ecSJonathan Lemon dir = 1; 1709e1daf0ecSJonathan Lemon idx++; 1710e1daf0ecSJonathan Lemon } 1711e1daf0ecSJonathan Lemon *mode = dir == 0 ? SMA_MODE_IN : SMA_MODE_OUT; 1712e1daf0ecSJonathan Lemon 1713e1daf0ecSJonathan Lemon ret = 0; 1714e1daf0ecSJonathan Lemon for (; idx < count; idx++) 1715e1daf0ecSJonathan Lemon ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]); 1716e1daf0ecSJonathan Lemon if (ret < 0) 1717e1daf0ecSJonathan Lemon ret = -EINVAL; 1718e1daf0ecSJonathan Lemon 1719e1daf0ecSJonathan Lemon out: 1720e1daf0ecSJonathan Lemon argv_free(argv); 1721e1daf0ecSJonathan Lemon return ret; 1722e1daf0ecSJonathan Lemon } 1723e1daf0ecSJonathan Lemon 1724a509a7c6SJonathan Lemon static u32 1725a509a7c6SJonathan Lemon ptp_ocp_sma_get(struct ptp_ocp *bp, int sma_nr, enum ptp_ocp_sma_mode mode) 1726e1daf0ecSJonathan Lemon { 1727a509a7c6SJonathan Lemon u32 __iomem *gpio; 1728a509a7c6SJonathan Lemon u32 shift; 1729a509a7c6SJonathan Lemon 1730a509a7c6SJonathan Lemon if (bp->sma[sma_nr - 1].fixed_fcn) 1731a509a7c6SJonathan Lemon return (sma_nr - 1) & 1; 1732a509a7c6SJonathan Lemon 1733a509a7c6SJonathan Lemon if (mode == SMA_MODE_IN) 1734a509a7c6SJonathan Lemon gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1; 1735a509a7c6SJonathan Lemon else 1736a509a7c6SJonathan Lemon gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2; 1737a509a7c6SJonathan Lemon shift = sma_nr & 1 ? 0 : 16; 1738a509a7c6SJonathan Lemon 1739a509a7c6SJonathan Lemon return (ioread32(gpio) >> shift) & 0xffff; 1740a509a7c6SJonathan Lemon } 1741a509a7c6SJonathan Lemon 1742a509a7c6SJonathan Lemon static ssize_t 1743a509a7c6SJonathan Lemon ptp_ocp_sma_show(struct ptp_ocp *bp, int sma_nr, char *buf, 1744b2c4f0acSJonathan Lemon int default_in_val, int default_out_val) 1745a509a7c6SJonathan Lemon { 1746a509a7c6SJonathan Lemon struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1]; 1747a509a7c6SJonathan Lemon u32 val; 1748a509a7c6SJonathan Lemon 1749a509a7c6SJonathan Lemon val = ptp_ocp_sma_get(bp, sma_nr, sma->mode) & SMA_SELECT_MASK; 1750e1daf0ecSJonathan Lemon 1751b2c4f0acSJonathan Lemon if (sma->mode == SMA_MODE_IN) { 1752b2c4f0acSJonathan Lemon if (sma->disabled) 1753b2c4f0acSJonathan Lemon val = SMA_DISABLE; 1754b2c4f0acSJonathan Lemon return ptp_ocp_show_inputs(val, buf, default_in_val); 1755b2c4f0acSJonathan Lemon } 1756e1daf0ecSJonathan Lemon 1757b2c4f0acSJonathan Lemon return ptp_ocp_show_output(val, buf, default_out_val); 1758e1daf0ecSJonathan Lemon } 1759e1daf0ecSJonathan Lemon 1760e1daf0ecSJonathan Lemon static ssize_t 1761e1daf0ecSJonathan Lemon sma1_show(struct device *dev, struct device_attribute *attr, char *buf) 1762e1daf0ecSJonathan Lemon { 1763e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1764e1daf0ecSJonathan Lemon 1765a509a7c6SJonathan Lemon return ptp_ocp_sma_show(bp, 1, buf, 0, 1); 1766e1daf0ecSJonathan Lemon } 1767e1daf0ecSJonathan Lemon 1768e1daf0ecSJonathan Lemon static ssize_t 1769e1daf0ecSJonathan Lemon sma2_show(struct device *dev, struct device_attribute *attr, char *buf) 1770e1daf0ecSJonathan Lemon { 1771e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1772e1daf0ecSJonathan Lemon 1773a509a7c6SJonathan Lemon return ptp_ocp_sma_show(bp, 2, buf, -1, 1); 1774e1daf0ecSJonathan Lemon } 1775e1daf0ecSJonathan Lemon 1776e1daf0ecSJonathan Lemon static ssize_t 1777e1daf0ecSJonathan Lemon sma3_show(struct device *dev, struct device_attribute *attr, char *buf) 1778e1daf0ecSJonathan Lemon { 1779e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1780e1daf0ecSJonathan Lemon 1781a509a7c6SJonathan Lemon return ptp_ocp_sma_show(bp, 3, buf, -1, 0); 1782e1daf0ecSJonathan Lemon } 1783e1daf0ecSJonathan Lemon 1784e1daf0ecSJonathan Lemon static ssize_t 1785e1daf0ecSJonathan Lemon sma4_show(struct device *dev, struct device_attribute *attr, char *buf) 1786e1daf0ecSJonathan Lemon { 1787e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1788e1daf0ecSJonathan Lemon 1789a509a7c6SJonathan Lemon return ptp_ocp_sma_show(bp, 4, buf, -1, 1); 1790e1daf0ecSJonathan Lemon } 1791e1daf0ecSJonathan Lemon 1792e1daf0ecSJonathan Lemon static void 1793a509a7c6SJonathan Lemon ptp_ocp_sma_store_output(struct ptp_ocp *bp, int sma_nr, u32 val) 1794e1daf0ecSJonathan Lemon { 1795a509a7c6SJonathan Lemon u32 reg, mask, shift; 1796e1daf0ecSJonathan Lemon unsigned long flags; 1797a509a7c6SJonathan Lemon u32 __iomem *gpio; 1798a509a7c6SJonathan Lemon 1799a509a7c6SJonathan Lemon gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2; 1800a509a7c6SJonathan Lemon shift = sma_nr & 1 ? 0 : 16; 1801e1daf0ecSJonathan Lemon 1802e1daf0ecSJonathan Lemon mask = 0xffff << (16 - shift); 1803e1daf0ecSJonathan Lemon 1804e1daf0ecSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1805e1daf0ecSJonathan Lemon 1806a509a7c6SJonathan Lemon reg = ioread32(gpio); 1807a509a7c6SJonathan Lemon reg = (reg & mask) | (val << shift); 18086baf2925SJonathan Lemon 1809a509a7c6SJonathan Lemon __handle_signal_outputs(bp, reg); 18106baf2925SJonathan Lemon 1811a509a7c6SJonathan Lemon iowrite32(reg, gpio); 1812e1daf0ecSJonathan Lemon 1813e1daf0ecSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1814e1daf0ecSJonathan Lemon } 1815e1daf0ecSJonathan Lemon 1816e1daf0ecSJonathan Lemon static void 1817a509a7c6SJonathan Lemon ptp_ocp_sma_store_inputs(struct ptp_ocp *bp, int sma_nr, u32 val) 1818e1daf0ecSJonathan Lemon { 1819a509a7c6SJonathan Lemon u32 reg, mask, shift; 1820e1daf0ecSJonathan Lemon unsigned long flags; 1821a509a7c6SJonathan Lemon u32 __iomem *gpio; 1822a509a7c6SJonathan Lemon 1823a509a7c6SJonathan Lemon gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1; 1824a509a7c6SJonathan Lemon shift = sma_nr & 1 ? 0 : 16; 1825e1daf0ecSJonathan Lemon 1826e1daf0ecSJonathan Lemon mask = 0xffff << (16 - shift); 1827e1daf0ecSJonathan Lemon 1828e1daf0ecSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1829e1daf0ecSJonathan Lemon 1830a509a7c6SJonathan Lemon reg = ioread32(gpio); 1831a509a7c6SJonathan Lemon reg = (reg & mask) | (val << shift); 18326baf2925SJonathan Lemon 1833a509a7c6SJonathan Lemon __handle_signal_inputs(bp, reg); 18346baf2925SJonathan Lemon 1835a509a7c6SJonathan Lemon iowrite32(reg, gpio); 1836e1daf0ecSJonathan Lemon 1837e1daf0ecSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1838e1daf0ecSJonathan Lemon } 1839e1daf0ecSJonathan Lemon 1840a509a7c6SJonathan Lemon static int 1841a509a7c6SJonathan Lemon ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr) 1842e1daf0ecSJonathan Lemon { 1843a509a7c6SJonathan Lemon struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1]; 1844e1daf0ecSJonathan Lemon enum ptp_ocp_sma_mode mode; 1845e1daf0ecSJonathan Lemon int val; 1846e1daf0ecSJonathan Lemon 1847e1daf0ecSJonathan Lemon mode = sma->mode; 1848e1daf0ecSJonathan Lemon val = sma_parse_inputs(buf, &mode); 1849e1daf0ecSJonathan Lemon if (val < 0) 1850e1daf0ecSJonathan Lemon return val; 1851e1daf0ecSJonathan Lemon 1852b2c4f0acSJonathan Lemon if (sma->fixed_dir && (mode != sma->mode || val & SMA_DISABLE)) 1853e1daf0ecSJonathan Lemon return -EOPNOTSUPP; 1854e1daf0ecSJonathan Lemon 1855a509a7c6SJonathan Lemon if (sma->fixed_fcn) { 1856a509a7c6SJonathan Lemon if (val != ((sma_nr - 1) & 1)) 1857e1daf0ecSJonathan Lemon return -EOPNOTSUPP; 1858a509a7c6SJonathan Lemon return 0; 1859e1daf0ecSJonathan Lemon } 1860e1daf0ecSJonathan Lemon 1861b2c4f0acSJonathan Lemon sma->disabled = !!(val & SMA_DISABLE); 1862b2c4f0acSJonathan Lemon 1863a509a7c6SJonathan Lemon if (mode != sma->mode) { 1864a509a7c6SJonathan Lemon if (mode == SMA_MODE_IN) 1865a509a7c6SJonathan Lemon ptp_ocp_sma_store_output(bp, sma_nr, 0); 1866e1daf0ecSJonathan Lemon else 1867a509a7c6SJonathan Lemon ptp_ocp_sma_store_inputs(bp, sma_nr, 0); 1868a509a7c6SJonathan Lemon sma->mode = mode; 1869a509a7c6SJonathan Lemon } 1870a509a7c6SJonathan Lemon 1871a509a7c6SJonathan Lemon if (!sma->fixed_dir) 1872a509a7c6SJonathan Lemon val |= SMA_ENABLE; /* add enable bit */ 1873a509a7c6SJonathan Lemon 1874b2c4f0acSJonathan Lemon if (sma->disabled) 1875b2c4f0acSJonathan Lemon val = 0; 1876b2c4f0acSJonathan Lemon 1877a509a7c6SJonathan Lemon if (mode == SMA_MODE_IN) 1878a509a7c6SJonathan Lemon ptp_ocp_sma_store_inputs(bp, sma_nr, val); 1879a509a7c6SJonathan Lemon else 1880a509a7c6SJonathan Lemon ptp_ocp_sma_store_output(bp, sma_nr, val); 1881e1daf0ecSJonathan Lemon 1882e1daf0ecSJonathan Lemon return 0; 1883e1daf0ecSJonathan Lemon } 1884e1daf0ecSJonathan Lemon 1885e1daf0ecSJonathan Lemon static ssize_t 1886e1daf0ecSJonathan Lemon sma1_store(struct device *dev, struct device_attribute *attr, 1887e1daf0ecSJonathan Lemon const char *buf, size_t count) 1888e1daf0ecSJonathan Lemon { 1889e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1890e1daf0ecSJonathan Lemon int err; 1891e1daf0ecSJonathan Lemon 1892a509a7c6SJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 1); 1893e1daf0ecSJonathan Lemon return err ? err : count; 1894e1daf0ecSJonathan Lemon } 1895e1daf0ecSJonathan Lemon 1896e1daf0ecSJonathan Lemon static ssize_t 1897e1daf0ecSJonathan Lemon sma2_store(struct device *dev, struct device_attribute *attr, 1898e1daf0ecSJonathan Lemon const char *buf, size_t count) 1899e1daf0ecSJonathan Lemon { 1900e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1901e1daf0ecSJonathan Lemon int err; 1902e1daf0ecSJonathan Lemon 1903a509a7c6SJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 2); 1904e1daf0ecSJonathan Lemon return err ? err : count; 1905e1daf0ecSJonathan Lemon } 1906e1daf0ecSJonathan Lemon 1907e1daf0ecSJonathan Lemon static ssize_t 1908e1daf0ecSJonathan Lemon sma3_store(struct device *dev, struct device_attribute *attr, 1909e1daf0ecSJonathan Lemon const char *buf, size_t count) 1910e1daf0ecSJonathan Lemon { 1911e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1912e1daf0ecSJonathan Lemon int err; 1913e1daf0ecSJonathan Lemon 1914a509a7c6SJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 3); 1915e1daf0ecSJonathan Lemon return err ? err : count; 1916e1daf0ecSJonathan Lemon } 1917e1daf0ecSJonathan Lemon 1918e1daf0ecSJonathan Lemon static ssize_t 1919e1daf0ecSJonathan Lemon sma4_store(struct device *dev, struct device_attribute *attr, 1920e1daf0ecSJonathan Lemon const char *buf, size_t count) 1921e1daf0ecSJonathan Lemon { 1922e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1923e1daf0ecSJonathan Lemon int err; 1924e1daf0ecSJonathan Lemon 1925a509a7c6SJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 4); 1926e1daf0ecSJonathan Lemon return err ? err : count; 1927e1daf0ecSJonathan Lemon } 1928e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma1); 1929e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma2); 1930e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma3); 1931e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma4); 1932e1daf0ecSJonathan Lemon 1933e1daf0ecSJonathan Lemon static ssize_t 1934e1daf0ecSJonathan Lemon available_sma_inputs_show(struct device *dev, 1935e1daf0ecSJonathan Lemon struct device_attribute *attr, char *buf) 1936e1daf0ecSJonathan Lemon { 1937e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_sma_in, buf); 1938e1daf0ecSJonathan Lemon } 1939e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_inputs); 1940e1daf0ecSJonathan Lemon 1941e1daf0ecSJonathan Lemon static ssize_t 1942e1daf0ecSJonathan Lemon available_sma_outputs_show(struct device *dev, 1943e1daf0ecSJonathan Lemon struct device_attribute *attr, char *buf) 1944e1daf0ecSJonathan Lemon { 1945e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_sma_out, buf); 1946e1daf0ecSJonathan Lemon } 1947e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_outputs); 1948e1daf0ecSJonathan Lemon 1949773bda96SJonathan Lemon static ssize_t 1950773bda96SJonathan Lemon serialnum_show(struct device *dev, struct device_attribute *attr, char *buf) 1951773bda96SJonathan Lemon { 1952773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1953773bda96SJonathan Lemon 19540cfcdd1eSJonathan Lemon if (!bp->has_eeprom_data) 19550cfcdd1eSJonathan Lemon ptp_ocp_read_eeprom(bp); 1956773bda96SJonathan Lemon 1957773bda96SJonathan Lemon return sysfs_emit(buf, "%pM\n", bp->serial); 1958773bda96SJonathan Lemon } 1959773bda96SJonathan Lemon static DEVICE_ATTR_RO(serialnum); 1960773bda96SJonathan Lemon 1961773bda96SJonathan Lemon static ssize_t 1962ef0cfb34SJonathan Lemon gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf) 1963773bda96SJonathan Lemon { 1964773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1965773bda96SJonathan Lemon ssize_t ret; 1966773bda96SJonathan Lemon 1967ef0cfb34SJonathan Lemon if (bp->gnss_lost) 1968ef0cfb34SJonathan Lemon ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost); 1969773bda96SJonathan Lemon else 1970773bda96SJonathan Lemon ret = sysfs_emit(buf, "SYNC\n"); 1971773bda96SJonathan Lemon 1972773bda96SJonathan Lemon return ret; 1973773bda96SJonathan Lemon } 1974ef0cfb34SJonathan Lemon static DEVICE_ATTR_RO(gnss_sync); 1975773bda96SJonathan Lemon 1976773bda96SJonathan Lemon static ssize_t 197789260d87SJonathan Lemon utc_tai_offset_show(struct device *dev, 197889260d87SJonathan Lemon struct device_attribute *attr, char *buf) 197989260d87SJonathan Lemon { 198089260d87SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 198189260d87SJonathan Lemon 198289260d87SJonathan Lemon return sysfs_emit(buf, "%d\n", bp->utc_tai_offset); 198389260d87SJonathan Lemon } 198489260d87SJonathan Lemon 198589260d87SJonathan Lemon static ssize_t 198689260d87SJonathan Lemon utc_tai_offset_store(struct device *dev, 198789260d87SJonathan Lemon struct device_attribute *attr, 198889260d87SJonathan Lemon const char *buf, size_t count) 198989260d87SJonathan Lemon { 199089260d87SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 199189260d87SJonathan Lemon int err; 199289260d87SJonathan Lemon u32 val; 199389260d87SJonathan Lemon 199489260d87SJonathan Lemon err = kstrtou32(buf, 0, &val); 199589260d87SJonathan Lemon if (err) 199689260d87SJonathan Lemon return err; 199789260d87SJonathan Lemon 199889260d87SJonathan Lemon ptp_ocp_utc_distribute(bp, val); 199989260d87SJonathan Lemon 200089260d87SJonathan Lemon return count; 200189260d87SJonathan Lemon } 200289260d87SJonathan Lemon static DEVICE_ATTR_RW(utc_tai_offset); 200389260d87SJonathan Lemon 200489260d87SJonathan Lemon static ssize_t 20051acffc6eSJonathan Lemon ts_window_adjust_show(struct device *dev, 20061acffc6eSJonathan Lemon struct device_attribute *attr, char *buf) 20071acffc6eSJonathan Lemon { 20081acffc6eSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 20091acffc6eSJonathan Lemon 20101acffc6eSJonathan Lemon return sysfs_emit(buf, "%d\n", bp->ts_window_adjust); 20111acffc6eSJonathan Lemon } 20121acffc6eSJonathan Lemon 20131acffc6eSJonathan Lemon static ssize_t 20141acffc6eSJonathan Lemon ts_window_adjust_store(struct device *dev, 20151acffc6eSJonathan Lemon struct device_attribute *attr, 20161acffc6eSJonathan Lemon const char *buf, size_t count) 20171acffc6eSJonathan Lemon { 20181acffc6eSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 20191acffc6eSJonathan Lemon int err; 20201acffc6eSJonathan Lemon u32 val; 20211acffc6eSJonathan Lemon 20221acffc6eSJonathan Lemon err = kstrtou32(buf, 0, &val); 20231acffc6eSJonathan Lemon if (err) 20241acffc6eSJonathan Lemon return err; 20251acffc6eSJonathan Lemon 20261acffc6eSJonathan Lemon bp->ts_window_adjust = val; 20271acffc6eSJonathan Lemon 20281acffc6eSJonathan Lemon return count; 20291acffc6eSJonathan Lemon } 20301acffc6eSJonathan Lemon static DEVICE_ATTR_RW(ts_window_adjust); 20311acffc6eSJonathan Lemon 20321acffc6eSJonathan Lemon static ssize_t 2033d14ee252SJonathan Lemon irig_b_mode_show(struct device *dev, struct device_attribute *attr, char *buf) 2034d14ee252SJonathan Lemon { 2035d14ee252SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2036d14ee252SJonathan Lemon u32 val; 2037d14ee252SJonathan Lemon 2038d14ee252SJonathan Lemon val = ioread32(&bp->irig_out->ctrl); 2039d14ee252SJonathan Lemon val = (val >> 16) & 0x07; 2040d14ee252SJonathan Lemon return sysfs_emit(buf, "%d\n", val); 2041d14ee252SJonathan Lemon } 2042d14ee252SJonathan Lemon 2043d14ee252SJonathan Lemon static ssize_t 2044d14ee252SJonathan Lemon irig_b_mode_store(struct device *dev, 2045d14ee252SJonathan Lemon struct device_attribute *attr, 2046d14ee252SJonathan Lemon const char *buf, size_t count) 2047d14ee252SJonathan Lemon { 2048d14ee252SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2049d14ee252SJonathan Lemon unsigned long flags; 2050d14ee252SJonathan Lemon int err; 2051d14ee252SJonathan Lemon u32 reg; 2052d14ee252SJonathan Lemon u8 val; 2053d14ee252SJonathan Lemon 2054d14ee252SJonathan Lemon err = kstrtou8(buf, 0, &val); 2055d14ee252SJonathan Lemon if (err) 2056d14ee252SJonathan Lemon return err; 2057d14ee252SJonathan Lemon if (val > 7) 2058d14ee252SJonathan Lemon return -EINVAL; 2059d14ee252SJonathan Lemon 2060d14ee252SJonathan Lemon reg = ((val & 0x7) << 16); 2061d14ee252SJonathan Lemon 2062d14ee252SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 2063d14ee252SJonathan Lemon iowrite32(0, &bp->irig_out->ctrl); /* disable */ 2064d14ee252SJonathan Lemon iowrite32(reg, &bp->irig_out->ctrl); /* change mode */ 2065d14ee252SJonathan Lemon iowrite32(reg | IRIG_M_CTRL_ENABLE, &bp->irig_out->ctrl); 2066d14ee252SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 2067d14ee252SJonathan Lemon 2068d14ee252SJonathan Lemon return count; 2069d14ee252SJonathan Lemon } 2070d14ee252SJonathan Lemon static DEVICE_ATTR_RW(irig_b_mode); 2071d14ee252SJonathan Lemon 2072d14ee252SJonathan Lemon static ssize_t 2073773bda96SJonathan Lemon clock_source_show(struct device *dev, struct device_attribute *attr, char *buf) 2074773bda96SJonathan Lemon { 2075773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2076773bda96SJonathan Lemon const char *p; 2077773bda96SJonathan Lemon u32 select; 2078773bda96SJonathan Lemon 2079773bda96SJonathan Lemon select = ioread32(&bp->reg->select); 2080e1daf0ecSJonathan Lemon p = ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16); 2081773bda96SJonathan Lemon 2082773bda96SJonathan Lemon return sysfs_emit(buf, "%s\n", p); 2083773bda96SJonathan Lemon } 2084773bda96SJonathan Lemon 2085773bda96SJonathan Lemon static ssize_t 2086773bda96SJonathan Lemon clock_source_store(struct device *dev, struct device_attribute *attr, 2087773bda96SJonathan Lemon const char *buf, size_t count) 2088773bda96SJonathan Lemon { 2089773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2090773bda96SJonathan Lemon unsigned long flags; 2091773bda96SJonathan Lemon int val; 2092773bda96SJonathan Lemon 2093e1daf0ecSJonathan Lemon val = ptp_ocp_select_val_from_name(ptp_ocp_clock, buf); 2094773bda96SJonathan Lemon if (val < 0) 2095773bda96SJonathan Lemon return val; 2096773bda96SJonathan Lemon 2097773bda96SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 2098773bda96SJonathan Lemon iowrite32(val, &bp->reg->select); 2099773bda96SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 2100773bda96SJonathan Lemon 2101773bda96SJonathan Lemon return count; 2102773bda96SJonathan Lemon } 2103773bda96SJonathan Lemon static DEVICE_ATTR_RW(clock_source); 2104773bda96SJonathan Lemon 2105773bda96SJonathan Lemon static ssize_t 2106773bda96SJonathan Lemon available_clock_sources_show(struct device *dev, 2107773bda96SJonathan Lemon struct device_attribute *attr, char *buf) 2108773bda96SJonathan Lemon { 2109e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_clock, buf); 2110773bda96SJonathan Lemon } 2111773bda96SJonathan Lemon static DEVICE_ATTR_RO(available_clock_sources); 2112773bda96SJonathan Lemon 21132f23f486SVadim Fedorenko static ssize_t 21142f23f486SVadim Fedorenko clock_status_drift_show(struct device *dev, 21152f23f486SVadim Fedorenko struct device_attribute *attr, char *buf) 21162f23f486SVadim Fedorenko { 21172f23f486SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 21182f23f486SVadim Fedorenko u32 val; 21192f23f486SVadim Fedorenko int res; 21202f23f486SVadim Fedorenko 21212f23f486SVadim Fedorenko val = ioread32(&bp->reg->status_drift); 21222f23f486SVadim Fedorenko res = (val & ~INT_MAX) ? -1 : 1; 21232f23f486SVadim Fedorenko res *= (val & INT_MAX); 21242f23f486SVadim Fedorenko return sysfs_emit(buf, "%d\n", res); 21252f23f486SVadim Fedorenko } 21262f23f486SVadim Fedorenko static DEVICE_ATTR_RO(clock_status_drift); 21272f23f486SVadim Fedorenko 21282f23f486SVadim Fedorenko static ssize_t 21292f23f486SVadim Fedorenko clock_status_offset_show(struct device *dev, 21302f23f486SVadim Fedorenko struct device_attribute *attr, char *buf) 21312f23f486SVadim Fedorenko { 21322f23f486SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 21332f23f486SVadim Fedorenko u32 val; 21342f23f486SVadim Fedorenko int res; 21352f23f486SVadim Fedorenko 21362f23f486SVadim Fedorenko val = ioread32(&bp->reg->status_offset); 21372f23f486SVadim Fedorenko res = (val & ~INT_MAX) ? -1 : 1; 21382f23f486SVadim Fedorenko res *= (val & INT_MAX); 21392f23f486SVadim Fedorenko return sysfs_emit(buf, "%d\n", res); 21402f23f486SVadim Fedorenko } 21412f23f486SVadim Fedorenko static DEVICE_ATTR_RO(clock_status_offset); 21422f23f486SVadim Fedorenko 214344a412d1SVadim Fedorenko static ssize_t 214444a412d1SVadim Fedorenko tod_correction_show(struct device *dev, 214544a412d1SVadim Fedorenko struct device_attribute *attr, char *buf) 214644a412d1SVadim Fedorenko { 214744a412d1SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 214844a412d1SVadim Fedorenko u32 val; 214944a412d1SVadim Fedorenko int res; 215044a412d1SVadim Fedorenko 215144a412d1SVadim Fedorenko val = ioread32(&bp->tod->adj_sec); 215244a412d1SVadim Fedorenko res = (val & ~INT_MAX) ? -1 : 1; 215344a412d1SVadim Fedorenko res *= (val & INT_MAX); 215444a412d1SVadim Fedorenko return sysfs_emit(buf, "%d\n", res); 215544a412d1SVadim Fedorenko } 215644a412d1SVadim Fedorenko 215744a412d1SVadim Fedorenko static ssize_t 215844a412d1SVadim Fedorenko tod_correction_store(struct device *dev, struct device_attribute *attr, 215944a412d1SVadim Fedorenko const char *buf, size_t count) 216044a412d1SVadim Fedorenko { 216144a412d1SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 216244a412d1SVadim Fedorenko unsigned long flags; 216344a412d1SVadim Fedorenko int err, res; 216444a412d1SVadim Fedorenko u32 val = 0; 216544a412d1SVadim Fedorenko 216644a412d1SVadim Fedorenko err = kstrtos32(buf, 0, &res); 216744a412d1SVadim Fedorenko if (err) 216844a412d1SVadim Fedorenko return err; 216944a412d1SVadim Fedorenko if (res < 0) { 217044a412d1SVadim Fedorenko res *= -1; 217144a412d1SVadim Fedorenko val |= BIT(31); 217244a412d1SVadim Fedorenko } 217344a412d1SVadim Fedorenko val |= res; 217444a412d1SVadim Fedorenko 217544a412d1SVadim Fedorenko spin_lock_irqsave(&bp->lock, flags); 217644a412d1SVadim Fedorenko iowrite32(val, &bp->tod->adj_sec); 217744a412d1SVadim Fedorenko spin_unlock_irqrestore(&bp->lock, flags); 217844a412d1SVadim Fedorenko 217944a412d1SVadim Fedorenko return count; 218044a412d1SVadim Fedorenko } 218144a412d1SVadim Fedorenko static DEVICE_ATTR_RW(tod_correction); 218244a412d1SVadim Fedorenko 2183*c205d53cSJonathan Lemon static struct attribute *fb_timecard_attrs[] = { 2184773bda96SJonathan Lemon &dev_attr_serialnum.attr, 2185ef0cfb34SJonathan Lemon &dev_attr_gnss_sync.attr, 2186773bda96SJonathan Lemon &dev_attr_clock_source.attr, 2187773bda96SJonathan Lemon &dev_attr_available_clock_sources.attr, 2188e1daf0ecSJonathan Lemon &dev_attr_sma1.attr, 2189e1daf0ecSJonathan Lemon &dev_attr_sma2.attr, 2190e1daf0ecSJonathan Lemon &dev_attr_sma3.attr, 2191e1daf0ecSJonathan Lemon &dev_attr_sma4.attr, 2192e1daf0ecSJonathan Lemon &dev_attr_available_sma_inputs.attr, 2193e1daf0ecSJonathan Lemon &dev_attr_available_sma_outputs.attr, 21942f23f486SVadim Fedorenko &dev_attr_clock_status_drift.attr, 21952f23f486SVadim Fedorenko &dev_attr_clock_status_offset.attr, 2196d14ee252SJonathan Lemon &dev_attr_irig_b_mode.attr, 219789260d87SJonathan Lemon &dev_attr_utc_tai_offset.attr, 21981acffc6eSJonathan Lemon &dev_attr_ts_window_adjust.attr, 219944a412d1SVadim Fedorenko &dev_attr_tod_correction.attr, 2200773bda96SJonathan Lemon NULL, 2201773bda96SJonathan Lemon }; 2202*c205d53cSJonathan Lemon static const struct attribute_group fb_timecard_group = { 2203*c205d53cSJonathan Lemon .attrs = fb_timecard_attrs, 2204*c205d53cSJonathan Lemon }; 2205*c205d53cSJonathan Lemon static const struct ocp_attr_group fb_timecard_groups[] = { 2206*c205d53cSJonathan Lemon { .cap = OCP_CAP_BASIC, .group = &fb_timecard_group }, 2207*c205d53cSJonathan Lemon { }, 2208*c205d53cSJonathan Lemon }; 2209773bda96SJonathan Lemon 2210a509a7c6SJonathan Lemon static void 2211a509a7c6SJonathan Lemon gpio_input_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit, 2212a509a7c6SJonathan Lemon const char *def) 2213f67bf662SJonathan Lemon { 2214a509a7c6SJonathan Lemon int i; 2215f67bf662SJonathan Lemon 2216a509a7c6SJonathan Lemon for (i = 0; i < 4; i++) { 2217a509a7c6SJonathan Lemon if (bp->sma[i].mode != SMA_MODE_IN) 2218a509a7c6SJonathan Lemon continue; 2219a509a7c6SJonathan Lemon if (map[i][0] & (1 << bit)) { 2220a509a7c6SJonathan Lemon sprintf(buf, "sma%d", i + 1); 2221a509a7c6SJonathan Lemon return; 2222a509a7c6SJonathan Lemon } 2223a509a7c6SJonathan Lemon } 2224a509a7c6SJonathan Lemon if (!def) 2225a509a7c6SJonathan Lemon def = "----"; 2226a509a7c6SJonathan Lemon strcpy(buf, def); 2227f67bf662SJonathan Lemon } 2228f67bf662SJonathan Lemon 2229f67bf662SJonathan Lemon static void 2230a509a7c6SJonathan Lemon gpio_output_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit) 2231f67bf662SJonathan Lemon { 2232f67bf662SJonathan Lemon char *ans = buf; 2233a509a7c6SJonathan Lemon int i; 2234f67bf662SJonathan Lemon 2235a509a7c6SJonathan Lemon strcpy(ans, "----"); 2236a509a7c6SJonathan Lemon for (i = 0; i < 4; i++) { 2237a509a7c6SJonathan Lemon if (bp->sma[i].mode != SMA_MODE_OUT) 2238a509a7c6SJonathan Lemon continue; 2239a509a7c6SJonathan Lemon if (map[i][1] & (1 << bit)) 2240a509a7c6SJonathan Lemon ans += sprintf(ans, "sma%d ", i + 1); 2241a509a7c6SJonathan Lemon } 2242f67bf662SJonathan Lemon } 2243f67bf662SJonathan Lemon 2244f67bf662SJonathan Lemon static int 2245f67bf662SJonathan Lemon ptp_ocp_summary_show(struct seq_file *s, void *data) 2246f67bf662SJonathan Lemon { 2247f67bf662SJonathan Lemon struct device *dev = s->private; 2248f67bf662SJonathan Lemon struct ptp_system_timestamp sts; 2249a509a7c6SJonathan Lemon u16 sma_val[4][2], ctrl, val; 2250f67bf662SJonathan Lemon struct ts_reg __iomem *ts_reg; 2251f67bf662SJonathan Lemon struct timespec64 ts; 2252f67bf662SJonathan Lemon struct ptp_ocp *bp; 2253a509a7c6SJonathan Lemon char *src, *buf; 2254a62a56d0SJonathan Lemon bool on, map; 2255f67bf662SJonathan Lemon 2256f67bf662SJonathan Lemon buf = (char *)__get_free_page(GFP_KERNEL); 2257f67bf662SJonathan Lemon if (!buf) 2258f67bf662SJonathan Lemon return -ENOMEM; 2259f67bf662SJonathan Lemon 2260f67bf662SJonathan Lemon bp = dev_get_drvdata(dev); 2261f67bf662SJonathan Lemon 2262f67bf662SJonathan Lemon seq_printf(s, "%7s: /dev/ptp%d\n", "PTP", ptp_clock_index(bp->ptp)); 226361fd7ac2SJonathan Lemon if (bp->gnss_port != -1) 226461fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS1", bp->gnss_port); 226561fd7ac2SJonathan Lemon if (bp->gnss2_port != -1) 226661fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS2", bp->gnss2_port); 226761fd7ac2SJonathan Lemon if (bp->mac_port != -1) 226861fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "MAC", bp->mac_port); 226961fd7ac2SJonathan Lemon if (bp->nmea_port != -1) 227061fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "NMEA", bp->nmea_port); 2271f67bf662SJonathan Lemon 2272a509a7c6SJonathan Lemon memset(sma_val, 0xff, sizeof(sma_val)); 2273a509a7c6SJonathan Lemon if (bp->sma_map1) { 2274a509a7c6SJonathan Lemon u32 reg; 2275a509a7c6SJonathan Lemon 2276a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map1->gpio1); 2277a509a7c6SJonathan Lemon sma_val[0][0] = reg & 0xffff; 2278a509a7c6SJonathan Lemon sma_val[1][0] = reg >> 16; 2279a509a7c6SJonathan Lemon 2280a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map1->gpio2); 2281a509a7c6SJonathan Lemon sma_val[2][1] = reg & 0xffff; 2282a509a7c6SJonathan Lemon sma_val[3][1] = reg >> 16; 2283a509a7c6SJonathan Lemon 2284a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map2->gpio1); 2285a509a7c6SJonathan Lemon sma_val[2][0] = reg & 0xffff; 2286a509a7c6SJonathan Lemon sma_val[3][0] = reg >> 16; 2287a509a7c6SJonathan Lemon 2288a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map2->gpio2); 2289a509a7c6SJonathan Lemon sma_val[0][1] = reg & 0xffff; 2290a509a7c6SJonathan Lemon sma_val[1][1] = reg >> 16; 2291a509a7c6SJonathan Lemon } 2292a509a7c6SJonathan Lemon 2293f67bf662SJonathan Lemon sma1_show(dev, NULL, buf); 2294a509a7c6SJonathan Lemon seq_printf(s, " sma1: %04x,%04x %s", 2295a509a7c6SJonathan Lemon sma_val[0][0], sma_val[0][1], buf); 2296f67bf662SJonathan Lemon 2297f67bf662SJonathan Lemon sma2_show(dev, NULL, buf); 2298a509a7c6SJonathan Lemon seq_printf(s, " sma2: %04x,%04x %s", 2299a509a7c6SJonathan Lemon sma_val[1][0], sma_val[1][1], buf); 2300f67bf662SJonathan Lemon 2301f67bf662SJonathan Lemon sma3_show(dev, NULL, buf); 2302a509a7c6SJonathan Lemon seq_printf(s, " sma3: %04x,%04x %s", 2303a509a7c6SJonathan Lemon sma_val[2][0], sma_val[2][1], buf); 2304f67bf662SJonathan Lemon 2305f67bf662SJonathan Lemon sma4_show(dev, NULL, buf); 2306a509a7c6SJonathan Lemon seq_printf(s, " sma4: %04x,%04x %s", 2307a509a7c6SJonathan Lemon sma_val[3][0], sma_val[3][1], buf); 2308f67bf662SJonathan Lemon 2309f67bf662SJonathan Lemon if (bp->ts0) { 2310f67bf662SJonathan Lemon ts_reg = bp->ts0->mem; 2311f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 2312be69087cSJonathan Lemon src = "GNSS1"; 2313f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS0", 2314f67bf662SJonathan Lemon on ? " ON" : "OFF", src); 2315f67bf662SJonathan Lemon } 2316f67bf662SJonathan Lemon 2317f67bf662SJonathan Lemon if (bp->ts1) { 2318f67bf662SJonathan Lemon ts_reg = bp->ts1->mem; 2319f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 2320a509a7c6SJonathan Lemon gpio_input_map(buf, bp, sma_val, 2, NULL); 2321f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS1", 2322a509a7c6SJonathan Lemon on ? " ON" : "OFF", buf); 2323f67bf662SJonathan Lemon } 2324f67bf662SJonathan Lemon 2325f67bf662SJonathan Lemon if (bp->ts2) { 2326f67bf662SJonathan Lemon ts_reg = bp->ts2->mem; 2327f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 2328a509a7c6SJonathan Lemon gpio_input_map(buf, bp, sma_val, 3, NULL); 2329f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS2", 2330a509a7c6SJonathan Lemon on ? " ON" : "OFF", buf); 2331f67bf662SJonathan Lemon } 2332f67bf662SJonathan Lemon 2333a62a56d0SJonathan Lemon if (bp->pps) { 2334a62a56d0SJonathan Lemon ts_reg = bp->pps->mem; 2335a62a56d0SJonathan Lemon src = "PHC"; 2336a62a56d0SJonathan Lemon on = ioread32(&ts_reg->enable); 2337a62a56d0SJonathan Lemon map = !!(bp->pps_req_map & OCP_REQ_TIMESTAMP); 2338a62a56d0SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS3", 23391a575cdeSNathan Chancellor on && map ? " ON" : "OFF", src); 2340a62a56d0SJonathan Lemon 2341a62a56d0SJonathan Lemon map = !!(bp->pps_req_map & OCP_REQ_PPS); 2342a62a56d0SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "PPS", 23431a575cdeSNathan Chancellor on && map ? " ON" : "OFF", src); 2344a62a56d0SJonathan Lemon } 2345a62a56d0SJonathan Lemon 2346f67bf662SJonathan Lemon if (bp->irig_out) { 2347f67bf662SJonathan Lemon ctrl = ioread32(&bp->irig_out->ctrl); 2348f67bf662SJonathan Lemon on = ctrl & IRIG_M_CTRL_ENABLE; 2349f67bf662SJonathan Lemon val = ioread32(&bp->irig_out->status); 2350a509a7c6SJonathan Lemon gpio_output_map(buf, bp, sma_val, 4); 2351f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, mode %d, out: %s\n", "IRIG", 2352f67bf662SJonathan Lemon on ? " ON" : "OFF", val, (ctrl >> 16), buf); 2353f67bf662SJonathan Lemon } 2354f67bf662SJonathan Lemon 2355f67bf662SJonathan Lemon if (bp->irig_in) { 2356f67bf662SJonathan Lemon on = ioread32(&bp->irig_in->ctrl) & IRIG_S_CTRL_ENABLE; 2357f67bf662SJonathan Lemon val = ioread32(&bp->irig_in->status); 2358a509a7c6SJonathan Lemon gpio_input_map(buf, bp, sma_val, 4, NULL); 2359f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, src: %s\n", "IRIG in", 2360a509a7c6SJonathan Lemon on ? " ON" : "OFF", val, buf); 2361f67bf662SJonathan Lemon } 2362f67bf662SJonathan Lemon 2363f67bf662SJonathan Lemon if (bp->dcf_out) { 2364f67bf662SJonathan Lemon on = ioread32(&bp->dcf_out->ctrl) & DCF_M_CTRL_ENABLE; 2365f67bf662SJonathan Lemon val = ioread32(&bp->dcf_out->status); 2366a509a7c6SJonathan Lemon gpio_output_map(buf, bp, sma_val, 5); 2367f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, out: %s\n", "DCF", 2368f67bf662SJonathan Lemon on ? " ON" : "OFF", val, buf); 2369f67bf662SJonathan Lemon } 2370f67bf662SJonathan Lemon 2371f67bf662SJonathan Lemon if (bp->dcf_in) { 2372f67bf662SJonathan Lemon on = ioread32(&bp->dcf_in->ctrl) & DCF_S_CTRL_ENABLE; 2373f67bf662SJonathan Lemon val = ioread32(&bp->dcf_in->status); 2374a509a7c6SJonathan Lemon gpio_input_map(buf, bp, sma_val, 5, NULL); 2375f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, src: %s\n", "DCF in", 2376a509a7c6SJonathan Lemon on ? " ON" : "OFF", val, buf); 2377f67bf662SJonathan Lemon } 2378f67bf662SJonathan Lemon 2379e3516bb4SJonathan Lemon if (bp->nmea_out) { 2380e3516bb4SJonathan Lemon on = ioread32(&bp->nmea_out->ctrl) & 1; 2381e3516bb4SJonathan Lemon val = ioread32(&bp->nmea_out->status); 2382e3516bb4SJonathan Lemon seq_printf(s, "%7s: %s, error: %d\n", "NMEA", 2383e3516bb4SJonathan Lemon on ? " ON" : "OFF", val); 2384e3516bb4SJonathan Lemon } 2385e3516bb4SJonathan Lemon 2386f67bf662SJonathan Lemon /* compute src for PPS1, used below. */ 2387f67bf662SJonathan Lemon if (bp->pps_select) { 2388f67bf662SJonathan Lemon val = ioread32(&bp->pps_select->gpio1); 2389a509a7c6SJonathan Lemon src = &buf[80]; 2390f67bf662SJonathan Lemon if (val & 0x01) 2391a509a7c6SJonathan Lemon gpio_input_map(src, bp, sma_val, 0, NULL); 2392f67bf662SJonathan Lemon else if (val & 0x02) 2393f67bf662SJonathan Lemon src = "MAC"; 2394f67bf662SJonathan Lemon else if (val & 0x04) 2395be69087cSJonathan Lemon src = "GNSS1"; 2396f67bf662SJonathan Lemon else 2397f67bf662SJonathan Lemon src = "----"; 2398f67bf662SJonathan Lemon } else { 2399f67bf662SJonathan Lemon src = "?"; 2400f67bf662SJonathan Lemon } 2401f67bf662SJonathan Lemon 2402f67bf662SJonathan Lemon /* assumes automatic switchover/selection */ 2403f67bf662SJonathan Lemon val = ioread32(&bp->reg->select); 2404f67bf662SJonathan Lemon switch (val >> 16) { 2405f67bf662SJonathan Lemon case 0: 2406f67bf662SJonathan Lemon sprintf(buf, "----"); 2407f67bf662SJonathan Lemon break; 2408f67bf662SJonathan Lemon case 2: 2409f67bf662SJonathan Lemon sprintf(buf, "IRIG"); 2410f67bf662SJonathan Lemon break; 2411f67bf662SJonathan Lemon case 3: 2412f67bf662SJonathan Lemon sprintf(buf, "%s via PPS1", src); 2413f67bf662SJonathan Lemon break; 2414f67bf662SJonathan Lemon case 6: 2415f67bf662SJonathan Lemon sprintf(buf, "DCF"); 2416f67bf662SJonathan Lemon break; 2417f67bf662SJonathan Lemon default: 2418f67bf662SJonathan Lemon strcpy(buf, "unknown"); 2419f67bf662SJonathan Lemon break; 2420f67bf662SJonathan Lemon } 2421f67bf662SJonathan Lemon val = ioread32(&bp->reg->status); 2422f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, state: %s\n", "PHC src", buf, 2423f67bf662SJonathan Lemon val & OCP_STATUS_IN_SYNC ? "sync" : "unsynced"); 2424f67bf662SJonathan Lemon 2425f67bf662SJonathan Lemon /* reuses PPS1 src from earlier */ 2426f67bf662SJonathan Lemon seq_printf(s, "MAC PPS1 src: %s\n", src); 2427f67bf662SJonathan Lemon 2428a509a7c6SJonathan Lemon gpio_input_map(buf, bp, sma_val, 1, "GNSS2"); 2429a509a7c6SJonathan Lemon seq_printf(s, "MAC PPS2 src: %s\n", buf); 2430f67bf662SJonathan Lemon 2431f67bf662SJonathan Lemon if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts)) { 2432f67bf662SJonathan Lemon struct timespec64 sys_ts; 2433f67bf662SJonathan Lemon s64 pre_ns, post_ns, ns; 2434f67bf662SJonathan Lemon 2435f67bf662SJonathan Lemon pre_ns = timespec64_to_ns(&sts.pre_ts); 2436f67bf662SJonathan Lemon post_ns = timespec64_to_ns(&sts.post_ts); 2437f67bf662SJonathan Lemon ns = (pre_ns + post_ns) / 2; 2438f67bf662SJonathan Lemon ns += (s64)bp->utc_tai_offset * NSEC_PER_SEC; 2439f67bf662SJonathan Lemon sys_ts = ns_to_timespec64(ns); 2440f67bf662SJonathan Lemon 2441f67bf662SJonathan Lemon seq_printf(s, "%7s: %lld.%ld == %ptT TAI\n", "PHC", 2442f67bf662SJonathan Lemon ts.tv_sec, ts.tv_nsec, &ts); 2443f67bf662SJonathan Lemon seq_printf(s, "%7s: %lld.%ld == %ptT UTC offset %d\n", "SYS", 2444f67bf662SJonathan Lemon sys_ts.tv_sec, sys_ts.tv_nsec, &sys_ts, 2445f67bf662SJonathan Lemon bp->utc_tai_offset); 2446f67bf662SJonathan Lemon seq_printf(s, "%7s: PHC:SYS offset: %lld window: %lld\n", "", 2447f67bf662SJonathan Lemon timespec64_to_ns(&ts) - ns, 2448f67bf662SJonathan Lemon post_ns - pre_ns); 2449f67bf662SJonathan Lemon } 2450f67bf662SJonathan Lemon 2451f67bf662SJonathan Lemon free_page((unsigned long)buf); 2452f67bf662SJonathan Lemon return 0; 2453f67bf662SJonathan Lemon } 2454f67bf662SJonathan Lemon DEFINE_SHOW_ATTRIBUTE(ptp_ocp_summary); 2455f67bf662SJonathan Lemon 24569f492c4cSVadim Fedorenko static int 24579f492c4cSVadim Fedorenko ptp_ocp_tod_status_show(struct seq_file *s, void *data) 24589f492c4cSVadim Fedorenko { 24599f492c4cSVadim Fedorenko struct device *dev = s->private; 24609f492c4cSVadim Fedorenko struct ptp_ocp *bp; 24619f492c4cSVadim Fedorenko u32 val; 24629f492c4cSVadim Fedorenko int idx; 24639f492c4cSVadim Fedorenko 24649f492c4cSVadim Fedorenko bp = dev_get_drvdata(dev); 24659f492c4cSVadim Fedorenko 24669f492c4cSVadim Fedorenko val = ioread32(&bp->tod->ctrl); 24679f492c4cSVadim Fedorenko if (!(val & TOD_CTRL_ENABLE)) { 24689f492c4cSVadim Fedorenko seq_printf(s, "TOD Slave disabled\n"); 24699f492c4cSVadim Fedorenko return 0; 24709f492c4cSVadim Fedorenko } 24719f492c4cSVadim Fedorenko seq_printf(s, "TOD Slave enabled, Control Register 0x%08X\n", val); 24729f492c4cSVadim Fedorenko 24739f492c4cSVadim Fedorenko idx = val & TOD_CTRL_PROTOCOL ? 4 : 0; 24749f492c4cSVadim Fedorenko idx += (val >> 16) & 3; 24759f492c4cSVadim Fedorenko seq_printf(s, "Protocol %s\n", ptp_ocp_tod_proto_name(idx)); 24769f492c4cSVadim Fedorenko 24779f492c4cSVadim Fedorenko idx = (val >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK; 24789f492c4cSVadim Fedorenko seq_printf(s, "GNSS %s\n", ptp_ocp_tod_gnss_name(idx)); 24799f492c4cSVadim Fedorenko 24809f492c4cSVadim Fedorenko val = ioread32(&bp->tod->version); 24819f492c4cSVadim Fedorenko seq_printf(s, "TOD Version %d.%d.%d\n", 24829f492c4cSVadim Fedorenko val >> 24, (val >> 16) & 0xff, val & 0xffff); 24839f492c4cSVadim Fedorenko 24849f492c4cSVadim Fedorenko val = ioread32(&bp->tod->status); 24859f492c4cSVadim Fedorenko seq_printf(s, "Status register: 0x%08X\n", val); 24869f492c4cSVadim Fedorenko 24879f492c4cSVadim Fedorenko val = ioread32(&bp->tod->adj_sec); 24889f492c4cSVadim Fedorenko idx = (val & ~INT_MAX) ? -1 : 1; 24899f492c4cSVadim Fedorenko idx *= (val & INT_MAX); 24909f492c4cSVadim Fedorenko seq_printf(s, "Correction seconds: %d\n", idx); 24919f492c4cSVadim Fedorenko 24929f492c4cSVadim Fedorenko val = ioread32(&bp->tod->utc_status); 24939f492c4cSVadim Fedorenko seq_printf(s, "UTC status register: 0x%08X\n", val); 24949f492c4cSVadim Fedorenko seq_printf(s, "UTC offset: %d valid:%d\n", 24959f492c4cSVadim Fedorenko val & TOD_STATUS_UTC_MASK, val & TOD_STATUS_UTC_VALID ? 1 : 0); 24969f492c4cSVadim Fedorenko seq_printf(s, "Leap second info valid:%d, Leap second announce %d\n", 24979f492c4cSVadim Fedorenko val & TOD_STATUS_LEAP_VALID ? 1 : 0, 24989f492c4cSVadim Fedorenko val & TOD_STATUS_LEAP_ANNOUNCE ? 1 : 0); 24999f492c4cSVadim Fedorenko 25009f492c4cSVadim Fedorenko val = ioread32(&bp->tod->leap); 25019f492c4cSVadim Fedorenko seq_printf(s, "Time to next leap second (in sec): %d\n", (s32) val); 25029f492c4cSVadim Fedorenko 25039f492c4cSVadim Fedorenko return 0; 25049f492c4cSVadim Fedorenko } 25059f492c4cSVadim Fedorenko DEFINE_SHOW_ATTRIBUTE(ptp_ocp_tod_status); 25069f492c4cSVadim Fedorenko 2507f67bf662SJonathan Lemon static struct dentry *ptp_ocp_debugfs_root; 2508f67bf662SJonathan Lemon 2509f67bf662SJonathan Lemon static void 2510f67bf662SJonathan Lemon ptp_ocp_debugfs_add_device(struct ptp_ocp *bp) 2511f67bf662SJonathan Lemon { 2512f67bf662SJonathan Lemon struct dentry *d; 2513f67bf662SJonathan Lemon 2514f67bf662SJonathan Lemon d = debugfs_create_dir(dev_name(&bp->dev), ptp_ocp_debugfs_root); 2515f67bf662SJonathan Lemon bp->debug_root = d; 2516f67bf662SJonathan Lemon debugfs_create_file("summary", 0444, bp->debug_root, 2517f67bf662SJonathan Lemon &bp->dev, &ptp_ocp_summary_fops); 25189f492c4cSVadim Fedorenko if (bp->tod) 25199f492c4cSVadim Fedorenko debugfs_create_file("tod_status", 0444, bp->debug_root, 25209f492c4cSVadim Fedorenko &bp->dev, &ptp_ocp_tod_status_fops); 2521f67bf662SJonathan Lemon } 2522f67bf662SJonathan Lemon 2523f67bf662SJonathan Lemon static void 2524f67bf662SJonathan Lemon ptp_ocp_debugfs_remove_device(struct ptp_ocp *bp) 2525f67bf662SJonathan Lemon { 2526f67bf662SJonathan Lemon debugfs_remove_recursive(bp->debug_root); 2527f67bf662SJonathan Lemon } 2528f67bf662SJonathan Lemon 2529f67bf662SJonathan Lemon static void 2530f67bf662SJonathan Lemon ptp_ocp_debugfs_init(void) 2531f67bf662SJonathan Lemon { 2532f67bf662SJonathan Lemon ptp_ocp_debugfs_root = debugfs_create_dir("timecard", NULL); 2533f67bf662SJonathan Lemon } 2534f67bf662SJonathan Lemon 2535f67bf662SJonathan Lemon static void 2536f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(void) 2537f67bf662SJonathan Lemon { 2538f67bf662SJonathan Lemon debugfs_remove_recursive(ptp_ocp_debugfs_root); 2539f67bf662SJonathan Lemon } 2540f67bf662SJonathan Lemon 2541773bda96SJonathan Lemon static void 2542773bda96SJonathan Lemon ptp_ocp_dev_release(struct device *dev) 2543773bda96SJonathan Lemon { 2544773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2545773bda96SJonathan Lemon 2546773bda96SJonathan Lemon mutex_lock(&ptp_ocp_lock); 2547773bda96SJonathan Lemon idr_remove(&ptp_ocp_idr, bp->id); 2548773bda96SJonathan Lemon mutex_unlock(&ptp_ocp_lock); 2549773bda96SJonathan Lemon } 2550773bda96SJonathan Lemon 2551773bda96SJonathan Lemon static int 2552773bda96SJonathan Lemon ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev) 2553773bda96SJonathan Lemon { 2554773bda96SJonathan Lemon int err; 2555773bda96SJonathan Lemon 2556773bda96SJonathan Lemon mutex_lock(&ptp_ocp_lock); 2557773bda96SJonathan Lemon err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL); 2558773bda96SJonathan Lemon mutex_unlock(&ptp_ocp_lock); 2559773bda96SJonathan Lemon if (err < 0) { 2560773bda96SJonathan Lemon dev_err(&pdev->dev, "idr_alloc failed: %d\n", err); 2561773bda96SJonathan Lemon return err; 2562773bda96SJonathan Lemon } 2563773bda96SJonathan Lemon bp->id = err; 2564773bda96SJonathan Lemon 2565773bda96SJonathan Lemon bp->ptp_info = ptp_ocp_clock_info; 2566773bda96SJonathan Lemon spin_lock_init(&bp->lock); 2567ef0cfb34SJonathan Lemon bp->gnss_port = -1; 256871d7e085SJonathan Lemon bp->gnss2_port = -1; 2569773bda96SJonathan Lemon bp->mac_port = -1; 2570e3516bb4SJonathan Lemon bp->nmea_port = -1; 2571773bda96SJonathan Lemon bp->pdev = pdev; 2572773bda96SJonathan Lemon 2573773bda96SJonathan Lemon device_initialize(&bp->dev); 2574773bda96SJonathan Lemon dev_set_name(&bp->dev, "ocp%d", bp->id); 2575773bda96SJonathan Lemon bp->dev.class = &timecard_class; 2576773bda96SJonathan Lemon bp->dev.parent = &pdev->dev; 2577773bda96SJonathan Lemon bp->dev.release = ptp_ocp_dev_release; 2578773bda96SJonathan Lemon dev_set_drvdata(&bp->dev, bp); 2579773bda96SJonathan Lemon 2580773bda96SJonathan Lemon err = device_add(&bp->dev); 2581773bda96SJonathan Lemon if (err) { 2582773bda96SJonathan Lemon dev_err(&bp->dev, "device add failed: %d\n", err); 2583773bda96SJonathan Lemon goto out; 2584773bda96SJonathan Lemon } 2585773bda96SJonathan Lemon 2586773bda96SJonathan Lemon pci_set_drvdata(pdev, bp); 2587773bda96SJonathan Lemon 2588773bda96SJonathan Lemon return 0; 2589773bda96SJonathan Lemon 2590773bda96SJonathan Lemon out: 2591773bda96SJonathan Lemon ptp_ocp_dev_release(&bp->dev); 2592d12f23faSJonathan Lemon put_device(&bp->dev); 2593773bda96SJonathan Lemon return err; 2594773bda96SJonathan Lemon } 2595773bda96SJonathan Lemon 2596773bda96SJonathan Lemon static void 2597773bda96SJonathan Lemon ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link) 2598773bda96SJonathan Lemon { 2599773bda96SJonathan Lemon struct device *dev = &bp->dev; 2600773bda96SJonathan Lemon 2601773bda96SJonathan Lemon if (sysfs_create_link(&dev->kobj, &child->kobj, link)) 2602773bda96SJonathan Lemon dev_err(dev, "%s symlink failed\n", link); 2603773bda96SJonathan Lemon } 2604773bda96SJonathan Lemon 2605773bda96SJonathan Lemon static void 2606773bda96SJonathan Lemon ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link) 2607773bda96SJonathan Lemon { 2608773bda96SJonathan Lemon struct device *dev, *child; 2609773bda96SJonathan Lemon 2610773bda96SJonathan Lemon dev = &bp->pdev->dev; 2611773bda96SJonathan Lemon 2612773bda96SJonathan Lemon child = device_find_child_by_name(dev, name); 2613773bda96SJonathan Lemon if (!child) { 2614773bda96SJonathan Lemon dev_err(dev, "Could not find device %s\n", name); 2615773bda96SJonathan Lemon return; 2616773bda96SJonathan Lemon } 2617773bda96SJonathan Lemon 2618773bda96SJonathan Lemon ptp_ocp_symlink(bp, child, link); 2619773bda96SJonathan Lemon put_device(child); 2620773bda96SJonathan Lemon } 2621773bda96SJonathan Lemon 2622773bda96SJonathan Lemon static int 2623773bda96SJonathan Lemon ptp_ocp_complete(struct ptp_ocp *bp) 2624773bda96SJonathan Lemon { 2625773bda96SJonathan Lemon struct pps_device *pps; 2626773bda96SJonathan Lemon char buf[32]; 2627*c205d53cSJonathan Lemon int i, err; 2628773bda96SJonathan Lemon 2629ef0cfb34SJonathan Lemon if (bp->gnss_port != -1) { 2630ef0cfb34SJonathan Lemon sprintf(buf, "ttyS%d", bp->gnss_port); 2631ef0cfb34SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyGNSS"); 2632773bda96SJonathan Lemon } 263371d7e085SJonathan Lemon if (bp->gnss2_port != -1) { 263471d7e085SJonathan Lemon sprintf(buf, "ttyS%d", bp->gnss2_port); 263571d7e085SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyGNSS2"); 263671d7e085SJonathan Lemon } 2637773bda96SJonathan Lemon if (bp->mac_port != -1) { 2638773bda96SJonathan Lemon sprintf(buf, "ttyS%d", bp->mac_port); 2639773bda96SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyMAC"); 2640773bda96SJonathan Lemon } 2641e3516bb4SJonathan Lemon if (bp->nmea_port != -1) { 2642e3516bb4SJonathan Lemon sprintf(buf, "ttyS%d", bp->nmea_port); 2643e3516bb4SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyNMEA"); 2644e3516bb4SJonathan Lemon } 2645773bda96SJonathan Lemon sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp)); 2646773bda96SJonathan Lemon ptp_ocp_link_child(bp, buf, "ptp"); 2647773bda96SJonathan Lemon 2648773bda96SJonathan Lemon pps = pps_lookup_dev(bp->ptp); 2649773bda96SJonathan Lemon if (pps) 2650773bda96SJonathan Lemon ptp_ocp_symlink(bp, pps->dev, "pps"); 2651773bda96SJonathan Lemon 2652*c205d53cSJonathan Lemon for (i = 0; bp->attr_tbl[i].cap; i++) { 2653*c205d53cSJonathan Lemon if (!(bp->attr_tbl[i].cap & bp->fw_cap)) 2654*c205d53cSJonathan Lemon continue; 2655*c205d53cSJonathan Lemon err = sysfs_create_group(&bp->dev.kobj, bp->attr_tbl[i].group); 2656*c205d53cSJonathan Lemon if (err) 2657*c205d53cSJonathan Lemon return err; 2658*c205d53cSJonathan Lemon } 2659773bda96SJonathan Lemon 2660f67bf662SJonathan Lemon ptp_ocp_debugfs_add_device(bp); 2661f67bf662SJonathan Lemon 2662773bda96SJonathan Lemon return 0; 2663773bda96SJonathan Lemon } 2664773bda96SJonathan Lemon 2665773bda96SJonathan Lemon static void 2666065efcc5SJonathan Lemon ptp_ocp_phc_info(struct ptp_ocp *bp) 2667065efcc5SJonathan Lemon { 2668065efcc5SJonathan Lemon struct timespec64 ts; 2669065efcc5SJonathan Lemon u32 version, select; 2670065efcc5SJonathan Lemon bool sync; 2671065efcc5SJonathan Lemon 2672065efcc5SJonathan Lemon version = ioread32(&bp->reg->version); 2673065efcc5SJonathan Lemon select = ioread32(&bp->reg->select); 2674065efcc5SJonathan Lemon dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n", 2675065efcc5SJonathan Lemon version >> 24, (version >> 16) & 0xff, version & 0xffff, 2676065efcc5SJonathan Lemon ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16), 2677065efcc5SJonathan Lemon ptp_clock_index(bp->ptp)); 2678065efcc5SJonathan Lemon 2679065efcc5SJonathan Lemon sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC; 2680065efcc5SJonathan Lemon if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL)) 2681065efcc5SJonathan Lemon dev_info(&bp->pdev->dev, "Time: %lld.%ld, %s\n", 2682065efcc5SJonathan Lemon ts.tv_sec, ts.tv_nsec, 2683065efcc5SJonathan Lemon sync ? "in-sync" : "UNSYNCED"); 2684065efcc5SJonathan Lemon } 2685065efcc5SJonathan Lemon 2686065efcc5SJonathan Lemon static void 2687065efcc5SJonathan Lemon ptp_ocp_serial_info(struct device *dev, const char *name, int port, int baud) 2688065efcc5SJonathan Lemon { 2689065efcc5SJonathan Lemon if (port != -1) 2690065efcc5SJonathan Lemon dev_info(dev, "%5s: /dev/ttyS%-2d @ %6d\n", name, port, baud); 2691065efcc5SJonathan Lemon } 2692065efcc5SJonathan Lemon 2693065efcc5SJonathan Lemon static void 2694065efcc5SJonathan Lemon ptp_ocp_info(struct ptp_ocp *bp) 2695773bda96SJonathan Lemon { 2696e3516bb4SJonathan Lemon static int nmea_baud[] = { 2697e3516bb4SJonathan Lemon 1200, 2400, 4800, 9600, 19200, 38400, 2698e3516bb4SJonathan Lemon 57600, 115200, 230400, 460800, 921600, 2699e3516bb4SJonathan Lemon 1000000, 2000000 2700e3516bb4SJonathan Lemon }; 2701773bda96SJonathan Lemon struct device *dev = &bp->pdev->dev; 2702e3516bb4SJonathan Lemon u32 reg; 2703773bda96SJonathan Lemon 2704065efcc5SJonathan Lemon ptp_ocp_phc_info(bp); 2705065efcc5SJonathan Lemon 2706b0ca789aSJonathan Lemon dev_info(dev, "version %x\n", bp->fw_version); 2707b0ca789aSJonathan Lemon if (bp->fw_version & 0xffff) 2708773bda96SJonathan Lemon dev_info(dev, "regular image, version %d\n", 2709b0ca789aSJonathan Lemon bp->fw_version & 0xffff); 2710773bda96SJonathan Lemon else 2711773bda96SJonathan Lemon dev_info(dev, "golden image, version %d\n", 2712b0ca789aSJonathan Lemon bp->fw_version >> 16); 2713b0ca789aSJonathan Lemon 2714065efcc5SJonathan Lemon ptp_ocp_serial_info(dev, "GNSS", bp->gnss_port, 115200); 271571d7e085SJonathan Lemon ptp_ocp_serial_info(dev, "GNSS2", bp->gnss2_port, 115200); 2716065efcc5SJonathan Lemon ptp_ocp_serial_info(dev, "MAC", bp->mac_port, 57600); 2717e3516bb4SJonathan Lemon if (bp->nmea_out && bp->nmea_port != -1) { 2718e3516bb4SJonathan Lemon int baud = -1; 2719e3516bb4SJonathan Lemon 2720e3516bb4SJonathan Lemon reg = ioread32(&bp->nmea_out->uart_baud); 2721e3516bb4SJonathan Lemon if (reg < ARRAY_SIZE(nmea_baud)) 2722e3516bb4SJonathan Lemon baud = nmea_baud[reg]; 2723e3516bb4SJonathan Lemon ptp_ocp_serial_info(dev, "NMEA", bp->nmea_port, baud); 2724e3516bb4SJonathan Lemon } 2725773bda96SJonathan Lemon } 2726773bda96SJonathan Lemon 2727773bda96SJonathan Lemon static void 2728773bda96SJonathan Lemon ptp_ocp_detach_sysfs(struct ptp_ocp *bp) 2729773bda96SJonathan Lemon { 2730773bda96SJonathan Lemon struct device *dev = &bp->dev; 2731*c205d53cSJonathan Lemon int i; 2732773bda96SJonathan Lemon 2733ef0cfb34SJonathan Lemon sysfs_remove_link(&dev->kobj, "ttyGNSS"); 2734773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "ttyMAC"); 2735773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "ptp"); 2736773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "pps"); 2737*c205d53cSJonathan Lemon if (bp->attr_tbl) 2738*c205d53cSJonathan Lemon for (i = 0; bp->attr_tbl[i].cap; i++) 2739*c205d53cSJonathan Lemon sysfs_remove_group(&dev->kobj, bp->attr_tbl[i].group); 2740773bda96SJonathan Lemon } 2741773bda96SJonathan Lemon 2742773bda96SJonathan Lemon static void 2743773bda96SJonathan Lemon ptp_ocp_detach(struct ptp_ocp *bp) 2744773bda96SJonathan Lemon { 2745f67bf662SJonathan Lemon ptp_ocp_debugfs_remove_device(bp); 2746773bda96SJonathan Lemon ptp_ocp_detach_sysfs(bp); 2747773bda96SJonathan Lemon if (timer_pending(&bp->watchdog)) 2748773bda96SJonathan Lemon del_timer_sync(&bp->watchdog); 2749773bda96SJonathan Lemon if (bp->ts0) 2750773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->ts0); 2751773bda96SJonathan Lemon if (bp->ts1) 2752773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->ts1); 2753dcf61469SJonathan Lemon if (bp->ts2) 2754dcf61469SJonathan Lemon ptp_ocp_unregister_ext(bp->ts2); 2755773bda96SJonathan Lemon if (bp->pps) 2756773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->pps); 2757ef0cfb34SJonathan Lemon if (bp->gnss_port != -1) 2758ef0cfb34SJonathan Lemon serial8250_unregister_port(bp->gnss_port); 275971d7e085SJonathan Lemon if (bp->gnss2_port != -1) 276071d7e085SJonathan Lemon serial8250_unregister_port(bp->gnss2_port); 2761773bda96SJonathan Lemon if (bp->mac_port != -1) 2762773bda96SJonathan Lemon serial8250_unregister_port(bp->mac_port); 2763e3516bb4SJonathan Lemon if (bp->nmea_port != -1) 2764e3516bb4SJonathan Lemon serial8250_unregister_port(bp->nmea_port); 2765773bda96SJonathan Lemon if (bp->spi_flash) 2766773bda96SJonathan Lemon platform_device_unregister(bp->spi_flash); 2767773bda96SJonathan Lemon if (bp->i2c_ctrl) 2768773bda96SJonathan Lemon platform_device_unregister(bp->i2c_ctrl); 2769773bda96SJonathan Lemon if (bp->i2c_clk) 2770773bda96SJonathan Lemon clk_hw_unregister_fixed_rate(bp->i2c_clk); 2771773bda96SJonathan Lemon if (bp->n_irqs) 2772773bda96SJonathan Lemon pci_free_irq_vectors(bp->pdev); 2773773bda96SJonathan Lemon if (bp->ptp) 2774773bda96SJonathan Lemon ptp_clock_unregister(bp->ptp); 2775773bda96SJonathan Lemon device_unregister(&bp->dev); 2776773bda96SJonathan Lemon } 2777773bda96SJonathan Lemon 2778a7e1abadSJonathan Lemon static int 2779a7e1abadSJonathan Lemon ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id) 2780a7e1abadSJonathan Lemon { 2781773bda96SJonathan Lemon struct devlink *devlink; 2782a7e1abadSJonathan Lemon struct ptp_ocp *bp; 2783a7e1abadSJonathan Lemon int err; 2784a7e1abadSJonathan Lemon 2785919d13a7SLeon Romanovsky devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp), &pdev->dev); 2786773bda96SJonathan Lemon if (!devlink) { 2787773bda96SJonathan Lemon dev_err(&pdev->dev, "devlink_alloc failed\n"); 2788a7e1abadSJonathan Lemon return -ENOMEM; 2789773bda96SJonathan Lemon } 2790773bda96SJonathan Lemon 2791a7e1abadSJonathan Lemon err = pci_enable_device(pdev); 2792a7e1abadSJonathan Lemon if (err) { 2793a7e1abadSJonathan Lemon dev_err(&pdev->dev, "pci_enable_device\n"); 27944587369bSJonathan Lemon goto out_free; 2795a7e1abadSJonathan Lemon } 2796a7e1abadSJonathan Lemon 2797773bda96SJonathan Lemon bp = devlink_priv(devlink); 2798773bda96SJonathan Lemon err = ptp_ocp_device_init(bp, pdev); 2799773bda96SJonathan Lemon if (err) 2800d9fdbf13SJonathan Lemon goto out_disable; 2801a7e1abadSJonathan Lemon 2802773bda96SJonathan Lemon /* compat mode. 2803773bda96SJonathan Lemon * Older FPGA firmware only returns 2 irq's. 2804773bda96SJonathan Lemon * allow this - if not all of the IRQ's are returned, skip the 2805773bda96SJonathan Lemon * extra devices and just register the clock. 2806773bda96SJonathan Lemon */ 2807e3516bb4SJonathan Lemon err = pci_alloc_irq_vectors(pdev, 1, 11, PCI_IRQ_MSI | PCI_IRQ_MSIX); 2808773bda96SJonathan Lemon if (err < 0) { 2809773bda96SJonathan Lemon dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err); 2810773bda96SJonathan Lemon goto out; 2811a7e1abadSJonathan Lemon } 2812773bda96SJonathan Lemon bp->n_irqs = err; 2813773bda96SJonathan Lemon pci_set_master(pdev); 2814a7e1abadSJonathan Lemon 2815773bda96SJonathan Lemon err = ptp_ocp_register_resources(bp, id->driver_data); 2816a7e1abadSJonathan Lemon if (err) 2817a7e1abadSJonathan Lemon goto out; 2818a7e1abadSJonathan Lemon 2819a7e1abadSJonathan Lemon bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev); 2820a7e1abadSJonathan Lemon if (IS_ERR(bp->ptp)) { 2821a7e1abadSJonathan Lemon err = PTR_ERR(bp->ptp); 2822773bda96SJonathan Lemon dev_err(&pdev->dev, "ptp_clock_register: %d\n", err); 2823773bda96SJonathan Lemon bp->ptp = NULL; 2824a7e1abadSJonathan Lemon goto out; 2825a7e1abadSJonathan Lemon } 2826a7e1abadSJonathan Lemon 2827773bda96SJonathan Lemon err = ptp_ocp_complete(bp); 2828773bda96SJonathan Lemon if (err) 2829773bda96SJonathan Lemon goto out; 2830773bda96SJonathan Lemon 2831a7e1abadSJonathan Lemon ptp_ocp_info(bp); 2832c89f78e9SLeon Romanovsky devlink_register(devlink); 2833a7e1abadSJonathan Lemon return 0; 2834a7e1abadSJonathan Lemon 2835a7e1abadSJonathan Lemon out: 2836773bda96SJonathan Lemon ptp_ocp_detach(bp); 2837773bda96SJonathan Lemon pci_set_drvdata(pdev, NULL); 2838d9fdbf13SJonathan Lemon out_disable: 2839d9fdbf13SJonathan Lemon pci_disable_device(pdev); 28404587369bSJonathan Lemon out_free: 2841773bda96SJonathan Lemon devlink_free(devlink); 2842a7e1abadSJonathan Lemon return err; 2843a7e1abadSJonathan Lemon } 2844a7e1abadSJonathan Lemon 2845a7e1abadSJonathan Lemon static void 2846a7e1abadSJonathan Lemon ptp_ocp_remove(struct pci_dev *pdev) 2847a7e1abadSJonathan Lemon { 2848a7e1abadSJonathan Lemon struct ptp_ocp *bp = pci_get_drvdata(pdev); 2849773bda96SJonathan Lemon struct devlink *devlink = priv_to_devlink(bp); 2850a7e1abadSJonathan Lemon 2851c89f78e9SLeon Romanovsky devlink_unregister(devlink); 2852773bda96SJonathan Lemon ptp_ocp_detach(bp); 2853a7e1abadSJonathan Lemon pci_set_drvdata(pdev, NULL); 2854d9fdbf13SJonathan Lemon pci_disable_device(pdev); 2855773bda96SJonathan Lemon 2856773bda96SJonathan Lemon devlink_free(devlink); 2857a7e1abadSJonathan Lemon } 2858a7e1abadSJonathan Lemon 2859a7e1abadSJonathan Lemon static struct pci_driver ptp_ocp_driver = { 2860a7e1abadSJonathan Lemon .name = KBUILD_MODNAME, 2861a7e1abadSJonathan Lemon .id_table = ptp_ocp_pcidev_id, 2862a7e1abadSJonathan Lemon .probe = ptp_ocp_probe, 2863a7e1abadSJonathan Lemon .remove = ptp_ocp_remove, 2864a7e1abadSJonathan Lemon }; 2865a7e1abadSJonathan Lemon 2866773bda96SJonathan Lemon static int 2867773bda96SJonathan Lemon ptp_ocp_i2c_notifier_call(struct notifier_block *nb, 2868773bda96SJonathan Lemon unsigned long action, void *data) 2869773bda96SJonathan Lemon { 2870773bda96SJonathan Lemon struct device *dev, *child = data; 2871773bda96SJonathan Lemon struct ptp_ocp *bp; 2872773bda96SJonathan Lemon bool add; 2873773bda96SJonathan Lemon 2874773bda96SJonathan Lemon switch (action) { 2875773bda96SJonathan Lemon case BUS_NOTIFY_ADD_DEVICE: 2876773bda96SJonathan Lemon case BUS_NOTIFY_DEL_DEVICE: 2877773bda96SJonathan Lemon add = action == BUS_NOTIFY_ADD_DEVICE; 2878773bda96SJonathan Lemon break; 2879773bda96SJonathan Lemon default: 2880773bda96SJonathan Lemon return 0; 2881773bda96SJonathan Lemon } 2882773bda96SJonathan Lemon 2883773bda96SJonathan Lemon if (!i2c_verify_adapter(child)) 2884773bda96SJonathan Lemon return 0; 2885773bda96SJonathan Lemon 2886773bda96SJonathan Lemon dev = child; 2887773bda96SJonathan Lemon while ((dev = dev->parent)) 2888773bda96SJonathan Lemon if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME)) 2889773bda96SJonathan Lemon goto found; 2890773bda96SJonathan Lemon return 0; 2891773bda96SJonathan Lemon 2892773bda96SJonathan Lemon found: 2893773bda96SJonathan Lemon bp = dev_get_drvdata(dev); 2894773bda96SJonathan Lemon if (add) 2895773bda96SJonathan Lemon ptp_ocp_symlink(bp, child, "i2c"); 2896773bda96SJonathan Lemon else 2897773bda96SJonathan Lemon sysfs_remove_link(&bp->dev.kobj, "i2c"); 2898773bda96SJonathan Lemon 2899773bda96SJonathan Lemon return 0; 2900773bda96SJonathan Lemon } 2901773bda96SJonathan Lemon 2902773bda96SJonathan Lemon static struct notifier_block ptp_ocp_i2c_notifier = { 2903773bda96SJonathan Lemon .notifier_call = ptp_ocp_i2c_notifier_call, 2904773bda96SJonathan Lemon }; 2905773bda96SJonathan Lemon 2906a7e1abadSJonathan Lemon static int __init 2907a7e1abadSJonathan Lemon ptp_ocp_init(void) 2908a7e1abadSJonathan Lemon { 2909773bda96SJonathan Lemon const char *what; 2910a7e1abadSJonathan Lemon int err; 2911a7e1abadSJonathan Lemon 2912f67bf662SJonathan Lemon ptp_ocp_debugfs_init(); 2913f67bf662SJonathan Lemon 2914773bda96SJonathan Lemon what = "timecard class"; 2915773bda96SJonathan Lemon err = class_register(&timecard_class); 2916773bda96SJonathan Lemon if (err) 2917773bda96SJonathan Lemon goto out; 2918773bda96SJonathan Lemon 2919773bda96SJonathan Lemon what = "i2c notifier"; 2920773bda96SJonathan Lemon err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2921773bda96SJonathan Lemon if (err) 2922773bda96SJonathan Lemon goto out_notifier; 2923773bda96SJonathan Lemon 2924773bda96SJonathan Lemon what = "ptp_ocp driver"; 2925a7e1abadSJonathan Lemon err = pci_register_driver(&ptp_ocp_driver); 2926773bda96SJonathan Lemon if (err) 2927773bda96SJonathan Lemon goto out_register; 2928773bda96SJonathan Lemon 2929773bda96SJonathan Lemon return 0; 2930773bda96SJonathan Lemon 2931773bda96SJonathan Lemon out_register: 2932773bda96SJonathan Lemon bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2933773bda96SJonathan Lemon out_notifier: 2934773bda96SJonathan Lemon class_unregister(&timecard_class); 2935773bda96SJonathan Lemon out: 2936f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(); 2937773bda96SJonathan Lemon pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err); 2938a7e1abadSJonathan Lemon return err; 2939a7e1abadSJonathan Lemon } 2940a7e1abadSJonathan Lemon 2941a7e1abadSJonathan Lemon static void __exit 2942a7e1abadSJonathan Lemon ptp_ocp_fini(void) 2943a7e1abadSJonathan Lemon { 2944773bda96SJonathan Lemon bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2945a7e1abadSJonathan Lemon pci_unregister_driver(&ptp_ocp_driver); 2946773bda96SJonathan Lemon class_unregister(&timecard_class); 2947f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(); 2948a7e1abadSJonathan Lemon } 2949a7e1abadSJonathan Lemon 2950a7e1abadSJonathan Lemon module_init(ptp_ocp_init); 2951a7e1abadSJonathan Lemon module_exit(ptp_ocp_fini); 2952a7e1abadSJonathan Lemon 2953a7e1abadSJonathan Lemon MODULE_DESCRIPTION("OpenCompute TimeCard driver"); 2954a7e1abadSJonathan Lemon MODULE_LICENSE("GPL v2"); 2955