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 2210cfcdd1eSJonathan Lemon #define OCP_BOARD_ID_LEN 13 2220cfcdd1eSJonathan Lemon #define OCP_SERIAL_LEN 6 2230cfcdd1eSJonathan Lemon 224a7e1abadSJonathan Lemon struct ptp_ocp { 225a7e1abadSJonathan Lemon struct pci_dev *pdev; 226773bda96SJonathan Lemon struct device dev; 227a7e1abadSJonathan Lemon spinlock_t lock; 228a7e1abadSJonathan Lemon struct ocp_reg __iomem *reg; 229a7e1abadSJonathan Lemon struct tod_reg __iomem *tod; 2300d43d4f2SJonathan Lemon struct pps_reg __iomem *pps_to_ext; 2310d43d4f2SJonathan Lemon struct pps_reg __iomem *pps_to_clk; 232f67bf662SJonathan Lemon struct gpio_reg __iomem *pps_select; 233a509a7c6SJonathan Lemon struct gpio_reg __iomem *sma_map1; 234a509a7c6SJonathan Lemon struct gpio_reg __iomem *sma_map2; 2356baf2925SJonathan Lemon struct irig_master_reg __iomem *irig_out; 2366baf2925SJonathan Lemon struct irig_slave_reg __iomem *irig_in; 2376baf2925SJonathan Lemon struct dcf_master_reg __iomem *dcf_out; 2386baf2925SJonathan Lemon struct dcf_slave_reg __iomem *dcf_in; 239e3516bb4SJonathan Lemon struct tod_reg __iomem *nmea_out; 240773bda96SJonathan Lemon struct ptp_ocp_ext_src *pps; 241773bda96SJonathan Lemon struct ptp_ocp_ext_src *ts0; 242773bda96SJonathan Lemon struct ptp_ocp_ext_src *ts1; 243dcf61469SJonathan Lemon struct ptp_ocp_ext_src *ts2; 244773bda96SJonathan Lemon struct img_reg __iomem *image; 245a7e1abadSJonathan Lemon struct ptp_clock *ptp; 246a7e1abadSJonathan Lemon struct ptp_clock_info ptp_info; 247773bda96SJonathan Lemon struct platform_device *i2c_ctrl; 248773bda96SJonathan Lemon struct platform_device *spi_flash; 249773bda96SJonathan Lemon struct clk_hw *i2c_clk; 250773bda96SJonathan Lemon struct timer_list watchdog; 2510cfcdd1eSJonathan Lemon const struct ptp_ocp_eeprom_map *eeprom_map; 252f67bf662SJonathan Lemon struct dentry *debug_root; 253ef0cfb34SJonathan Lemon time64_t gnss_lost; 254773bda96SJonathan Lemon int id; 255773bda96SJonathan Lemon int n_irqs; 256ef0cfb34SJonathan Lemon int gnss_port; 25771d7e085SJonathan Lemon int gnss2_port; 258773bda96SJonathan Lemon int mac_port; /* miniature atomic clock */ 259e3516bb4SJonathan Lemon int nmea_port; 260b0ca789aSJonathan Lemon u32 fw_version; 2610cfcdd1eSJonathan Lemon u8 board_id[OCP_BOARD_ID_LEN]; 2620cfcdd1eSJonathan Lemon u8 serial[OCP_SERIAL_LEN]; 2630cfcdd1eSJonathan Lemon bool has_eeprom_data; 264a62a56d0SJonathan Lemon u32 pps_req_map; 26589260d87SJonathan Lemon int flash_start; 26689260d87SJonathan Lemon u32 utc_tai_offset; 2671acffc6eSJonathan Lemon u32 ts_window_adjust; 268a509a7c6SJonathan Lemon struct ptp_ocp_sma_connector sma[4]; 269a7e1abadSJonathan Lemon }; 270a7e1abadSJonathan Lemon 271a62a56d0SJonathan Lemon #define OCP_REQ_TIMESTAMP BIT(0) 272a62a56d0SJonathan Lemon #define OCP_REQ_PPS BIT(1) 273a62a56d0SJonathan Lemon 274773bda96SJonathan Lemon struct ocp_resource { 275773bda96SJonathan Lemon unsigned long offset; 276773bda96SJonathan Lemon int size; 277773bda96SJonathan Lemon int irq_vec; 278773bda96SJonathan Lemon int (*setup)(struct ptp_ocp *bp, struct ocp_resource *r); 279773bda96SJonathan Lemon void *extra; 280773bda96SJonathan Lemon unsigned long bp_offset; 28156ec4403SJonathan Lemon const char * const name; 282773bda96SJonathan Lemon }; 283773bda96SJonathan Lemon 284773bda96SJonathan Lemon static int ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r); 285773bda96SJonathan Lemon static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r); 286773bda96SJonathan Lemon static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r); 287773bda96SJonathan Lemon static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r); 288773bda96SJonathan Lemon static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r); 289773bda96SJonathan Lemon static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r); 290773bda96SJonathan Lemon static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv); 291a62a56d0SJonathan Lemon static int ptp_ocp_ts_enable(void *priv, u32 req, bool enable); 292773bda96SJonathan Lemon 2930cfcdd1eSJonathan Lemon struct ptp_ocp_eeprom_map { 2940cfcdd1eSJonathan Lemon u16 off; 2950cfcdd1eSJonathan Lemon u16 len; 2960cfcdd1eSJonathan Lemon u32 bp_offset; 2970cfcdd1eSJonathan Lemon const void * const tag; 2980cfcdd1eSJonathan Lemon }; 2990cfcdd1eSJonathan Lemon 3000cfcdd1eSJonathan Lemon #define EEPROM_ENTRY(addr, member) \ 3010cfcdd1eSJonathan Lemon .off = addr, \ 3020cfcdd1eSJonathan Lemon .len = sizeof_field(struct ptp_ocp, member), \ 3030cfcdd1eSJonathan Lemon .bp_offset = offsetof(struct ptp_ocp, member) 3040cfcdd1eSJonathan Lemon 3050cfcdd1eSJonathan Lemon #define BP_MAP_ENTRY_ADDR(bp, map) ({ \ 3060cfcdd1eSJonathan Lemon (void *)((uintptr_t)(bp) + (map)->bp_offset); \ 3070cfcdd1eSJonathan Lemon }) 3080cfcdd1eSJonathan Lemon 3090cfcdd1eSJonathan Lemon static struct ptp_ocp_eeprom_map fb_eeprom_map[] = { 3100cfcdd1eSJonathan Lemon { EEPROM_ENTRY(0x43, board_id) }, 3110cfcdd1eSJonathan Lemon { EEPROM_ENTRY(0x00, serial), .tag = "mac" }, 3120cfcdd1eSJonathan Lemon { } 3130cfcdd1eSJonathan Lemon }; 3140cfcdd1eSJonathan Lemon 315773bda96SJonathan Lemon #define bp_assign_entry(bp, res, val) ({ \ 316773bda96SJonathan Lemon uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset; \ 317773bda96SJonathan Lemon *(typeof(val) *)addr = val; \ 318773bda96SJonathan Lemon }) 319773bda96SJonathan Lemon 320773bda96SJonathan Lemon #define OCP_RES_LOCATION(member) \ 32156ec4403SJonathan Lemon .name = #member, .bp_offset = offsetof(struct ptp_ocp, member) 322773bda96SJonathan Lemon 323773bda96SJonathan Lemon #define OCP_MEM_RESOURCE(member) \ 324773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_mem 325773bda96SJonathan Lemon 326773bda96SJonathan Lemon #define OCP_SERIAL_RESOURCE(member) \ 327773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_serial 328773bda96SJonathan Lemon 329773bda96SJonathan Lemon #define OCP_I2C_RESOURCE(member) \ 330773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_i2c 331773bda96SJonathan Lemon 332773bda96SJonathan Lemon #define OCP_SPI_RESOURCE(member) \ 333773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_spi 334773bda96SJonathan Lemon 335773bda96SJonathan Lemon #define OCP_EXT_RESOURCE(member) \ 336773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_ext 337773bda96SJonathan Lemon 338773bda96SJonathan Lemon /* This is the MSI vector mapping used. 339a62a56d0SJonathan Lemon * 0: TS3 (and PPS) 340773bda96SJonathan Lemon * 1: TS0 341773bda96SJonathan Lemon * 2: TS1 342*be69087cSJonathan Lemon * 3: GNSS1 34371d7e085SJonathan Lemon * 4: GNSS2 344773bda96SJonathan Lemon * 5: MAC 345dcf61469SJonathan Lemon * 6: TS2 3461447149dSJonathan Lemon * 7: I2C controller 347e3516bb4SJonathan Lemon * 8: HWICAP (notused) 348773bda96SJonathan Lemon * 9: SPI Flash 349e3516bb4SJonathan Lemon * 10: NMEA 350773bda96SJonathan Lemon */ 351773bda96SJonathan Lemon 352773bda96SJonathan Lemon static struct ocp_resource ocp_fb_resource[] = { 353773bda96SJonathan Lemon { 354773bda96SJonathan Lemon OCP_MEM_RESOURCE(reg), 355773bda96SJonathan Lemon .offset = 0x01000000, .size = 0x10000, 356773bda96SJonathan Lemon }, 357773bda96SJonathan Lemon { 358773bda96SJonathan Lemon OCP_EXT_RESOURCE(ts0), 359773bda96SJonathan Lemon .offset = 0x01010000, .size = 0x10000, .irq_vec = 1, 360773bda96SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 36156ec4403SJonathan Lemon .index = 0, 362773bda96SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 363773bda96SJonathan Lemon .enable = ptp_ocp_ts_enable, 364773bda96SJonathan Lemon }, 365773bda96SJonathan Lemon }, 366773bda96SJonathan Lemon { 367773bda96SJonathan Lemon OCP_EXT_RESOURCE(ts1), 368773bda96SJonathan Lemon .offset = 0x01020000, .size = 0x10000, .irq_vec = 2, 369773bda96SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 37056ec4403SJonathan Lemon .index = 1, 371773bda96SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 372773bda96SJonathan Lemon .enable = ptp_ocp_ts_enable, 373773bda96SJonathan Lemon }, 374773bda96SJonathan Lemon }, 375773bda96SJonathan Lemon { 376dcf61469SJonathan Lemon OCP_EXT_RESOURCE(ts2), 377dcf61469SJonathan Lemon .offset = 0x01060000, .size = 0x10000, .irq_vec = 6, 378dcf61469SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 379dcf61469SJonathan Lemon .index = 2, 380dcf61469SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 381dcf61469SJonathan Lemon .enable = ptp_ocp_ts_enable, 382dcf61469SJonathan Lemon }, 383dcf61469SJonathan Lemon }, 384dcf61469SJonathan Lemon { 385a62a56d0SJonathan Lemon OCP_EXT_RESOURCE(pps), 386a62a56d0SJonathan Lemon .offset = 0x010C0000, .size = 0x10000, .irq_vec = 0, 387a62a56d0SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 388a62a56d0SJonathan Lemon .index = 3, 389a62a56d0SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 390a62a56d0SJonathan Lemon .enable = ptp_ocp_ts_enable, 391a62a56d0SJonathan Lemon }, 392a62a56d0SJonathan Lemon }, 393a62a56d0SJonathan Lemon { 3940d43d4f2SJonathan Lemon OCP_MEM_RESOURCE(pps_to_ext), 3950d43d4f2SJonathan Lemon .offset = 0x01030000, .size = 0x10000, 3960d43d4f2SJonathan Lemon }, 3970d43d4f2SJonathan Lemon { 3980d43d4f2SJonathan Lemon OCP_MEM_RESOURCE(pps_to_clk), 399773bda96SJonathan Lemon .offset = 0x01040000, .size = 0x10000, 400773bda96SJonathan Lemon }, 401773bda96SJonathan Lemon { 402773bda96SJonathan Lemon OCP_MEM_RESOURCE(tod), 403773bda96SJonathan Lemon .offset = 0x01050000, .size = 0x10000, 404773bda96SJonathan Lemon }, 405773bda96SJonathan Lemon { 4066baf2925SJonathan Lemon OCP_MEM_RESOURCE(irig_in), 4076baf2925SJonathan Lemon .offset = 0x01070000, .size = 0x10000, 4086baf2925SJonathan Lemon }, 4096baf2925SJonathan Lemon { 4106baf2925SJonathan Lemon OCP_MEM_RESOURCE(irig_out), 4116baf2925SJonathan Lemon .offset = 0x01080000, .size = 0x10000, 4126baf2925SJonathan Lemon }, 4136baf2925SJonathan Lemon { 4146baf2925SJonathan Lemon OCP_MEM_RESOURCE(dcf_in), 4156baf2925SJonathan Lemon .offset = 0x01090000, .size = 0x10000, 4166baf2925SJonathan Lemon }, 4176baf2925SJonathan Lemon { 4186baf2925SJonathan Lemon OCP_MEM_RESOURCE(dcf_out), 4196baf2925SJonathan Lemon .offset = 0x010A0000, .size = 0x10000, 4206baf2925SJonathan Lemon }, 4216baf2925SJonathan Lemon { 422e3516bb4SJonathan Lemon OCP_MEM_RESOURCE(nmea_out), 423e3516bb4SJonathan Lemon .offset = 0x010B0000, .size = 0x10000, 424e3516bb4SJonathan Lemon }, 425e3516bb4SJonathan Lemon { 426773bda96SJonathan Lemon OCP_MEM_RESOURCE(image), 427773bda96SJonathan Lemon .offset = 0x00020000, .size = 0x1000, 428773bda96SJonathan Lemon }, 429773bda96SJonathan Lemon { 430f67bf662SJonathan Lemon OCP_MEM_RESOURCE(pps_select), 431f67bf662SJonathan Lemon .offset = 0x00130000, .size = 0x1000, 432f67bf662SJonathan Lemon }, 433f67bf662SJonathan Lemon { 434a509a7c6SJonathan Lemon OCP_MEM_RESOURCE(sma_map1), 435e1daf0ecSJonathan Lemon .offset = 0x00140000, .size = 0x1000, 436e1daf0ecSJonathan Lemon }, 437e1daf0ecSJonathan Lemon { 438a509a7c6SJonathan Lemon OCP_MEM_RESOURCE(sma_map2), 439a509a7c6SJonathan Lemon .offset = 0x00220000, .size = 0x1000, 440a509a7c6SJonathan Lemon }, 441a509a7c6SJonathan Lemon { 442773bda96SJonathan Lemon OCP_I2C_RESOURCE(i2c_ctrl), 443773bda96SJonathan Lemon .offset = 0x00150000, .size = 0x10000, .irq_vec = 7, 4441618df6aSJonathan Lemon .extra = &(struct ptp_ocp_i2c_info) { 4451618df6aSJonathan Lemon .name = "xiic-i2c", 4461618df6aSJonathan Lemon .fixed_rate = 50000000, 4470cfcdd1eSJonathan Lemon .data_size = sizeof(struct xiic_i2c_platform_data), 4480cfcdd1eSJonathan Lemon .data = &(struct xiic_i2c_platform_data) { 4490cfcdd1eSJonathan Lemon .num_devices = 2, 4500cfcdd1eSJonathan Lemon .devices = (struct i2c_board_info[]) { 4510cfcdd1eSJonathan Lemon { I2C_BOARD_INFO("24c02", 0x50) }, 4520cfcdd1eSJonathan Lemon { I2C_BOARD_INFO("24mac402", 0x58), 4530cfcdd1eSJonathan Lemon .platform_data = "mac" }, 4540cfcdd1eSJonathan Lemon }, 4550cfcdd1eSJonathan Lemon }, 4561618df6aSJonathan Lemon }, 457773bda96SJonathan Lemon }, 458773bda96SJonathan Lemon { 459ef0cfb34SJonathan Lemon OCP_SERIAL_RESOURCE(gnss_port), 460773bda96SJonathan Lemon .offset = 0x00160000 + 0x1000, .irq_vec = 3, 461773bda96SJonathan Lemon }, 462773bda96SJonathan Lemon { 46371d7e085SJonathan Lemon OCP_SERIAL_RESOURCE(gnss2_port), 46471d7e085SJonathan Lemon .offset = 0x00170000 + 0x1000, .irq_vec = 4, 46571d7e085SJonathan Lemon }, 46671d7e085SJonathan Lemon { 467773bda96SJonathan Lemon OCP_SERIAL_RESOURCE(mac_port), 468773bda96SJonathan Lemon .offset = 0x00180000 + 0x1000, .irq_vec = 5, 469773bda96SJonathan Lemon }, 470773bda96SJonathan Lemon { 471e3516bb4SJonathan Lemon OCP_SERIAL_RESOURCE(nmea_port), 472e3516bb4SJonathan Lemon .offset = 0x00190000 + 0x1000, .irq_vec = 10, 473e3516bb4SJonathan Lemon }, 474e3516bb4SJonathan Lemon { 475773bda96SJonathan Lemon OCP_SPI_RESOURCE(spi_flash), 476773bda96SJonathan Lemon .offset = 0x00310000, .size = 0x10000, .irq_vec = 9, 477773bda96SJonathan Lemon .extra = &(struct ptp_ocp_flash_info) { 478773bda96SJonathan Lemon .name = "xilinx_spi", .pci_offset = 0, 479773bda96SJonathan Lemon .data_size = sizeof(struct xspi_platform_data), 480773bda96SJonathan Lemon .data = &(struct xspi_platform_data) { 481773bda96SJonathan Lemon .num_chipselect = 1, 482773bda96SJonathan Lemon .bits_per_word = 8, 483773bda96SJonathan Lemon .num_devices = 1, 484773bda96SJonathan Lemon .devices = &(struct spi_board_info) { 485773bda96SJonathan Lemon .modalias = "spi-nor", 486773bda96SJonathan Lemon }, 487773bda96SJonathan Lemon }, 488773bda96SJonathan Lemon }, 489773bda96SJonathan Lemon }, 490773bda96SJonathan Lemon { 491773bda96SJonathan Lemon .setup = ptp_ocp_fb_board_init, 492773bda96SJonathan Lemon }, 493773bda96SJonathan Lemon { } 494773bda96SJonathan Lemon }; 495773bda96SJonathan Lemon 496773bda96SJonathan Lemon static const struct pci_device_id ptp_ocp_pcidev_id[] = { 497773bda96SJonathan Lemon { PCI_DEVICE_DATA(FACEBOOK, TIMECARD, &ocp_fb_resource) }, 498773bda96SJonathan Lemon { 0 } 499773bda96SJonathan Lemon }; 500773bda96SJonathan Lemon MODULE_DEVICE_TABLE(pci, ptp_ocp_pcidev_id); 501773bda96SJonathan Lemon 502773bda96SJonathan Lemon static DEFINE_MUTEX(ptp_ocp_lock); 503773bda96SJonathan Lemon static DEFINE_IDR(ptp_ocp_idr); 504773bda96SJonathan Lemon 505e1daf0ecSJonathan Lemon struct ocp_selector { 506773bda96SJonathan Lemon const char *name; 507773bda96SJonathan Lemon int value; 508e1daf0ecSJonathan Lemon }; 509e1daf0ecSJonathan Lemon 510e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_clock[] = { 511773bda96SJonathan Lemon { .name = "NONE", .value = 0 }, 512773bda96SJonathan Lemon { .name = "TOD", .value = 1 }, 513773bda96SJonathan Lemon { .name = "IRIG", .value = 2 }, 514773bda96SJonathan Lemon { .name = "PPS", .value = 3 }, 515773bda96SJonathan Lemon { .name = "PTP", .value = 4 }, 516773bda96SJonathan Lemon { .name = "RTC", .value = 5 }, 517773bda96SJonathan Lemon { .name = "DCF", .value = 6 }, 518773bda96SJonathan Lemon { .name = "REGS", .value = 0xfe }, 519773bda96SJonathan Lemon { .name = "EXT", .value = 0xff }, 520e1daf0ecSJonathan Lemon { } 521e1daf0ecSJonathan Lemon }; 522e1daf0ecSJonathan Lemon 523a509a7c6SJonathan Lemon #define SMA_ENABLE BIT(15) 524a509a7c6SJonathan Lemon #define SMA_SELECT_MASK ((1U << 15) - 1) 525b2c4f0acSJonathan Lemon #define SMA_DISABLE 0x10000 526a509a7c6SJonathan Lemon 527e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_sma_in[] = { 528e1daf0ecSJonathan Lemon { .name = "10Mhz", .value = 0x00 }, 529e1daf0ecSJonathan Lemon { .name = "PPS1", .value = 0x01 }, 530e1daf0ecSJonathan Lemon { .name = "PPS2", .value = 0x02 }, 531e1daf0ecSJonathan Lemon { .name = "TS1", .value = 0x04 }, 532e1daf0ecSJonathan Lemon { .name = "TS2", .value = 0x08 }, 5336baf2925SJonathan Lemon { .name = "IRIG", .value = 0x10 }, 5346baf2925SJonathan Lemon { .name = "DCF", .value = 0x20 }, 535b2c4f0acSJonathan Lemon { .name = "None", .value = SMA_DISABLE }, 536e1daf0ecSJonathan Lemon { } 537e1daf0ecSJonathan Lemon }; 538e1daf0ecSJonathan Lemon 539e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_sma_out[] = { 540e1daf0ecSJonathan Lemon { .name = "10Mhz", .value = 0x00 }, 541e1daf0ecSJonathan Lemon { .name = "PHC", .value = 0x01 }, 542e1daf0ecSJonathan Lemon { .name = "MAC", .value = 0x02 }, 543*be69087cSJonathan Lemon { .name = "GNSS1", .value = 0x04 }, 544e1daf0ecSJonathan Lemon { .name = "GNSS2", .value = 0x08 }, 5456baf2925SJonathan Lemon { .name = "IRIG", .value = 0x10 }, 5466baf2925SJonathan Lemon { .name = "DCF", .value = 0x20 }, 547e1daf0ecSJonathan Lemon { } 548773bda96SJonathan Lemon }; 549773bda96SJonathan Lemon 550773bda96SJonathan Lemon static const char * 551e1daf0ecSJonathan Lemon ptp_ocp_select_name_from_val(struct ocp_selector *tbl, int val) 552773bda96SJonathan Lemon { 553773bda96SJonathan Lemon int i; 554773bda96SJonathan Lemon 555e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) 556e1daf0ecSJonathan Lemon if (tbl[i].value == val) 557e1daf0ecSJonathan Lemon return tbl[i].name; 558773bda96SJonathan Lemon return NULL; 559773bda96SJonathan Lemon } 560773bda96SJonathan Lemon 561773bda96SJonathan Lemon static int 562e1daf0ecSJonathan Lemon ptp_ocp_select_val_from_name(struct ocp_selector *tbl, const char *name) 563773bda96SJonathan Lemon { 564e1daf0ecSJonathan Lemon const char *select; 565773bda96SJonathan Lemon int i; 566773bda96SJonathan Lemon 567e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) { 568e1daf0ecSJonathan Lemon select = tbl[i].name; 569e1daf0ecSJonathan Lemon if (!strncasecmp(name, select, strlen(select))) 570e1daf0ecSJonathan Lemon return tbl[i].value; 571773bda96SJonathan Lemon } 572773bda96SJonathan Lemon return -EINVAL; 573773bda96SJonathan Lemon } 574773bda96SJonathan Lemon 575e1daf0ecSJonathan Lemon static ssize_t 576e1daf0ecSJonathan Lemon ptp_ocp_select_table_show(struct ocp_selector *tbl, char *buf) 577e1daf0ecSJonathan Lemon { 578e1daf0ecSJonathan Lemon ssize_t count; 579e1daf0ecSJonathan Lemon int i; 580e1daf0ecSJonathan Lemon 581e1daf0ecSJonathan Lemon count = 0; 582e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) 583e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", tbl[i].name); 584e1daf0ecSJonathan Lemon if (count) 585e1daf0ecSJonathan Lemon count--; 586e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "\n"); 587e1daf0ecSJonathan Lemon return count; 588e1daf0ecSJonathan Lemon } 589e1daf0ecSJonathan Lemon 590a7e1abadSJonathan Lemon static int 591a7e1abadSJonathan Lemon __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts, 592a7e1abadSJonathan Lemon struct ptp_system_timestamp *sts) 593a7e1abadSJonathan Lemon { 594a7e1abadSJonathan Lemon u32 ctrl, time_sec, time_ns; 595a7e1abadSJonathan Lemon int i; 596a7e1abadSJonathan Lemon 597a7e1abadSJonathan Lemon ptp_read_system_prets(sts); 5981acffc6eSJonathan Lemon 5991acffc6eSJonathan Lemon ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE; 600a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 601a7e1abadSJonathan Lemon 602a7e1abadSJonathan Lemon for (i = 0; i < 100; i++) { 603a7e1abadSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 604a7e1abadSJonathan Lemon if (ctrl & OCP_CTRL_READ_TIME_DONE) 605a7e1abadSJonathan Lemon break; 606a7e1abadSJonathan Lemon } 607a7e1abadSJonathan Lemon ptp_read_system_postts(sts); 608a7e1abadSJonathan Lemon 6091acffc6eSJonathan Lemon if (sts && bp->ts_window_adjust) { 6101acffc6eSJonathan Lemon s64 ns = timespec64_to_ns(&sts->post_ts); 6111acffc6eSJonathan Lemon 6121acffc6eSJonathan Lemon sts->post_ts = ns_to_timespec64(ns - bp->ts_window_adjust); 6131acffc6eSJonathan Lemon } 6141acffc6eSJonathan Lemon 615a7e1abadSJonathan Lemon time_ns = ioread32(&bp->reg->time_ns); 616a7e1abadSJonathan Lemon time_sec = ioread32(&bp->reg->time_sec); 617a7e1abadSJonathan Lemon 618a7e1abadSJonathan Lemon ts->tv_sec = time_sec; 619a7e1abadSJonathan Lemon ts->tv_nsec = time_ns; 620a7e1abadSJonathan Lemon 621a7e1abadSJonathan Lemon return ctrl & OCP_CTRL_READ_TIME_DONE ? 0 : -ETIMEDOUT; 622a7e1abadSJonathan Lemon } 623a7e1abadSJonathan Lemon 624a7e1abadSJonathan Lemon static int 625a7e1abadSJonathan Lemon ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts, 626a7e1abadSJonathan Lemon struct ptp_system_timestamp *sts) 627a7e1abadSJonathan Lemon { 628a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 629a7e1abadSJonathan Lemon unsigned long flags; 630a7e1abadSJonathan Lemon int err; 631a7e1abadSJonathan Lemon 632a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 633a7e1abadSJonathan Lemon err = __ptp_ocp_gettime_locked(bp, ts, sts); 634a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 635a7e1abadSJonathan Lemon 636a7e1abadSJonathan Lemon return err; 637a7e1abadSJonathan Lemon } 638a7e1abadSJonathan Lemon 639a7e1abadSJonathan Lemon static void 640a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts) 641a7e1abadSJonathan Lemon { 642a7e1abadSJonathan Lemon u32 ctrl, time_sec, time_ns; 643a7e1abadSJonathan Lemon u32 select; 644a7e1abadSJonathan Lemon 645a7e1abadSJonathan Lemon time_ns = ts->tv_nsec; 646a7e1abadSJonathan Lemon time_sec = ts->tv_sec; 647a7e1abadSJonathan Lemon 648a7e1abadSJonathan Lemon select = ioread32(&bp->reg->select); 649a7e1abadSJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 650a7e1abadSJonathan Lemon 651a7e1abadSJonathan Lemon iowrite32(time_ns, &bp->reg->adjust_ns); 652a7e1abadSJonathan Lemon iowrite32(time_sec, &bp->reg->adjust_sec); 653a7e1abadSJonathan Lemon 6541acffc6eSJonathan Lemon ctrl = OCP_CTRL_ADJUST_TIME | OCP_CTRL_ENABLE; 655a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 656a7e1abadSJonathan Lemon 657a7e1abadSJonathan Lemon /* restore clock selection */ 658a7e1abadSJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 659a7e1abadSJonathan Lemon } 660a7e1abadSJonathan Lemon 661a7e1abadSJonathan Lemon static int 662a7e1abadSJonathan Lemon ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts) 663a7e1abadSJonathan Lemon { 664a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 665a7e1abadSJonathan Lemon unsigned long flags; 666a7e1abadSJonathan Lemon 667a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 668a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(bp, ts); 669a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 670a7e1abadSJonathan Lemon 671a7e1abadSJonathan Lemon return 0; 672a7e1abadSJonathan Lemon } 673a7e1abadSJonathan Lemon 6746d59d4faSJonathan Lemon static void 67590f8f4c0SJonathan Lemon __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val) 6766d59d4faSJonathan Lemon { 6776d59d4faSJonathan Lemon u32 select, ctrl; 6786d59d4faSJonathan Lemon 6796d59d4faSJonathan Lemon select = ioread32(&bp->reg->select); 6806d59d4faSJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 6816d59d4faSJonathan Lemon 6826d59d4faSJonathan Lemon iowrite32(adj_val, &bp->reg->offset_ns); 68390f8f4c0SJonathan Lemon iowrite32(NSEC_PER_SEC, &bp->reg->offset_window_ns); 6846d59d4faSJonathan Lemon 6856d59d4faSJonathan Lemon ctrl = OCP_CTRL_ADJUST_OFFSET | OCP_CTRL_ENABLE; 6866d59d4faSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 6876d59d4faSJonathan Lemon 6886d59d4faSJonathan Lemon /* restore clock selection */ 6896d59d4faSJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 6906d59d4faSJonathan Lemon } 6916d59d4faSJonathan Lemon 69290f8f4c0SJonathan Lemon static void 69390f8f4c0SJonathan Lemon ptp_ocp_adjtime_coarse(struct ptp_ocp *bp, u64 delta_ns) 69490f8f4c0SJonathan Lemon { 69590f8f4c0SJonathan Lemon struct timespec64 ts; 69690f8f4c0SJonathan Lemon unsigned long flags; 69790f8f4c0SJonathan Lemon int err; 69890f8f4c0SJonathan Lemon 69990f8f4c0SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 70090f8f4c0SJonathan Lemon err = __ptp_ocp_gettime_locked(bp, &ts, NULL); 70190f8f4c0SJonathan Lemon if (likely(!err)) { 70290f8f4c0SJonathan Lemon timespec64_add_ns(&ts, delta_ns); 70390f8f4c0SJonathan Lemon __ptp_ocp_settime_locked(bp, &ts); 70490f8f4c0SJonathan Lemon } 70590f8f4c0SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 70690f8f4c0SJonathan Lemon } 70790f8f4c0SJonathan Lemon 708a7e1abadSJonathan Lemon static int 709a7e1abadSJonathan Lemon ptp_ocp_adjtime(struct ptp_clock_info *ptp_info, s64 delta_ns) 710a7e1abadSJonathan Lemon { 711a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 712a7e1abadSJonathan Lemon unsigned long flags; 7136d59d4faSJonathan Lemon u32 adj_ns, sign; 714a7e1abadSJonathan Lemon 71590f8f4c0SJonathan Lemon if (delta_ns > NSEC_PER_SEC || -delta_ns > NSEC_PER_SEC) { 71690f8f4c0SJonathan Lemon ptp_ocp_adjtime_coarse(bp, delta_ns); 71790f8f4c0SJonathan Lemon return 0; 71890f8f4c0SJonathan Lemon } 71990f8f4c0SJonathan Lemon 7206d59d4faSJonathan Lemon sign = delta_ns < 0 ? BIT(31) : 0; 7216d59d4faSJonathan Lemon adj_ns = sign ? -delta_ns : delta_ns; 722a7e1abadSJonathan Lemon 723a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 7246d59d4faSJonathan Lemon __ptp_ocp_adjtime_locked(bp, sign | adj_ns); 725a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 726a7e1abadSJonathan Lemon 7276d59d4faSJonathan Lemon return 0; 728a7e1abadSJonathan Lemon } 729a7e1abadSJonathan Lemon 730a7e1abadSJonathan Lemon static int 731a7e1abadSJonathan Lemon ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm) 732a7e1abadSJonathan Lemon { 733a7e1abadSJonathan Lemon if (scaled_ppm == 0) 734a7e1abadSJonathan Lemon return 0; 735a7e1abadSJonathan Lemon 736a7e1abadSJonathan Lemon return -EOPNOTSUPP; 737a7e1abadSJonathan Lemon } 738a7e1abadSJonathan Lemon 739773bda96SJonathan Lemon static int 7406d59d4faSJonathan Lemon ptp_ocp_null_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns) 741773bda96SJonathan Lemon { 742773bda96SJonathan Lemon return -EOPNOTSUPP; 743773bda96SJonathan Lemon } 744773bda96SJonathan Lemon 745773bda96SJonathan Lemon static int 746773bda96SJonathan Lemon ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq, 747773bda96SJonathan Lemon int on) 748773bda96SJonathan Lemon { 749773bda96SJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 750773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = NULL; 751a62a56d0SJonathan Lemon u32 req; 752773bda96SJonathan Lemon int err; 753773bda96SJonathan Lemon 754773bda96SJonathan Lemon switch (rq->type) { 755773bda96SJonathan Lemon case PTP_CLK_REQ_EXTTS: 756a62a56d0SJonathan Lemon req = OCP_REQ_TIMESTAMP; 757773bda96SJonathan Lemon switch (rq->extts.index) { 758773bda96SJonathan Lemon case 0: 759773bda96SJonathan Lemon ext = bp->ts0; 760773bda96SJonathan Lemon break; 761773bda96SJonathan Lemon case 1: 762773bda96SJonathan Lemon ext = bp->ts1; 763773bda96SJonathan Lemon break; 764dcf61469SJonathan Lemon case 2: 765dcf61469SJonathan Lemon ext = bp->ts2; 766dcf61469SJonathan Lemon break; 767a62a56d0SJonathan Lemon case 3: 768a62a56d0SJonathan Lemon ext = bp->pps; 769a62a56d0SJonathan Lemon break; 770773bda96SJonathan Lemon } 771773bda96SJonathan Lemon break; 772773bda96SJonathan Lemon case PTP_CLK_REQ_PPS: 773a62a56d0SJonathan Lemon req = OCP_REQ_PPS; 774773bda96SJonathan Lemon ext = bp->pps; 775773bda96SJonathan Lemon break; 776a62a56d0SJonathan Lemon case PTP_CLK_REQ_PEROUT: 777a62a56d0SJonathan Lemon if (on && 778a62a56d0SJonathan Lemon (rq->perout.period.sec != 1 || rq->perout.period.nsec != 0)) 779a62a56d0SJonathan Lemon return -EINVAL; 780a62a56d0SJonathan Lemon /* This is a request for 1PPS on an output SMA. 781a62a56d0SJonathan Lemon * Allow, but assume manual configuration. 782a62a56d0SJonathan Lemon */ 783a62a56d0SJonathan Lemon return 0; 784773bda96SJonathan Lemon default: 785773bda96SJonathan Lemon return -EOPNOTSUPP; 786773bda96SJonathan Lemon } 787773bda96SJonathan Lemon 788773bda96SJonathan Lemon err = -ENXIO; 789773bda96SJonathan Lemon if (ext) 790a62a56d0SJonathan Lemon err = ext->info->enable(ext, req, on); 791773bda96SJonathan Lemon 792773bda96SJonathan Lemon return err; 793773bda96SJonathan Lemon } 794773bda96SJonathan Lemon 795a7e1abadSJonathan Lemon static const struct ptp_clock_info ptp_ocp_clock_info = { 796a7e1abadSJonathan Lemon .owner = THIS_MODULE, 797a7e1abadSJonathan Lemon .name = KBUILD_MODNAME, 798a7e1abadSJonathan Lemon .max_adj = 100000000, 799a7e1abadSJonathan Lemon .gettimex64 = ptp_ocp_gettimex, 800a7e1abadSJonathan Lemon .settime64 = ptp_ocp_settime, 801a7e1abadSJonathan Lemon .adjtime = ptp_ocp_adjtime, 802a7e1abadSJonathan Lemon .adjfine = ptp_ocp_null_adjfine, 8036d59d4faSJonathan Lemon .adjphase = ptp_ocp_null_adjphase, 804773bda96SJonathan Lemon .enable = ptp_ocp_enable, 805773bda96SJonathan Lemon .pps = true, 806a62a56d0SJonathan Lemon .n_ext_ts = 4, 807a62a56d0SJonathan Lemon .n_per_out = 1, 808a7e1abadSJonathan Lemon }; 809a7e1abadSJonathan Lemon 810773bda96SJonathan Lemon static void 811773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp) 812773bda96SJonathan Lemon { 813773bda96SJonathan Lemon u32 ctrl, select; 814773bda96SJonathan Lemon 815773bda96SJonathan Lemon select = ioread32(&bp->reg->select); 816773bda96SJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 817773bda96SJonathan Lemon 818773bda96SJonathan Lemon iowrite32(0, &bp->reg->drift_ns); 819773bda96SJonathan Lemon 8201acffc6eSJonathan Lemon ctrl = OCP_CTRL_ADJUST_DRIFT | OCP_CTRL_ENABLE; 821773bda96SJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 822773bda96SJonathan Lemon 823773bda96SJonathan Lemon /* restore clock selection */ 824773bda96SJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 825773bda96SJonathan Lemon } 826773bda96SJonathan Lemon 827773bda96SJonathan Lemon static void 828e68462a0SVadim Fedorenko ptp_ocp_utc_distribute(struct ptp_ocp *bp, u32 val) 829e68462a0SVadim Fedorenko { 830e68462a0SVadim Fedorenko unsigned long flags; 831e68462a0SVadim Fedorenko 832e68462a0SVadim Fedorenko spin_lock_irqsave(&bp->lock, flags); 833e68462a0SVadim Fedorenko 834e68462a0SVadim Fedorenko bp->utc_tai_offset = val; 835e68462a0SVadim Fedorenko 836e68462a0SVadim Fedorenko if (bp->irig_out) 837e68462a0SVadim Fedorenko iowrite32(val, &bp->irig_out->adj_sec); 838e68462a0SVadim Fedorenko if (bp->dcf_out) 839e68462a0SVadim Fedorenko iowrite32(val, &bp->dcf_out->adj_sec); 840e68462a0SVadim Fedorenko if (bp->nmea_out) 841e68462a0SVadim Fedorenko iowrite32(val, &bp->nmea_out->adj_sec); 842e68462a0SVadim Fedorenko 843e68462a0SVadim Fedorenko spin_unlock_irqrestore(&bp->lock, flags); 844e68462a0SVadim Fedorenko } 845e68462a0SVadim Fedorenko 846e68462a0SVadim Fedorenko static void 847773bda96SJonathan Lemon ptp_ocp_watchdog(struct timer_list *t) 848773bda96SJonathan Lemon { 849773bda96SJonathan Lemon struct ptp_ocp *bp = from_timer(bp, t, watchdog); 850773bda96SJonathan Lemon unsigned long flags; 851e68462a0SVadim Fedorenko u32 status, utc_offset; 852773bda96SJonathan Lemon 8530d43d4f2SJonathan Lemon status = ioread32(&bp->pps_to_clk->status); 854773bda96SJonathan Lemon 855773bda96SJonathan Lemon if (status & PPS_STATUS_SUPERV_ERR) { 8560d43d4f2SJonathan Lemon iowrite32(status, &bp->pps_to_clk->status); 857ef0cfb34SJonathan Lemon if (!bp->gnss_lost) { 858773bda96SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 859773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(bp); 860773bda96SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 861ef0cfb34SJonathan Lemon bp->gnss_lost = ktime_get_real_seconds(); 862773bda96SJonathan Lemon } 863773bda96SJonathan Lemon 864ef0cfb34SJonathan Lemon } else if (bp->gnss_lost) { 865ef0cfb34SJonathan Lemon bp->gnss_lost = 0; 866773bda96SJonathan Lemon } 867773bda96SJonathan Lemon 868e68462a0SVadim Fedorenko /* if GNSS provides correct data we can rely on 869e68462a0SVadim Fedorenko * it to get leap second information 870e68462a0SVadim Fedorenko */ 871e68462a0SVadim Fedorenko if (bp->tod) { 872e68462a0SVadim Fedorenko status = ioread32(&bp->tod->utc_status); 873e68462a0SVadim Fedorenko utc_offset = status & TOD_STATUS_UTC_MASK; 874e68462a0SVadim Fedorenko if (status & TOD_STATUS_UTC_VALID && 875e68462a0SVadim Fedorenko utc_offset != bp->utc_tai_offset) 876e68462a0SVadim Fedorenko ptp_ocp_utc_distribute(bp, utc_offset); 877e68462a0SVadim Fedorenko } 878e68462a0SVadim Fedorenko 879773bda96SJonathan Lemon mod_timer(&bp->watchdog, jiffies + HZ); 880773bda96SJonathan Lemon } 881773bda96SJonathan Lemon 8821acffc6eSJonathan Lemon static void 8831acffc6eSJonathan Lemon ptp_ocp_estimate_pci_timing(struct ptp_ocp *bp) 8841acffc6eSJonathan Lemon { 8851acffc6eSJonathan Lemon ktime_t start, end; 8861acffc6eSJonathan Lemon ktime_t delay; 8871acffc6eSJonathan Lemon u32 ctrl; 8881acffc6eSJonathan Lemon 8891acffc6eSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 8901acffc6eSJonathan Lemon ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE; 8911acffc6eSJonathan Lemon 8921acffc6eSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 8931acffc6eSJonathan Lemon 8941acffc6eSJonathan Lemon start = ktime_get_ns(); 8951acffc6eSJonathan Lemon 8961acffc6eSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 8971acffc6eSJonathan Lemon 8981acffc6eSJonathan Lemon end = ktime_get_ns(); 8991acffc6eSJonathan Lemon 9001acffc6eSJonathan Lemon delay = end - start; 9011acffc6eSJonathan Lemon bp->ts_window_adjust = (delay >> 5) * 3; 9021acffc6eSJonathan Lemon } 9031acffc6eSJonathan Lemon 904a7e1abadSJonathan Lemon static int 905773bda96SJonathan Lemon ptp_ocp_init_clock(struct ptp_ocp *bp) 906a7e1abadSJonathan Lemon { 907a7e1abadSJonathan Lemon struct timespec64 ts; 908a7e1abadSJonathan Lemon bool sync; 909a7e1abadSJonathan Lemon u32 ctrl; 910a7e1abadSJonathan Lemon 9111acffc6eSJonathan Lemon ctrl = OCP_CTRL_ENABLE; 912a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 913a7e1abadSJonathan Lemon 914773bda96SJonathan Lemon /* NO DRIFT Correction */ 915773bda96SJonathan Lemon /* offset_p:i 1/8, offset_i: 1/16, drift_p: 0, drift_i: 0 */ 916773bda96SJonathan Lemon iowrite32(0x2000, &bp->reg->servo_offset_p); 917773bda96SJonathan Lemon iowrite32(0x1000, &bp->reg->servo_offset_i); 918773bda96SJonathan Lemon iowrite32(0, &bp->reg->servo_drift_p); 919773bda96SJonathan Lemon iowrite32(0, &bp->reg->servo_drift_i); 920773bda96SJonathan Lemon 921773bda96SJonathan Lemon /* latch servo values */ 922773bda96SJonathan Lemon ctrl |= OCP_CTRL_ADJUST_SERVO; 923773bda96SJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 924773bda96SJonathan Lemon 925a7e1abadSJonathan Lemon if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) { 926a7e1abadSJonathan Lemon dev_err(&bp->pdev->dev, "clock not enabled\n"); 927a7e1abadSJonathan Lemon return -ENODEV; 928a7e1abadSJonathan Lemon } 929a7e1abadSJonathan Lemon 9301acffc6eSJonathan Lemon ptp_ocp_estimate_pci_timing(bp); 9311acffc6eSJonathan Lemon 932a7e1abadSJonathan Lemon sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC; 933a7e1abadSJonathan Lemon if (!sync) { 934065efcc5SJonathan Lemon ktime_get_clocktai_ts64(&ts); 935a7e1abadSJonathan Lemon ptp_ocp_settime(&bp->ptp_info, &ts); 936a7e1abadSJonathan Lemon } 937a7e1abadSJonathan Lemon 938065efcc5SJonathan Lemon /* If there is a clock supervisor, then enable the watchdog */ 939065efcc5SJonathan Lemon if (bp->pps_to_clk) { 940773bda96SJonathan Lemon timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0); 941773bda96SJonathan Lemon mod_timer(&bp->watchdog, jiffies + HZ); 942065efcc5SJonathan Lemon } 943773bda96SJonathan Lemon 944a7e1abadSJonathan Lemon return 0; 945a7e1abadSJonathan Lemon } 946a7e1abadSJonathan Lemon 947a7e1abadSJonathan Lemon static void 948065efcc5SJonathan Lemon ptp_ocp_tod_init(struct ptp_ocp *bp) 949065efcc5SJonathan Lemon { 950065efcc5SJonathan Lemon u32 ctrl, reg; 951065efcc5SJonathan Lemon 952065efcc5SJonathan Lemon ctrl = ioread32(&bp->tod->ctrl); 953065efcc5SJonathan Lemon ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE; 954065efcc5SJonathan Lemon ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B); 955065efcc5SJonathan Lemon iowrite32(ctrl, &bp->tod->ctrl); 956065efcc5SJonathan Lemon 957065efcc5SJonathan Lemon reg = ioread32(&bp->tod->utc_status); 958065efcc5SJonathan Lemon if (reg & TOD_STATUS_UTC_VALID) 959065efcc5SJonathan Lemon ptp_ocp_utc_distribute(bp, reg & TOD_STATUS_UTC_MASK); 960065efcc5SJonathan Lemon } 961065efcc5SJonathan Lemon 9629f492c4cSVadim Fedorenko static const char * 9639f492c4cSVadim Fedorenko ptp_ocp_tod_proto_name(const int idx) 964a7e1abadSJonathan Lemon { 965a7e1abadSJonathan Lemon static const char * const proto_name[] = { 966a7e1abadSJonathan Lemon "NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none", 967a7e1abadSJonathan Lemon "UBX", "UBX_UTC", "UBX_LS", "UBX_none" 968a7e1abadSJonathan Lemon }; 9699f492c4cSVadim Fedorenko return proto_name[idx]; 9709f492c4cSVadim Fedorenko } 9719f492c4cSVadim Fedorenko 9729f492c4cSVadim Fedorenko static const char * 9739f492c4cSVadim Fedorenko ptp_ocp_tod_gnss_name(int idx) 9749f492c4cSVadim Fedorenko { 975a7e1abadSJonathan Lemon static const char * const gnss_name[] = { 976a7e1abadSJonathan Lemon "ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU", 9779f492c4cSVadim Fedorenko "Unknown" 978a7e1abadSJonathan Lemon }; 97972f00505SDan Carpenter if (idx >= ARRAY_SIZE(gnss_name)) 9809f492c4cSVadim Fedorenko idx = ARRAY_SIZE(gnss_name) - 1; 9819f492c4cSVadim Fedorenko return gnss_name[idx]; 982a7e1abadSJonathan Lemon } 983a7e1abadSJonathan Lemon 9840cfcdd1eSJonathan Lemon struct ptp_ocp_nvmem_match_info { 9850cfcdd1eSJonathan Lemon struct ptp_ocp *bp; 9860cfcdd1eSJonathan Lemon const void * const tag; 987773bda96SJonathan Lemon }; 988773bda96SJonathan Lemon 9890cfcdd1eSJonathan Lemon static int 9900cfcdd1eSJonathan Lemon ptp_ocp_nvmem_match(struct device *dev, const void *data) 9910cfcdd1eSJonathan Lemon { 9920cfcdd1eSJonathan Lemon const struct ptp_ocp_nvmem_match_info *info = data; 9930cfcdd1eSJonathan Lemon 9940cfcdd1eSJonathan Lemon dev = dev->parent; 9950cfcdd1eSJonathan Lemon if (!i2c_verify_client(dev) || info->tag != dev->platform_data) 9960cfcdd1eSJonathan Lemon return 0; 9970cfcdd1eSJonathan Lemon 9980cfcdd1eSJonathan Lemon while ((dev = dev->parent)) 9990cfcdd1eSJonathan Lemon if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME)) 10000cfcdd1eSJonathan Lemon return info->bp == dev_get_drvdata(dev); 1001773bda96SJonathan Lemon return 0; 1002773bda96SJonathan Lemon } 1003773bda96SJonathan Lemon 10040cfcdd1eSJonathan Lemon static inline struct nvmem_device * 10050cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_get(struct ptp_ocp *bp, const void * const tag) 1006773bda96SJonathan Lemon { 10070cfcdd1eSJonathan Lemon struct ptp_ocp_nvmem_match_info info = { .bp = bp, .tag = tag }; 10080cfcdd1eSJonathan Lemon 10090cfcdd1eSJonathan Lemon return nvmem_device_find(&info, ptp_ocp_nvmem_match); 10100cfcdd1eSJonathan Lemon } 10110cfcdd1eSJonathan Lemon 10120cfcdd1eSJonathan Lemon static inline void 10130cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_put(struct nvmem_device **nvmemp) 10140cfcdd1eSJonathan Lemon { 10150cfcdd1eSJonathan Lemon if (*nvmemp != NULL) { 10160cfcdd1eSJonathan Lemon nvmem_device_put(*nvmemp); 10170cfcdd1eSJonathan Lemon *nvmemp = NULL; 10180cfcdd1eSJonathan Lemon } 10190cfcdd1eSJonathan Lemon } 10200cfcdd1eSJonathan Lemon 10210cfcdd1eSJonathan Lemon static void 10220cfcdd1eSJonathan Lemon ptp_ocp_read_eeprom(struct ptp_ocp *bp) 10230cfcdd1eSJonathan Lemon { 10240cfcdd1eSJonathan Lemon const struct ptp_ocp_eeprom_map *map; 10250cfcdd1eSJonathan Lemon struct nvmem_device *nvmem; 10260cfcdd1eSJonathan Lemon const void *tag; 10270cfcdd1eSJonathan Lemon int ret; 1028773bda96SJonathan Lemon 10291447149dSJonathan Lemon if (!bp->i2c_ctrl) 10301447149dSJonathan Lemon return; 10311447149dSJonathan Lemon 10320cfcdd1eSJonathan Lemon tag = NULL; 10330cfcdd1eSJonathan Lemon nvmem = NULL; 1034773bda96SJonathan Lemon 10350cfcdd1eSJonathan Lemon for (map = bp->eeprom_map; map->len; map++) { 10360cfcdd1eSJonathan Lemon if (map->tag != tag) { 10370cfcdd1eSJonathan Lemon tag = map->tag; 10380cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_put(&nvmem); 10390cfcdd1eSJonathan Lemon } 10400cfcdd1eSJonathan Lemon if (!nvmem) { 10410cfcdd1eSJonathan Lemon nvmem = ptp_ocp_nvmem_device_get(bp, tag); 10420cfcdd1eSJonathan Lemon if (!nvmem) 1043773bda96SJonathan Lemon goto out; 1044773bda96SJonathan Lemon } 10450cfcdd1eSJonathan Lemon ret = nvmem_device_read(nvmem, map->off, map->len, 10460cfcdd1eSJonathan Lemon BP_MAP_ENTRY_ADDR(bp, map)); 10470cfcdd1eSJonathan Lemon if (ret != map->len) 10480cfcdd1eSJonathan Lemon goto read_fail; 1049773bda96SJonathan Lemon } 1050773bda96SJonathan Lemon 10510cfcdd1eSJonathan Lemon bp->has_eeprom_data = true; 1052773bda96SJonathan Lemon 1053773bda96SJonathan Lemon out: 10540cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_put(&nvmem); 10550cfcdd1eSJonathan Lemon return; 10560cfcdd1eSJonathan Lemon 10570cfcdd1eSJonathan Lemon read_fail: 10580cfcdd1eSJonathan Lemon dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", ret); 10590cfcdd1eSJonathan Lemon goto out; 10600cfcdd1eSJonathan Lemon } 10610cfcdd1eSJonathan Lemon 10620cfcdd1eSJonathan Lemon static int 10630cfcdd1eSJonathan Lemon ptp_ocp_firstchild(struct device *dev, void *data) 10640cfcdd1eSJonathan Lemon { 10650cfcdd1eSJonathan Lemon return 1; 1066773bda96SJonathan Lemon } 1067773bda96SJonathan Lemon 1068773bda96SJonathan Lemon static struct device * 1069773bda96SJonathan Lemon ptp_ocp_find_flash(struct ptp_ocp *bp) 1070773bda96SJonathan Lemon { 1071773bda96SJonathan Lemon struct device *dev, *last; 1072773bda96SJonathan Lemon 1073773bda96SJonathan Lemon last = NULL; 1074773bda96SJonathan Lemon dev = &bp->spi_flash->dev; 1075773bda96SJonathan Lemon 1076773bda96SJonathan Lemon while ((dev = device_find_child(dev, NULL, ptp_ocp_firstchild))) { 1077773bda96SJonathan Lemon if (!strcmp("mtd", dev_bus_name(dev))) 1078773bda96SJonathan Lemon break; 1079773bda96SJonathan Lemon put_device(last); 1080773bda96SJonathan Lemon last = dev; 1081773bda96SJonathan Lemon } 1082773bda96SJonathan Lemon put_device(last); 1083773bda96SJonathan Lemon 1084773bda96SJonathan Lemon return dev; 1085773bda96SJonathan Lemon } 1086773bda96SJonathan Lemon 1087773bda96SJonathan Lemon static int 1088773bda96SJonathan Lemon ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev, 1089773bda96SJonathan Lemon const struct firmware *fw) 1090773bda96SJonathan Lemon { 1091773bda96SJonathan Lemon struct mtd_info *mtd = dev_get_drvdata(dev); 1092773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 1093773bda96SJonathan Lemon size_t off, len, resid, wrote; 1094773bda96SJonathan Lemon struct erase_info erase; 1095773bda96SJonathan Lemon size_t base, blksz; 10967c807572SJonathan Lemon int err = 0; 1097773bda96SJonathan Lemon 1098773bda96SJonathan Lemon off = 0; 1099773bda96SJonathan Lemon base = bp->flash_start; 1100773bda96SJonathan Lemon blksz = 4096; 1101773bda96SJonathan Lemon resid = fw->size; 1102773bda96SJonathan Lemon 1103773bda96SJonathan Lemon while (resid) { 1104773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, "Flashing", 1105773bda96SJonathan Lemon NULL, off, fw->size); 1106773bda96SJonathan Lemon 1107773bda96SJonathan Lemon len = min_t(size_t, resid, blksz); 1108773bda96SJonathan Lemon erase.addr = base + off; 1109773bda96SJonathan Lemon erase.len = blksz; 1110773bda96SJonathan Lemon 1111773bda96SJonathan Lemon err = mtd_erase(mtd, &erase); 1112773bda96SJonathan Lemon if (err) 1113773bda96SJonathan Lemon goto out; 1114773bda96SJonathan Lemon 1115773bda96SJonathan Lemon err = mtd_write(mtd, base + off, len, &wrote, &fw->data[off]); 1116773bda96SJonathan Lemon if (err) 1117773bda96SJonathan Lemon goto out; 1118773bda96SJonathan Lemon 1119773bda96SJonathan Lemon off += blksz; 1120773bda96SJonathan Lemon resid -= len; 1121773bda96SJonathan Lemon } 1122773bda96SJonathan Lemon out: 1123773bda96SJonathan Lemon return err; 1124773bda96SJonathan Lemon } 1125773bda96SJonathan Lemon 1126773bda96SJonathan Lemon static int 1127773bda96SJonathan Lemon ptp_ocp_devlink_flash_update(struct devlink *devlink, 1128773bda96SJonathan Lemon struct devlink_flash_update_params *params, 1129773bda96SJonathan Lemon struct netlink_ext_ack *extack) 1130773bda96SJonathan Lemon { 1131773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 1132773bda96SJonathan Lemon struct device *dev; 1133773bda96SJonathan Lemon const char *msg; 1134773bda96SJonathan Lemon int err; 1135773bda96SJonathan Lemon 1136773bda96SJonathan Lemon dev = ptp_ocp_find_flash(bp); 1137773bda96SJonathan Lemon if (!dev) { 1138773bda96SJonathan Lemon dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n"); 1139773bda96SJonathan Lemon return -ENODEV; 1140773bda96SJonathan Lemon } 1141773bda96SJonathan Lemon 1142773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, "Preparing to flash", 1143773bda96SJonathan Lemon NULL, 0, 0); 1144773bda96SJonathan Lemon 1145773bda96SJonathan Lemon err = ptp_ocp_devlink_flash(devlink, dev, params->fw); 1146773bda96SJonathan Lemon 1147773bda96SJonathan Lemon msg = err ? "Flash error" : "Flash complete"; 1148773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0); 1149773bda96SJonathan Lemon 1150773bda96SJonathan Lemon put_device(dev); 1151773bda96SJonathan Lemon return err; 1152773bda96SJonathan Lemon } 1153773bda96SJonathan Lemon 1154773bda96SJonathan Lemon static int 1155773bda96SJonathan Lemon ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req, 1156773bda96SJonathan Lemon struct netlink_ext_ack *extack) 1157773bda96SJonathan Lemon { 1158773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 1159773bda96SJonathan Lemon char buf[32]; 1160773bda96SJonathan Lemon int err; 1161773bda96SJonathan Lemon 1162773bda96SJonathan Lemon err = devlink_info_driver_name_put(req, KBUILD_MODNAME); 1163773bda96SJonathan Lemon if (err) 1164773bda96SJonathan Lemon return err; 1165773bda96SJonathan Lemon 1166b0ca789aSJonathan Lemon if (bp->fw_version & 0xffff) { 1167b0ca789aSJonathan Lemon sprintf(buf, "%d", bp->fw_version); 1168b0ca789aSJonathan Lemon err = devlink_info_version_running_put(req, "fw", buf); 1169773bda96SJonathan Lemon } else { 1170b0ca789aSJonathan Lemon sprintf(buf, "%d", bp->fw_version >> 16); 1171b0ca789aSJonathan Lemon err = devlink_info_version_running_put(req, "loader", buf); 1172773bda96SJonathan Lemon } 1173773bda96SJonathan Lemon if (err) 1174773bda96SJonathan Lemon return err; 1175773bda96SJonathan Lemon 11760cfcdd1eSJonathan Lemon if (!bp->has_eeprom_data) { 11770cfcdd1eSJonathan Lemon ptp_ocp_read_eeprom(bp); 11780cfcdd1eSJonathan Lemon if (!bp->has_eeprom_data) 11790cfcdd1eSJonathan Lemon return 0; 11800cfcdd1eSJonathan Lemon } 1181773bda96SJonathan Lemon 1182773bda96SJonathan Lemon sprintf(buf, "%pM", bp->serial); 1183773bda96SJonathan Lemon err = devlink_info_serial_number_put(req, buf); 1184773bda96SJonathan Lemon if (err) 1185773bda96SJonathan Lemon return err; 11860cfcdd1eSJonathan Lemon 11870cfcdd1eSJonathan Lemon err = devlink_info_version_fixed_put(req, 11880cfcdd1eSJonathan Lemon DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, 11890cfcdd1eSJonathan Lemon bp->board_id); 11900cfcdd1eSJonathan Lemon if (err) 11910cfcdd1eSJonathan Lemon return err; 1192773bda96SJonathan Lemon 1193773bda96SJonathan Lemon return 0; 1194773bda96SJonathan Lemon } 1195773bda96SJonathan Lemon 1196773bda96SJonathan Lemon static const struct devlink_ops ptp_ocp_devlink_ops = { 1197773bda96SJonathan Lemon .flash_update = ptp_ocp_devlink_flash_update, 1198773bda96SJonathan Lemon .info_get = ptp_ocp_devlink_info_get, 1199773bda96SJonathan Lemon }; 1200773bda96SJonathan Lemon 1201773bda96SJonathan Lemon static void __iomem * 1202773bda96SJonathan Lemon __ptp_ocp_get_mem(struct ptp_ocp *bp, unsigned long start, int size) 1203773bda96SJonathan Lemon { 1204773bda96SJonathan Lemon struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp"); 1205773bda96SJonathan Lemon 1206773bda96SJonathan Lemon return devm_ioremap_resource(&bp->pdev->dev, &res); 1207773bda96SJonathan Lemon } 1208773bda96SJonathan Lemon 1209773bda96SJonathan Lemon static void __iomem * 1210773bda96SJonathan Lemon ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r) 1211773bda96SJonathan Lemon { 1212773bda96SJonathan Lemon unsigned long start; 1213773bda96SJonathan Lemon 1214773bda96SJonathan Lemon start = pci_resource_start(bp->pdev, 0) + r->offset; 1215773bda96SJonathan Lemon return __ptp_ocp_get_mem(bp, start, r->size); 1216773bda96SJonathan Lemon } 1217773bda96SJonathan Lemon 1218773bda96SJonathan Lemon static void 1219773bda96SJonathan Lemon ptp_ocp_set_irq_resource(struct resource *res, int irq) 1220773bda96SJonathan Lemon { 1221773bda96SJonathan Lemon struct resource r = DEFINE_RES_IRQ(irq); 1222773bda96SJonathan Lemon *res = r; 1223773bda96SJonathan Lemon } 1224773bda96SJonathan Lemon 1225773bda96SJonathan Lemon static void 1226773bda96SJonathan Lemon ptp_ocp_set_mem_resource(struct resource *res, unsigned long start, int size) 1227773bda96SJonathan Lemon { 1228773bda96SJonathan Lemon struct resource r = DEFINE_RES_MEM(start, size); 1229773bda96SJonathan Lemon *res = r; 1230773bda96SJonathan Lemon } 1231773bda96SJonathan Lemon 1232773bda96SJonathan Lemon static int 1233773bda96SJonathan Lemon ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r) 1234773bda96SJonathan Lemon { 1235773bda96SJonathan Lemon struct ptp_ocp_flash_info *info; 1236773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1237773bda96SJonathan Lemon struct platform_device *p; 1238773bda96SJonathan Lemon struct resource res[2]; 1239773bda96SJonathan Lemon unsigned long start; 1240773bda96SJonathan Lemon int id; 1241773bda96SJonathan Lemon 1242773bda96SJonathan Lemon start = pci_resource_start(pdev, 0) + r->offset; 1243773bda96SJonathan Lemon ptp_ocp_set_mem_resource(&res[0], start, r->size); 1244773bda96SJonathan Lemon ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1245773bda96SJonathan Lemon 1246773bda96SJonathan Lemon info = r->extra; 1247773bda96SJonathan Lemon id = pci_dev_id(pdev) << 1; 1248773bda96SJonathan Lemon id += info->pci_offset; 1249773bda96SJonathan Lemon 1250773bda96SJonathan Lemon p = platform_device_register_resndata(&pdev->dev, info->name, id, 1251773bda96SJonathan Lemon res, 2, info->data, 1252773bda96SJonathan Lemon info->data_size); 1253773bda96SJonathan Lemon if (IS_ERR(p)) 1254773bda96SJonathan Lemon return PTR_ERR(p); 1255773bda96SJonathan Lemon 1256773bda96SJonathan Lemon bp_assign_entry(bp, r, p); 1257773bda96SJonathan Lemon 1258773bda96SJonathan Lemon return 0; 1259773bda96SJonathan Lemon } 1260773bda96SJonathan Lemon 1261773bda96SJonathan Lemon static struct platform_device * 1262773bda96SJonathan Lemon ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id) 1263773bda96SJonathan Lemon { 12641618df6aSJonathan Lemon struct ptp_ocp_i2c_info *info; 1265773bda96SJonathan Lemon struct resource res[2]; 1266773bda96SJonathan Lemon unsigned long start; 1267773bda96SJonathan Lemon 12681618df6aSJonathan Lemon info = r->extra; 1269773bda96SJonathan Lemon start = pci_resource_start(pdev, 0) + r->offset; 1270773bda96SJonathan Lemon ptp_ocp_set_mem_resource(&res[0], start, r->size); 1271773bda96SJonathan Lemon ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1272773bda96SJonathan Lemon 12731618df6aSJonathan Lemon return platform_device_register_resndata(&pdev->dev, info->name, 12741618df6aSJonathan Lemon id, res, 2, 12751618df6aSJonathan Lemon info->data, info->data_size); 1276773bda96SJonathan Lemon } 1277773bda96SJonathan Lemon 1278773bda96SJonathan Lemon static int 1279773bda96SJonathan Lemon ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r) 1280773bda96SJonathan Lemon { 1281773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 12821618df6aSJonathan Lemon struct ptp_ocp_i2c_info *info; 1283773bda96SJonathan Lemon struct platform_device *p; 1284773bda96SJonathan Lemon struct clk_hw *clk; 1285773bda96SJonathan Lemon char buf[32]; 1286773bda96SJonathan Lemon int id; 1287773bda96SJonathan Lemon 12881618df6aSJonathan Lemon info = r->extra; 1289773bda96SJonathan Lemon id = pci_dev_id(bp->pdev); 1290773bda96SJonathan Lemon 1291773bda96SJonathan Lemon sprintf(buf, "AXI.%d", id); 12921618df6aSJonathan Lemon clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0, 12931618df6aSJonathan Lemon info->fixed_rate); 1294773bda96SJonathan Lemon if (IS_ERR(clk)) 1295773bda96SJonathan Lemon return PTR_ERR(clk); 1296773bda96SJonathan Lemon bp->i2c_clk = clk; 1297773bda96SJonathan Lemon 12981618df6aSJonathan Lemon sprintf(buf, "%s.%d", info->name, id); 1299773bda96SJonathan Lemon devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf); 1300773bda96SJonathan Lemon p = ptp_ocp_i2c_bus(bp->pdev, r, id); 1301773bda96SJonathan Lemon if (IS_ERR(p)) 1302773bda96SJonathan Lemon return PTR_ERR(p); 1303773bda96SJonathan Lemon 1304773bda96SJonathan Lemon bp_assign_entry(bp, r, p); 1305773bda96SJonathan Lemon 1306773bda96SJonathan Lemon return 0; 1307773bda96SJonathan Lemon } 1308773bda96SJonathan Lemon 1309773bda96SJonathan Lemon static irqreturn_t 1310773bda96SJonathan Lemon ptp_ocp_ts_irq(int irq, void *priv) 1311773bda96SJonathan Lemon { 1312773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1313773bda96SJonathan Lemon struct ts_reg __iomem *reg = ext->mem; 1314773bda96SJonathan Lemon struct ptp_clock_event ev; 1315773bda96SJonathan Lemon u32 sec, nsec; 1316773bda96SJonathan Lemon 1317a62a56d0SJonathan Lemon if (ext == ext->bp->pps) { 1318a62a56d0SJonathan Lemon if (ext->bp->pps_req_map & OCP_REQ_PPS) { 1319a62a56d0SJonathan Lemon ev.type = PTP_CLOCK_PPS; 1320a62a56d0SJonathan Lemon ptp_clock_event(ext->bp->ptp, &ev); 1321a62a56d0SJonathan Lemon } 1322a62a56d0SJonathan Lemon 1323a62a56d0SJonathan Lemon if ((ext->bp->pps_req_map & ~OCP_REQ_PPS) == 0) 1324a62a56d0SJonathan Lemon goto out; 1325a62a56d0SJonathan Lemon } 1326a62a56d0SJonathan Lemon 1327773bda96SJonathan Lemon /* XXX should fix API - this converts s/ns -> ts -> s/ns */ 1328773bda96SJonathan Lemon sec = ioread32(®->time_sec); 1329773bda96SJonathan Lemon nsec = ioread32(®->time_ns); 1330773bda96SJonathan Lemon 1331773bda96SJonathan Lemon ev.type = PTP_CLOCK_EXTTS; 1332773bda96SJonathan Lemon ev.index = ext->info->index; 13331acffc6eSJonathan Lemon ev.timestamp = sec * NSEC_PER_SEC + nsec; 1334773bda96SJonathan Lemon 1335773bda96SJonathan Lemon ptp_clock_event(ext->bp->ptp, &ev); 1336773bda96SJonathan Lemon 1337a62a56d0SJonathan Lemon out: 1338773bda96SJonathan Lemon iowrite32(1, ®->intr); /* write 1 to ack */ 1339773bda96SJonathan Lemon 1340773bda96SJonathan Lemon return IRQ_HANDLED; 1341773bda96SJonathan Lemon } 1342773bda96SJonathan Lemon 1343773bda96SJonathan Lemon static int 1344a62a56d0SJonathan Lemon ptp_ocp_ts_enable(void *priv, u32 req, bool enable) 1345773bda96SJonathan Lemon { 1346773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1347773bda96SJonathan Lemon struct ts_reg __iomem *reg = ext->mem; 1348a62a56d0SJonathan Lemon struct ptp_ocp *bp = ext->bp; 1349a62a56d0SJonathan Lemon 1350a62a56d0SJonathan Lemon if (ext == bp->pps) { 1351a62a56d0SJonathan Lemon u32 old_map = bp->pps_req_map; 1352a62a56d0SJonathan Lemon 1353a62a56d0SJonathan Lemon if (enable) 1354a62a56d0SJonathan Lemon bp->pps_req_map |= req; 1355a62a56d0SJonathan Lemon else 1356a62a56d0SJonathan Lemon bp->pps_req_map &= ~req; 1357a62a56d0SJonathan Lemon 1358a62a56d0SJonathan Lemon /* if no state change, just return */ 1359a62a56d0SJonathan Lemon if ((!!old_map ^ !!bp->pps_req_map) == 0) 1360a62a56d0SJonathan Lemon return 0; 1361a62a56d0SJonathan Lemon } 1362773bda96SJonathan Lemon 1363773bda96SJonathan Lemon if (enable) { 1364773bda96SJonathan Lemon iowrite32(1, ®->enable); 1365773bda96SJonathan Lemon iowrite32(1, ®->intr_mask); 1366773bda96SJonathan Lemon iowrite32(1, ®->intr); 1367773bda96SJonathan Lemon } else { 1368773bda96SJonathan Lemon iowrite32(0, ®->intr_mask); 1369773bda96SJonathan Lemon iowrite32(0, ®->enable); 1370773bda96SJonathan Lemon } 1371773bda96SJonathan Lemon 1372773bda96SJonathan Lemon return 0; 1373773bda96SJonathan Lemon } 1374773bda96SJonathan Lemon 1375773bda96SJonathan Lemon static void 1376773bda96SJonathan Lemon ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext) 1377773bda96SJonathan Lemon { 1378a62a56d0SJonathan Lemon ext->info->enable(ext, ~0, false); 1379773bda96SJonathan Lemon pci_free_irq(ext->bp->pdev, ext->irq_vec, ext); 1380773bda96SJonathan Lemon kfree(ext); 1381773bda96SJonathan Lemon } 1382773bda96SJonathan Lemon 1383773bda96SJonathan Lemon static int 1384773bda96SJonathan Lemon ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r) 1385773bda96SJonathan Lemon { 1386773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1387773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext; 1388773bda96SJonathan Lemon int err; 1389773bda96SJonathan Lemon 1390773bda96SJonathan Lemon ext = kzalloc(sizeof(*ext), GFP_KERNEL); 1391773bda96SJonathan Lemon if (!ext) 1392773bda96SJonathan Lemon return -ENOMEM; 1393773bda96SJonathan Lemon 1394773bda96SJonathan Lemon ext->mem = ptp_ocp_get_mem(bp, r); 1395c7521d3aSDan Carpenter if (IS_ERR(ext->mem)) { 1396c7521d3aSDan Carpenter err = PTR_ERR(ext->mem); 1397773bda96SJonathan Lemon goto out; 1398c7521d3aSDan Carpenter } 1399773bda96SJonathan Lemon 1400773bda96SJonathan Lemon ext->bp = bp; 1401773bda96SJonathan Lemon ext->info = r->extra; 1402773bda96SJonathan Lemon ext->irq_vec = r->irq_vec; 1403773bda96SJonathan Lemon 1404773bda96SJonathan Lemon err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL, 140556ec4403SJonathan Lemon ext, "ocp%d.%s", bp->id, r->name); 1406773bda96SJonathan Lemon if (err) { 1407773bda96SJonathan Lemon dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec); 1408773bda96SJonathan Lemon goto out; 1409773bda96SJonathan Lemon } 1410773bda96SJonathan Lemon 1411773bda96SJonathan Lemon bp_assign_entry(bp, r, ext); 1412773bda96SJonathan Lemon 1413773bda96SJonathan Lemon return 0; 1414773bda96SJonathan Lemon 1415773bda96SJonathan Lemon out: 1416773bda96SJonathan Lemon kfree(ext); 1417773bda96SJonathan Lemon return err; 1418773bda96SJonathan Lemon } 1419773bda96SJonathan Lemon 1420773bda96SJonathan Lemon static int 1421773bda96SJonathan Lemon ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r) 1422773bda96SJonathan Lemon { 1423773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1424773bda96SJonathan Lemon struct uart_8250_port uart; 1425773bda96SJonathan Lemon 1426773bda96SJonathan Lemon /* Setting UPF_IOREMAP and leaving port.membase unspecified lets 1427773bda96SJonathan Lemon * the serial port device claim and release the pci resource. 1428773bda96SJonathan Lemon */ 1429773bda96SJonathan Lemon memset(&uart, 0, sizeof(uart)); 1430773bda96SJonathan Lemon uart.port.dev = &pdev->dev; 1431773bda96SJonathan Lemon uart.port.iotype = UPIO_MEM; 1432773bda96SJonathan Lemon uart.port.regshift = 2; 1433773bda96SJonathan Lemon uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset; 1434773bda96SJonathan Lemon uart.port.irq = pci_irq_vector(pdev, r->irq_vec); 1435773bda96SJonathan Lemon uart.port.uartclk = 50000000; 1436c17c4059SJonathan Lemon uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP | UPF_NO_THRE_TEST; 1437773bda96SJonathan Lemon uart.port.type = PORT_16550A; 1438773bda96SJonathan Lemon 1439773bda96SJonathan Lemon return serial8250_register_8250_port(&uart); 1440773bda96SJonathan Lemon } 1441773bda96SJonathan Lemon 1442773bda96SJonathan Lemon static int 1443773bda96SJonathan Lemon ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r) 1444773bda96SJonathan Lemon { 1445773bda96SJonathan Lemon int port; 1446773bda96SJonathan Lemon 1447773bda96SJonathan Lemon port = ptp_ocp_serial_line(bp, r); 1448773bda96SJonathan Lemon if (port < 0) 1449773bda96SJonathan Lemon return port; 1450773bda96SJonathan Lemon 1451773bda96SJonathan Lemon bp_assign_entry(bp, r, port); 1452773bda96SJonathan Lemon 1453773bda96SJonathan Lemon return 0; 1454773bda96SJonathan Lemon } 1455773bda96SJonathan Lemon 1456773bda96SJonathan Lemon static int 1457773bda96SJonathan Lemon ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r) 1458773bda96SJonathan Lemon { 1459773bda96SJonathan Lemon void __iomem *mem; 1460773bda96SJonathan Lemon 1461773bda96SJonathan Lemon mem = ptp_ocp_get_mem(bp, r); 1462c7521d3aSDan Carpenter if (IS_ERR(mem)) 1463c7521d3aSDan Carpenter return PTR_ERR(mem); 1464773bda96SJonathan Lemon 1465773bda96SJonathan Lemon bp_assign_entry(bp, r, mem); 1466773bda96SJonathan Lemon 1467773bda96SJonathan Lemon return 0; 1468773bda96SJonathan Lemon } 1469773bda96SJonathan Lemon 1470e3516bb4SJonathan Lemon static void 1471e3516bb4SJonathan Lemon ptp_ocp_nmea_out_init(struct ptp_ocp *bp) 1472e3516bb4SJonathan Lemon { 1473e3516bb4SJonathan Lemon if (!bp->nmea_out) 1474e3516bb4SJonathan Lemon return; 1475e3516bb4SJonathan Lemon 1476e3516bb4SJonathan Lemon iowrite32(0, &bp->nmea_out->ctrl); /* disable */ 1477e3516bb4SJonathan Lemon iowrite32(7, &bp->nmea_out->uart_baud); /* 115200 */ 1478e3516bb4SJonathan Lemon iowrite32(1, &bp->nmea_out->ctrl); /* enable */ 1479e3516bb4SJonathan Lemon } 1480e3516bb4SJonathan Lemon 1481a509a7c6SJonathan Lemon static void 1482a509a7c6SJonathan Lemon ptp_ocp_sma_init(struct ptp_ocp *bp) 1483a509a7c6SJonathan Lemon { 1484a509a7c6SJonathan Lemon u32 reg; 1485a509a7c6SJonathan Lemon int i; 1486a509a7c6SJonathan Lemon 1487a509a7c6SJonathan Lemon /* defaults */ 1488a509a7c6SJonathan Lemon bp->sma[0].mode = SMA_MODE_IN; 1489a509a7c6SJonathan Lemon bp->sma[1].mode = SMA_MODE_IN; 1490a509a7c6SJonathan Lemon bp->sma[2].mode = SMA_MODE_OUT; 1491a509a7c6SJonathan Lemon bp->sma[3].mode = SMA_MODE_OUT; 1492a509a7c6SJonathan Lemon 1493a509a7c6SJonathan Lemon /* If no SMA1 map, the pin functions and directions are fixed. */ 1494a509a7c6SJonathan Lemon if (!bp->sma_map1) { 1495a509a7c6SJonathan Lemon for (i = 0; i < 4; i++) { 1496a509a7c6SJonathan Lemon bp->sma[i].fixed_fcn = true; 1497a509a7c6SJonathan Lemon bp->sma[i].fixed_dir = true; 1498a509a7c6SJonathan Lemon } 1499a509a7c6SJonathan Lemon return; 1500a509a7c6SJonathan Lemon } 1501a509a7c6SJonathan Lemon 1502a509a7c6SJonathan Lemon /* If SMA2 GPIO output map is all 1, it is not present. 1503a509a7c6SJonathan Lemon * This indicates the firmware has fixed direction SMA pins. 1504a509a7c6SJonathan Lemon */ 1505a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map2->gpio2); 1506a509a7c6SJonathan Lemon if (reg == 0xffffffff) { 1507a509a7c6SJonathan Lemon for (i = 0; i < 4; i++) 1508a509a7c6SJonathan Lemon bp->sma[i].fixed_dir = true; 1509a509a7c6SJonathan Lemon } else { 1510a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map1->gpio1); 1511a509a7c6SJonathan Lemon bp->sma[0].mode = reg & BIT(15) ? SMA_MODE_IN : SMA_MODE_OUT; 1512a509a7c6SJonathan Lemon bp->sma[1].mode = reg & BIT(31) ? SMA_MODE_IN : SMA_MODE_OUT; 1513a509a7c6SJonathan Lemon 1514a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map1->gpio2); 1515a509a7c6SJonathan Lemon bp->sma[2].mode = reg & BIT(15) ? SMA_MODE_OUT : SMA_MODE_IN; 1516a509a7c6SJonathan Lemon bp->sma[3].mode = reg & BIT(31) ? SMA_MODE_OUT : SMA_MODE_IN; 1517a509a7c6SJonathan Lemon } 1518a509a7c6SJonathan Lemon } 1519a509a7c6SJonathan Lemon 1520773bda96SJonathan Lemon /* FB specific board initializers; last "resource" registered. */ 1521773bda96SJonathan Lemon static int 1522773bda96SJonathan Lemon ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r) 1523773bda96SJonathan Lemon { 1524773bda96SJonathan Lemon bp->flash_start = 1024 * 4096; 15250cfcdd1eSJonathan Lemon bp->eeprom_map = fb_eeprom_map; 1526b0ca789aSJonathan Lemon bp->fw_version = ioread32(&bp->image->version); 1527773bda96SJonathan Lemon 1528065efcc5SJonathan Lemon ptp_ocp_tod_init(bp); 1529e3516bb4SJonathan Lemon ptp_ocp_nmea_out_init(bp); 1530a509a7c6SJonathan Lemon ptp_ocp_sma_init(bp); 1531065efcc5SJonathan Lemon 1532773bda96SJonathan Lemon return ptp_ocp_init_clock(bp); 1533773bda96SJonathan Lemon } 1534773bda96SJonathan Lemon 153556ec4403SJonathan Lemon static bool 153656ec4403SJonathan Lemon ptp_ocp_allow_irq(struct ptp_ocp *bp, struct ocp_resource *r) 153756ec4403SJonathan Lemon { 153856ec4403SJonathan Lemon bool allow = !r->irq_vec || r->irq_vec < bp->n_irqs; 153956ec4403SJonathan Lemon 154056ec4403SJonathan Lemon if (!allow) 154156ec4403SJonathan Lemon dev_err(&bp->pdev->dev, "irq %d out of range, skipping %s\n", 154256ec4403SJonathan Lemon r->irq_vec, r->name); 154356ec4403SJonathan Lemon return allow; 154456ec4403SJonathan Lemon } 154556ec4403SJonathan Lemon 1546773bda96SJonathan Lemon static int 1547773bda96SJonathan Lemon ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data) 1548773bda96SJonathan Lemon { 1549773bda96SJonathan Lemon struct ocp_resource *r, *table; 1550773bda96SJonathan Lemon int err = 0; 1551773bda96SJonathan Lemon 1552773bda96SJonathan Lemon table = (struct ocp_resource *)driver_data; 1553773bda96SJonathan Lemon for (r = table; r->setup; r++) { 155456ec4403SJonathan Lemon if (!ptp_ocp_allow_irq(bp, r)) 155556ec4403SJonathan Lemon continue; 1556773bda96SJonathan Lemon err = r->setup(bp, r); 1557bceff290SJonathan Lemon if (err) { 1558bceff290SJonathan Lemon dev_err(&bp->pdev->dev, 1559bceff290SJonathan Lemon "Could not register %s: err %d\n", 1560bceff290SJonathan Lemon r->name, err); 1561773bda96SJonathan Lemon break; 1562773bda96SJonathan Lemon } 1563bceff290SJonathan Lemon } 1564773bda96SJonathan Lemon return err; 1565773bda96SJonathan Lemon } 1566773bda96SJonathan Lemon 15676baf2925SJonathan Lemon static void 15686baf2925SJonathan Lemon ptp_ocp_enable_fpga(u32 __iomem *reg, u32 bit, bool enable) 15696baf2925SJonathan Lemon { 15706baf2925SJonathan Lemon u32 ctrl; 15716baf2925SJonathan Lemon bool on; 15726baf2925SJonathan Lemon 15736baf2925SJonathan Lemon ctrl = ioread32(reg); 15746baf2925SJonathan Lemon on = ctrl & bit; 15756baf2925SJonathan Lemon if (on ^ enable) { 15766baf2925SJonathan Lemon ctrl &= ~bit; 15776baf2925SJonathan Lemon ctrl |= enable ? bit : 0; 15786baf2925SJonathan Lemon iowrite32(ctrl, reg); 15796baf2925SJonathan Lemon } 15806baf2925SJonathan Lemon } 15816baf2925SJonathan Lemon 15826baf2925SJonathan Lemon static void 15836baf2925SJonathan Lemon ptp_ocp_irig_out(struct ptp_ocp *bp, bool enable) 15846baf2925SJonathan Lemon { 15856baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->irig_out->ctrl, 15866baf2925SJonathan Lemon IRIG_M_CTRL_ENABLE, enable); 15876baf2925SJonathan Lemon } 15886baf2925SJonathan Lemon 15896baf2925SJonathan Lemon static void 15906baf2925SJonathan Lemon ptp_ocp_irig_in(struct ptp_ocp *bp, bool enable) 15916baf2925SJonathan Lemon { 15926baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->irig_in->ctrl, 15936baf2925SJonathan Lemon IRIG_S_CTRL_ENABLE, enable); 15946baf2925SJonathan Lemon } 15956baf2925SJonathan Lemon 15966baf2925SJonathan Lemon static void 15976baf2925SJonathan Lemon ptp_ocp_dcf_out(struct ptp_ocp *bp, bool enable) 15986baf2925SJonathan Lemon { 15996baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->dcf_out->ctrl, 16006baf2925SJonathan Lemon DCF_M_CTRL_ENABLE, enable); 16016baf2925SJonathan Lemon } 16026baf2925SJonathan Lemon 16036baf2925SJonathan Lemon static void 16046baf2925SJonathan Lemon ptp_ocp_dcf_in(struct ptp_ocp *bp, bool enable) 16056baf2925SJonathan Lemon { 16066baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->dcf_in->ctrl, 16076baf2925SJonathan Lemon DCF_S_CTRL_ENABLE, enable); 16086baf2925SJonathan Lemon } 16096baf2925SJonathan Lemon 16106baf2925SJonathan Lemon static void 16116baf2925SJonathan Lemon __handle_signal_outputs(struct ptp_ocp *bp, u32 val) 16126baf2925SJonathan Lemon { 16136baf2925SJonathan Lemon ptp_ocp_irig_out(bp, val & 0x00100010); 16146baf2925SJonathan Lemon ptp_ocp_dcf_out(bp, val & 0x00200020); 16156baf2925SJonathan Lemon } 16166baf2925SJonathan Lemon 16176baf2925SJonathan Lemon static void 16186baf2925SJonathan Lemon __handle_signal_inputs(struct ptp_ocp *bp, u32 val) 16196baf2925SJonathan Lemon { 16206baf2925SJonathan Lemon ptp_ocp_irig_in(bp, val & 0x00100010); 16216baf2925SJonathan Lemon ptp_ocp_dcf_in(bp, val & 0x00200020); 16226baf2925SJonathan Lemon } 16236baf2925SJonathan Lemon 1624e1daf0ecSJonathan Lemon /* 1625e1daf0ecSJonathan Lemon * ANT0 == gps (in) 1626e1daf0ecSJonathan Lemon * ANT1 == sma1 (in) 1627e1daf0ecSJonathan Lemon * ANT2 == sma2 (in) 1628e1daf0ecSJonathan Lemon * ANT3 == sma3 (out) 1629e1daf0ecSJonathan Lemon * ANT4 == sma4 (out) 1630e1daf0ecSJonathan Lemon */ 1631e1daf0ecSJonathan Lemon 1632e1daf0ecSJonathan Lemon static ssize_t 1633b2c4f0acSJonathan Lemon ptp_ocp_show_output(u32 val, char *buf, int def_val) 1634e1daf0ecSJonathan Lemon { 1635e1daf0ecSJonathan Lemon const char *name; 1636e1daf0ecSJonathan Lemon ssize_t count; 1637e1daf0ecSJonathan Lemon 1638e1daf0ecSJonathan Lemon count = sysfs_emit(buf, "OUT: "); 1639e1daf0ecSJonathan Lemon name = ptp_ocp_select_name_from_val(ptp_ocp_sma_out, val); 1640e1daf0ecSJonathan Lemon if (!name) 1641b2c4f0acSJonathan Lemon name = ptp_ocp_select_name_from_val(ptp_ocp_sma_out, def_val); 1642e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s\n", name); 1643e1daf0ecSJonathan Lemon return count; 1644e1daf0ecSJonathan Lemon } 1645e1daf0ecSJonathan Lemon 1646e1daf0ecSJonathan Lemon static ssize_t 1647b2c4f0acSJonathan Lemon ptp_ocp_show_inputs(u32 val, char *buf, int def_val) 1648e1daf0ecSJonathan Lemon { 1649e1daf0ecSJonathan Lemon const char *name; 1650e1daf0ecSJonathan Lemon ssize_t count; 1651e1daf0ecSJonathan Lemon int i; 1652e1daf0ecSJonathan Lemon 1653e1daf0ecSJonathan Lemon count = sysfs_emit(buf, "IN: "); 1654e1daf0ecSJonathan Lemon for (i = 0; i < ARRAY_SIZE(ptp_ocp_sma_in); i++) { 1655e1daf0ecSJonathan Lemon if (val & ptp_ocp_sma_in[i].value) { 1656e1daf0ecSJonathan Lemon name = ptp_ocp_sma_in[i].name; 1657e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", name); 1658e1daf0ecSJonathan Lemon } 1659e1daf0ecSJonathan Lemon } 1660b2c4f0acSJonathan Lemon if (!val && def_val >= 0) { 1661b2c4f0acSJonathan Lemon name = ptp_ocp_select_name_from_val(ptp_ocp_sma_in, def_val); 1662b2c4f0acSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", name); 1663b2c4f0acSJonathan Lemon } 1664e1daf0ecSJonathan Lemon if (count) 1665e1daf0ecSJonathan Lemon count--; 1666e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "\n"); 1667e1daf0ecSJonathan Lemon return count; 1668e1daf0ecSJonathan Lemon } 1669e1daf0ecSJonathan Lemon 1670e1daf0ecSJonathan Lemon static int 1671e1daf0ecSJonathan Lemon sma_parse_inputs(const char *buf, enum ptp_ocp_sma_mode *mode) 1672e1daf0ecSJonathan Lemon { 1673e1daf0ecSJonathan Lemon struct ocp_selector *tbl[] = { ptp_ocp_sma_in, ptp_ocp_sma_out }; 1674e1daf0ecSJonathan Lemon int idx, count, dir; 1675e1daf0ecSJonathan Lemon char **argv; 1676e1daf0ecSJonathan Lemon int ret; 1677e1daf0ecSJonathan Lemon 1678e1daf0ecSJonathan Lemon argv = argv_split(GFP_KERNEL, buf, &count); 1679e1daf0ecSJonathan Lemon if (!argv) 1680e1daf0ecSJonathan Lemon return -ENOMEM; 1681e1daf0ecSJonathan Lemon 1682e1daf0ecSJonathan Lemon ret = -EINVAL; 1683e1daf0ecSJonathan Lemon if (!count) 1684e1daf0ecSJonathan Lemon goto out; 1685e1daf0ecSJonathan Lemon 1686e1daf0ecSJonathan Lemon idx = 0; 1687e1daf0ecSJonathan Lemon dir = *mode == SMA_MODE_IN ? 0 : 1; 1688a509a7c6SJonathan Lemon if (!strcasecmp("IN:", argv[0])) { 1689e1daf0ecSJonathan Lemon dir = 0; 1690e1daf0ecSJonathan Lemon idx++; 1691e1daf0ecSJonathan Lemon } 1692e1daf0ecSJonathan Lemon if (!strcasecmp("OUT:", argv[0])) { 1693e1daf0ecSJonathan Lemon dir = 1; 1694e1daf0ecSJonathan Lemon idx++; 1695e1daf0ecSJonathan Lemon } 1696e1daf0ecSJonathan Lemon *mode = dir == 0 ? SMA_MODE_IN : SMA_MODE_OUT; 1697e1daf0ecSJonathan Lemon 1698e1daf0ecSJonathan Lemon ret = 0; 1699e1daf0ecSJonathan Lemon for (; idx < count; idx++) 1700e1daf0ecSJonathan Lemon ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]); 1701e1daf0ecSJonathan Lemon if (ret < 0) 1702e1daf0ecSJonathan Lemon ret = -EINVAL; 1703e1daf0ecSJonathan Lemon 1704e1daf0ecSJonathan Lemon out: 1705e1daf0ecSJonathan Lemon argv_free(argv); 1706e1daf0ecSJonathan Lemon return ret; 1707e1daf0ecSJonathan Lemon } 1708e1daf0ecSJonathan Lemon 1709a509a7c6SJonathan Lemon static u32 1710a509a7c6SJonathan Lemon ptp_ocp_sma_get(struct ptp_ocp *bp, int sma_nr, enum ptp_ocp_sma_mode mode) 1711e1daf0ecSJonathan Lemon { 1712a509a7c6SJonathan Lemon u32 __iomem *gpio; 1713a509a7c6SJonathan Lemon u32 shift; 1714a509a7c6SJonathan Lemon 1715a509a7c6SJonathan Lemon if (bp->sma[sma_nr - 1].fixed_fcn) 1716a509a7c6SJonathan Lemon return (sma_nr - 1) & 1; 1717a509a7c6SJonathan Lemon 1718a509a7c6SJonathan Lemon if (mode == SMA_MODE_IN) 1719a509a7c6SJonathan Lemon gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1; 1720a509a7c6SJonathan Lemon else 1721a509a7c6SJonathan Lemon gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2; 1722a509a7c6SJonathan Lemon shift = sma_nr & 1 ? 0 : 16; 1723a509a7c6SJonathan Lemon 1724a509a7c6SJonathan Lemon return (ioread32(gpio) >> shift) & 0xffff; 1725a509a7c6SJonathan Lemon } 1726a509a7c6SJonathan Lemon 1727a509a7c6SJonathan Lemon static ssize_t 1728a509a7c6SJonathan Lemon ptp_ocp_sma_show(struct ptp_ocp *bp, int sma_nr, char *buf, 1729b2c4f0acSJonathan Lemon int default_in_val, int default_out_val) 1730a509a7c6SJonathan Lemon { 1731a509a7c6SJonathan Lemon struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1]; 1732a509a7c6SJonathan Lemon u32 val; 1733a509a7c6SJonathan Lemon 1734a509a7c6SJonathan Lemon val = ptp_ocp_sma_get(bp, sma_nr, sma->mode) & SMA_SELECT_MASK; 1735e1daf0ecSJonathan Lemon 1736b2c4f0acSJonathan Lemon if (sma->mode == SMA_MODE_IN) { 1737b2c4f0acSJonathan Lemon if (sma->disabled) 1738b2c4f0acSJonathan Lemon val = SMA_DISABLE; 1739b2c4f0acSJonathan Lemon return ptp_ocp_show_inputs(val, buf, default_in_val); 1740b2c4f0acSJonathan Lemon } 1741e1daf0ecSJonathan Lemon 1742b2c4f0acSJonathan Lemon return ptp_ocp_show_output(val, buf, default_out_val); 1743e1daf0ecSJonathan Lemon } 1744e1daf0ecSJonathan Lemon 1745e1daf0ecSJonathan Lemon static ssize_t 1746e1daf0ecSJonathan Lemon sma1_show(struct device *dev, struct device_attribute *attr, char *buf) 1747e1daf0ecSJonathan Lemon { 1748e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1749e1daf0ecSJonathan Lemon 1750a509a7c6SJonathan Lemon return ptp_ocp_sma_show(bp, 1, buf, 0, 1); 1751e1daf0ecSJonathan Lemon } 1752e1daf0ecSJonathan Lemon 1753e1daf0ecSJonathan Lemon static ssize_t 1754e1daf0ecSJonathan Lemon sma2_show(struct device *dev, struct device_attribute *attr, char *buf) 1755e1daf0ecSJonathan Lemon { 1756e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1757e1daf0ecSJonathan Lemon 1758a509a7c6SJonathan Lemon return ptp_ocp_sma_show(bp, 2, buf, -1, 1); 1759e1daf0ecSJonathan Lemon } 1760e1daf0ecSJonathan Lemon 1761e1daf0ecSJonathan Lemon static ssize_t 1762e1daf0ecSJonathan Lemon sma3_show(struct device *dev, struct device_attribute *attr, char *buf) 1763e1daf0ecSJonathan Lemon { 1764e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1765e1daf0ecSJonathan Lemon 1766a509a7c6SJonathan Lemon return ptp_ocp_sma_show(bp, 3, buf, -1, 0); 1767e1daf0ecSJonathan Lemon } 1768e1daf0ecSJonathan Lemon 1769e1daf0ecSJonathan Lemon static ssize_t 1770e1daf0ecSJonathan Lemon sma4_show(struct device *dev, struct device_attribute *attr, char *buf) 1771e1daf0ecSJonathan Lemon { 1772e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1773e1daf0ecSJonathan Lemon 1774a509a7c6SJonathan Lemon return ptp_ocp_sma_show(bp, 4, buf, -1, 1); 1775e1daf0ecSJonathan Lemon } 1776e1daf0ecSJonathan Lemon 1777e1daf0ecSJonathan Lemon static void 1778a509a7c6SJonathan Lemon ptp_ocp_sma_store_output(struct ptp_ocp *bp, int sma_nr, u32 val) 1779e1daf0ecSJonathan Lemon { 1780a509a7c6SJonathan Lemon u32 reg, mask, shift; 1781e1daf0ecSJonathan Lemon unsigned long flags; 1782a509a7c6SJonathan Lemon u32 __iomem *gpio; 1783a509a7c6SJonathan Lemon 1784a509a7c6SJonathan Lemon gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2; 1785a509a7c6SJonathan Lemon shift = sma_nr & 1 ? 0 : 16; 1786e1daf0ecSJonathan Lemon 1787e1daf0ecSJonathan Lemon mask = 0xffff << (16 - shift); 1788e1daf0ecSJonathan Lemon 1789e1daf0ecSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1790e1daf0ecSJonathan Lemon 1791a509a7c6SJonathan Lemon reg = ioread32(gpio); 1792a509a7c6SJonathan Lemon reg = (reg & mask) | (val << shift); 17936baf2925SJonathan Lemon 1794a509a7c6SJonathan Lemon __handle_signal_outputs(bp, reg); 17956baf2925SJonathan Lemon 1796a509a7c6SJonathan Lemon iowrite32(reg, gpio); 1797e1daf0ecSJonathan Lemon 1798e1daf0ecSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1799e1daf0ecSJonathan Lemon } 1800e1daf0ecSJonathan Lemon 1801e1daf0ecSJonathan Lemon static void 1802a509a7c6SJonathan Lemon ptp_ocp_sma_store_inputs(struct ptp_ocp *bp, int sma_nr, u32 val) 1803e1daf0ecSJonathan Lemon { 1804a509a7c6SJonathan Lemon u32 reg, mask, shift; 1805e1daf0ecSJonathan Lemon unsigned long flags; 1806a509a7c6SJonathan Lemon u32 __iomem *gpio; 1807a509a7c6SJonathan Lemon 1808a509a7c6SJonathan Lemon gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1; 1809a509a7c6SJonathan Lemon shift = sma_nr & 1 ? 0 : 16; 1810e1daf0ecSJonathan Lemon 1811e1daf0ecSJonathan Lemon mask = 0xffff << (16 - shift); 1812e1daf0ecSJonathan Lemon 1813e1daf0ecSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1814e1daf0ecSJonathan Lemon 1815a509a7c6SJonathan Lemon reg = ioread32(gpio); 1816a509a7c6SJonathan Lemon reg = (reg & mask) | (val << shift); 18176baf2925SJonathan Lemon 1818a509a7c6SJonathan Lemon __handle_signal_inputs(bp, reg); 18196baf2925SJonathan Lemon 1820a509a7c6SJonathan Lemon iowrite32(reg, gpio); 1821e1daf0ecSJonathan Lemon 1822e1daf0ecSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1823e1daf0ecSJonathan Lemon } 1824e1daf0ecSJonathan Lemon 1825a509a7c6SJonathan Lemon static int 1826a509a7c6SJonathan Lemon ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr) 1827e1daf0ecSJonathan Lemon { 1828a509a7c6SJonathan Lemon struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1]; 1829e1daf0ecSJonathan Lemon enum ptp_ocp_sma_mode mode; 1830e1daf0ecSJonathan Lemon int val; 1831e1daf0ecSJonathan Lemon 1832e1daf0ecSJonathan Lemon mode = sma->mode; 1833e1daf0ecSJonathan Lemon val = sma_parse_inputs(buf, &mode); 1834e1daf0ecSJonathan Lemon if (val < 0) 1835e1daf0ecSJonathan Lemon return val; 1836e1daf0ecSJonathan Lemon 1837b2c4f0acSJonathan Lemon if (sma->fixed_dir && (mode != sma->mode || val & SMA_DISABLE)) 1838e1daf0ecSJonathan Lemon return -EOPNOTSUPP; 1839e1daf0ecSJonathan Lemon 1840a509a7c6SJonathan Lemon if (sma->fixed_fcn) { 1841a509a7c6SJonathan Lemon if (val != ((sma_nr - 1) & 1)) 1842e1daf0ecSJonathan Lemon return -EOPNOTSUPP; 1843a509a7c6SJonathan Lemon return 0; 1844e1daf0ecSJonathan Lemon } 1845e1daf0ecSJonathan Lemon 1846b2c4f0acSJonathan Lemon sma->disabled = !!(val & SMA_DISABLE); 1847b2c4f0acSJonathan Lemon 1848a509a7c6SJonathan Lemon if (mode != sma->mode) { 1849a509a7c6SJonathan Lemon if (mode == SMA_MODE_IN) 1850a509a7c6SJonathan Lemon ptp_ocp_sma_store_output(bp, sma_nr, 0); 1851e1daf0ecSJonathan Lemon else 1852a509a7c6SJonathan Lemon ptp_ocp_sma_store_inputs(bp, sma_nr, 0); 1853a509a7c6SJonathan Lemon sma->mode = mode; 1854a509a7c6SJonathan Lemon } 1855a509a7c6SJonathan Lemon 1856a509a7c6SJonathan Lemon if (!sma->fixed_dir) 1857a509a7c6SJonathan Lemon val |= SMA_ENABLE; /* add enable bit */ 1858a509a7c6SJonathan Lemon 1859b2c4f0acSJonathan Lemon if (sma->disabled) 1860b2c4f0acSJonathan Lemon val = 0; 1861b2c4f0acSJonathan Lemon 1862a509a7c6SJonathan Lemon if (mode == SMA_MODE_IN) 1863a509a7c6SJonathan Lemon ptp_ocp_sma_store_inputs(bp, sma_nr, val); 1864a509a7c6SJonathan Lemon else 1865a509a7c6SJonathan Lemon ptp_ocp_sma_store_output(bp, sma_nr, val); 1866e1daf0ecSJonathan Lemon 1867e1daf0ecSJonathan Lemon return 0; 1868e1daf0ecSJonathan Lemon } 1869e1daf0ecSJonathan Lemon 1870e1daf0ecSJonathan Lemon static ssize_t 1871e1daf0ecSJonathan Lemon sma1_store(struct device *dev, struct device_attribute *attr, 1872e1daf0ecSJonathan Lemon const char *buf, size_t count) 1873e1daf0ecSJonathan Lemon { 1874e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1875e1daf0ecSJonathan Lemon int err; 1876e1daf0ecSJonathan Lemon 1877a509a7c6SJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 1); 1878e1daf0ecSJonathan Lemon return err ? err : count; 1879e1daf0ecSJonathan Lemon } 1880e1daf0ecSJonathan Lemon 1881e1daf0ecSJonathan Lemon static ssize_t 1882e1daf0ecSJonathan Lemon sma2_store(struct device *dev, struct device_attribute *attr, 1883e1daf0ecSJonathan Lemon const char *buf, size_t count) 1884e1daf0ecSJonathan Lemon { 1885e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1886e1daf0ecSJonathan Lemon int err; 1887e1daf0ecSJonathan Lemon 1888a509a7c6SJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 2); 1889e1daf0ecSJonathan Lemon return err ? err : count; 1890e1daf0ecSJonathan Lemon } 1891e1daf0ecSJonathan Lemon 1892e1daf0ecSJonathan Lemon static ssize_t 1893e1daf0ecSJonathan Lemon sma3_store(struct device *dev, struct device_attribute *attr, 1894e1daf0ecSJonathan Lemon const char *buf, size_t count) 1895e1daf0ecSJonathan Lemon { 1896e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1897e1daf0ecSJonathan Lemon int err; 1898e1daf0ecSJonathan Lemon 1899a509a7c6SJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 3); 1900e1daf0ecSJonathan Lemon return err ? err : count; 1901e1daf0ecSJonathan Lemon } 1902e1daf0ecSJonathan Lemon 1903e1daf0ecSJonathan Lemon static ssize_t 1904e1daf0ecSJonathan Lemon sma4_store(struct device *dev, struct device_attribute *attr, 1905e1daf0ecSJonathan Lemon const char *buf, size_t count) 1906e1daf0ecSJonathan Lemon { 1907e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1908e1daf0ecSJonathan Lemon int err; 1909e1daf0ecSJonathan Lemon 1910a509a7c6SJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 4); 1911e1daf0ecSJonathan Lemon return err ? err : count; 1912e1daf0ecSJonathan Lemon } 1913e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma1); 1914e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma2); 1915e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma3); 1916e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma4); 1917e1daf0ecSJonathan Lemon 1918e1daf0ecSJonathan Lemon static ssize_t 1919e1daf0ecSJonathan Lemon available_sma_inputs_show(struct device *dev, 1920e1daf0ecSJonathan Lemon struct device_attribute *attr, char *buf) 1921e1daf0ecSJonathan Lemon { 1922e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_sma_in, buf); 1923e1daf0ecSJonathan Lemon } 1924e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_inputs); 1925e1daf0ecSJonathan Lemon 1926e1daf0ecSJonathan Lemon static ssize_t 1927e1daf0ecSJonathan Lemon available_sma_outputs_show(struct device *dev, 1928e1daf0ecSJonathan Lemon struct device_attribute *attr, char *buf) 1929e1daf0ecSJonathan Lemon { 1930e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_sma_out, buf); 1931e1daf0ecSJonathan Lemon } 1932e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_outputs); 1933e1daf0ecSJonathan Lemon 1934773bda96SJonathan Lemon static ssize_t 1935773bda96SJonathan Lemon serialnum_show(struct device *dev, struct device_attribute *attr, char *buf) 1936773bda96SJonathan Lemon { 1937773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1938773bda96SJonathan Lemon 19390cfcdd1eSJonathan Lemon if (!bp->has_eeprom_data) 19400cfcdd1eSJonathan Lemon ptp_ocp_read_eeprom(bp); 1941773bda96SJonathan Lemon 1942773bda96SJonathan Lemon return sysfs_emit(buf, "%pM\n", bp->serial); 1943773bda96SJonathan Lemon } 1944773bda96SJonathan Lemon static DEVICE_ATTR_RO(serialnum); 1945773bda96SJonathan Lemon 1946773bda96SJonathan Lemon static ssize_t 1947ef0cfb34SJonathan Lemon gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf) 1948773bda96SJonathan Lemon { 1949773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1950773bda96SJonathan Lemon ssize_t ret; 1951773bda96SJonathan Lemon 1952ef0cfb34SJonathan Lemon if (bp->gnss_lost) 1953ef0cfb34SJonathan Lemon ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost); 1954773bda96SJonathan Lemon else 1955773bda96SJonathan Lemon ret = sysfs_emit(buf, "SYNC\n"); 1956773bda96SJonathan Lemon 1957773bda96SJonathan Lemon return ret; 1958773bda96SJonathan Lemon } 1959ef0cfb34SJonathan Lemon static DEVICE_ATTR_RO(gnss_sync); 1960773bda96SJonathan Lemon 1961773bda96SJonathan Lemon static ssize_t 196289260d87SJonathan Lemon utc_tai_offset_show(struct device *dev, 196389260d87SJonathan Lemon struct device_attribute *attr, char *buf) 196489260d87SJonathan Lemon { 196589260d87SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 196689260d87SJonathan Lemon 196789260d87SJonathan Lemon return sysfs_emit(buf, "%d\n", bp->utc_tai_offset); 196889260d87SJonathan Lemon } 196989260d87SJonathan Lemon 197089260d87SJonathan Lemon static ssize_t 197189260d87SJonathan Lemon utc_tai_offset_store(struct device *dev, 197289260d87SJonathan Lemon struct device_attribute *attr, 197389260d87SJonathan Lemon const char *buf, size_t count) 197489260d87SJonathan Lemon { 197589260d87SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 197689260d87SJonathan Lemon int err; 197789260d87SJonathan Lemon u32 val; 197889260d87SJonathan Lemon 197989260d87SJonathan Lemon err = kstrtou32(buf, 0, &val); 198089260d87SJonathan Lemon if (err) 198189260d87SJonathan Lemon return err; 198289260d87SJonathan Lemon 198389260d87SJonathan Lemon ptp_ocp_utc_distribute(bp, val); 198489260d87SJonathan Lemon 198589260d87SJonathan Lemon return count; 198689260d87SJonathan Lemon } 198789260d87SJonathan Lemon static DEVICE_ATTR_RW(utc_tai_offset); 198889260d87SJonathan Lemon 198989260d87SJonathan Lemon static ssize_t 19901acffc6eSJonathan Lemon ts_window_adjust_show(struct device *dev, 19911acffc6eSJonathan Lemon struct device_attribute *attr, char *buf) 19921acffc6eSJonathan Lemon { 19931acffc6eSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 19941acffc6eSJonathan Lemon 19951acffc6eSJonathan Lemon return sysfs_emit(buf, "%d\n", bp->ts_window_adjust); 19961acffc6eSJonathan Lemon } 19971acffc6eSJonathan Lemon 19981acffc6eSJonathan Lemon static ssize_t 19991acffc6eSJonathan Lemon ts_window_adjust_store(struct device *dev, 20001acffc6eSJonathan Lemon struct device_attribute *attr, 20011acffc6eSJonathan Lemon const char *buf, size_t count) 20021acffc6eSJonathan Lemon { 20031acffc6eSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 20041acffc6eSJonathan Lemon int err; 20051acffc6eSJonathan Lemon u32 val; 20061acffc6eSJonathan Lemon 20071acffc6eSJonathan Lemon err = kstrtou32(buf, 0, &val); 20081acffc6eSJonathan Lemon if (err) 20091acffc6eSJonathan Lemon return err; 20101acffc6eSJonathan Lemon 20111acffc6eSJonathan Lemon bp->ts_window_adjust = val; 20121acffc6eSJonathan Lemon 20131acffc6eSJonathan Lemon return count; 20141acffc6eSJonathan Lemon } 20151acffc6eSJonathan Lemon static DEVICE_ATTR_RW(ts_window_adjust); 20161acffc6eSJonathan Lemon 20171acffc6eSJonathan Lemon static ssize_t 2018d14ee252SJonathan Lemon irig_b_mode_show(struct device *dev, struct device_attribute *attr, char *buf) 2019d14ee252SJonathan Lemon { 2020d14ee252SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2021d14ee252SJonathan Lemon u32 val; 2022d14ee252SJonathan Lemon 2023d14ee252SJonathan Lemon val = ioread32(&bp->irig_out->ctrl); 2024d14ee252SJonathan Lemon val = (val >> 16) & 0x07; 2025d14ee252SJonathan Lemon return sysfs_emit(buf, "%d\n", val); 2026d14ee252SJonathan Lemon } 2027d14ee252SJonathan Lemon 2028d14ee252SJonathan Lemon static ssize_t 2029d14ee252SJonathan Lemon irig_b_mode_store(struct device *dev, 2030d14ee252SJonathan Lemon struct device_attribute *attr, 2031d14ee252SJonathan Lemon const char *buf, size_t count) 2032d14ee252SJonathan Lemon { 2033d14ee252SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2034d14ee252SJonathan Lemon unsigned long flags; 2035d14ee252SJonathan Lemon int err; 2036d14ee252SJonathan Lemon u32 reg; 2037d14ee252SJonathan Lemon u8 val; 2038d14ee252SJonathan Lemon 2039d14ee252SJonathan Lemon err = kstrtou8(buf, 0, &val); 2040d14ee252SJonathan Lemon if (err) 2041d14ee252SJonathan Lemon return err; 2042d14ee252SJonathan Lemon if (val > 7) 2043d14ee252SJonathan Lemon return -EINVAL; 2044d14ee252SJonathan Lemon 2045d14ee252SJonathan Lemon reg = ((val & 0x7) << 16); 2046d14ee252SJonathan Lemon 2047d14ee252SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 2048d14ee252SJonathan Lemon iowrite32(0, &bp->irig_out->ctrl); /* disable */ 2049d14ee252SJonathan Lemon iowrite32(reg, &bp->irig_out->ctrl); /* change mode */ 2050d14ee252SJonathan Lemon iowrite32(reg | IRIG_M_CTRL_ENABLE, &bp->irig_out->ctrl); 2051d14ee252SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 2052d14ee252SJonathan Lemon 2053d14ee252SJonathan Lemon return count; 2054d14ee252SJonathan Lemon } 2055d14ee252SJonathan Lemon static DEVICE_ATTR_RW(irig_b_mode); 2056d14ee252SJonathan Lemon 2057d14ee252SJonathan Lemon static ssize_t 2058773bda96SJonathan Lemon clock_source_show(struct device *dev, struct device_attribute *attr, char *buf) 2059773bda96SJonathan Lemon { 2060773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2061773bda96SJonathan Lemon const char *p; 2062773bda96SJonathan Lemon u32 select; 2063773bda96SJonathan Lemon 2064773bda96SJonathan Lemon select = ioread32(&bp->reg->select); 2065e1daf0ecSJonathan Lemon p = ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16); 2066773bda96SJonathan Lemon 2067773bda96SJonathan Lemon return sysfs_emit(buf, "%s\n", p); 2068773bda96SJonathan Lemon } 2069773bda96SJonathan Lemon 2070773bda96SJonathan Lemon static ssize_t 2071773bda96SJonathan Lemon clock_source_store(struct device *dev, struct device_attribute *attr, 2072773bda96SJonathan Lemon const char *buf, size_t count) 2073773bda96SJonathan Lemon { 2074773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2075773bda96SJonathan Lemon unsigned long flags; 2076773bda96SJonathan Lemon int val; 2077773bda96SJonathan Lemon 2078e1daf0ecSJonathan Lemon val = ptp_ocp_select_val_from_name(ptp_ocp_clock, buf); 2079773bda96SJonathan Lemon if (val < 0) 2080773bda96SJonathan Lemon return val; 2081773bda96SJonathan Lemon 2082773bda96SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 2083773bda96SJonathan Lemon iowrite32(val, &bp->reg->select); 2084773bda96SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 2085773bda96SJonathan Lemon 2086773bda96SJonathan Lemon return count; 2087773bda96SJonathan Lemon } 2088773bda96SJonathan Lemon static DEVICE_ATTR_RW(clock_source); 2089773bda96SJonathan Lemon 2090773bda96SJonathan Lemon static ssize_t 2091773bda96SJonathan Lemon available_clock_sources_show(struct device *dev, 2092773bda96SJonathan Lemon struct device_attribute *attr, char *buf) 2093773bda96SJonathan Lemon { 2094e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_clock, buf); 2095773bda96SJonathan Lemon } 2096773bda96SJonathan Lemon static DEVICE_ATTR_RO(available_clock_sources); 2097773bda96SJonathan Lemon 20982f23f486SVadim Fedorenko static ssize_t 20992f23f486SVadim Fedorenko clock_status_drift_show(struct device *dev, 21002f23f486SVadim Fedorenko struct device_attribute *attr, char *buf) 21012f23f486SVadim Fedorenko { 21022f23f486SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 21032f23f486SVadim Fedorenko u32 val; 21042f23f486SVadim Fedorenko int res; 21052f23f486SVadim Fedorenko 21062f23f486SVadim Fedorenko val = ioread32(&bp->reg->status_drift); 21072f23f486SVadim Fedorenko res = (val & ~INT_MAX) ? -1 : 1; 21082f23f486SVadim Fedorenko res *= (val & INT_MAX); 21092f23f486SVadim Fedorenko return sysfs_emit(buf, "%d\n", res); 21102f23f486SVadim Fedorenko } 21112f23f486SVadim Fedorenko static DEVICE_ATTR_RO(clock_status_drift); 21122f23f486SVadim Fedorenko 21132f23f486SVadim Fedorenko static ssize_t 21142f23f486SVadim Fedorenko clock_status_offset_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_offset); 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_offset); 21272f23f486SVadim Fedorenko 212844a412d1SVadim Fedorenko static ssize_t 212944a412d1SVadim Fedorenko tod_correction_show(struct device *dev, 213044a412d1SVadim Fedorenko struct device_attribute *attr, char *buf) 213144a412d1SVadim Fedorenko { 213244a412d1SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 213344a412d1SVadim Fedorenko u32 val; 213444a412d1SVadim Fedorenko int res; 213544a412d1SVadim Fedorenko 213644a412d1SVadim Fedorenko val = ioread32(&bp->tod->adj_sec); 213744a412d1SVadim Fedorenko res = (val & ~INT_MAX) ? -1 : 1; 213844a412d1SVadim Fedorenko res *= (val & INT_MAX); 213944a412d1SVadim Fedorenko return sysfs_emit(buf, "%d\n", res); 214044a412d1SVadim Fedorenko } 214144a412d1SVadim Fedorenko 214244a412d1SVadim Fedorenko static ssize_t 214344a412d1SVadim Fedorenko tod_correction_store(struct device *dev, struct device_attribute *attr, 214444a412d1SVadim Fedorenko const char *buf, size_t count) 214544a412d1SVadim Fedorenko { 214644a412d1SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 214744a412d1SVadim Fedorenko unsigned long flags; 214844a412d1SVadim Fedorenko int err, res; 214944a412d1SVadim Fedorenko u32 val = 0; 215044a412d1SVadim Fedorenko 215144a412d1SVadim Fedorenko err = kstrtos32(buf, 0, &res); 215244a412d1SVadim Fedorenko if (err) 215344a412d1SVadim Fedorenko return err; 215444a412d1SVadim Fedorenko if (res < 0) { 215544a412d1SVadim Fedorenko res *= -1; 215644a412d1SVadim Fedorenko val |= BIT(31); 215744a412d1SVadim Fedorenko } 215844a412d1SVadim Fedorenko val |= res; 215944a412d1SVadim Fedorenko 216044a412d1SVadim Fedorenko spin_lock_irqsave(&bp->lock, flags); 216144a412d1SVadim Fedorenko iowrite32(val, &bp->tod->adj_sec); 216244a412d1SVadim Fedorenko spin_unlock_irqrestore(&bp->lock, flags); 216344a412d1SVadim Fedorenko 216444a412d1SVadim Fedorenko return count; 216544a412d1SVadim Fedorenko } 216644a412d1SVadim Fedorenko static DEVICE_ATTR_RW(tod_correction); 216744a412d1SVadim Fedorenko 2168773bda96SJonathan Lemon static struct attribute *timecard_attrs[] = { 2169773bda96SJonathan Lemon &dev_attr_serialnum.attr, 2170ef0cfb34SJonathan Lemon &dev_attr_gnss_sync.attr, 2171773bda96SJonathan Lemon &dev_attr_clock_source.attr, 2172773bda96SJonathan Lemon &dev_attr_available_clock_sources.attr, 2173e1daf0ecSJonathan Lemon &dev_attr_sma1.attr, 2174e1daf0ecSJonathan Lemon &dev_attr_sma2.attr, 2175e1daf0ecSJonathan Lemon &dev_attr_sma3.attr, 2176e1daf0ecSJonathan Lemon &dev_attr_sma4.attr, 2177e1daf0ecSJonathan Lemon &dev_attr_available_sma_inputs.attr, 2178e1daf0ecSJonathan Lemon &dev_attr_available_sma_outputs.attr, 21792f23f486SVadim Fedorenko &dev_attr_clock_status_drift.attr, 21802f23f486SVadim Fedorenko &dev_attr_clock_status_offset.attr, 2181d14ee252SJonathan Lemon &dev_attr_irig_b_mode.attr, 218289260d87SJonathan Lemon &dev_attr_utc_tai_offset.attr, 21831acffc6eSJonathan Lemon &dev_attr_ts_window_adjust.attr, 218444a412d1SVadim Fedorenko &dev_attr_tod_correction.attr, 2185773bda96SJonathan Lemon NULL, 2186773bda96SJonathan Lemon }; 2187773bda96SJonathan Lemon ATTRIBUTE_GROUPS(timecard); 2188773bda96SJonathan Lemon 2189a509a7c6SJonathan Lemon static void 2190a509a7c6SJonathan Lemon gpio_input_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit, 2191a509a7c6SJonathan Lemon const char *def) 2192f67bf662SJonathan Lemon { 2193a509a7c6SJonathan Lemon int i; 2194f67bf662SJonathan Lemon 2195a509a7c6SJonathan Lemon for (i = 0; i < 4; i++) { 2196a509a7c6SJonathan Lemon if (bp->sma[i].mode != SMA_MODE_IN) 2197a509a7c6SJonathan Lemon continue; 2198a509a7c6SJonathan Lemon if (map[i][0] & (1 << bit)) { 2199a509a7c6SJonathan Lemon sprintf(buf, "sma%d", i + 1); 2200a509a7c6SJonathan Lemon return; 2201a509a7c6SJonathan Lemon } 2202a509a7c6SJonathan Lemon } 2203a509a7c6SJonathan Lemon if (!def) 2204a509a7c6SJonathan Lemon def = "----"; 2205a509a7c6SJonathan Lemon strcpy(buf, def); 2206f67bf662SJonathan Lemon } 2207f67bf662SJonathan Lemon 2208f67bf662SJonathan Lemon static void 2209a509a7c6SJonathan Lemon gpio_output_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit) 2210f67bf662SJonathan Lemon { 2211f67bf662SJonathan Lemon char *ans = buf; 2212a509a7c6SJonathan Lemon int i; 2213f67bf662SJonathan Lemon 2214a509a7c6SJonathan Lemon strcpy(ans, "----"); 2215a509a7c6SJonathan Lemon for (i = 0; i < 4; i++) { 2216a509a7c6SJonathan Lemon if (bp->sma[i].mode != SMA_MODE_OUT) 2217a509a7c6SJonathan Lemon continue; 2218a509a7c6SJonathan Lemon if (map[i][1] & (1 << bit)) 2219a509a7c6SJonathan Lemon ans += sprintf(ans, "sma%d ", i + 1); 2220a509a7c6SJonathan Lemon } 2221f67bf662SJonathan Lemon } 2222f67bf662SJonathan Lemon 2223f67bf662SJonathan Lemon static int 2224f67bf662SJonathan Lemon ptp_ocp_summary_show(struct seq_file *s, void *data) 2225f67bf662SJonathan Lemon { 2226f67bf662SJonathan Lemon struct device *dev = s->private; 2227f67bf662SJonathan Lemon struct ptp_system_timestamp sts; 2228a509a7c6SJonathan Lemon u16 sma_val[4][2], ctrl, val; 2229f67bf662SJonathan Lemon struct ts_reg __iomem *ts_reg; 2230f67bf662SJonathan Lemon struct timespec64 ts; 2231f67bf662SJonathan Lemon struct ptp_ocp *bp; 2232a509a7c6SJonathan Lemon char *src, *buf; 2233a62a56d0SJonathan Lemon bool on, map; 2234f67bf662SJonathan Lemon 2235f67bf662SJonathan Lemon buf = (char *)__get_free_page(GFP_KERNEL); 2236f67bf662SJonathan Lemon if (!buf) 2237f67bf662SJonathan Lemon return -ENOMEM; 2238f67bf662SJonathan Lemon 2239f67bf662SJonathan Lemon bp = dev_get_drvdata(dev); 2240f67bf662SJonathan Lemon 2241f67bf662SJonathan Lemon seq_printf(s, "%7s: /dev/ptp%d\n", "PTP", ptp_clock_index(bp->ptp)); 224261fd7ac2SJonathan Lemon if (bp->gnss_port != -1) 224361fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS1", bp->gnss_port); 224461fd7ac2SJonathan Lemon if (bp->gnss2_port != -1) 224561fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS2", bp->gnss2_port); 224661fd7ac2SJonathan Lemon if (bp->mac_port != -1) 224761fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "MAC", bp->mac_port); 224861fd7ac2SJonathan Lemon if (bp->nmea_port != -1) 224961fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "NMEA", bp->nmea_port); 2250f67bf662SJonathan Lemon 2251a509a7c6SJonathan Lemon memset(sma_val, 0xff, sizeof(sma_val)); 2252a509a7c6SJonathan Lemon if (bp->sma_map1) { 2253a509a7c6SJonathan Lemon u32 reg; 2254a509a7c6SJonathan Lemon 2255a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map1->gpio1); 2256a509a7c6SJonathan Lemon sma_val[0][0] = reg & 0xffff; 2257a509a7c6SJonathan Lemon sma_val[1][0] = reg >> 16; 2258a509a7c6SJonathan Lemon 2259a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map1->gpio2); 2260a509a7c6SJonathan Lemon sma_val[2][1] = reg & 0xffff; 2261a509a7c6SJonathan Lemon sma_val[3][1] = reg >> 16; 2262a509a7c6SJonathan Lemon 2263a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map2->gpio1); 2264a509a7c6SJonathan Lemon sma_val[2][0] = reg & 0xffff; 2265a509a7c6SJonathan Lemon sma_val[3][0] = reg >> 16; 2266a509a7c6SJonathan Lemon 2267a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map2->gpio2); 2268a509a7c6SJonathan Lemon sma_val[0][1] = reg & 0xffff; 2269a509a7c6SJonathan Lemon sma_val[1][1] = reg >> 16; 2270a509a7c6SJonathan Lemon } 2271a509a7c6SJonathan Lemon 2272f67bf662SJonathan Lemon sma1_show(dev, NULL, buf); 2273a509a7c6SJonathan Lemon seq_printf(s, " sma1: %04x,%04x %s", 2274a509a7c6SJonathan Lemon sma_val[0][0], sma_val[0][1], buf); 2275f67bf662SJonathan Lemon 2276f67bf662SJonathan Lemon sma2_show(dev, NULL, buf); 2277a509a7c6SJonathan Lemon seq_printf(s, " sma2: %04x,%04x %s", 2278a509a7c6SJonathan Lemon sma_val[1][0], sma_val[1][1], buf); 2279f67bf662SJonathan Lemon 2280f67bf662SJonathan Lemon sma3_show(dev, NULL, buf); 2281a509a7c6SJonathan Lemon seq_printf(s, " sma3: %04x,%04x %s", 2282a509a7c6SJonathan Lemon sma_val[2][0], sma_val[2][1], buf); 2283f67bf662SJonathan Lemon 2284f67bf662SJonathan Lemon sma4_show(dev, NULL, buf); 2285a509a7c6SJonathan Lemon seq_printf(s, " sma4: %04x,%04x %s", 2286a509a7c6SJonathan Lemon sma_val[3][0], sma_val[3][1], buf); 2287f67bf662SJonathan Lemon 2288f67bf662SJonathan Lemon if (bp->ts0) { 2289f67bf662SJonathan Lemon ts_reg = bp->ts0->mem; 2290f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 2291*be69087cSJonathan Lemon src = "GNSS1"; 2292f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS0", 2293f67bf662SJonathan Lemon on ? " ON" : "OFF", src); 2294f67bf662SJonathan Lemon } 2295f67bf662SJonathan Lemon 2296f67bf662SJonathan Lemon if (bp->ts1) { 2297f67bf662SJonathan Lemon ts_reg = bp->ts1->mem; 2298f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 2299a509a7c6SJonathan Lemon gpio_input_map(buf, bp, sma_val, 2, NULL); 2300f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS1", 2301a509a7c6SJonathan Lemon on ? " ON" : "OFF", buf); 2302f67bf662SJonathan Lemon } 2303f67bf662SJonathan Lemon 2304f67bf662SJonathan Lemon if (bp->ts2) { 2305f67bf662SJonathan Lemon ts_reg = bp->ts2->mem; 2306f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 2307a509a7c6SJonathan Lemon gpio_input_map(buf, bp, sma_val, 3, NULL); 2308f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS2", 2309a509a7c6SJonathan Lemon on ? " ON" : "OFF", buf); 2310f67bf662SJonathan Lemon } 2311f67bf662SJonathan Lemon 2312a62a56d0SJonathan Lemon if (bp->pps) { 2313a62a56d0SJonathan Lemon ts_reg = bp->pps->mem; 2314a62a56d0SJonathan Lemon src = "PHC"; 2315a62a56d0SJonathan Lemon on = ioread32(&ts_reg->enable); 2316a62a56d0SJonathan Lemon map = !!(bp->pps_req_map & OCP_REQ_TIMESTAMP); 2317a62a56d0SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS3", 23181a575cdeSNathan Chancellor on && map ? " ON" : "OFF", src); 2319a62a56d0SJonathan Lemon 2320a62a56d0SJonathan Lemon map = !!(bp->pps_req_map & OCP_REQ_PPS); 2321a62a56d0SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "PPS", 23221a575cdeSNathan Chancellor on && map ? " ON" : "OFF", src); 2323a62a56d0SJonathan Lemon } 2324a62a56d0SJonathan Lemon 2325f67bf662SJonathan Lemon if (bp->irig_out) { 2326f67bf662SJonathan Lemon ctrl = ioread32(&bp->irig_out->ctrl); 2327f67bf662SJonathan Lemon on = ctrl & IRIG_M_CTRL_ENABLE; 2328f67bf662SJonathan Lemon val = ioread32(&bp->irig_out->status); 2329a509a7c6SJonathan Lemon gpio_output_map(buf, bp, sma_val, 4); 2330f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, mode %d, out: %s\n", "IRIG", 2331f67bf662SJonathan Lemon on ? " ON" : "OFF", val, (ctrl >> 16), buf); 2332f67bf662SJonathan Lemon } 2333f67bf662SJonathan Lemon 2334f67bf662SJonathan Lemon if (bp->irig_in) { 2335f67bf662SJonathan Lemon on = ioread32(&bp->irig_in->ctrl) & IRIG_S_CTRL_ENABLE; 2336f67bf662SJonathan Lemon val = ioread32(&bp->irig_in->status); 2337a509a7c6SJonathan Lemon gpio_input_map(buf, bp, sma_val, 4, NULL); 2338f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, src: %s\n", "IRIG in", 2339a509a7c6SJonathan Lemon on ? " ON" : "OFF", val, buf); 2340f67bf662SJonathan Lemon } 2341f67bf662SJonathan Lemon 2342f67bf662SJonathan Lemon if (bp->dcf_out) { 2343f67bf662SJonathan Lemon on = ioread32(&bp->dcf_out->ctrl) & DCF_M_CTRL_ENABLE; 2344f67bf662SJonathan Lemon val = ioread32(&bp->dcf_out->status); 2345a509a7c6SJonathan Lemon gpio_output_map(buf, bp, sma_val, 5); 2346f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, out: %s\n", "DCF", 2347f67bf662SJonathan Lemon on ? " ON" : "OFF", val, buf); 2348f67bf662SJonathan Lemon } 2349f67bf662SJonathan Lemon 2350f67bf662SJonathan Lemon if (bp->dcf_in) { 2351f67bf662SJonathan Lemon on = ioread32(&bp->dcf_in->ctrl) & DCF_S_CTRL_ENABLE; 2352f67bf662SJonathan Lemon val = ioread32(&bp->dcf_in->status); 2353a509a7c6SJonathan Lemon gpio_input_map(buf, bp, sma_val, 5, NULL); 2354f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, src: %s\n", "DCF in", 2355a509a7c6SJonathan Lemon on ? " ON" : "OFF", val, buf); 2356f67bf662SJonathan Lemon } 2357f67bf662SJonathan Lemon 2358e3516bb4SJonathan Lemon if (bp->nmea_out) { 2359e3516bb4SJonathan Lemon on = ioread32(&bp->nmea_out->ctrl) & 1; 2360e3516bb4SJonathan Lemon val = ioread32(&bp->nmea_out->status); 2361e3516bb4SJonathan Lemon seq_printf(s, "%7s: %s, error: %d\n", "NMEA", 2362e3516bb4SJonathan Lemon on ? " ON" : "OFF", val); 2363e3516bb4SJonathan Lemon } 2364e3516bb4SJonathan Lemon 2365f67bf662SJonathan Lemon /* compute src for PPS1, used below. */ 2366f67bf662SJonathan Lemon if (bp->pps_select) { 2367f67bf662SJonathan Lemon val = ioread32(&bp->pps_select->gpio1); 2368a509a7c6SJonathan Lemon src = &buf[80]; 2369f67bf662SJonathan Lemon if (val & 0x01) 2370a509a7c6SJonathan Lemon gpio_input_map(src, bp, sma_val, 0, NULL); 2371f67bf662SJonathan Lemon else if (val & 0x02) 2372f67bf662SJonathan Lemon src = "MAC"; 2373f67bf662SJonathan Lemon else if (val & 0x04) 2374*be69087cSJonathan Lemon src = "GNSS1"; 2375f67bf662SJonathan Lemon else 2376f67bf662SJonathan Lemon src = "----"; 2377f67bf662SJonathan Lemon } else { 2378f67bf662SJonathan Lemon src = "?"; 2379f67bf662SJonathan Lemon } 2380f67bf662SJonathan Lemon 2381f67bf662SJonathan Lemon /* assumes automatic switchover/selection */ 2382f67bf662SJonathan Lemon val = ioread32(&bp->reg->select); 2383f67bf662SJonathan Lemon switch (val >> 16) { 2384f67bf662SJonathan Lemon case 0: 2385f67bf662SJonathan Lemon sprintf(buf, "----"); 2386f67bf662SJonathan Lemon break; 2387f67bf662SJonathan Lemon case 2: 2388f67bf662SJonathan Lemon sprintf(buf, "IRIG"); 2389f67bf662SJonathan Lemon break; 2390f67bf662SJonathan Lemon case 3: 2391f67bf662SJonathan Lemon sprintf(buf, "%s via PPS1", src); 2392f67bf662SJonathan Lemon break; 2393f67bf662SJonathan Lemon case 6: 2394f67bf662SJonathan Lemon sprintf(buf, "DCF"); 2395f67bf662SJonathan Lemon break; 2396f67bf662SJonathan Lemon default: 2397f67bf662SJonathan Lemon strcpy(buf, "unknown"); 2398f67bf662SJonathan Lemon break; 2399f67bf662SJonathan Lemon } 2400f67bf662SJonathan Lemon val = ioread32(&bp->reg->status); 2401f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, state: %s\n", "PHC src", buf, 2402f67bf662SJonathan Lemon val & OCP_STATUS_IN_SYNC ? "sync" : "unsynced"); 2403f67bf662SJonathan Lemon 2404f67bf662SJonathan Lemon /* reuses PPS1 src from earlier */ 2405f67bf662SJonathan Lemon seq_printf(s, "MAC PPS1 src: %s\n", src); 2406f67bf662SJonathan Lemon 2407a509a7c6SJonathan Lemon gpio_input_map(buf, bp, sma_val, 1, "GNSS2"); 2408a509a7c6SJonathan Lemon seq_printf(s, "MAC PPS2 src: %s\n", buf); 2409f67bf662SJonathan Lemon 2410f67bf662SJonathan Lemon if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts)) { 2411f67bf662SJonathan Lemon struct timespec64 sys_ts; 2412f67bf662SJonathan Lemon s64 pre_ns, post_ns, ns; 2413f67bf662SJonathan Lemon 2414f67bf662SJonathan Lemon pre_ns = timespec64_to_ns(&sts.pre_ts); 2415f67bf662SJonathan Lemon post_ns = timespec64_to_ns(&sts.post_ts); 2416f67bf662SJonathan Lemon ns = (pre_ns + post_ns) / 2; 2417f67bf662SJonathan Lemon ns += (s64)bp->utc_tai_offset * NSEC_PER_SEC; 2418f67bf662SJonathan Lemon sys_ts = ns_to_timespec64(ns); 2419f67bf662SJonathan Lemon 2420f67bf662SJonathan Lemon seq_printf(s, "%7s: %lld.%ld == %ptT TAI\n", "PHC", 2421f67bf662SJonathan Lemon ts.tv_sec, ts.tv_nsec, &ts); 2422f67bf662SJonathan Lemon seq_printf(s, "%7s: %lld.%ld == %ptT UTC offset %d\n", "SYS", 2423f67bf662SJonathan Lemon sys_ts.tv_sec, sys_ts.tv_nsec, &sys_ts, 2424f67bf662SJonathan Lemon bp->utc_tai_offset); 2425f67bf662SJonathan Lemon seq_printf(s, "%7s: PHC:SYS offset: %lld window: %lld\n", "", 2426f67bf662SJonathan Lemon timespec64_to_ns(&ts) - ns, 2427f67bf662SJonathan Lemon post_ns - pre_ns); 2428f67bf662SJonathan Lemon } 2429f67bf662SJonathan Lemon 2430f67bf662SJonathan Lemon free_page((unsigned long)buf); 2431f67bf662SJonathan Lemon return 0; 2432f67bf662SJonathan Lemon } 2433f67bf662SJonathan Lemon DEFINE_SHOW_ATTRIBUTE(ptp_ocp_summary); 2434f67bf662SJonathan Lemon 24359f492c4cSVadim Fedorenko static int 24369f492c4cSVadim Fedorenko ptp_ocp_tod_status_show(struct seq_file *s, void *data) 24379f492c4cSVadim Fedorenko { 24389f492c4cSVadim Fedorenko struct device *dev = s->private; 24399f492c4cSVadim Fedorenko struct ptp_ocp *bp; 24409f492c4cSVadim Fedorenko u32 val; 24419f492c4cSVadim Fedorenko int idx; 24429f492c4cSVadim Fedorenko 24439f492c4cSVadim Fedorenko bp = dev_get_drvdata(dev); 24449f492c4cSVadim Fedorenko 24459f492c4cSVadim Fedorenko val = ioread32(&bp->tod->ctrl); 24469f492c4cSVadim Fedorenko if (!(val & TOD_CTRL_ENABLE)) { 24479f492c4cSVadim Fedorenko seq_printf(s, "TOD Slave disabled\n"); 24489f492c4cSVadim Fedorenko return 0; 24499f492c4cSVadim Fedorenko } 24509f492c4cSVadim Fedorenko seq_printf(s, "TOD Slave enabled, Control Register 0x%08X\n", val); 24519f492c4cSVadim Fedorenko 24529f492c4cSVadim Fedorenko idx = val & TOD_CTRL_PROTOCOL ? 4 : 0; 24539f492c4cSVadim Fedorenko idx += (val >> 16) & 3; 24549f492c4cSVadim Fedorenko seq_printf(s, "Protocol %s\n", ptp_ocp_tod_proto_name(idx)); 24559f492c4cSVadim Fedorenko 24569f492c4cSVadim Fedorenko idx = (val >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK; 24579f492c4cSVadim Fedorenko seq_printf(s, "GNSS %s\n", ptp_ocp_tod_gnss_name(idx)); 24589f492c4cSVadim Fedorenko 24599f492c4cSVadim Fedorenko val = ioread32(&bp->tod->version); 24609f492c4cSVadim Fedorenko seq_printf(s, "TOD Version %d.%d.%d\n", 24619f492c4cSVadim Fedorenko val >> 24, (val >> 16) & 0xff, val & 0xffff); 24629f492c4cSVadim Fedorenko 24639f492c4cSVadim Fedorenko val = ioread32(&bp->tod->status); 24649f492c4cSVadim Fedorenko seq_printf(s, "Status register: 0x%08X\n", val); 24659f492c4cSVadim Fedorenko 24669f492c4cSVadim Fedorenko val = ioread32(&bp->tod->adj_sec); 24679f492c4cSVadim Fedorenko idx = (val & ~INT_MAX) ? -1 : 1; 24689f492c4cSVadim Fedorenko idx *= (val & INT_MAX); 24699f492c4cSVadim Fedorenko seq_printf(s, "Correction seconds: %d\n", idx); 24709f492c4cSVadim Fedorenko 24719f492c4cSVadim Fedorenko val = ioread32(&bp->tod->utc_status); 24729f492c4cSVadim Fedorenko seq_printf(s, "UTC status register: 0x%08X\n", val); 24739f492c4cSVadim Fedorenko seq_printf(s, "UTC offset: %d valid:%d\n", 24749f492c4cSVadim Fedorenko val & TOD_STATUS_UTC_MASK, val & TOD_STATUS_UTC_VALID ? 1 : 0); 24759f492c4cSVadim Fedorenko seq_printf(s, "Leap second info valid:%d, Leap second announce %d\n", 24769f492c4cSVadim Fedorenko val & TOD_STATUS_LEAP_VALID ? 1 : 0, 24779f492c4cSVadim Fedorenko val & TOD_STATUS_LEAP_ANNOUNCE ? 1 : 0); 24789f492c4cSVadim Fedorenko 24799f492c4cSVadim Fedorenko val = ioread32(&bp->tod->leap); 24809f492c4cSVadim Fedorenko seq_printf(s, "Time to next leap second (in sec): %d\n", (s32) val); 24819f492c4cSVadim Fedorenko 24829f492c4cSVadim Fedorenko return 0; 24839f492c4cSVadim Fedorenko } 24849f492c4cSVadim Fedorenko DEFINE_SHOW_ATTRIBUTE(ptp_ocp_tod_status); 24859f492c4cSVadim Fedorenko 2486f67bf662SJonathan Lemon static struct dentry *ptp_ocp_debugfs_root; 2487f67bf662SJonathan Lemon 2488f67bf662SJonathan Lemon static void 2489f67bf662SJonathan Lemon ptp_ocp_debugfs_add_device(struct ptp_ocp *bp) 2490f67bf662SJonathan Lemon { 2491f67bf662SJonathan Lemon struct dentry *d; 2492f67bf662SJonathan Lemon 2493f67bf662SJonathan Lemon d = debugfs_create_dir(dev_name(&bp->dev), ptp_ocp_debugfs_root); 2494f67bf662SJonathan Lemon bp->debug_root = d; 2495f67bf662SJonathan Lemon debugfs_create_file("summary", 0444, bp->debug_root, 2496f67bf662SJonathan Lemon &bp->dev, &ptp_ocp_summary_fops); 24979f492c4cSVadim Fedorenko if (bp->tod) 24989f492c4cSVadim Fedorenko debugfs_create_file("tod_status", 0444, bp->debug_root, 24999f492c4cSVadim Fedorenko &bp->dev, &ptp_ocp_tod_status_fops); 2500f67bf662SJonathan Lemon } 2501f67bf662SJonathan Lemon 2502f67bf662SJonathan Lemon static void 2503f67bf662SJonathan Lemon ptp_ocp_debugfs_remove_device(struct ptp_ocp *bp) 2504f67bf662SJonathan Lemon { 2505f67bf662SJonathan Lemon debugfs_remove_recursive(bp->debug_root); 2506f67bf662SJonathan Lemon } 2507f67bf662SJonathan Lemon 2508f67bf662SJonathan Lemon static void 2509f67bf662SJonathan Lemon ptp_ocp_debugfs_init(void) 2510f67bf662SJonathan Lemon { 2511f67bf662SJonathan Lemon ptp_ocp_debugfs_root = debugfs_create_dir("timecard", NULL); 2512f67bf662SJonathan Lemon } 2513f67bf662SJonathan Lemon 2514f67bf662SJonathan Lemon static void 2515f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(void) 2516f67bf662SJonathan Lemon { 2517f67bf662SJonathan Lemon debugfs_remove_recursive(ptp_ocp_debugfs_root); 2518f67bf662SJonathan Lemon } 2519f67bf662SJonathan Lemon 2520773bda96SJonathan Lemon static void 2521773bda96SJonathan Lemon ptp_ocp_dev_release(struct device *dev) 2522773bda96SJonathan Lemon { 2523773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2524773bda96SJonathan Lemon 2525773bda96SJonathan Lemon mutex_lock(&ptp_ocp_lock); 2526773bda96SJonathan Lemon idr_remove(&ptp_ocp_idr, bp->id); 2527773bda96SJonathan Lemon mutex_unlock(&ptp_ocp_lock); 2528773bda96SJonathan Lemon } 2529773bda96SJonathan Lemon 2530773bda96SJonathan Lemon static int 2531773bda96SJonathan Lemon ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev) 2532773bda96SJonathan Lemon { 2533773bda96SJonathan Lemon int err; 2534773bda96SJonathan Lemon 2535773bda96SJonathan Lemon mutex_lock(&ptp_ocp_lock); 2536773bda96SJonathan Lemon err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL); 2537773bda96SJonathan Lemon mutex_unlock(&ptp_ocp_lock); 2538773bda96SJonathan Lemon if (err < 0) { 2539773bda96SJonathan Lemon dev_err(&pdev->dev, "idr_alloc failed: %d\n", err); 2540773bda96SJonathan Lemon return err; 2541773bda96SJonathan Lemon } 2542773bda96SJonathan Lemon bp->id = err; 2543773bda96SJonathan Lemon 2544773bda96SJonathan Lemon bp->ptp_info = ptp_ocp_clock_info; 2545773bda96SJonathan Lemon spin_lock_init(&bp->lock); 2546ef0cfb34SJonathan Lemon bp->gnss_port = -1; 254771d7e085SJonathan Lemon bp->gnss2_port = -1; 2548773bda96SJonathan Lemon bp->mac_port = -1; 2549e3516bb4SJonathan Lemon bp->nmea_port = -1; 2550773bda96SJonathan Lemon bp->pdev = pdev; 2551773bda96SJonathan Lemon 2552773bda96SJonathan Lemon device_initialize(&bp->dev); 2553773bda96SJonathan Lemon dev_set_name(&bp->dev, "ocp%d", bp->id); 2554773bda96SJonathan Lemon bp->dev.class = &timecard_class; 2555773bda96SJonathan Lemon bp->dev.parent = &pdev->dev; 2556773bda96SJonathan Lemon bp->dev.release = ptp_ocp_dev_release; 2557773bda96SJonathan Lemon dev_set_drvdata(&bp->dev, bp); 2558773bda96SJonathan Lemon 2559773bda96SJonathan Lemon err = device_add(&bp->dev); 2560773bda96SJonathan Lemon if (err) { 2561773bda96SJonathan Lemon dev_err(&bp->dev, "device add failed: %d\n", err); 2562773bda96SJonathan Lemon goto out; 2563773bda96SJonathan Lemon } 2564773bda96SJonathan Lemon 2565773bda96SJonathan Lemon pci_set_drvdata(pdev, bp); 2566773bda96SJonathan Lemon 2567773bda96SJonathan Lemon return 0; 2568773bda96SJonathan Lemon 2569773bda96SJonathan Lemon out: 2570773bda96SJonathan Lemon ptp_ocp_dev_release(&bp->dev); 2571d12f23faSJonathan Lemon put_device(&bp->dev); 2572773bda96SJonathan Lemon return err; 2573773bda96SJonathan Lemon } 2574773bda96SJonathan Lemon 2575773bda96SJonathan Lemon static void 2576773bda96SJonathan Lemon ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link) 2577773bda96SJonathan Lemon { 2578773bda96SJonathan Lemon struct device *dev = &bp->dev; 2579773bda96SJonathan Lemon 2580773bda96SJonathan Lemon if (sysfs_create_link(&dev->kobj, &child->kobj, link)) 2581773bda96SJonathan Lemon dev_err(dev, "%s symlink failed\n", link); 2582773bda96SJonathan Lemon } 2583773bda96SJonathan Lemon 2584773bda96SJonathan Lemon static void 2585773bda96SJonathan Lemon ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link) 2586773bda96SJonathan Lemon { 2587773bda96SJonathan Lemon struct device *dev, *child; 2588773bda96SJonathan Lemon 2589773bda96SJonathan Lemon dev = &bp->pdev->dev; 2590773bda96SJonathan Lemon 2591773bda96SJonathan Lemon child = device_find_child_by_name(dev, name); 2592773bda96SJonathan Lemon if (!child) { 2593773bda96SJonathan Lemon dev_err(dev, "Could not find device %s\n", name); 2594773bda96SJonathan Lemon return; 2595773bda96SJonathan Lemon } 2596773bda96SJonathan Lemon 2597773bda96SJonathan Lemon ptp_ocp_symlink(bp, child, link); 2598773bda96SJonathan Lemon put_device(child); 2599773bda96SJonathan Lemon } 2600773bda96SJonathan Lemon 2601773bda96SJonathan Lemon static int 2602773bda96SJonathan Lemon ptp_ocp_complete(struct ptp_ocp *bp) 2603773bda96SJonathan Lemon { 2604773bda96SJonathan Lemon struct pps_device *pps; 2605773bda96SJonathan Lemon char buf[32]; 2606773bda96SJonathan Lemon 2607ef0cfb34SJonathan Lemon if (bp->gnss_port != -1) { 2608ef0cfb34SJonathan Lemon sprintf(buf, "ttyS%d", bp->gnss_port); 2609ef0cfb34SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyGNSS"); 2610773bda96SJonathan Lemon } 261171d7e085SJonathan Lemon if (bp->gnss2_port != -1) { 261271d7e085SJonathan Lemon sprintf(buf, "ttyS%d", bp->gnss2_port); 261371d7e085SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyGNSS2"); 261471d7e085SJonathan Lemon } 2615773bda96SJonathan Lemon if (bp->mac_port != -1) { 2616773bda96SJonathan Lemon sprintf(buf, "ttyS%d", bp->mac_port); 2617773bda96SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyMAC"); 2618773bda96SJonathan Lemon } 2619e3516bb4SJonathan Lemon if (bp->nmea_port != -1) { 2620e3516bb4SJonathan Lemon sprintf(buf, "ttyS%d", bp->nmea_port); 2621e3516bb4SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyNMEA"); 2622e3516bb4SJonathan Lemon } 2623773bda96SJonathan Lemon sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp)); 2624773bda96SJonathan Lemon ptp_ocp_link_child(bp, buf, "ptp"); 2625773bda96SJonathan Lemon 2626773bda96SJonathan Lemon pps = pps_lookup_dev(bp->ptp); 2627773bda96SJonathan Lemon if (pps) 2628773bda96SJonathan Lemon ptp_ocp_symlink(bp, pps->dev, "pps"); 2629773bda96SJonathan Lemon 2630773bda96SJonathan Lemon if (device_add_groups(&bp->dev, timecard_groups)) 2631773bda96SJonathan Lemon pr_err("device add groups failed\n"); 2632773bda96SJonathan Lemon 2633f67bf662SJonathan Lemon ptp_ocp_debugfs_add_device(bp); 2634f67bf662SJonathan Lemon 2635773bda96SJonathan Lemon return 0; 2636773bda96SJonathan Lemon } 2637773bda96SJonathan Lemon 2638773bda96SJonathan Lemon static void 2639065efcc5SJonathan Lemon ptp_ocp_phc_info(struct ptp_ocp *bp) 2640065efcc5SJonathan Lemon { 2641065efcc5SJonathan Lemon struct timespec64 ts; 2642065efcc5SJonathan Lemon u32 version, select; 2643065efcc5SJonathan Lemon bool sync; 2644065efcc5SJonathan Lemon 2645065efcc5SJonathan Lemon version = ioread32(&bp->reg->version); 2646065efcc5SJonathan Lemon select = ioread32(&bp->reg->select); 2647065efcc5SJonathan Lemon dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n", 2648065efcc5SJonathan Lemon version >> 24, (version >> 16) & 0xff, version & 0xffff, 2649065efcc5SJonathan Lemon ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16), 2650065efcc5SJonathan Lemon ptp_clock_index(bp->ptp)); 2651065efcc5SJonathan Lemon 2652065efcc5SJonathan Lemon sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC; 2653065efcc5SJonathan Lemon if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL)) 2654065efcc5SJonathan Lemon dev_info(&bp->pdev->dev, "Time: %lld.%ld, %s\n", 2655065efcc5SJonathan Lemon ts.tv_sec, ts.tv_nsec, 2656065efcc5SJonathan Lemon sync ? "in-sync" : "UNSYNCED"); 2657065efcc5SJonathan Lemon } 2658065efcc5SJonathan Lemon 2659065efcc5SJonathan Lemon static void 2660065efcc5SJonathan Lemon ptp_ocp_serial_info(struct device *dev, const char *name, int port, int baud) 2661065efcc5SJonathan Lemon { 2662065efcc5SJonathan Lemon if (port != -1) 2663065efcc5SJonathan Lemon dev_info(dev, "%5s: /dev/ttyS%-2d @ %6d\n", name, port, baud); 2664065efcc5SJonathan Lemon } 2665065efcc5SJonathan Lemon 2666065efcc5SJonathan Lemon static void 2667065efcc5SJonathan Lemon ptp_ocp_info(struct ptp_ocp *bp) 2668773bda96SJonathan Lemon { 2669e3516bb4SJonathan Lemon static int nmea_baud[] = { 2670e3516bb4SJonathan Lemon 1200, 2400, 4800, 9600, 19200, 38400, 2671e3516bb4SJonathan Lemon 57600, 115200, 230400, 460800, 921600, 2672e3516bb4SJonathan Lemon 1000000, 2000000 2673e3516bb4SJonathan Lemon }; 2674773bda96SJonathan Lemon struct device *dev = &bp->pdev->dev; 2675e3516bb4SJonathan Lemon u32 reg; 2676773bda96SJonathan Lemon 2677065efcc5SJonathan Lemon ptp_ocp_phc_info(bp); 2678065efcc5SJonathan Lemon 2679b0ca789aSJonathan Lemon dev_info(dev, "version %x\n", bp->fw_version); 2680b0ca789aSJonathan Lemon if (bp->fw_version & 0xffff) 2681773bda96SJonathan Lemon dev_info(dev, "regular image, version %d\n", 2682b0ca789aSJonathan Lemon bp->fw_version & 0xffff); 2683773bda96SJonathan Lemon else 2684773bda96SJonathan Lemon dev_info(dev, "golden image, version %d\n", 2685b0ca789aSJonathan Lemon bp->fw_version >> 16); 2686b0ca789aSJonathan Lemon 2687065efcc5SJonathan Lemon ptp_ocp_serial_info(dev, "GNSS", bp->gnss_port, 115200); 268871d7e085SJonathan Lemon ptp_ocp_serial_info(dev, "GNSS2", bp->gnss2_port, 115200); 2689065efcc5SJonathan Lemon ptp_ocp_serial_info(dev, "MAC", bp->mac_port, 57600); 2690e3516bb4SJonathan Lemon if (bp->nmea_out && bp->nmea_port != -1) { 2691e3516bb4SJonathan Lemon int baud = -1; 2692e3516bb4SJonathan Lemon 2693e3516bb4SJonathan Lemon reg = ioread32(&bp->nmea_out->uart_baud); 2694e3516bb4SJonathan Lemon if (reg < ARRAY_SIZE(nmea_baud)) 2695e3516bb4SJonathan Lemon baud = nmea_baud[reg]; 2696e3516bb4SJonathan Lemon ptp_ocp_serial_info(dev, "NMEA", bp->nmea_port, baud); 2697e3516bb4SJonathan Lemon } 2698773bda96SJonathan Lemon } 2699773bda96SJonathan Lemon 2700773bda96SJonathan Lemon static void 2701773bda96SJonathan Lemon ptp_ocp_detach_sysfs(struct ptp_ocp *bp) 2702773bda96SJonathan Lemon { 2703773bda96SJonathan Lemon struct device *dev = &bp->dev; 2704773bda96SJonathan Lemon 2705ef0cfb34SJonathan Lemon sysfs_remove_link(&dev->kobj, "ttyGNSS"); 2706773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "ttyMAC"); 2707773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "ptp"); 2708773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "pps"); 2709773bda96SJonathan Lemon device_remove_groups(dev, timecard_groups); 2710773bda96SJonathan Lemon } 2711773bda96SJonathan Lemon 2712773bda96SJonathan Lemon static void 2713773bda96SJonathan Lemon ptp_ocp_detach(struct ptp_ocp *bp) 2714773bda96SJonathan Lemon { 2715f67bf662SJonathan Lemon ptp_ocp_debugfs_remove_device(bp); 2716773bda96SJonathan Lemon ptp_ocp_detach_sysfs(bp); 2717773bda96SJonathan Lemon if (timer_pending(&bp->watchdog)) 2718773bda96SJonathan Lemon del_timer_sync(&bp->watchdog); 2719773bda96SJonathan Lemon if (bp->ts0) 2720773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->ts0); 2721773bda96SJonathan Lemon if (bp->ts1) 2722773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->ts1); 2723dcf61469SJonathan Lemon if (bp->ts2) 2724dcf61469SJonathan Lemon ptp_ocp_unregister_ext(bp->ts2); 2725773bda96SJonathan Lemon if (bp->pps) 2726773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->pps); 2727ef0cfb34SJonathan Lemon if (bp->gnss_port != -1) 2728ef0cfb34SJonathan Lemon serial8250_unregister_port(bp->gnss_port); 272971d7e085SJonathan Lemon if (bp->gnss2_port != -1) 273071d7e085SJonathan Lemon serial8250_unregister_port(bp->gnss2_port); 2731773bda96SJonathan Lemon if (bp->mac_port != -1) 2732773bda96SJonathan Lemon serial8250_unregister_port(bp->mac_port); 2733e3516bb4SJonathan Lemon if (bp->nmea_port != -1) 2734e3516bb4SJonathan Lemon serial8250_unregister_port(bp->nmea_port); 2735773bda96SJonathan Lemon if (bp->spi_flash) 2736773bda96SJonathan Lemon platform_device_unregister(bp->spi_flash); 2737773bda96SJonathan Lemon if (bp->i2c_ctrl) 2738773bda96SJonathan Lemon platform_device_unregister(bp->i2c_ctrl); 2739773bda96SJonathan Lemon if (bp->i2c_clk) 2740773bda96SJonathan Lemon clk_hw_unregister_fixed_rate(bp->i2c_clk); 2741773bda96SJonathan Lemon if (bp->n_irqs) 2742773bda96SJonathan Lemon pci_free_irq_vectors(bp->pdev); 2743773bda96SJonathan Lemon if (bp->ptp) 2744773bda96SJonathan Lemon ptp_clock_unregister(bp->ptp); 2745773bda96SJonathan Lemon device_unregister(&bp->dev); 2746773bda96SJonathan Lemon } 2747773bda96SJonathan Lemon 2748a7e1abadSJonathan Lemon static int 2749a7e1abadSJonathan Lemon ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id) 2750a7e1abadSJonathan Lemon { 2751773bda96SJonathan Lemon struct devlink *devlink; 2752a7e1abadSJonathan Lemon struct ptp_ocp *bp; 2753a7e1abadSJonathan Lemon int err; 2754a7e1abadSJonathan Lemon 2755919d13a7SLeon Romanovsky devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp), &pdev->dev); 2756773bda96SJonathan Lemon if (!devlink) { 2757773bda96SJonathan Lemon dev_err(&pdev->dev, "devlink_alloc failed\n"); 2758a7e1abadSJonathan Lemon return -ENOMEM; 2759773bda96SJonathan Lemon } 2760773bda96SJonathan Lemon 2761a7e1abadSJonathan Lemon err = pci_enable_device(pdev); 2762a7e1abadSJonathan Lemon if (err) { 2763a7e1abadSJonathan Lemon dev_err(&pdev->dev, "pci_enable_device\n"); 27644587369bSJonathan Lemon goto out_free; 2765a7e1abadSJonathan Lemon } 2766a7e1abadSJonathan Lemon 2767773bda96SJonathan Lemon bp = devlink_priv(devlink); 2768773bda96SJonathan Lemon err = ptp_ocp_device_init(bp, pdev); 2769773bda96SJonathan Lemon if (err) 2770d9fdbf13SJonathan Lemon goto out_disable; 2771a7e1abadSJonathan Lemon 2772773bda96SJonathan Lemon /* compat mode. 2773773bda96SJonathan Lemon * Older FPGA firmware only returns 2 irq's. 2774773bda96SJonathan Lemon * allow this - if not all of the IRQ's are returned, skip the 2775773bda96SJonathan Lemon * extra devices and just register the clock. 2776773bda96SJonathan Lemon */ 2777e3516bb4SJonathan Lemon err = pci_alloc_irq_vectors(pdev, 1, 11, PCI_IRQ_MSI | PCI_IRQ_MSIX); 2778773bda96SJonathan Lemon if (err < 0) { 2779773bda96SJonathan Lemon dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err); 2780773bda96SJonathan Lemon goto out; 2781a7e1abadSJonathan Lemon } 2782773bda96SJonathan Lemon bp->n_irqs = err; 2783773bda96SJonathan Lemon pci_set_master(pdev); 2784a7e1abadSJonathan Lemon 2785773bda96SJonathan Lemon err = ptp_ocp_register_resources(bp, id->driver_data); 2786a7e1abadSJonathan Lemon if (err) 2787a7e1abadSJonathan Lemon goto out; 2788a7e1abadSJonathan Lemon 2789a7e1abadSJonathan Lemon bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev); 2790a7e1abadSJonathan Lemon if (IS_ERR(bp->ptp)) { 2791a7e1abadSJonathan Lemon err = PTR_ERR(bp->ptp); 2792773bda96SJonathan Lemon dev_err(&pdev->dev, "ptp_clock_register: %d\n", err); 2793773bda96SJonathan Lemon bp->ptp = NULL; 2794a7e1abadSJonathan Lemon goto out; 2795a7e1abadSJonathan Lemon } 2796a7e1abadSJonathan Lemon 2797773bda96SJonathan Lemon err = ptp_ocp_complete(bp); 2798773bda96SJonathan Lemon if (err) 2799773bda96SJonathan Lemon goto out; 2800773bda96SJonathan Lemon 2801a7e1abadSJonathan Lemon ptp_ocp_info(bp); 2802c89f78e9SLeon Romanovsky devlink_register(devlink); 2803a7e1abadSJonathan Lemon return 0; 2804a7e1abadSJonathan Lemon 2805a7e1abadSJonathan Lemon out: 2806773bda96SJonathan Lemon ptp_ocp_detach(bp); 2807773bda96SJonathan Lemon pci_set_drvdata(pdev, NULL); 2808d9fdbf13SJonathan Lemon out_disable: 2809d9fdbf13SJonathan Lemon pci_disable_device(pdev); 28104587369bSJonathan Lemon out_free: 2811773bda96SJonathan Lemon devlink_free(devlink); 2812a7e1abadSJonathan Lemon return err; 2813a7e1abadSJonathan Lemon } 2814a7e1abadSJonathan Lemon 2815a7e1abadSJonathan Lemon static void 2816a7e1abadSJonathan Lemon ptp_ocp_remove(struct pci_dev *pdev) 2817a7e1abadSJonathan Lemon { 2818a7e1abadSJonathan Lemon struct ptp_ocp *bp = pci_get_drvdata(pdev); 2819773bda96SJonathan Lemon struct devlink *devlink = priv_to_devlink(bp); 2820a7e1abadSJonathan Lemon 2821c89f78e9SLeon Romanovsky devlink_unregister(devlink); 2822773bda96SJonathan Lemon ptp_ocp_detach(bp); 2823a7e1abadSJonathan Lemon pci_set_drvdata(pdev, NULL); 2824d9fdbf13SJonathan Lemon pci_disable_device(pdev); 2825773bda96SJonathan Lemon 2826773bda96SJonathan Lemon devlink_free(devlink); 2827a7e1abadSJonathan Lemon } 2828a7e1abadSJonathan Lemon 2829a7e1abadSJonathan Lemon static struct pci_driver ptp_ocp_driver = { 2830a7e1abadSJonathan Lemon .name = KBUILD_MODNAME, 2831a7e1abadSJonathan Lemon .id_table = ptp_ocp_pcidev_id, 2832a7e1abadSJonathan Lemon .probe = ptp_ocp_probe, 2833a7e1abadSJonathan Lemon .remove = ptp_ocp_remove, 2834a7e1abadSJonathan Lemon }; 2835a7e1abadSJonathan Lemon 2836773bda96SJonathan Lemon static int 2837773bda96SJonathan Lemon ptp_ocp_i2c_notifier_call(struct notifier_block *nb, 2838773bda96SJonathan Lemon unsigned long action, void *data) 2839773bda96SJonathan Lemon { 2840773bda96SJonathan Lemon struct device *dev, *child = data; 2841773bda96SJonathan Lemon struct ptp_ocp *bp; 2842773bda96SJonathan Lemon bool add; 2843773bda96SJonathan Lemon 2844773bda96SJonathan Lemon switch (action) { 2845773bda96SJonathan Lemon case BUS_NOTIFY_ADD_DEVICE: 2846773bda96SJonathan Lemon case BUS_NOTIFY_DEL_DEVICE: 2847773bda96SJonathan Lemon add = action == BUS_NOTIFY_ADD_DEVICE; 2848773bda96SJonathan Lemon break; 2849773bda96SJonathan Lemon default: 2850773bda96SJonathan Lemon return 0; 2851773bda96SJonathan Lemon } 2852773bda96SJonathan Lemon 2853773bda96SJonathan Lemon if (!i2c_verify_adapter(child)) 2854773bda96SJonathan Lemon return 0; 2855773bda96SJonathan Lemon 2856773bda96SJonathan Lemon dev = child; 2857773bda96SJonathan Lemon while ((dev = dev->parent)) 2858773bda96SJonathan Lemon if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME)) 2859773bda96SJonathan Lemon goto found; 2860773bda96SJonathan Lemon return 0; 2861773bda96SJonathan Lemon 2862773bda96SJonathan Lemon found: 2863773bda96SJonathan Lemon bp = dev_get_drvdata(dev); 2864773bda96SJonathan Lemon if (add) 2865773bda96SJonathan Lemon ptp_ocp_symlink(bp, child, "i2c"); 2866773bda96SJonathan Lemon else 2867773bda96SJonathan Lemon sysfs_remove_link(&bp->dev.kobj, "i2c"); 2868773bda96SJonathan Lemon 2869773bda96SJonathan Lemon return 0; 2870773bda96SJonathan Lemon } 2871773bda96SJonathan Lemon 2872773bda96SJonathan Lemon static struct notifier_block ptp_ocp_i2c_notifier = { 2873773bda96SJonathan Lemon .notifier_call = ptp_ocp_i2c_notifier_call, 2874773bda96SJonathan Lemon }; 2875773bda96SJonathan Lemon 2876a7e1abadSJonathan Lemon static int __init 2877a7e1abadSJonathan Lemon ptp_ocp_init(void) 2878a7e1abadSJonathan Lemon { 2879773bda96SJonathan Lemon const char *what; 2880a7e1abadSJonathan Lemon int err; 2881a7e1abadSJonathan Lemon 2882f67bf662SJonathan Lemon ptp_ocp_debugfs_init(); 2883f67bf662SJonathan Lemon 2884773bda96SJonathan Lemon what = "timecard class"; 2885773bda96SJonathan Lemon err = class_register(&timecard_class); 2886773bda96SJonathan Lemon if (err) 2887773bda96SJonathan Lemon goto out; 2888773bda96SJonathan Lemon 2889773bda96SJonathan Lemon what = "i2c notifier"; 2890773bda96SJonathan Lemon err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2891773bda96SJonathan Lemon if (err) 2892773bda96SJonathan Lemon goto out_notifier; 2893773bda96SJonathan Lemon 2894773bda96SJonathan Lemon what = "ptp_ocp driver"; 2895a7e1abadSJonathan Lemon err = pci_register_driver(&ptp_ocp_driver); 2896773bda96SJonathan Lemon if (err) 2897773bda96SJonathan Lemon goto out_register; 2898773bda96SJonathan Lemon 2899773bda96SJonathan Lemon return 0; 2900773bda96SJonathan Lemon 2901773bda96SJonathan Lemon out_register: 2902773bda96SJonathan Lemon bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2903773bda96SJonathan Lemon out_notifier: 2904773bda96SJonathan Lemon class_unregister(&timecard_class); 2905773bda96SJonathan Lemon out: 2906f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(); 2907773bda96SJonathan Lemon pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err); 2908a7e1abadSJonathan Lemon return err; 2909a7e1abadSJonathan Lemon } 2910a7e1abadSJonathan Lemon 2911a7e1abadSJonathan Lemon static void __exit 2912a7e1abadSJonathan Lemon ptp_ocp_fini(void) 2913a7e1abadSJonathan Lemon { 2914773bda96SJonathan Lemon bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2915a7e1abadSJonathan Lemon pci_unregister_driver(&ptp_ocp_driver); 2916773bda96SJonathan Lemon class_unregister(&timecard_class); 2917f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(); 2918a7e1abadSJonathan Lemon } 2919a7e1abadSJonathan Lemon 2920a7e1abadSJonathan Lemon module_init(ptp_ocp_init); 2921a7e1abadSJonathan Lemon module_exit(ptp_ocp_fini); 2922a7e1abadSJonathan Lemon 2923a7e1abadSJonathan Lemon MODULE_DESCRIPTION("OpenCompute TimeCard driver"); 2924a7e1abadSJonathan Lemon MODULE_LICENSE("GPL v2"); 2925