1a7e1abadSJonathan Lemon // SPDX-License-Identifier: GPL-2.0-only 2a7e1abadSJonathan Lemon /* Copyright (c) 2020 Facebook */ 3a7e1abadSJonathan Lemon 41132bb29SAndy Shevchenko #include <linux/bits.h> 5a7e1abadSJonathan Lemon #include <linux/err.h> 6a7e1abadSJonathan Lemon #include <linux/kernel.h> 7a7e1abadSJonathan Lemon #include <linux/module.h> 8f67bf662SJonathan Lemon #include <linux/debugfs.h> 9a7e1abadSJonathan Lemon #include <linux/init.h> 10a7e1abadSJonathan Lemon #include <linux/pci.h> 11773bda96SJonathan Lemon #include <linux/serial_8250.h> 12773bda96SJonathan Lemon #include <linux/clkdev.h> 13773bda96SJonathan Lemon #include <linux/clk-provider.h> 14773bda96SJonathan Lemon #include <linux/platform_device.h> 150cfcdd1eSJonathan Lemon #include <linux/platform_data/i2c-xiic.h> 1669dbe107SVadim Fedorenko #include <linux/platform_data/i2c-ocores.h> 17a7e1abadSJonathan Lemon #include <linux/ptp_clock_kernel.h> 18773bda96SJonathan Lemon #include <linux/spi/spi.h> 19773bda96SJonathan Lemon #include <linux/spi/xilinx_spi.h> 2069dbe107SVadim Fedorenko #include <linux/spi/altera.h> 21773bda96SJonathan Lemon #include <net/devlink.h> 22773bda96SJonathan Lemon #include <linux/i2c.h> 23773bda96SJonathan Lemon #include <linux/mtd/mtd.h> 240cfcdd1eSJonathan Lemon #include <linux/nvmem-consumer.h> 253c3673bdSVadim Fedorenko #include <linux/crc16.h> 26a7e1abadSJonathan Lemon 27773bda96SJonathan Lemon #define PCI_VENDOR_ID_FACEBOOK 0x1d9b 28773bda96SJonathan Lemon #define PCI_DEVICE_ID_FACEBOOK_TIMECARD 0x0400 29773bda96SJonathan Lemon 3081fa652eSVadim Fedorenko #define PCI_VENDOR_ID_CELESTICA 0x18d4 3181fa652eSVadim Fedorenko #define PCI_DEVICE_ID_CELESTICA_TIMECARD 0x1008 3281fa652eSVadim Fedorenko 3369dbe107SVadim Fedorenko #define PCI_VENDOR_ID_OROLIA 0x1ad7 3469dbe107SVadim Fedorenko #define PCI_DEVICE_ID_OROLIA_ARTCARD 0xa000 3569dbe107SVadim Fedorenko 36773bda96SJonathan Lemon static struct class timecard_class = { 37773bda96SJonathan Lemon .owner = THIS_MODULE, 38773bda96SJonathan Lemon .name = "timecard", 39a7e1abadSJonathan Lemon }; 40a7e1abadSJonathan Lemon 41a7e1abadSJonathan Lemon struct ocp_reg { 42a7e1abadSJonathan Lemon u32 ctrl; 43a7e1abadSJonathan Lemon u32 status; 44a7e1abadSJonathan Lemon u32 select; 45a7e1abadSJonathan Lemon u32 version; 46a7e1abadSJonathan Lemon u32 time_ns; 47a7e1abadSJonathan Lemon u32 time_sec; 48a7e1abadSJonathan Lemon u32 __pad0[2]; 49a7e1abadSJonathan Lemon u32 adjust_ns; 50a7e1abadSJonathan Lemon u32 adjust_sec; 51a7e1abadSJonathan Lemon u32 __pad1[2]; 52a7e1abadSJonathan Lemon u32 offset_ns; 53a7e1abadSJonathan Lemon u32 offset_window_ns; 54773bda96SJonathan Lemon u32 __pad2[2]; 55773bda96SJonathan Lemon u32 drift_ns; 56773bda96SJonathan Lemon u32 drift_window_ns; 57773bda96SJonathan Lemon u32 __pad3[6]; 58773bda96SJonathan Lemon u32 servo_offset_p; 59773bda96SJonathan Lemon u32 servo_offset_i; 60773bda96SJonathan Lemon u32 servo_drift_p; 61773bda96SJonathan Lemon u32 servo_drift_i; 622f23f486SVadim Fedorenko u32 status_offset; 632f23f486SVadim Fedorenko u32 status_drift; 64a7e1abadSJonathan Lemon }; 65a7e1abadSJonathan Lemon 66a7e1abadSJonathan Lemon #define OCP_CTRL_ENABLE BIT(0) 67a7e1abadSJonathan Lemon #define OCP_CTRL_ADJUST_TIME BIT(1) 68a7e1abadSJonathan Lemon #define OCP_CTRL_ADJUST_OFFSET BIT(2) 69773bda96SJonathan Lemon #define OCP_CTRL_ADJUST_DRIFT BIT(3) 70773bda96SJonathan Lemon #define OCP_CTRL_ADJUST_SERVO BIT(8) 71a7e1abadSJonathan Lemon #define OCP_CTRL_READ_TIME_REQ BIT(30) 72a7e1abadSJonathan Lemon #define OCP_CTRL_READ_TIME_DONE BIT(31) 73a7e1abadSJonathan Lemon 74a7e1abadSJonathan Lemon #define OCP_STATUS_IN_SYNC BIT(0) 75773bda96SJonathan Lemon #define OCP_STATUS_IN_HOLDOVER BIT(1) 76a7e1abadSJonathan Lemon 77a7e1abadSJonathan Lemon #define OCP_SELECT_CLK_NONE 0 78773bda96SJonathan Lemon #define OCP_SELECT_CLK_REG 0xfe 79a7e1abadSJonathan Lemon 80a7e1abadSJonathan Lemon struct tod_reg { 81a7e1abadSJonathan Lemon u32 ctrl; 82a7e1abadSJonathan Lemon u32 status; 83a7e1abadSJonathan Lemon u32 uart_polarity; 84a7e1abadSJonathan Lemon u32 version; 85065efcc5SJonathan Lemon u32 adj_sec; 86a7e1abadSJonathan Lemon u32 __pad0[3]; 87a7e1abadSJonathan Lemon u32 uart_baud; 88a7e1abadSJonathan Lemon u32 __pad1[3]; 89a7e1abadSJonathan Lemon u32 utc_status; 90a7e1abadSJonathan Lemon u32 leap; 91a7e1abadSJonathan Lemon }; 92a7e1abadSJonathan Lemon 93a7e1abadSJonathan Lemon #define TOD_CTRL_PROTOCOL BIT(28) 94a7e1abadSJonathan Lemon #define TOD_CTRL_DISABLE_FMT_A BIT(17) 95a7e1abadSJonathan Lemon #define TOD_CTRL_DISABLE_FMT_B BIT(16) 96a7e1abadSJonathan Lemon #define TOD_CTRL_ENABLE BIT(0) 971132bb29SAndy Shevchenko #define TOD_CTRL_GNSS_MASK GENMASK(3, 0) 98a7e1abadSJonathan Lemon #define TOD_CTRL_GNSS_SHIFT 24 99a7e1abadSJonathan Lemon 1001132bb29SAndy Shevchenko #define TOD_STATUS_UTC_MASK GENMASK(7, 0) 101a7e1abadSJonathan Lemon #define TOD_STATUS_UTC_VALID BIT(8) 1029f492c4cSVadim Fedorenko #define TOD_STATUS_LEAP_ANNOUNCE BIT(12) 103a7e1abadSJonathan Lemon #define TOD_STATUS_LEAP_VALID BIT(16) 104a7e1abadSJonathan Lemon 105773bda96SJonathan Lemon struct ts_reg { 106773bda96SJonathan Lemon u32 enable; 107773bda96SJonathan Lemon u32 error; 108773bda96SJonathan Lemon u32 polarity; 109773bda96SJonathan Lemon u32 version; 110773bda96SJonathan Lemon u32 __pad0[4]; 111773bda96SJonathan Lemon u32 cable_delay; 112773bda96SJonathan Lemon u32 __pad1[3]; 113773bda96SJonathan Lemon u32 intr; 114773bda96SJonathan Lemon u32 intr_mask; 115773bda96SJonathan Lemon u32 event_count; 116773bda96SJonathan Lemon u32 __pad2[1]; 117773bda96SJonathan Lemon u32 ts_count; 118773bda96SJonathan Lemon u32 time_ns; 119773bda96SJonathan Lemon u32 time_sec; 120773bda96SJonathan Lemon u32 data_width; 121773bda96SJonathan Lemon u32 data; 122773bda96SJonathan Lemon }; 123773bda96SJonathan Lemon 124773bda96SJonathan Lemon struct pps_reg { 125773bda96SJonathan Lemon u32 ctrl; 126773bda96SJonathan Lemon u32 status; 1270d43d4f2SJonathan Lemon u32 __pad0[6]; 1280d43d4f2SJonathan Lemon u32 cable_delay; 129773bda96SJonathan Lemon }; 130773bda96SJonathan Lemon 131773bda96SJonathan Lemon #define PPS_STATUS_FILTER_ERR BIT(0) 132773bda96SJonathan Lemon #define PPS_STATUS_SUPERV_ERR BIT(1) 133773bda96SJonathan Lemon 134773bda96SJonathan Lemon struct img_reg { 135773bda96SJonathan Lemon u32 version; 136773bda96SJonathan Lemon }; 137773bda96SJonathan Lemon 138e1daf0ecSJonathan Lemon struct gpio_reg { 139e1daf0ecSJonathan Lemon u32 gpio1; 140e1daf0ecSJonathan Lemon u32 __pad0; 141e1daf0ecSJonathan Lemon u32 gpio2; 142e1daf0ecSJonathan Lemon u32 __pad1; 143e1daf0ecSJonathan Lemon }; 144e1daf0ecSJonathan Lemon 1456baf2925SJonathan Lemon struct irig_master_reg { 1466baf2925SJonathan Lemon u32 ctrl; 1476baf2925SJonathan Lemon u32 status; 1486baf2925SJonathan Lemon u32 __pad0; 1496baf2925SJonathan Lemon u32 version; 1506baf2925SJonathan Lemon u32 adj_sec; 1516baf2925SJonathan Lemon u32 mode_ctrl; 1526baf2925SJonathan Lemon }; 1536baf2925SJonathan Lemon 1546baf2925SJonathan Lemon #define IRIG_M_CTRL_ENABLE BIT(0) 1556baf2925SJonathan Lemon 1566baf2925SJonathan Lemon struct irig_slave_reg { 1576baf2925SJonathan Lemon u32 ctrl; 1586baf2925SJonathan Lemon u32 status; 1596baf2925SJonathan Lemon u32 __pad0; 1606baf2925SJonathan Lemon u32 version; 1616baf2925SJonathan Lemon u32 adj_sec; 1626baf2925SJonathan Lemon u32 mode_ctrl; 1636baf2925SJonathan Lemon }; 1646baf2925SJonathan Lemon 1656baf2925SJonathan Lemon #define IRIG_S_CTRL_ENABLE BIT(0) 1666baf2925SJonathan Lemon 1676baf2925SJonathan Lemon struct dcf_master_reg { 1686baf2925SJonathan Lemon u32 ctrl; 1696baf2925SJonathan Lemon u32 status; 1706baf2925SJonathan Lemon u32 __pad0; 1716baf2925SJonathan Lemon u32 version; 1726baf2925SJonathan Lemon u32 adj_sec; 1736baf2925SJonathan Lemon }; 1746baf2925SJonathan Lemon 1756baf2925SJonathan Lemon #define DCF_M_CTRL_ENABLE BIT(0) 1766baf2925SJonathan Lemon 1776baf2925SJonathan Lemon struct dcf_slave_reg { 1786baf2925SJonathan Lemon u32 ctrl; 1796baf2925SJonathan Lemon u32 status; 1806baf2925SJonathan Lemon u32 __pad0; 1816baf2925SJonathan Lemon u32 version; 1826baf2925SJonathan Lemon u32 adj_sec; 1836baf2925SJonathan Lemon }; 1846baf2925SJonathan Lemon 1856baf2925SJonathan Lemon #define DCF_S_CTRL_ENABLE BIT(0) 1866baf2925SJonathan Lemon 187b325af3cSJonathan Lemon struct signal_reg { 188b325af3cSJonathan Lemon u32 enable; 189b325af3cSJonathan Lemon u32 status; 190b325af3cSJonathan Lemon u32 polarity; 191b325af3cSJonathan Lemon u32 version; 192b325af3cSJonathan Lemon u32 __pad0[4]; 193b325af3cSJonathan Lemon u32 cable_delay; 194b325af3cSJonathan Lemon u32 __pad1[3]; 195b325af3cSJonathan Lemon u32 intr; 196b325af3cSJonathan Lemon u32 intr_mask; 197b325af3cSJonathan Lemon u32 __pad2[2]; 198b325af3cSJonathan Lemon u32 start_ns; 199b325af3cSJonathan Lemon u32 start_sec; 200b325af3cSJonathan Lemon u32 pulse_ns; 201b325af3cSJonathan Lemon u32 pulse_sec; 202b325af3cSJonathan Lemon u32 period_ns; 203b325af3cSJonathan Lemon u32 period_sec; 204b325af3cSJonathan Lemon u32 repeat_count; 205b325af3cSJonathan Lemon }; 206b325af3cSJonathan Lemon 2072407f5d6SJonathan Lemon struct frequency_reg { 2082407f5d6SJonathan Lemon u32 ctrl; 2092407f5d6SJonathan Lemon u32 status; 2102407f5d6SJonathan Lemon }; 211*9c44a7acSVadim Fedorenko 212*9c44a7acSVadim Fedorenko struct board_config_reg { 213*9c44a7acSVadim Fedorenko u32 mro50_serial_activate; 214*9c44a7acSVadim Fedorenko }; 215*9c44a7acSVadim Fedorenko 2162407f5d6SJonathan Lemon #define FREQ_STATUS_VALID BIT(31) 2172407f5d6SJonathan Lemon #define FREQ_STATUS_ERROR BIT(30) 2182407f5d6SJonathan Lemon #define FREQ_STATUS_OVERRUN BIT(29) 2191132bb29SAndy Shevchenko #define FREQ_STATUS_MASK GENMASK(23, 0) 2202407f5d6SJonathan Lemon 221773bda96SJonathan Lemon struct ptp_ocp_flash_info { 222773bda96SJonathan Lemon const char *name; 223773bda96SJonathan Lemon int pci_offset; 224773bda96SJonathan Lemon int data_size; 225773bda96SJonathan Lemon void *data; 226773bda96SJonathan Lemon }; 227773bda96SJonathan Lemon 2283c3673bdSVadim Fedorenko struct ptp_ocp_firmware_header { 2293c3673bdSVadim Fedorenko char magic[4]; 2303c3673bdSVadim Fedorenko __be16 pci_vendor_id; 2313c3673bdSVadim Fedorenko __be16 pci_device_id; 2323c3673bdSVadim Fedorenko __be32 image_size; 2333c3673bdSVadim Fedorenko __be16 hw_revision; 2343c3673bdSVadim Fedorenko __be16 crc; 2353c3673bdSVadim Fedorenko }; 2363c3673bdSVadim Fedorenko 2373c3673bdSVadim Fedorenko #define OCP_FIRMWARE_MAGIC_HEADER "OCPC" 2383c3673bdSVadim Fedorenko 2391618df6aSJonathan Lemon struct ptp_ocp_i2c_info { 2401618df6aSJonathan Lemon const char *name; 2411618df6aSJonathan Lemon unsigned long fixed_rate; 2421618df6aSJonathan Lemon size_t data_size; 2431618df6aSJonathan Lemon void *data; 2441618df6aSJonathan Lemon }; 2451618df6aSJonathan Lemon 246773bda96SJonathan Lemon struct ptp_ocp_ext_info { 247773bda96SJonathan Lemon int index; 248773bda96SJonathan Lemon irqreturn_t (*irq_fcn)(int irq, void *priv); 249a62a56d0SJonathan Lemon int (*enable)(void *priv, u32 req, bool enable); 250773bda96SJonathan Lemon }; 251773bda96SJonathan Lemon 252773bda96SJonathan Lemon struct ptp_ocp_ext_src { 253773bda96SJonathan Lemon void __iomem *mem; 254773bda96SJonathan Lemon struct ptp_ocp *bp; 255773bda96SJonathan Lemon struct ptp_ocp_ext_info *info; 256773bda96SJonathan Lemon int irq_vec; 257773bda96SJonathan Lemon }; 258773bda96SJonathan Lemon 259a509a7c6SJonathan Lemon enum ptp_ocp_sma_mode { 260a509a7c6SJonathan Lemon SMA_MODE_IN, 261a509a7c6SJonathan Lemon SMA_MODE_OUT, 262a509a7c6SJonathan Lemon }; 263a509a7c6SJonathan Lemon 264a509a7c6SJonathan Lemon struct ptp_ocp_sma_connector { 265a509a7c6SJonathan Lemon enum ptp_ocp_sma_mode mode; 266a509a7c6SJonathan Lemon bool fixed_fcn; 267a509a7c6SJonathan Lemon bool fixed_dir; 268b2c4f0acSJonathan Lemon bool disabled; 269ee4cd725SJonathan Lemon u8 default_fcn; 270a509a7c6SJonathan Lemon }; 271a509a7c6SJonathan Lemon 272c205d53cSJonathan Lemon struct ocp_attr_group { 273c205d53cSJonathan Lemon u64 cap; 274c205d53cSJonathan Lemon const struct attribute_group *group; 275c205d53cSJonathan Lemon }; 276c205d53cSJonathan Lemon 277c205d53cSJonathan Lemon #define OCP_CAP_BASIC BIT(0) 278b325af3cSJonathan Lemon #define OCP_CAP_SIGNAL BIT(1) 2792407f5d6SJonathan Lemon #define OCP_CAP_FREQ BIT(2) 280b325af3cSJonathan Lemon 281b325af3cSJonathan Lemon struct ptp_ocp_signal { 282b325af3cSJonathan Lemon ktime_t period; 283b325af3cSJonathan Lemon ktime_t pulse; 284b325af3cSJonathan Lemon ktime_t phase; 285b325af3cSJonathan Lemon ktime_t start; 286b325af3cSJonathan Lemon int duty; 287b325af3cSJonathan Lemon bool polarity; 288b325af3cSJonathan Lemon bool running; 289b325af3cSJonathan Lemon }; 290c205d53cSJonathan Lemon 291895ac5a5SVadim Fedorenko struct ptp_ocp_serial_port { 292895ac5a5SVadim Fedorenko int line; 293895ac5a5SVadim Fedorenko int baud; 294895ac5a5SVadim Fedorenko }; 295895ac5a5SVadim Fedorenko 2960cfcdd1eSJonathan Lemon #define OCP_BOARD_ID_LEN 13 2970cfcdd1eSJonathan Lemon #define OCP_SERIAL_LEN 6 2980cfcdd1eSJonathan Lemon 299a7e1abadSJonathan Lemon struct ptp_ocp { 300a7e1abadSJonathan Lemon struct pci_dev *pdev; 301773bda96SJonathan Lemon struct device dev; 302a7e1abadSJonathan Lemon spinlock_t lock; 303a7e1abadSJonathan Lemon struct ocp_reg __iomem *reg; 304a7e1abadSJonathan Lemon struct tod_reg __iomem *tod; 3050d43d4f2SJonathan Lemon struct pps_reg __iomem *pps_to_ext; 3060d43d4f2SJonathan Lemon struct pps_reg __iomem *pps_to_clk; 307*9c44a7acSVadim Fedorenko struct board_config_reg __iomem *board_config; 308f67bf662SJonathan Lemon struct gpio_reg __iomem *pps_select; 309a509a7c6SJonathan Lemon struct gpio_reg __iomem *sma_map1; 310a509a7c6SJonathan Lemon struct gpio_reg __iomem *sma_map2; 3116baf2925SJonathan Lemon struct irig_master_reg __iomem *irig_out; 3126baf2925SJonathan Lemon struct irig_slave_reg __iomem *irig_in; 3136baf2925SJonathan Lemon struct dcf_master_reg __iomem *dcf_out; 3146baf2925SJonathan Lemon struct dcf_slave_reg __iomem *dcf_in; 315e3516bb4SJonathan Lemon struct tod_reg __iomem *nmea_out; 3162407f5d6SJonathan Lemon struct frequency_reg __iomem *freq_in[4]; 317b325af3cSJonathan Lemon struct ptp_ocp_ext_src *signal_out[4]; 318773bda96SJonathan Lemon struct ptp_ocp_ext_src *pps; 319773bda96SJonathan Lemon struct ptp_ocp_ext_src *ts0; 320773bda96SJonathan Lemon struct ptp_ocp_ext_src *ts1; 321dcf61469SJonathan Lemon struct ptp_ocp_ext_src *ts2; 3220fa3ff7eSJonathan Lemon struct ptp_ocp_ext_src *ts3; 3230fa3ff7eSJonathan Lemon struct ptp_ocp_ext_src *ts4; 32469dbe107SVadim Fedorenko struct ocp_art_gpio_reg __iomem *art_sma; 325773bda96SJonathan Lemon struct img_reg __iomem *image; 326a7e1abadSJonathan Lemon struct ptp_clock *ptp; 327a7e1abadSJonathan Lemon struct ptp_clock_info ptp_info; 328773bda96SJonathan Lemon struct platform_device *i2c_ctrl; 329773bda96SJonathan Lemon struct platform_device *spi_flash; 330773bda96SJonathan Lemon struct clk_hw *i2c_clk; 331773bda96SJonathan Lemon struct timer_list watchdog; 332c2239294SJonathan Lemon const struct attribute_group **attr_group; 3330cfcdd1eSJonathan Lemon const struct ptp_ocp_eeprom_map *eeprom_map; 334f67bf662SJonathan Lemon struct dentry *debug_root; 335ef0cfb34SJonathan Lemon time64_t gnss_lost; 336773bda96SJonathan Lemon int id; 337773bda96SJonathan Lemon int n_irqs; 338895ac5a5SVadim Fedorenko struct ptp_ocp_serial_port gnss_port; 339895ac5a5SVadim Fedorenko struct ptp_ocp_serial_port gnss2_port; 340895ac5a5SVadim Fedorenko struct ptp_ocp_serial_port mac_port; /* miniature atomic clock */ 341895ac5a5SVadim Fedorenko struct ptp_ocp_serial_port nmea_port; 3425a728ac5SJonathan Lemon bool fw_loader; 3435a728ac5SJonathan Lemon u8 fw_tag; 3445a728ac5SJonathan Lemon u16 fw_version; 3450cfcdd1eSJonathan Lemon u8 board_id[OCP_BOARD_ID_LEN]; 3460cfcdd1eSJonathan Lemon u8 serial[OCP_SERIAL_LEN]; 3470cfcdd1eSJonathan Lemon bool has_eeprom_data; 348a62a56d0SJonathan Lemon u32 pps_req_map; 34989260d87SJonathan Lemon int flash_start; 35089260d87SJonathan Lemon u32 utc_tai_offset; 3511acffc6eSJonathan Lemon u32 ts_window_adjust; 352c205d53cSJonathan Lemon u64 fw_cap; 353b325af3cSJonathan Lemon struct ptp_ocp_signal signal[4]; 354a509a7c6SJonathan Lemon struct ptp_ocp_sma_connector sma[4]; 355caab82cdSJonathan Lemon const struct ocp_sma_op *sma_op; 356a7e1abadSJonathan Lemon }; 357a7e1abadSJonathan Lemon 358a62a56d0SJonathan Lemon #define OCP_REQ_TIMESTAMP BIT(0) 359a62a56d0SJonathan Lemon #define OCP_REQ_PPS BIT(1) 360a62a56d0SJonathan Lemon 361773bda96SJonathan Lemon struct ocp_resource { 362773bda96SJonathan Lemon unsigned long offset; 363773bda96SJonathan Lemon int size; 364773bda96SJonathan Lemon int irq_vec; 365773bda96SJonathan Lemon int (*setup)(struct ptp_ocp *bp, struct ocp_resource *r); 366773bda96SJonathan Lemon void *extra; 367773bda96SJonathan Lemon unsigned long bp_offset; 36856ec4403SJonathan Lemon const char * const name; 369773bda96SJonathan Lemon }; 370773bda96SJonathan Lemon 371773bda96SJonathan Lemon static int ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r); 372773bda96SJonathan Lemon static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r); 373773bda96SJonathan Lemon static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r); 374773bda96SJonathan Lemon static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r); 375773bda96SJonathan Lemon static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r); 376773bda96SJonathan Lemon static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r); 377773bda96SJonathan Lemon static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv); 378b325af3cSJonathan Lemon static irqreturn_t ptp_ocp_signal_irq(int irq, void *priv); 379a62a56d0SJonathan Lemon static int ptp_ocp_ts_enable(void *priv, u32 req, bool enable); 3801aa66a3aSJonathan Lemon static int ptp_ocp_signal_from_perout(struct ptp_ocp *bp, int gen, 3811aa66a3aSJonathan Lemon struct ptp_perout_request *req); 382b325af3cSJonathan Lemon static int ptp_ocp_signal_enable(void *priv, u32 req, bool enable); 383b325af3cSJonathan Lemon static int ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr); 384773bda96SJonathan Lemon 38569dbe107SVadim Fedorenko static int ptp_ocp_art_board_init(struct ptp_ocp *bp, struct ocp_resource *r); 38669dbe107SVadim Fedorenko 387c205d53cSJonathan Lemon static const struct ocp_attr_group fb_timecard_groups[]; 388c205d53cSJonathan Lemon 38969dbe107SVadim Fedorenko static const struct ocp_attr_group art_timecard_groups[]; 39069dbe107SVadim Fedorenko 3910cfcdd1eSJonathan Lemon struct ptp_ocp_eeprom_map { 3920cfcdd1eSJonathan Lemon u16 off; 3930cfcdd1eSJonathan Lemon u16 len; 3940cfcdd1eSJonathan Lemon u32 bp_offset; 3950cfcdd1eSJonathan Lemon const void * const tag; 3960cfcdd1eSJonathan Lemon }; 3970cfcdd1eSJonathan Lemon 3980cfcdd1eSJonathan Lemon #define EEPROM_ENTRY(addr, member) \ 3990cfcdd1eSJonathan Lemon .off = addr, \ 4000cfcdd1eSJonathan Lemon .len = sizeof_field(struct ptp_ocp, member), \ 4010cfcdd1eSJonathan Lemon .bp_offset = offsetof(struct ptp_ocp, member) 4020cfcdd1eSJonathan Lemon 4030cfcdd1eSJonathan Lemon #define BP_MAP_ENTRY_ADDR(bp, map) ({ \ 4040cfcdd1eSJonathan Lemon (void *)((uintptr_t)(bp) + (map)->bp_offset); \ 4050cfcdd1eSJonathan Lemon }) 4060cfcdd1eSJonathan Lemon 4070cfcdd1eSJonathan Lemon static struct ptp_ocp_eeprom_map fb_eeprom_map[] = { 4080cfcdd1eSJonathan Lemon { EEPROM_ENTRY(0x43, board_id) }, 4090cfcdd1eSJonathan Lemon { EEPROM_ENTRY(0x00, serial), .tag = "mac" }, 4100cfcdd1eSJonathan Lemon { } 4110cfcdd1eSJonathan Lemon }; 4120cfcdd1eSJonathan Lemon 41369dbe107SVadim Fedorenko static struct ptp_ocp_eeprom_map art_eeprom_map[] = { 41469dbe107SVadim Fedorenko { EEPROM_ENTRY(0x200 + 0x43, board_id) }, 41569dbe107SVadim Fedorenko { EEPROM_ENTRY(0x200 + 0x63, serial) }, 41669dbe107SVadim Fedorenko { } 41769dbe107SVadim Fedorenko }; 41869dbe107SVadim Fedorenko 419773bda96SJonathan Lemon #define bp_assign_entry(bp, res, val) ({ \ 420773bda96SJonathan Lemon uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset; \ 421773bda96SJonathan Lemon *(typeof(val) *)addr = val; \ 422773bda96SJonathan Lemon }) 423773bda96SJonathan Lemon 424773bda96SJonathan Lemon #define OCP_RES_LOCATION(member) \ 42556ec4403SJonathan Lemon .name = #member, .bp_offset = offsetof(struct ptp_ocp, member) 426773bda96SJonathan Lemon 427773bda96SJonathan Lemon #define OCP_MEM_RESOURCE(member) \ 428773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_mem 429773bda96SJonathan Lemon 430773bda96SJonathan Lemon #define OCP_SERIAL_RESOURCE(member) \ 431773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_serial 432773bda96SJonathan Lemon 433773bda96SJonathan Lemon #define OCP_I2C_RESOURCE(member) \ 434773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_i2c 435773bda96SJonathan Lemon 436773bda96SJonathan Lemon #define OCP_SPI_RESOURCE(member) \ 437773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_spi 438773bda96SJonathan Lemon 439773bda96SJonathan Lemon #define OCP_EXT_RESOURCE(member) \ 440773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_ext 441773bda96SJonathan Lemon 442773bda96SJonathan Lemon /* This is the MSI vector mapping used. 4430fa3ff7eSJonathan Lemon * 0: PPS (TS5) 444773bda96SJonathan Lemon * 1: TS0 445773bda96SJonathan Lemon * 2: TS1 446be69087cSJonathan Lemon * 3: GNSS1 44771d7e085SJonathan Lemon * 4: GNSS2 448773bda96SJonathan Lemon * 5: MAC 449dcf61469SJonathan Lemon * 6: TS2 4501447149dSJonathan Lemon * 7: I2C controller 451e3516bb4SJonathan Lemon * 8: HWICAP (notused) 452773bda96SJonathan Lemon * 9: SPI Flash 453e3516bb4SJonathan Lemon * 10: NMEA 454b325af3cSJonathan Lemon * 11: Signal Generator 1 455b325af3cSJonathan Lemon * 12: Signal Generator 2 456b325af3cSJonathan Lemon * 13: Signal Generator 3 457b325af3cSJonathan Lemon * 14: Signal Generator 4 4580fa3ff7eSJonathan Lemon * 15: TS3 4590fa3ff7eSJonathan Lemon * 16: TS4 46069dbe107SVadim Fedorenko -- 46169dbe107SVadim Fedorenko * 8: Orolia TS1 46269dbe107SVadim Fedorenko * 10: Orolia TS2 46369dbe107SVadim Fedorenko * 11: Orolia TS0 (GNSS) 46469dbe107SVadim Fedorenko * 12: Orolia PPS 46569dbe107SVadim Fedorenko * 14: Orolia TS3 46669dbe107SVadim Fedorenko * 15: Orolia TS4 467773bda96SJonathan Lemon */ 468773bda96SJonathan Lemon 469773bda96SJonathan Lemon static struct ocp_resource ocp_fb_resource[] = { 470773bda96SJonathan Lemon { 471773bda96SJonathan Lemon OCP_MEM_RESOURCE(reg), 472773bda96SJonathan Lemon .offset = 0x01000000, .size = 0x10000, 473773bda96SJonathan Lemon }, 474773bda96SJonathan Lemon { 475773bda96SJonathan Lemon OCP_EXT_RESOURCE(ts0), 476773bda96SJonathan Lemon .offset = 0x01010000, .size = 0x10000, .irq_vec = 1, 477773bda96SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 47856ec4403SJonathan Lemon .index = 0, 479773bda96SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 480773bda96SJonathan Lemon .enable = ptp_ocp_ts_enable, 481773bda96SJonathan Lemon }, 482773bda96SJonathan Lemon }, 483773bda96SJonathan Lemon { 484773bda96SJonathan Lemon OCP_EXT_RESOURCE(ts1), 485773bda96SJonathan Lemon .offset = 0x01020000, .size = 0x10000, .irq_vec = 2, 486773bda96SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 48756ec4403SJonathan Lemon .index = 1, 488773bda96SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 489773bda96SJonathan Lemon .enable = ptp_ocp_ts_enable, 490773bda96SJonathan Lemon }, 491773bda96SJonathan Lemon }, 492773bda96SJonathan Lemon { 493dcf61469SJonathan Lemon OCP_EXT_RESOURCE(ts2), 494dcf61469SJonathan Lemon .offset = 0x01060000, .size = 0x10000, .irq_vec = 6, 495dcf61469SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 496dcf61469SJonathan Lemon .index = 2, 497dcf61469SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 498dcf61469SJonathan Lemon .enable = ptp_ocp_ts_enable, 499dcf61469SJonathan Lemon }, 500dcf61469SJonathan Lemon }, 501dcf61469SJonathan Lemon { 5020fa3ff7eSJonathan Lemon OCP_EXT_RESOURCE(ts3), 5030fa3ff7eSJonathan Lemon .offset = 0x01110000, .size = 0x10000, .irq_vec = 15, 5040fa3ff7eSJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 5050fa3ff7eSJonathan Lemon .index = 3, 5060fa3ff7eSJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 5070fa3ff7eSJonathan Lemon .enable = ptp_ocp_ts_enable, 5080fa3ff7eSJonathan Lemon }, 5090fa3ff7eSJonathan Lemon }, 5100fa3ff7eSJonathan Lemon { 5110fa3ff7eSJonathan Lemon OCP_EXT_RESOURCE(ts4), 5120fa3ff7eSJonathan Lemon .offset = 0x01120000, .size = 0x10000, .irq_vec = 16, 5130fa3ff7eSJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 5140fa3ff7eSJonathan Lemon .index = 4, 5150fa3ff7eSJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 5160fa3ff7eSJonathan Lemon .enable = ptp_ocp_ts_enable, 5170fa3ff7eSJonathan Lemon }, 5180fa3ff7eSJonathan Lemon }, 5190fa3ff7eSJonathan Lemon /* Timestamp for PHC and/or PPS generator */ 5200fa3ff7eSJonathan Lemon { 521a62a56d0SJonathan Lemon OCP_EXT_RESOURCE(pps), 522a62a56d0SJonathan Lemon .offset = 0x010C0000, .size = 0x10000, .irq_vec = 0, 523a62a56d0SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 5240fa3ff7eSJonathan Lemon .index = 5, 525a62a56d0SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 526a62a56d0SJonathan Lemon .enable = ptp_ocp_ts_enable, 527a62a56d0SJonathan Lemon }, 528a62a56d0SJonathan Lemon }, 529a62a56d0SJonathan Lemon { 530b325af3cSJonathan Lemon OCP_EXT_RESOURCE(signal_out[0]), 531b325af3cSJonathan Lemon .offset = 0x010D0000, .size = 0x10000, .irq_vec = 11, 532b325af3cSJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 533b325af3cSJonathan Lemon .index = 1, 534b325af3cSJonathan Lemon .irq_fcn = ptp_ocp_signal_irq, 535b325af3cSJonathan Lemon .enable = ptp_ocp_signal_enable, 536b325af3cSJonathan Lemon }, 537b325af3cSJonathan Lemon }, 538b325af3cSJonathan Lemon { 539b325af3cSJonathan Lemon OCP_EXT_RESOURCE(signal_out[1]), 540b325af3cSJonathan Lemon .offset = 0x010E0000, .size = 0x10000, .irq_vec = 12, 541b325af3cSJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 542b325af3cSJonathan Lemon .index = 2, 543b325af3cSJonathan Lemon .irq_fcn = ptp_ocp_signal_irq, 544b325af3cSJonathan Lemon .enable = ptp_ocp_signal_enable, 545b325af3cSJonathan Lemon }, 546b325af3cSJonathan Lemon }, 547b325af3cSJonathan Lemon { 548b325af3cSJonathan Lemon OCP_EXT_RESOURCE(signal_out[2]), 549b325af3cSJonathan Lemon .offset = 0x010F0000, .size = 0x10000, .irq_vec = 13, 550b325af3cSJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 551b325af3cSJonathan Lemon .index = 3, 552b325af3cSJonathan Lemon .irq_fcn = ptp_ocp_signal_irq, 553b325af3cSJonathan Lemon .enable = ptp_ocp_signal_enable, 554b325af3cSJonathan Lemon }, 555b325af3cSJonathan Lemon }, 556b325af3cSJonathan Lemon { 557b325af3cSJonathan Lemon OCP_EXT_RESOURCE(signal_out[3]), 558b325af3cSJonathan Lemon .offset = 0x01100000, .size = 0x10000, .irq_vec = 14, 559b325af3cSJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 560b325af3cSJonathan Lemon .index = 4, 561b325af3cSJonathan Lemon .irq_fcn = ptp_ocp_signal_irq, 562b325af3cSJonathan Lemon .enable = ptp_ocp_signal_enable, 563b325af3cSJonathan Lemon }, 564b325af3cSJonathan Lemon }, 565b325af3cSJonathan Lemon { 5660d43d4f2SJonathan Lemon OCP_MEM_RESOURCE(pps_to_ext), 5670d43d4f2SJonathan Lemon .offset = 0x01030000, .size = 0x10000, 5680d43d4f2SJonathan Lemon }, 5690d43d4f2SJonathan Lemon { 5700d43d4f2SJonathan Lemon OCP_MEM_RESOURCE(pps_to_clk), 571773bda96SJonathan Lemon .offset = 0x01040000, .size = 0x10000, 572773bda96SJonathan Lemon }, 573773bda96SJonathan Lemon { 574773bda96SJonathan Lemon OCP_MEM_RESOURCE(tod), 575773bda96SJonathan Lemon .offset = 0x01050000, .size = 0x10000, 576773bda96SJonathan Lemon }, 577773bda96SJonathan Lemon { 5786baf2925SJonathan Lemon OCP_MEM_RESOURCE(irig_in), 5796baf2925SJonathan Lemon .offset = 0x01070000, .size = 0x10000, 5806baf2925SJonathan Lemon }, 5816baf2925SJonathan Lemon { 5826baf2925SJonathan Lemon OCP_MEM_RESOURCE(irig_out), 5836baf2925SJonathan Lemon .offset = 0x01080000, .size = 0x10000, 5846baf2925SJonathan Lemon }, 5856baf2925SJonathan Lemon { 5866baf2925SJonathan Lemon OCP_MEM_RESOURCE(dcf_in), 5876baf2925SJonathan Lemon .offset = 0x01090000, .size = 0x10000, 5886baf2925SJonathan Lemon }, 5896baf2925SJonathan Lemon { 5906baf2925SJonathan Lemon OCP_MEM_RESOURCE(dcf_out), 5916baf2925SJonathan Lemon .offset = 0x010A0000, .size = 0x10000, 5926baf2925SJonathan Lemon }, 5936baf2925SJonathan Lemon { 594e3516bb4SJonathan Lemon OCP_MEM_RESOURCE(nmea_out), 595e3516bb4SJonathan Lemon .offset = 0x010B0000, .size = 0x10000, 596e3516bb4SJonathan Lemon }, 597e3516bb4SJonathan Lemon { 598773bda96SJonathan Lemon OCP_MEM_RESOURCE(image), 599773bda96SJonathan Lemon .offset = 0x00020000, .size = 0x1000, 600773bda96SJonathan Lemon }, 601773bda96SJonathan Lemon { 602f67bf662SJonathan Lemon OCP_MEM_RESOURCE(pps_select), 603f67bf662SJonathan Lemon .offset = 0x00130000, .size = 0x1000, 604f67bf662SJonathan Lemon }, 605f67bf662SJonathan Lemon { 606a509a7c6SJonathan Lemon OCP_MEM_RESOURCE(sma_map1), 607e1daf0ecSJonathan Lemon .offset = 0x00140000, .size = 0x1000, 608e1daf0ecSJonathan Lemon }, 609e1daf0ecSJonathan Lemon { 610a509a7c6SJonathan Lemon OCP_MEM_RESOURCE(sma_map2), 611a509a7c6SJonathan Lemon .offset = 0x00220000, .size = 0x1000, 612a509a7c6SJonathan Lemon }, 613a509a7c6SJonathan Lemon { 614773bda96SJonathan Lemon OCP_I2C_RESOURCE(i2c_ctrl), 615773bda96SJonathan Lemon .offset = 0x00150000, .size = 0x10000, .irq_vec = 7, 6161618df6aSJonathan Lemon .extra = &(struct ptp_ocp_i2c_info) { 6171618df6aSJonathan Lemon .name = "xiic-i2c", 6181618df6aSJonathan Lemon .fixed_rate = 50000000, 6190cfcdd1eSJonathan Lemon .data_size = sizeof(struct xiic_i2c_platform_data), 6200cfcdd1eSJonathan Lemon .data = &(struct xiic_i2c_platform_data) { 6210cfcdd1eSJonathan Lemon .num_devices = 2, 6220cfcdd1eSJonathan Lemon .devices = (struct i2c_board_info[]) { 6230cfcdd1eSJonathan Lemon { I2C_BOARD_INFO("24c02", 0x50) }, 6240cfcdd1eSJonathan Lemon { I2C_BOARD_INFO("24mac402", 0x58), 6250cfcdd1eSJonathan Lemon .platform_data = "mac" }, 6260cfcdd1eSJonathan Lemon }, 6270cfcdd1eSJonathan Lemon }, 6281618df6aSJonathan Lemon }, 629773bda96SJonathan Lemon }, 630773bda96SJonathan Lemon { 631ef0cfb34SJonathan Lemon OCP_SERIAL_RESOURCE(gnss_port), 632773bda96SJonathan Lemon .offset = 0x00160000 + 0x1000, .irq_vec = 3, 633895ac5a5SVadim Fedorenko .extra = &(struct ptp_ocp_serial_port) { 634895ac5a5SVadim Fedorenko .baud = 115200, 635895ac5a5SVadim Fedorenko }, 636773bda96SJonathan Lemon }, 637773bda96SJonathan Lemon { 63871d7e085SJonathan Lemon OCP_SERIAL_RESOURCE(gnss2_port), 63971d7e085SJonathan Lemon .offset = 0x00170000 + 0x1000, .irq_vec = 4, 640895ac5a5SVadim Fedorenko .extra = &(struct ptp_ocp_serial_port) { 641895ac5a5SVadim Fedorenko .baud = 115200, 642895ac5a5SVadim Fedorenko }, 64371d7e085SJonathan Lemon }, 64471d7e085SJonathan Lemon { 645773bda96SJonathan Lemon OCP_SERIAL_RESOURCE(mac_port), 646773bda96SJonathan Lemon .offset = 0x00180000 + 0x1000, .irq_vec = 5, 647895ac5a5SVadim Fedorenko .extra = &(struct ptp_ocp_serial_port) { 648895ac5a5SVadim Fedorenko .baud = 57600, 649895ac5a5SVadim Fedorenko }, 650773bda96SJonathan Lemon }, 651773bda96SJonathan Lemon { 652e3516bb4SJonathan Lemon OCP_SERIAL_RESOURCE(nmea_port), 653e3516bb4SJonathan Lemon .offset = 0x00190000 + 0x1000, .irq_vec = 10, 654e3516bb4SJonathan Lemon }, 655e3516bb4SJonathan Lemon { 656773bda96SJonathan Lemon OCP_SPI_RESOURCE(spi_flash), 657773bda96SJonathan Lemon .offset = 0x00310000, .size = 0x10000, .irq_vec = 9, 658773bda96SJonathan Lemon .extra = &(struct ptp_ocp_flash_info) { 659773bda96SJonathan Lemon .name = "xilinx_spi", .pci_offset = 0, 660773bda96SJonathan Lemon .data_size = sizeof(struct xspi_platform_data), 661773bda96SJonathan Lemon .data = &(struct xspi_platform_data) { 662773bda96SJonathan Lemon .num_chipselect = 1, 663773bda96SJonathan Lemon .bits_per_word = 8, 664773bda96SJonathan Lemon .num_devices = 1, 665773bda96SJonathan Lemon .devices = &(struct spi_board_info) { 666773bda96SJonathan Lemon .modalias = "spi-nor", 667773bda96SJonathan Lemon }, 668773bda96SJonathan Lemon }, 669773bda96SJonathan Lemon }, 670773bda96SJonathan Lemon }, 671773bda96SJonathan Lemon { 6722407f5d6SJonathan Lemon OCP_MEM_RESOURCE(freq_in[0]), 6732407f5d6SJonathan Lemon .offset = 0x01200000, .size = 0x10000, 6742407f5d6SJonathan Lemon }, 6752407f5d6SJonathan Lemon { 6762407f5d6SJonathan Lemon OCP_MEM_RESOURCE(freq_in[1]), 6772407f5d6SJonathan Lemon .offset = 0x01210000, .size = 0x10000, 6782407f5d6SJonathan Lemon }, 6792407f5d6SJonathan Lemon { 6802407f5d6SJonathan Lemon OCP_MEM_RESOURCE(freq_in[2]), 6812407f5d6SJonathan Lemon .offset = 0x01220000, .size = 0x10000, 6822407f5d6SJonathan Lemon }, 6832407f5d6SJonathan Lemon { 6842407f5d6SJonathan Lemon OCP_MEM_RESOURCE(freq_in[3]), 6852407f5d6SJonathan Lemon .offset = 0x01230000, .size = 0x10000, 6862407f5d6SJonathan Lemon }, 6872407f5d6SJonathan Lemon { 688773bda96SJonathan Lemon .setup = ptp_ocp_fb_board_init, 689773bda96SJonathan Lemon }, 690773bda96SJonathan Lemon { } 691773bda96SJonathan Lemon }; 692773bda96SJonathan Lemon 69369dbe107SVadim Fedorenko struct ocp_art_gpio_reg { 69469dbe107SVadim Fedorenko struct { 69569dbe107SVadim Fedorenko u32 gpio; 69669dbe107SVadim Fedorenko u32 __pad[3]; 69769dbe107SVadim Fedorenko } map[4]; 69869dbe107SVadim Fedorenko }; 69969dbe107SVadim Fedorenko 70069dbe107SVadim Fedorenko static struct ocp_resource ocp_art_resource[] = { 70169dbe107SVadim Fedorenko { 70269dbe107SVadim Fedorenko OCP_MEM_RESOURCE(reg), 70369dbe107SVadim Fedorenko .offset = 0x01000000, .size = 0x10000, 70469dbe107SVadim Fedorenko }, 70569dbe107SVadim Fedorenko { 70669dbe107SVadim Fedorenko OCP_SERIAL_RESOURCE(gnss_port), 70769dbe107SVadim Fedorenko .offset = 0x00160000 + 0x1000, .irq_vec = 3, 70869dbe107SVadim Fedorenko .extra = &(struct ptp_ocp_serial_port) { 70969dbe107SVadim Fedorenko .baud = 115200, 71069dbe107SVadim Fedorenko }, 71169dbe107SVadim Fedorenko }, 71269dbe107SVadim Fedorenko { 71369dbe107SVadim Fedorenko OCP_MEM_RESOURCE(art_sma), 71469dbe107SVadim Fedorenko .offset = 0x003C0000, .size = 0x1000, 71569dbe107SVadim Fedorenko }, 71669dbe107SVadim Fedorenko /* Timestamp associated with GNSS1 receiver PPS */ 71769dbe107SVadim Fedorenko { 71869dbe107SVadim Fedorenko OCP_EXT_RESOURCE(ts0), 71969dbe107SVadim Fedorenko .offset = 0x360000, .size = 0x20, .irq_vec = 12, 72069dbe107SVadim Fedorenko .extra = &(struct ptp_ocp_ext_info) { 72169dbe107SVadim Fedorenko .index = 0, 72269dbe107SVadim Fedorenko .irq_fcn = ptp_ocp_ts_irq, 72369dbe107SVadim Fedorenko .enable = ptp_ocp_ts_enable, 72469dbe107SVadim Fedorenko }, 72569dbe107SVadim Fedorenko }, 72669dbe107SVadim Fedorenko { 72769dbe107SVadim Fedorenko OCP_EXT_RESOURCE(ts1), 72869dbe107SVadim Fedorenko .offset = 0x380000, .size = 0x20, .irq_vec = 8, 72969dbe107SVadim Fedorenko .extra = &(struct ptp_ocp_ext_info) { 73069dbe107SVadim Fedorenko .index = 1, 73169dbe107SVadim Fedorenko .irq_fcn = ptp_ocp_ts_irq, 73269dbe107SVadim Fedorenko .enable = ptp_ocp_ts_enable, 73369dbe107SVadim Fedorenko }, 73469dbe107SVadim Fedorenko }, 73569dbe107SVadim Fedorenko { 73669dbe107SVadim Fedorenko OCP_EXT_RESOURCE(ts2), 73769dbe107SVadim Fedorenko .offset = 0x390000, .size = 0x20, .irq_vec = 10, 73869dbe107SVadim Fedorenko .extra = &(struct ptp_ocp_ext_info) { 73969dbe107SVadim Fedorenko .index = 2, 74069dbe107SVadim Fedorenko .irq_fcn = ptp_ocp_ts_irq, 74169dbe107SVadim Fedorenko .enable = ptp_ocp_ts_enable, 74269dbe107SVadim Fedorenko }, 74369dbe107SVadim Fedorenko }, 74469dbe107SVadim Fedorenko { 74569dbe107SVadim Fedorenko OCP_EXT_RESOURCE(ts3), 74669dbe107SVadim Fedorenko .offset = 0x3A0000, .size = 0x20, .irq_vec = 14, 74769dbe107SVadim Fedorenko .extra = &(struct ptp_ocp_ext_info) { 74869dbe107SVadim Fedorenko .index = 3, 74969dbe107SVadim Fedorenko .irq_fcn = ptp_ocp_ts_irq, 75069dbe107SVadim Fedorenko .enable = ptp_ocp_ts_enable, 75169dbe107SVadim Fedorenko }, 75269dbe107SVadim Fedorenko }, 75369dbe107SVadim Fedorenko { 75469dbe107SVadim Fedorenko OCP_EXT_RESOURCE(ts4), 75569dbe107SVadim Fedorenko .offset = 0x3B0000, .size = 0x20, .irq_vec = 15, 75669dbe107SVadim Fedorenko .extra = &(struct ptp_ocp_ext_info) { 75769dbe107SVadim Fedorenko .index = 4, 75869dbe107SVadim Fedorenko .irq_fcn = ptp_ocp_ts_irq, 75969dbe107SVadim Fedorenko .enable = ptp_ocp_ts_enable, 76069dbe107SVadim Fedorenko }, 76169dbe107SVadim Fedorenko }, 76269dbe107SVadim Fedorenko /* Timestamp associated with Internal PPS of the card */ 76369dbe107SVadim Fedorenko { 76469dbe107SVadim Fedorenko OCP_EXT_RESOURCE(pps), 76569dbe107SVadim Fedorenko .offset = 0x00330000, .size = 0x20, .irq_vec = 11, 76669dbe107SVadim Fedorenko .extra = &(struct ptp_ocp_ext_info) { 76769dbe107SVadim Fedorenko .index = 5, 76869dbe107SVadim Fedorenko .irq_fcn = ptp_ocp_ts_irq, 76969dbe107SVadim Fedorenko .enable = ptp_ocp_ts_enable, 77069dbe107SVadim Fedorenko }, 77169dbe107SVadim Fedorenko }, 77269dbe107SVadim Fedorenko { 77369dbe107SVadim Fedorenko OCP_SPI_RESOURCE(spi_flash), 77469dbe107SVadim Fedorenko .offset = 0x00310000, .size = 0x10000, .irq_vec = 9, 77569dbe107SVadim Fedorenko .extra = &(struct ptp_ocp_flash_info) { 77669dbe107SVadim Fedorenko .name = "spi_altera", .pci_offset = 0, 77769dbe107SVadim Fedorenko .data_size = sizeof(struct altera_spi_platform_data), 77869dbe107SVadim Fedorenko .data = &(struct altera_spi_platform_data) { 77969dbe107SVadim Fedorenko .num_chipselect = 1, 78069dbe107SVadim Fedorenko .num_devices = 1, 78169dbe107SVadim Fedorenko .devices = &(struct spi_board_info) { 78269dbe107SVadim Fedorenko .modalias = "spi-nor", 78369dbe107SVadim Fedorenko }, 78469dbe107SVadim Fedorenko }, 78569dbe107SVadim Fedorenko }, 78669dbe107SVadim Fedorenko }, 78769dbe107SVadim Fedorenko { 78869dbe107SVadim Fedorenko OCP_I2C_RESOURCE(i2c_ctrl), 78969dbe107SVadim Fedorenko .offset = 0x350000, .size = 0x100, .irq_vec = 4, 79069dbe107SVadim Fedorenko .extra = &(struct ptp_ocp_i2c_info) { 79169dbe107SVadim Fedorenko .name = "ocores-i2c", 79269dbe107SVadim Fedorenko .fixed_rate = 400000, 79369dbe107SVadim Fedorenko .data_size = sizeof(struct ocores_i2c_platform_data), 79469dbe107SVadim Fedorenko .data = &(struct ocores_i2c_platform_data) { 79569dbe107SVadim Fedorenko .clock_khz = 125000, 79669dbe107SVadim Fedorenko .bus_khz = 400, 79769dbe107SVadim Fedorenko .num_devices = 1, 79869dbe107SVadim Fedorenko .devices = &(struct i2c_board_info) { 79969dbe107SVadim Fedorenko I2C_BOARD_INFO("24c08", 0x50), 80069dbe107SVadim Fedorenko }, 80169dbe107SVadim Fedorenko }, 80269dbe107SVadim Fedorenko }, 80369dbe107SVadim Fedorenko }, 80469dbe107SVadim Fedorenko { 805*9c44a7acSVadim Fedorenko OCP_SERIAL_RESOURCE(mac_port), 806*9c44a7acSVadim Fedorenko .offset = 0x00190000, .irq_vec = 7, 807*9c44a7acSVadim Fedorenko .extra = &(struct ptp_ocp_serial_port) { 808*9c44a7acSVadim Fedorenko .baud = 9600, 809*9c44a7acSVadim Fedorenko }, 810*9c44a7acSVadim Fedorenko }, 811*9c44a7acSVadim Fedorenko { 812*9c44a7acSVadim Fedorenko OCP_MEM_RESOURCE(board_config), 813*9c44a7acSVadim Fedorenko .offset = 0x210000, .size = 0x1000, 814*9c44a7acSVadim Fedorenko }, 815*9c44a7acSVadim Fedorenko { 81669dbe107SVadim Fedorenko .setup = ptp_ocp_art_board_init, 81769dbe107SVadim Fedorenko }, 81869dbe107SVadim Fedorenko { } 81969dbe107SVadim Fedorenko }; 82069dbe107SVadim Fedorenko 821773bda96SJonathan Lemon static const struct pci_device_id ptp_ocp_pcidev_id[] = { 822773bda96SJonathan Lemon { PCI_DEVICE_DATA(FACEBOOK, TIMECARD, &ocp_fb_resource) }, 82381fa652eSVadim Fedorenko { PCI_DEVICE_DATA(CELESTICA, TIMECARD, &ocp_fb_resource) }, 82469dbe107SVadim Fedorenko { PCI_DEVICE_DATA(OROLIA, ARTCARD, &ocp_art_resource) }, 82581fa652eSVadim Fedorenko { } 826773bda96SJonathan Lemon }; 827773bda96SJonathan Lemon MODULE_DEVICE_TABLE(pci, ptp_ocp_pcidev_id); 828773bda96SJonathan Lemon 829773bda96SJonathan Lemon static DEFINE_MUTEX(ptp_ocp_lock); 830773bda96SJonathan Lemon static DEFINE_IDR(ptp_ocp_idr); 831773bda96SJonathan Lemon 832e1daf0ecSJonathan Lemon struct ocp_selector { 833773bda96SJonathan Lemon const char *name; 834773bda96SJonathan Lemon int value; 835e1daf0ecSJonathan Lemon }; 836e1daf0ecSJonathan Lemon 8373f3fe41cSJonathan Lemon static const struct ocp_selector ptp_ocp_clock[] = { 838773bda96SJonathan Lemon { .name = "NONE", .value = 0 }, 839773bda96SJonathan Lemon { .name = "TOD", .value = 1 }, 840773bda96SJonathan Lemon { .name = "IRIG", .value = 2 }, 841773bda96SJonathan Lemon { .name = "PPS", .value = 3 }, 842773bda96SJonathan Lemon { .name = "PTP", .value = 4 }, 843773bda96SJonathan Lemon { .name = "RTC", .value = 5 }, 844773bda96SJonathan Lemon { .name = "DCF", .value = 6 }, 845773bda96SJonathan Lemon { .name = "REGS", .value = 0xfe }, 846773bda96SJonathan Lemon { .name = "EXT", .value = 0xff }, 847e1daf0ecSJonathan Lemon { } 848e1daf0ecSJonathan Lemon }; 849e1daf0ecSJonathan Lemon 8501132bb29SAndy Shevchenko #define SMA_DISABLE BIT(16) 851a509a7c6SJonathan Lemon #define SMA_ENABLE BIT(15) 8521132bb29SAndy Shevchenko #define SMA_SELECT_MASK GENMASK(14, 0) 853a509a7c6SJonathan Lemon 8543f3fe41cSJonathan Lemon static const struct ocp_selector ptp_ocp_sma_in[] = { 8552407f5d6SJonathan Lemon { .name = "10Mhz", .value = 0x0000 }, 8562407f5d6SJonathan Lemon { .name = "PPS1", .value = 0x0001 }, 8572407f5d6SJonathan Lemon { .name = "PPS2", .value = 0x0002 }, 8582407f5d6SJonathan Lemon { .name = "TS1", .value = 0x0004 }, 8592407f5d6SJonathan Lemon { .name = "TS2", .value = 0x0008 }, 8602407f5d6SJonathan Lemon { .name = "IRIG", .value = 0x0010 }, 8612407f5d6SJonathan Lemon { .name = "DCF", .value = 0x0020 }, 8620fa3ff7eSJonathan Lemon { .name = "TS3", .value = 0x0040 }, 8630fa3ff7eSJonathan Lemon { .name = "TS4", .value = 0x0080 }, 8642407f5d6SJonathan Lemon { .name = "FREQ1", .value = 0x0100 }, 8652407f5d6SJonathan Lemon { .name = "FREQ2", .value = 0x0200 }, 8662407f5d6SJonathan Lemon { .name = "FREQ3", .value = 0x0400 }, 8672407f5d6SJonathan Lemon { .name = "FREQ4", .value = 0x0800 }, 868b2c4f0acSJonathan Lemon { .name = "None", .value = SMA_DISABLE }, 869e1daf0ecSJonathan Lemon { } 870e1daf0ecSJonathan Lemon }; 871e1daf0ecSJonathan Lemon 8723f3fe41cSJonathan Lemon static const struct ocp_selector ptp_ocp_sma_out[] = { 873b325af3cSJonathan Lemon { .name = "10Mhz", .value = 0x0000 }, 874b325af3cSJonathan Lemon { .name = "PHC", .value = 0x0001 }, 875b325af3cSJonathan Lemon { .name = "MAC", .value = 0x0002 }, 876b325af3cSJonathan Lemon { .name = "GNSS1", .value = 0x0004 }, 877b325af3cSJonathan Lemon { .name = "GNSS2", .value = 0x0008 }, 878b325af3cSJonathan Lemon { .name = "IRIG", .value = 0x0010 }, 879b325af3cSJonathan Lemon { .name = "DCF", .value = 0x0020 }, 880b325af3cSJonathan Lemon { .name = "GEN1", .value = 0x0040 }, 881b325af3cSJonathan Lemon { .name = "GEN2", .value = 0x0080 }, 882b325af3cSJonathan Lemon { .name = "GEN3", .value = 0x0100 }, 883b325af3cSJonathan Lemon { .name = "GEN4", .value = 0x0200 }, 884cd09193fSJonathan Lemon { .name = "GND", .value = 0x2000 }, 885cd09193fSJonathan Lemon { .name = "VCC", .value = 0x4000 }, 886e1daf0ecSJonathan Lemon { } 887773bda96SJonathan Lemon }; 888773bda96SJonathan Lemon 88969dbe107SVadim Fedorenko static const struct ocp_selector ptp_ocp_art_sma_in[] = { 89069dbe107SVadim Fedorenko { .name = "PPS1", .value = 0x0001 }, 89169dbe107SVadim Fedorenko { .name = "10Mhz", .value = 0x0008 }, 89269dbe107SVadim Fedorenko { } 89369dbe107SVadim Fedorenko }; 89469dbe107SVadim Fedorenko 89569dbe107SVadim Fedorenko static const struct ocp_selector ptp_ocp_art_sma_out[] = { 89669dbe107SVadim Fedorenko { .name = "PHC", .value = 0x0002 }, 89769dbe107SVadim Fedorenko { .name = "GNSS", .value = 0x0004 }, 89869dbe107SVadim Fedorenko { .name = "10Mhz", .value = 0x0010 }, 89969dbe107SVadim Fedorenko { } 90069dbe107SVadim Fedorenko }; 90169dbe107SVadim Fedorenko 902caab82cdSJonathan Lemon struct ocp_sma_op { 903caab82cdSJonathan Lemon const struct ocp_selector *tbl[2]; 904ee4cd725SJonathan Lemon void (*init)(struct ptp_ocp *bp); 905caab82cdSJonathan Lemon u32 (*get)(struct ptp_ocp *bp, int sma_nr); 906caab82cdSJonathan Lemon int (*set_inputs)(struct ptp_ocp *bp, int sma_nr, u32 val); 907caab82cdSJonathan Lemon int (*set_output)(struct ptp_ocp *bp, int sma_nr, u32 val); 908aa56a7ffSJonathan Lemon }; 909aa56a7ffSJonathan Lemon 910ee4cd725SJonathan Lemon static void 911ee4cd725SJonathan Lemon ptp_ocp_sma_init(struct ptp_ocp *bp) 912ee4cd725SJonathan Lemon { 913ee4cd725SJonathan Lemon return bp->sma_op->init(bp); 914ee4cd725SJonathan Lemon } 915ee4cd725SJonathan Lemon 916caab82cdSJonathan Lemon static u32 917caab82cdSJonathan Lemon ptp_ocp_sma_get(struct ptp_ocp *bp, int sma_nr) 918caab82cdSJonathan Lemon { 919caab82cdSJonathan Lemon return bp->sma_op->get(bp, sma_nr); 920caab82cdSJonathan Lemon } 921caab82cdSJonathan Lemon 922caab82cdSJonathan Lemon static int 923caab82cdSJonathan Lemon ptp_ocp_sma_set_inputs(struct ptp_ocp *bp, int sma_nr, u32 val) 924caab82cdSJonathan Lemon { 925caab82cdSJonathan Lemon return bp->sma_op->set_inputs(bp, sma_nr, val); 926caab82cdSJonathan Lemon } 927caab82cdSJonathan Lemon 928caab82cdSJonathan Lemon static int 929caab82cdSJonathan Lemon ptp_ocp_sma_set_output(struct ptp_ocp *bp, int sma_nr, u32 val) 930caab82cdSJonathan Lemon { 931caab82cdSJonathan Lemon return bp->sma_op->set_output(bp, sma_nr, val); 932caab82cdSJonathan Lemon } 933caab82cdSJonathan Lemon 934773bda96SJonathan Lemon static const char * 9353f3fe41cSJonathan Lemon ptp_ocp_select_name_from_val(const struct ocp_selector *tbl, int val) 936773bda96SJonathan Lemon { 937773bda96SJonathan Lemon int i; 938773bda96SJonathan Lemon 939e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) 940e1daf0ecSJonathan Lemon if (tbl[i].value == val) 941e1daf0ecSJonathan Lemon return tbl[i].name; 942773bda96SJonathan Lemon return NULL; 943773bda96SJonathan Lemon } 944773bda96SJonathan Lemon 945773bda96SJonathan Lemon static int 9463f3fe41cSJonathan Lemon ptp_ocp_select_val_from_name(const struct ocp_selector *tbl, const char *name) 947773bda96SJonathan Lemon { 948e1daf0ecSJonathan Lemon const char *select; 949773bda96SJonathan Lemon int i; 950773bda96SJonathan Lemon 951e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) { 952e1daf0ecSJonathan Lemon select = tbl[i].name; 953e1daf0ecSJonathan Lemon if (!strncasecmp(name, select, strlen(select))) 954e1daf0ecSJonathan Lemon return tbl[i].value; 955773bda96SJonathan Lemon } 956773bda96SJonathan Lemon return -EINVAL; 957773bda96SJonathan Lemon } 958773bda96SJonathan Lemon 959e1daf0ecSJonathan Lemon static ssize_t 9603f3fe41cSJonathan Lemon ptp_ocp_select_table_show(const struct ocp_selector *tbl, char *buf) 961e1daf0ecSJonathan Lemon { 962e1daf0ecSJonathan Lemon ssize_t count; 963e1daf0ecSJonathan Lemon int i; 964e1daf0ecSJonathan Lemon 965e1daf0ecSJonathan Lemon count = 0; 966e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) 967e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", tbl[i].name); 968e1daf0ecSJonathan Lemon if (count) 969e1daf0ecSJonathan Lemon count--; 970e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "\n"); 971e1daf0ecSJonathan Lemon return count; 972e1daf0ecSJonathan Lemon } 973e1daf0ecSJonathan Lemon 974a7e1abadSJonathan Lemon static int 975a7e1abadSJonathan Lemon __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts, 976a7e1abadSJonathan Lemon struct ptp_system_timestamp *sts) 977a7e1abadSJonathan Lemon { 978a7e1abadSJonathan Lemon u32 ctrl, time_sec, time_ns; 979a7e1abadSJonathan Lemon int i; 980a7e1abadSJonathan Lemon 981a7e1abadSJonathan Lemon ptp_read_system_prets(sts); 9821acffc6eSJonathan Lemon 9831acffc6eSJonathan Lemon ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE; 984a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 985a7e1abadSJonathan Lemon 986a7e1abadSJonathan Lemon for (i = 0; i < 100; i++) { 987a7e1abadSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 988a7e1abadSJonathan Lemon if (ctrl & OCP_CTRL_READ_TIME_DONE) 989a7e1abadSJonathan Lemon break; 990a7e1abadSJonathan Lemon } 991a7e1abadSJonathan Lemon ptp_read_system_postts(sts); 992a7e1abadSJonathan Lemon 9931acffc6eSJonathan Lemon if (sts && bp->ts_window_adjust) { 9941acffc6eSJonathan Lemon s64 ns = timespec64_to_ns(&sts->post_ts); 9951acffc6eSJonathan Lemon 9961acffc6eSJonathan Lemon sts->post_ts = ns_to_timespec64(ns - bp->ts_window_adjust); 9971acffc6eSJonathan Lemon } 9981acffc6eSJonathan Lemon 999a7e1abadSJonathan Lemon time_ns = ioread32(&bp->reg->time_ns); 1000a7e1abadSJonathan Lemon time_sec = ioread32(&bp->reg->time_sec); 1001a7e1abadSJonathan Lemon 1002a7e1abadSJonathan Lemon ts->tv_sec = time_sec; 1003a7e1abadSJonathan Lemon ts->tv_nsec = time_ns; 1004a7e1abadSJonathan Lemon 1005a7e1abadSJonathan Lemon return ctrl & OCP_CTRL_READ_TIME_DONE ? 0 : -ETIMEDOUT; 1006a7e1abadSJonathan Lemon } 1007a7e1abadSJonathan Lemon 1008a7e1abadSJonathan Lemon static int 1009a7e1abadSJonathan Lemon ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts, 1010a7e1abadSJonathan Lemon struct ptp_system_timestamp *sts) 1011a7e1abadSJonathan Lemon { 1012a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 1013a7e1abadSJonathan Lemon unsigned long flags; 1014a7e1abadSJonathan Lemon int err; 1015a7e1abadSJonathan Lemon 1016a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1017a7e1abadSJonathan Lemon err = __ptp_ocp_gettime_locked(bp, ts, sts); 1018a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1019a7e1abadSJonathan Lemon 1020a7e1abadSJonathan Lemon return err; 1021a7e1abadSJonathan Lemon } 1022a7e1abadSJonathan Lemon 1023a7e1abadSJonathan Lemon static void 1024a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts) 1025a7e1abadSJonathan Lemon { 1026a7e1abadSJonathan Lemon u32 ctrl, time_sec, time_ns; 1027a7e1abadSJonathan Lemon u32 select; 1028a7e1abadSJonathan Lemon 1029a7e1abadSJonathan Lemon time_ns = ts->tv_nsec; 1030a7e1abadSJonathan Lemon time_sec = ts->tv_sec; 1031a7e1abadSJonathan Lemon 1032a7e1abadSJonathan Lemon select = ioread32(&bp->reg->select); 1033a7e1abadSJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 1034a7e1abadSJonathan Lemon 1035a7e1abadSJonathan Lemon iowrite32(time_ns, &bp->reg->adjust_ns); 1036a7e1abadSJonathan Lemon iowrite32(time_sec, &bp->reg->adjust_sec); 1037a7e1abadSJonathan Lemon 10381acffc6eSJonathan Lemon ctrl = OCP_CTRL_ADJUST_TIME | OCP_CTRL_ENABLE; 1039a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 1040a7e1abadSJonathan Lemon 1041a7e1abadSJonathan Lemon /* restore clock selection */ 1042a7e1abadSJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 1043a7e1abadSJonathan Lemon } 1044a7e1abadSJonathan Lemon 1045a7e1abadSJonathan Lemon static int 1046a7e1abadSJonathan Lemon ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts) 1047a7e1abadSJonathan Lemon { 1048a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 1049a7e1abadSJonathan Lemon unsigned long flags; 1050a7e1abadSJonathan Lemon 1051a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1052a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(bp, ts); 1053a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1054a7e1abadSJonathan Lemon 1055a7e1abadSJonathan Lemon return 0; 1056a7e1abadSJonathan Lemon } 1057a7e1abadSJonathan Lemon 10586d59d4faSJonathan Lemon static void 105990f8f4c0SJonathan Lemon __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val) 10606d59d4faSJonathan Lemon { 10616d59d4faSJonathan Lemon u32 select, ctrl; 10626d59d4faSJonathan Lemon 10636d59d4faSJonathan Lemon select = ioread32(&bp->reg->select); 10646d59d4faSJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 10656d59d4faSJonathan Lemon 10666d59d4faSJonathan Lemon iowrite32(adj_val, &bp->reg->offset_ns); 106790f8f4c0SJonathan Lemon iowrite32(NSEC_PER_SEC, &bp->reg->offset_window_ns); 10686d59d4faSJonathan Lemon 10696d59d4faSJonathan Lemon ctrl = OCP_CTRL_ADJUST_OFFSET | OCP_CTRL_ENABLE; 10706d59d4faSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 10716d59d4faSJonathan Lemon 10726d59d4faSJonathan Lemon /* restore clock selection */ 10736d59d4faSJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 10746d59d4faSJonathan Lemon } 10756d59d4faSJonathan Lemon 107690f8f4c0SJonathan Lemon static void 1077da2172a9SJonathan Lemon ptp_ocp_adjtime_coarse(struct ptp_ocp *bp, s64 delta_ns) 107890f8f4c0SJonathan Lemon { 107990f8f4c0SJonathan Lemon struct timespec64 ts; 108090f8f4c0SJonathan Lemon unsigned long flags; 108190f8f4c0SJonathan Lemon int err; 108290f8f4c0SJonathan Lemon 108390f8f4c0SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 108490f8f4c0SJonathan Lemon err = __ptp_ocp_gettime_locked(bp, &ts, NULL); 108590f8f4c0SJonathan Lemon if (likely(!err)) { 1086da2172a9SJonathan Lemon set_normalized_timespec64(&ts, ts.tv_sec, 1087da2172a9SJonathan Lemon ts.tv_nsec + delta_ns); 108890f8f4c0SJonathan Lemon __ptp_ocp_settime_locked(bp, &ts); 108990f8f4c0SJonathan Lemon } 109090f8f4c0SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 109190f8f4c0SJonathan Lemon } 109290f8f4c0SJonathan Lemon 1093a7e1abadSJonathan Lemon static int 1094a7e1abadSJonathan Lemon ptp_ocp_adjtime(struct ptp_clock_info *ptp_info, s64 delta_ns) 1095a7e1abadSJonathan Lemon { 1096a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 1097a7e1abadSJonathan Lemon unsigned long flags; 10986d59d4faSJonathan Lemon u32 adj_ns, sign; 1099a7e1abadSJonathan Lemon 110090f8f4c0SJonathan Lemon if (delta_ns > NSEC_PER_SEC || -delta_ns > NSEC_PER_SEC) { 110190f8f4c0SJonathan Lemon ptp_ocp_adjtime_coarse(bp, delta_ns); 110290f8f4c0SJonathan Lemon return 0; 110390f8f4c0SJonathan Lemon } 110490f8f4c0SJonathan Lemon 11056d59d4faSJonathan Lemon sign = delta_ns < 0 ? BIT(31) : 0; 11066d59d4faSJonathan Lemon adj_ns = sign ? -delta_ns : delta_ns; 1107a7e1abadSJonathan Lemon 1108a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 11096d59d4faSJonathan Lemon __ptp_ocp_adjtime_locked(bp, sign | adj_ns); 1110a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1111a7e1abadSJonathan Lemon 11126d59d4faSJonathan Lemon return 0; 1113a7e1abadSJonathan Lemon } 1114a7e1abadSJonathan Lemon 1115a7e1abadSJonathan Lemon static int 1116a7e1abadSJonathan Lemon ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm) 1117a7e1abadSJonathan Lemon { 1118a7e1abadSJonathan Lemon if (scaled_ppm == 0) 1119a7e1abadSJonathan Lemon return 0; 1120a7e1abadSJonathan Lemon 1121a7e1abadSJonathan Lemon return -EOPNOTSUPP; 1122a7e1abadSJonathan Lemon } 1123a7e1abadSJonathan Lemon 1124773bda96SJonathan Lemon static int 11256d59d4faSJonathan Lemon ptp_ocp_null_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns) 1126773bda96SJonathan Lemon { 1127773bda96SJonathan Lemon return -EOPNOTSUPP; 1128773bda96SJonathan Lemon } 1129773bda96SJonathan Lemon 1130773bda96SJonathan Lemon static int 1131773bda96SJonathan Lemon ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq, 1132773bda96SJonathan Lemon int on) 1133773bda96SJonathan Lemon { 1134773bda96SJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 1135773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = NULL; 1136a62a56d0SJonathan Lemon u32 req; 1137773bda96SJonathan Lemon int err; 1138773bda96SJonathan Lemon 1139773bda96SJonathan Lemon switch (rq->type) { 1140773bda96SJonathan Lemon case PTP_CLK_REQ_EXTTS: 1141a62a56d0SJonathan Lemon req = OCP_REQ_TIMESTAMP; 1142773bda96SJonathan Lemon switch (rq->extts.index) { 1143773bda96SJonathan Lemon case 0: 1144773bda96SJonathan Lemon ext = bp->ts0; 1145773bda96SJonathan Lemon break; 1146773bda96SJonathan Lemon case 1: 1147773bda96SJonathan Lemon ext = bp->ts1; 1148773bda96SJonathan Lemon break; 1149dcf61469SJonathan Lemon case 2: 1150dcf61469SJonathan Lemon ext = bp->ts2; 1151dcf61469SJonathan Lemon break; 1152a62a56d0SJonathan Lemon case 3: 11530fa3ff7eSJonathan Lemon ext = bp->ts3; 11540fa3ff7eSJonathan Lemon break; 11550fa3ff7eSJonathan Lemon case 4: 11560fa3ff7eSJonathan Lemon ext = bp->ts4; 11570fa3ff7eSJonathan Lemon break; 11580fa3ff7eSJonathan Lemon case 5: 1159a62a56d0SJonathan Lemon ext = bp->pps; 1160a62a56d0SJonathan Lemon break; 1161773bda96SJonathan Lemon } 1162773bda96SJonathan Lemon break; 1163773bda96SJonathan Lemon case PTP_CLK_REQ_PPS: 1164a62a56d0SJonathan Lemon req = OCP_REQ_PPS; 1165773bda96SJonathan Lemon ext = bp->pps; 1166773bda96SJonathan Lemon break; 1167a62a56d0SJonathan Lemon case PTP_CLK_REQ_PEROUT: 11681aa66a3aSJonathan Lemon switch (rq->perout.index) { 11691aa66a3aSJonathan Lemon case 0: 1170a62a56d0SJonathan Lemon /* This is a request for 1PPS on an output SMA. 1171a62a56d0SJonathan Lemon * Allow, but assume manual configuration. 1172a62a56d0SJonathan Lemon */ 11731aa66a3aSJonathan Lemon if (on && (rq->perout.period.sec != 1 || 11741aa66a3aSJonathan Lemon rq->perout.period.nsec != 0)) 11751aa66a3aSJonathan Lemon return -EINVAL; 1176a62a56d0SJonathan Lemon return 0; 11771aa66a3aSJonathan Lemon case 1: 11781aa66a3aSJonathan Lemon case 2: 11791aa66a3aSJonathan Lemon case 3: 11801aa66a3aSJonathan Lemon case 4: 11811aa66a3aSJonathan Lemon req = rq->perout.index - 1; 11821aa66a3aSJonathan Lemon ext = bp->signal_out[req]; 11831aa66a3aSJonathan Lemon err = ptp_ocp_signal_from_perout(bp, req, &rq->perout); 11841aa66a3aSJonathan Lemon if (err) 11851aa66a3aSJonathan Lemon return err; 11861aa66a3aSJonathan Lemon break; 11871aa66a3aSJonathan Lemon } 11881aa66a3aSJonathan Lemon break; 1189773bda96SJonathan Lemon default: 1190773bda96SJonathan Lemon return -EOPNOTSUPP; 1191773bda96SJonathan Lemon } 1192773bda96SJonathan Lemon 1193773bda96SJonathan Lemon err = -ENXIO; 1194773bda96SJonathan Lemon if (ext) 1195a62a56d0SJonathan Lemon err = ext->info->enable(ext, req, on); 1196773bda96SJonathan Lemon 1197773bda96SJonathan Lemon return err; 1198773bda96SJonathan Lemon } 1199773bda96SJonathan Lemon 12001aa66a3aSJonathan Lemon static int 12011aa66a3aSJonathan Lemon ptp_ocp_verify(struct ptp_clock_info *ptp_info, unsigned pin, 12021aa66a3aSJonathan Lemon enum ptp_pin_function func, unsigned chan) 12031aa66a3aSJonathan Lemon { 12041aa66a3aSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 12051aa66a3aSJonathan Lemon char buf[16]; 12061aa66a3aSJonathan Lemon 120705fc65f3SJonathan Lemon switch (func) { 120805fc65f3SJonathan Lemon case PTP_PF_NONE: 1209d5f497b8SDan Carpenter snprintf(buf, sizeof(buf), "IN: None"); 121005fc65f3SJonathan Lemon break; 121105fc65f3SJonathan Lemon case PTP_PF_EXTTS: 121205fc65f3SJonathan Lemon /* Allow timestamps, but require sysfs configuration. */ 121305fc65f3SJonathan Lemon return 0; 121405fc65f3SJonathan Lemon case PTP_PF_PEROUT: 121505fc65f3SJonathan Lemon /* channel 0 is 1PPS from PHC. 121605fc65f3SJonathan Lemon * channels 1..4 are the frequency generators. 121705fc65f3SJonathan Lemon */ 12181aa66a3aSJonathan Lemon if (chan) 1219d5f497b8SDan Carpenter snprintf(buf, sizeof(buf), "OUT: GEN%d", chan); 12201aa66a3aSJonathan Lemon else 1221d5f497b8SDan Carpenter snprintf(buf, sizeof(buf), "OUT: PHC"); 122205fc65f3SJonathan Lemon break; 122305fc65f3SJonathan Lemon default: 122405fc65f3SJonathan Lemon return -EOPNOTSUPP; 122505fc65f3SJonathan Lemon } 12261aa66a3aSJonathan Lemon 12271aa66a3aSJonathan Lemon return ptp_ocp_sma_store(bp, buf, pin + 1); 12281aa66a3aSJonathan Lemon } 12291aa66a3aSJonathan Lemon 1230a7e1abadSJonathan Lemon static const struct ptp_clock_info ptp_ocp_clock_info = { 1231a7e1abadSJonathan Lemon .owner = THIS_MODULE, 1232a7e1abadSJonathan Lemon .name = KBUILD_MODNAME, 1233a7e1abadSJonathan Lemon .max_adj = 100000000, 1234a7e1abadSJonathan Lemon .gettimex64 = ptp_ocp_gettimex, 1235a7e1abadSJonathan Lemon .settime64 = ptp_ocp_settime, 1236a7e1abadSJonathan Lemon .adjtime = ptp_ocp_adjtime, 1237a7e1abadSJonathan Lemon .adjfine = ptp_ocp_null_adjfine, 12386d59d4faSJonathan Lemon .adjphase = ptp_ocp_null_adjphase, 1239773bda96SJonathan Lemon .enable = ptp_ocp_enable, 12401aa66a3aSJonathan Lemon .verify = ptp_ocp_verify, 1241773bda96SJonathan Lemon .pps = true, 12420fa3ff7eSJonathan Lemon .n_ext_ts = 6, 12431aa66a3aSJonathan Lemon .n_per_out = 5, 1244a7e1abadSJonathan Lemon }; 1245a7e1abadSJonathan Lemon 1246773bda96SJonathan Lemon static void 1247773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp) 1248773bda96SJonathan Lemon { 1249773bda96SJonathan Lemon u32 ctrl, select; 1250773bda96SJonathan Lemon 1251773bda96SJonathan Lemon select = ioread32(&bp->reg->select); 1252773bda96SJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 1253773bda96SJonathan Lemon 1254773bda96SJonathan Lemon iowrite32(0, &bp->reg->drift_ns); 1255773bda96SJonathan Lemon 12561acffc6eSJonathan Lemon ctrl = OCP_CTRL_ADJUST_DRIFT | OCP_CTRL_ENABLE; 1257773bda96SJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 1258773bda96SJonathan Lemon 1259773bda96SJonathan Lemon /* restore clock selection */ 1260773bda96SJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 1261773bda96SJonathan Lemon } 1262773bda96SJonathan Lemon 1263773bda96SJonathan Lemon static void 1264e68462a0SVadim Fedorenko ptp_ocp_utc_distribute(struct ptp_ocp *bp, u32 val) 1265e68462a0SVadim Fedorenko { 1266e68462a0SVadim Fedorenko unsigned long flags; 1267e68462a0SVadim Fedorenko 1268e68462a0SVadim Fedorenko spin_lock_irqsave(&bp->lock, flags); 1269e68462a0SVadim Fedorenko 1270e68462a0SVadim Fedorenko bp->utc_tai_offset = val; 1271e68462a0SVadim Fedorenko 1272e68462a0SVadim Fedorenko if (bp->irig_out) 1273e68462a0SVadim Fedorenko iowrite32(val, &bp->irig_out->adj_sec); 1274e68462a0SVadim Fedorenko if (bp->dcf_out) 1275e68462a0SVadim Fedorenko iowrite32(val, &bp->dcf_out->adj_sec); 1276e68462a0SVadim Fedorenko if (bp->nmea_out) 1277e68462a0SVadim Fedorenko iowrite32(val, &bp->nmea_out->adj_sec); 1278e68462a0SVadim Fedorenko 1279e68462a0SVadim Fedorenko spin_unlock_irqrestore(&bp->lock, flags); 1280e68462a0SVadim Fedorenko } 1281e68462a0SVadim Fedorenko 1282e68462a0SVadim Fedorenko static void 1283773bda96SJonathan Lemon ptp_ocp_watchdog(struct timer_list *t) 1284773bda96SJonathan Lemon { 1285773bda96SJonathan Lemon struct ptp_ocp *bp = from_timer(bp, t, watchdog); 1286773bda96SJonathan Lemon unsigned long flags; 1287e68462a0SVadim Fedorenko u32 status, utc_offset; 1288773bda96SJonathan Lemon 12890d43d4f2SJonathan Lemon status = ioread32(&bp->pps_to_clk->status); 1290773bda96SJonathan Lemon 1291773bda96SJonathan Lemon if (status & PPS_STATUS_SUPERV_ERR) { 12920d43d4f2SJonathan Lemon iowrite32(status, &bp->pps_to_clk->status); 1293ef0cfb34SJonathan Lemon if (!bp->gnss_lost) { 1294773bda96SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1295773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(bp); 1296773bda96SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1297ef0cfb34SJonathan Lemon bp->gnss_lost = ktime_get_real_seconds(); 1298773bda96SJonathan Lemon } 1299773bda96SJonathan Lemon 1300ef0cfb34SJonathan Lemon } else if (bp->gnss_lost) { 1301ef0cfb34SJonathan Lemon bp->gnss_lost = 0; 1302773bda96SJonathan Lemon } 1303773bda96SJonathan Lemon 1304e68462a0SVadim Fedorenko /* if GNSS provides correct data we can rely on 1305e68462a0SVadim Fedorenko * it to get leap second information 1306e68462a0SVadim Fedorenko */ 1307e68462a0SVadim Fedorenko if (bp->tod) { 1308e68462a0SVadim Fedorenko status = ioread32(&bp->tod->utc_status); 1309e68462a0SVadim Fedorenko utc_offset = status & TOD_STATUS_UTC_MASK; 1310e68462a0SVadim Fedorenko if (status & TOD_STATUS_UTC_VALID && 1311e68462a0SVadim Fedorenko utc_offset != bp->utc_tai_offset) 1312e68462a0SVadim Fedorenko ptp_ocp_utc_distribute(bp, utc_offset); 1313e68462a0SVadim Fedorenko } 1314e68462a0SVadim Fedorenko 1315773bda96SJonathan Lemon mod_timer(&bp->watchdog, jiffies + HZ); 1316773bda96SJonathan Lemon } 1317773bda96SJonathan Lemon 13181acffc6eSJonathan Lemon static void 13191acffc6eSJonathan Lemon ptp_ocp_estimate_pci_timing(struct ptp_ocp *bp) 13201acffc6eSJonathan Lemon { 13211acffc6eSJonathan Lemon ktime_t start, end; 13221acffc6eSJonathan Lemon ktime_t delay; 13231acffc6eSJonathan Lemon u32 ctrl; 13241acffc6eSJonathan Lemon 13251acffc6eSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 13261acffc6eSJonathan Lemon ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE; 13271acffc6eSJonathan Lemon 13281acffc6eSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 13291acffc6eSJonathan Lemon 13301acffc6eSJonathan Lemon start = ktime_get_ns(); 13311acffc6eSJonathan Lemon 13321acffc6eSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 13331acffc6eSJonathan Lemon 13341acffc6eSJonathan Lemon end = ktime_get_ns(); 13351acffc6eSJonathan Lemon 13361acffc6eSJonathan Lemon delay = end - start; 13371acffc6eSJonathan Lemon bp->ts_window_adjust = (delay >> 5) * 3; 13381acffc6eSJonathan Lemon } 13391acffc6eSJonathan Lemon 1340a7e1abadSJonathan Lemon static int 1341773bda96SJonathan Lemon ptp_ocp_init_clock(struct ptp_ocp *bp) 1342a7e1abadSJonathan Lemon { 1343a7e1abadSJonathan Lemon struct timespec64 ts; 1344a7e1abadSJonathan Lemon bool sync; 1345a7e1abadSJonathan Lemon u32 ctrl; 1346a7e1abadSJonathan Lemon 13471acffc6eSJonathan Lemon ctrl = OCP_CTRL_ENABLE; 1348a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 1349a7e1abadSJonathan Lemon 1350773bda96SJonathan Lemon /* NO DRIFT Correction */ 1351773bda96SJonathan Lemon /* offset_p:i 1/8, offset_i: 1/16, drift_p: 0, drift_i: 0 */ 1352773bda96SJonathan Lemon iowrite32(0x2000, &bp->reg->servo_offset_p); 1353773bda96SJonathan Lemon iowrite32(0x1000, &bp->reg->servo_offset_i); 1354773bda96SJonathan Lemon iowrite32(0, &bp->reg->servo_drift_p); 1355773bda96SJonathan Lemon iowrite32(0, &bp->reg->servo_drift_i); 1356773bda96SJonathan Lemon 1357773bda96SJonathan Lemon /* latch servo values */ 1358773bda96SJonathan Lemon ctrl |= OCP_CTRL_ADJUST_SERVO; 1359773bda96SJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 1360773bda96SJonathan Lemon 1361a7e1abadSJonathan Lemon if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) { 1362a7e1abadSJonathan Lemon dev_err(&bp->pdev->dev, "clock not enabled\n"); 1363a7e1abadSJonathan Lemon return -ENODEV; 1364a7e1abadSJonathan Lemon } 1365a7e1abadSJonathan Lemon 13661acffc6eSJonathan Lemon ptp_ocp_estimate_pci_timing(bp); 13671acffc6eSJonathan Lemon 1368a7e1abadSJonathan Lemon sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC; 1369a7e1abadSJonathan Lemon if (!sync) { 1370065efcc5SJonathan Lemon ktime_get_clocktai_ts64(&ts); 1371a7e1abadSJonathan Lemon ptp_ocp_settime(&bp->ptp_info, &ts); 1372a7e1abadSJonathan Lemon } 1373a7e1abadSJonathan Lemon 1374065efcc5SJonathan Lemon /* If there is a clock supervisor, then enable the watchdog */ 1375065efcc5SJonathan Lemon if (bp->pps_to_clk) { 1376773bda96SJonathan Lemon timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0); 1377773bda96SJonathan Lemon mod_timer(&bp->watchdog, jiffies + HZ); 1378065efcc5SJonathan Lemon } 1379773bda96SJonathan Lemon 1380a7e1abadSJonathan Lemon return 0; 1381a7e1abadSJonathan Lemon } 1382a7e1abadSJonathan Lemon 1383a7e1abadSJonathan Lemon static void 1384065efcc5SJonathan Lemon ptp_ocp_tod_init(struct ptp_ocp *bp) 1385065efcc5SJonathan Lemon { 1386065efcc5SJonathan Lemon u32 ctrl, reg; 1387065efcc5SJonathan Lemon 1388065efcc5SJonathan Lemon ctrl = ioread32(&bp->tod->ctrl); 1389065efcc5SJonathan Lemon ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE; 1390065efcc5SJonathan Lemon ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B); 1391065efcc5SJonathan Lemon iowrite32(ctrl, &bp->tod->ctrl); 1392065efcc5SJonathan Lemon 1393065efcc5SJonathan Lemon reg = ioread32(&bp->tod->utc_status); 1394065efcc5SJonathan Lemon if (reg & TOD_STATUS_UTC_VALID) 1395065efcc5SJonathan Lemon ptp_ocp_utc_distribute(bp, reg & TOD_STATUS_UTC_MASK); 1396065efcc5SJonathan Lemon } 1397065efcc5SJonathan Lemon 13989f492c4cSVadim Fedorenko static const char * 13999f492c4cSVadim Fedorenko ptp_ocp_tod_proto_name(const int idx) 1400a7e1abadSJonathan Lemon { 1401a7e1abadSJonathan Lemon static const char * const proto_name[] = { 1402a7e1abadSJonathan Lemon "NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none", 1403a7e1abadSJonathan Lemon "UBX", "UBX_UTC", "UBX_LS", "UBX_none" 1404a7e1abadSJonathan Lemon }; 14059f492c4cSVadim Fedorenko return proto_name[idx]; 14069f492c4cSVadim Fedorenko } 14079f492c4cSVadim Fedorenko 14089f492c4cSVadim Fedorenko static const char * 14099f492c4cSVadim Fedorenko ptp_ocp_tod_gnss_name(int idx) 14109f492c4cSVadim Fedorenko { 1411a7e1abadSJonathan Lemon static const char * const gnss_name[] = { 1412a7e1abadSJonathan Lemon "ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU", 14139f492c4cSVadim Fedorenko "Unknown" 1414a7e1abadSJonathan Lemon }; 141572f00505SDan Carpenter if (idx >= ARRAY_SIZE(gnss_name)) 14169f492c4cSVadim Fedorenko idx = ARRAY_SIZE(gnss_name) - 1; 14179f492c4cSVadim Fedorenko return gnss_name[idx]; 1418a7e1abadSJonathan Lemon } 1419a7e1abadSJonathan Lemon 14200cfcdd1eSJonathan Lemon struct ptp_ocp_nvmem_match_info { 14210cfcdd1eSJonathan Lemon struct ptp_ocp *bp; 14220cfcdd1eSJonathan Lemon const void * const tag; 1423773bda96SJonathan Lemon }; 1424773bda96SJonathan Lemon 14250cfcdd1eSJonathan Lemon static int 14260cfcdd1eSJonathan Lemon ptp_ocp_nvmem_match(struct device *dev, const void *data) 14270cfcdd1eSJonathan Lemon { 14280cfcdd1eSJonathan Lemon const struct ptp_ocp_nvmem_match_info *info = data; 14290cfcdd1eSJonathan Lemon 14300cfcdd1eSJonathan Lemon dev = dev->parent; 14310cfcdd1eSJonathan Lemon if (!i2c_verify_client(dev) || info->tag != dev->platform_data) 14320cfcdd1eSJonathan Lemon return 0; 14330cfcdd1eSJonathan Lemon 14340cfcdd1eSJonathan Lemon while ((dev = dev->parent)) 14350cfcdd1eSJonathan Lemon if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME)) 14360cfcdd1eSJonathan Lemon return info->bp == dev_get_drvdata(dev); 1437773bda96SJonathan Lemon return 0; 1438773bda96SJonathan Lemon } 1439773bda96SJonathan Lemon 14400cfcdd1eSJonathan Lemon static inline struct nvmem_device * 14410cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_get(struct ptp_ocp *bp, const void * const tag) 1442773bda96SJonathan Lemon { 14430cfcdd1eSJonathan Lemon struct ptp_ocp_nvmem_match_info info = { .bp = bp, .tag = tag }; 14440cfcdd1eSJonathan Lemon 14450cfcdd1eSJonathan Lemon return nvmem_device_find(&info, ptp_ocp_nvmem_match); 14460cfcdd1eSJonathan Lemon } 14470cfcdd1eSJonathan Lemon 14480cfcdd1eSJonathan Lemon static inline void 14490cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_put(struct nvmem_device **nvmemp) 14500cfcdd1eSJonathan Lemon { 14518f0588e8SJonathan Lemon if (!IS_ERR_OR_NULL(*nvmemp)) 14520cfcdd1eSJonathan Lemon nvmem_device_put(*nvmemp); 14530cfcdd1eSJonathan Lemon *nvmemp = NULL; 14540cfcdd1eSJonathan Lemon } 14550cfcdd1eSJonathan Lemon 14560cfcdd1eSJonathan Lemon static void 14570cfcdd1eSJonathan Lemon ptp_ocp_read_eeprom(struct ptp_ocp *bp) 14580cfcdd1eSJonathan Lemon { 14590cfcdd1eSJonathan Lemon const struct ptp_ocp_eeprom_map *map; 14600cfcdd1eSJonathan Lemon struct nvmem_device *nvmem; 14610cfcdd1eSJonathan Lemon const void *tag; 14620cfcdd1eSJonathan Lemon int ret; 1463773bda96SJonathan Lemon 14641447149dSJonathan Lemon if (!bp->i2c_ctrl) 14651447149dSJonathan Lemon return; 14661447149dSJonathan Lemon 14670cfcdd1eSJonathan Lemon tag = NULL; 14680cfcdd1eSJonathan Lemon nvmem = NULL; 1469773bda96SJonathan Lemon 14700cfcdd1eSJonathan Lemon for (map = bp->eeprom_map; map->len; map++) { 14710cfcdd1eSJonathan Lemon if (map->tag != tag) { 14720cfcdd1eSJonathan Lemon tag = map->tag; 14730cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_put(&nvmem); 14740cfcdd1eSJonathan Lemon } 14750cfcdd1eSJonathan Lemon if (!nvmem) { 14760cfcdd1eSJonathan Lemon nvmem = ptp_ocp_nvmem_device_get(bp, tag); 14778f0588e8SJonathan Lemon if (IS_ERR(nvmem)) { 14788f0588e8SJonathan Lemon ret = PTR_ERR(nvmem); 14798f0588e8SJonathan Lemon goto fail; 14808f0588e8SJonathan Lemon } 1481773bda96SJonathan Lemon } 14820cfcdd1eSJonathan Lemon ret = nvmem_device_read(nvmem, map->off, map->len, 14830cfcdd1eSJonathan Lemon BP_MAP_ENTRY_ADDR(bp, map)); 14840cfcdd1eSJonathan Lemon if (ret != map->len) 14858f0588e8SJonathan Lemon goto fail; 1486773bda96SJonathan Lemon } 1487773bda96SJonathan Lemon 14880cfcdd1eSJonathan Lemon bp->has_eeprom_data = true; 1489773bda96SJonathan Lemon 1490773bda96SJonathan Lemon out: 14910cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_put(&nvmem); 14920cfcdd1eSJonathan Lemon return; 14930cfcdd1eSJonathan Lemon 14948f0588e8SJonathan Lemon fail: 14950cfcdd1eSJonathan Lemon dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", ret); 14960cfcdd1eSJonathan Lemon goto out; 14970cfcdd1eSJonathan Lemon } 14980cfcdd1eSJonathan Lemon 1499773bda96SJonathan Lemon static struct device * 1500773bda96SJonathan Lemon ptp_ocp_find_flash(struct ptp_ocp *bp) 1501773bda96SJonathan Lemon { 1502773bda96SJonathan Lemon struct device *dev, *last; 1503773bda96SJonathan Lemon 1504773bda96SJonathan Lemon last = NULL; 1505773bda96SJonathan Lemon dev = &bp->spi_flash->dev; 1506773bda96SJonathan Lemon 1507304843c7SAndy Shevchenko while ((dev = device_find_any_child(dev))) { 1508773bda96SJonathan Lemon if (!strcmp("mtd", dev_bus_name(dev))) 1509773bda96SJonathan Lemon break; 1510773bda96SJonathan Lemon put_device(last); 1511773bda96SJonathan Lemon last = dev; 1512773bda96SJonathan Lemon } 1513773bda96SJonathan Lemon put_device(last); 1514773bda96SJonathan Lemon 1515773bda96SJonathan Lemon return dev; 1516773bda96SJonathan Lemon } 1517773bda96SJonathan Lemon 1518773bda96SJonathan Lemon static int 15193c3673bdSVadim Fedorenko ptp_ocp_devlink_fw_image(struct devlink *devlink, const struct firmware *fw, 15203c3673bdSVadim Fedorenko const u8 **data, size_t *size) 15213c3673bdSVadim Fedorenko { 15223c3673bdSVadim Fedorenko struct ptp_ocp *bp = devlink_priv(devlink); 15233c3673bdSVadim Fedorenko const struct ptp_ocp_firmware_header *hdr; 15243c3673bdSVadim Fedorenko size_t offset, length; 15253c3673bdSVadim Fedorenko u16 crc; 15263c3673bdSVadim Fedorenko 15273c3673bdSVadim Fedorenko hdr = (const struct ptp_ocp_firmware_header *)fw->data; 15283c3673bdSVadim Fedorenko if (memcmp(hdr->magic, OCP_FIRMWARE_MAGIC_HEADER, 4)) { 15293c3673bdSVadim Fedorenko devlink_flash_update_status_notify(devlink, 15303c3673bdSVadim Fedorenko "No firmware header found, flashing raw image", 15313c3673bdSVadim Fedorenko NULL, 0, 0); 15323c3673bdSVadim Fedorenko offset = 0; 15333c3673bdSVadim Fedorenko length = fw->size; 15343c3673bdSVadim Fedorenko goto out; 15353c3673bdSVadim Fedorenko } 15363c3673bdSVadim Fedorenko 15373c3673bdSVadim Fedorenko if (be16_to_cpu(hdr->pci_vendor_id) != bp->pdev->vendor || 15383c3673bdSVadim Fedorenko be16_to_cpu(hdr->pci_device_id) != bp->pdev->device) { 15393c3673bdSVadim Fedorenko devlink_flash_update_status_notify(devlink, 15403c3673bdSVadim Fedorenko "Firmware image compatibility check failed", 15413c3673bdSVadim Fedorenko NULL, 0, 0); 15423c3673bdSVadim Fedorenko return -EINVAL; 15433c3673bdSVadim Fedorenko } 15443c3673bdSVadim Fedorenko 15453c3673bdSVadim Fedorenko offset = sizeof(*hdr); 15463c3673bdSVadim Fedorenko length = be32_to_cpu(hdr->image_size); 15473c3673bdSVadim Fedorenko if (length != (fw->size - offset)) { 15483c3673bdSVadim Fedorenko devlink_flash_update_status_notify(devlink, 15493c3673bdSVadim Fedorenko "Firmware image size check failed", 15503c3673bdSVadim Fedorenko NULL, 0, 0); 15513c3673bdSVadim Fedorenko return -EINVAL; 15523c3673bdSVadim Fedorenko } 15533c3673bdSVadim Fedorenko 15543c3673bdSVadim Fedorenko crc = crc16(0xffff, &fw->data[offset], length); 15553c3673bdSVadim Fedorenko if (be16_to_cpu(hdr->crc) != crc) { 15563c3673bdSVadim Fedorenko devlink_flash_update_status_notify(devlink, 15573c3673bdSVadim Fedorenko "Firmware image CRC check failed", 15583c3673bdSVadim Fedorenko NULL, 0, 0); 15593c3673bdSVadim Fedorenko return -EINVAL; 15603c3673bdSVadim Fedorenko } 15613c3673bdSVadim Fedorenko 15623c3673bdSVadim Fedorenko out: 15633c3673bdSVadim Fedorenko *data = &fw->data[offset]; 15643c3673bdSVadim Fedorenko *size = length; 15653c3673bdSVadim Fedorenko 15663c3673bdSVadim Fedorenko return 0; 15673c3673bdSVadim Fedorenko } 15683c3673bdSVadim Fedorenko 15693c3673bdSVadim Fedorenko static int 1570773bda96SJonathan Lemon ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev, 1571773bda96SJonathan Lemon const struct firmware *fw) 1572773bda96SJonathan Lemon { 1573773bda96SJonathan Lemon struct mtd_info *mtd = dev_get_drvdata(dev); 1574773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 15753c3673bdSVadim Fedorenko size_t off, len, size, resid, wrote; 1576773bda96SJonathan Lemon struct erase_info erase; 1577773bda96SJonathan Lemon size_t base, blksz; 15783c3673bdSVadim Fedorenko const u8 *data; 15793c3673bdSVadim Fedorenko int err; 15803c3673bdSVadim Fedorenko 15813c3673bdSVadim Fedorenko err = ptp_ocp_devlink_fw_image(devlink, fw, &data, &size); 15823c3673bdSVadim Fedorenko if (err) 15833c3673bdSVadim Fedorenko goto out; 1584773bda96SJonathan Lemon 1585773bda96SJonathan Lemon off = 0; 1586773bda96SJonathan Lemon base = bp->flash_start; 1587773bda96SJonathan Lemon blksz = 4096; 15883c3673bdSVadim Fedorenko resid = size; 1589773bda96SJonathan Lemon 1590773bda96SJonathan Lemon while (resid) { 1591773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, "Flashing", 15923c3673bdSVadim Fedorenko NULL, off, size); 1593773bda96SJonathan Lemon 1594773bda96SJonathan Lemon len = min_t(size_t, resid, blksz); 1595773bda96SJonathan Lemon erase.addr = base + off; 1596773bda96SJonathan Lemon erase.len = blksz; 1597773bda96SJonathan Lemon 1598773bda96SJonathan Lemon err = mtd_erase(mtd, &erase); 1599773bda96SJonathan Lemon if (err) 1600773bda96SJonathan Lemon goto out; 1601773bda96SJonathan Lemon 16023c3673bdSVadim Fedorenko err = mtd_write(mtd, base + off, len, &wrote, data + off); 1603773bda96SJonathan Lemon if (err) 1604773bda96SJonathan Lemon goto out; 1605773bda96SJonathan Lemon 1606773bda96SJonathan Lemon off += blksz; 1607773bda96SJonathan Lemon resid -= len; 1608773bda96SJonathan Lemon } 1609773bda96SJonathan Lemon out: 1610773bda96SJonathan Lemon return err; 1611773bda96SJonathan Lemon } 1612773bda96SJonathan Lemon 1613773bda96SJonathan Lemon static int 1614773bda96SJonathan Lemon ptp_ocp_devlink_flash_update(struct devlink *devlink, 1615773bda96SJonathan Lemon struct devlink_flash_update_params *params, 1616773bda96SJonathan Lemon struct netlink_ext_ack *extack) 1617773bda96SJonathan Lemon { 1618773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 1619773bda96SJonathan Lemon struct device *dev; 1620773bda96SJonathan Lemon const char *msg; 1621773bda96SJonathan Lemon int err; 1622773bda96SJonathan Lemon 1623773bda96SJonathan Lemon dev = ptp_ocp_find_flash(bp); 1624773bda96SJonathan Lemon if (!dev) { 1625773bda96SJonathan Lemon dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n"); 1626773bda96SJonathan Lemon return -ENODEV; 1627773bda96SJonathan Lemon } 1628773bda96SJonathan Lemon 1629773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, "Preparing to flash", 1630773bda96SJonathan Lemon NULL, 0, 0); 1631773bda96SJonathan Lemon 1632773bda96SJonathan Lemon err = ptp_ocp_devlink_flash(devlink, dev, params->fw); 1633773bda96SJonathan Lemon 1634773bda96SJonathan Lemon msg = err ? "Flash error" : "Flash complete"; 1635773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0); 1636773bda96SJonathan Lemon 1637773bda96SJonathan Lemon put_device(dev); 1638773bda96SJonathan Lemon return err; 1639773bda96SJonathan Lemon } 1640773bda96SJonathan Lemon 1641773bda96SJonathan Lemon static int 1642773bda96SJonathan Lemon ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req, 1643773bda96SJonathan Lemon struct netlink_ext_ack *extack) 1644773bda96SJonathan Lemon { 1645773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 16465a728ac5SJonathan Lemon const char *fw_image; 1647773bda96SJonathan Lemon char buf[32]; 1648773bda96SJonathan Lemon int err; 1649773bda96SJonathan Lemon 1650773bda96SJonathan Lemon err = devlink_info_driver_name_put(req, KBUILD_MODNAME); 1651773bda96SJonathan Lemon if (err) 1652773bda96SJonathan Lemon return err; 1653773bda96SJonathan Lemon 16545a728ac5SJonathan Lemon fw_image = bp->fw_loader ? "loader" : "fw"; 16555a728ac5SJonathan Lemon sprintf(buf, "%d.%d", bp->fw_tag, bp->fw_version); 16565a728ac5SJonathan Lemon err = devlink_info_version_running_put(req, fw_image, buf); 1657773bda96SJonathan Lemon if (err) 1658773bda96SJonathan Lemon return err; 1659773bda96SJonathan Lemon 16600cfcdd1eSJonathan Lemon if (!bp->has_eeprom_data) { 16610cfcdd1eSJonathan Lemon ptp_ocp_read_eeprom(bp); 16620cfcdd1eSJonathan Lemon if (!bp->has_eeprom_data) 16630cfcdd1eSJonathan Lemon return 0; 16640cfcdd1eSJonathan Lemon } 1665773bda96SJonathan Lemon 1666773bda96SJonathan Lemon sprintf(buf, "%pM", bp->serial); 1667773bda96SJonathan Lemon err = devlink_info_serial_number_put(req, buf); 1668773bda96SJonathan Lemon if (err) 1669773bda96SJonathan Lemon return err; 16700cfcdd1eSJonathan Lemon 16710cfcdd1eSJonathan Lemon err = devlink_info_version_fixed_put(req, 16720cfcdd1eSJonathan Lemon DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, 16730cfcdd1eSJonathan Lemon bp->board_id); 16740cfcdd1eSJonathan Lemon if (err) 16750cfcdd1eSJonathan Lemon return err; 1676773bda96SJonathan Lemon 1677773bda96SJonathan Lemon return 0; 1678773bda96SJonathan Lemon } 1679773bda96SJonathan Lemon 1680773bda96SJonathan Lemon static const struct devlink_ops ptp_ocp_devlink_ops = { 1681773bda96SJonathan Lemon .flash_update = ptp_ocp_devlink_flash_update, 1682773bda96SJonathan Lemon .info_get = ptp_ocp_devlink_info_get, 1683773bda96SJonathan Lemon }; 1684773bda96SJonathan Lemon 1685773bda96SJonathan Lemon static void __iomem * 16868119c9eeSJonathan Lemon __ptp_ocp_get_mem(struct ptp_ocp *bp, resource_size_t start, int size) 1687773bda96SJonathan Lemon { 1688773bda96SJonathan Lemon struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp"); 1689773bda96SJonathan Lemon 1690773bda96SJonathan Lemon return devm_ioremap_resource(&bp->pdev->dev, &res); 1691773bda96SJonathan Lemon } 1692773bda96SJonathan Lemon 1693773bda96SJonathan Lemon static void __iomem * 1694773bda96SJonathan Lemon ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r) 1695773bda96SJonathan Lemon { 16968119c9eeSJonathan Lemon resource_size_t start; 1697773bda96SJonathan Lemon 1698773bda96SJonathan Lemon start = pci_resource_start(bp->pdev, 0) + r->offset; 1699773bda96SJonathan Lemon return __ptp_ocp_get_mem(bp, start, r->size); 1700773bda96SJonathan Lemon } 1701773bda96SJonathan Lemon 1702773bda96SJonathan Lemon static void 1703773bda96SJonathan Lemon ptp_ocp_set_irq_resource(struct resource *res, int irq) 1704773bda96SJonathan Lemon { 1705773bda96SJonathan Lemon struct resource r = DEFINE_RES_IRQ(irq); 1706773bda96SJonathan Lemon *res = r; 1707773bda96SJonathan Lemon } 1708773bda96SJonathan Lemon 1709773bda96SJonathan Lemon static void 17108119c9eeSJonathan Lemon ptp_ocp_set_mem_resource(struct resource *res, resource_size_t start, int size) 1711773bda96SJonathan Lemon { 1712773bda96SJonathan Lemon struct resource r = DEFINE_RES_MEM(start, size); 1713773bda96SJonathan Lemon *res = r; 1714773bda96SJonathan Lemon } 1715773bda96SJonathan Lemon 1716773bda96SJonathan Lemon static int 1717773bda96SJonathan Lemon ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r) 1718773bda96SJonathan Lemon { 1719773bda96SJonathan Lemon struct ptp_ocp_flash_info *info; 1720773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1721773bda96SJonathan Lemon struct platform_device *p; 1722773bda96SJonathan Lemon struct resource res[2]; 17238119c9eeSJonathan Lemon resource_size_t start; 1724773bda96SJonathan Lemon int id; 1725773bda96SJonathan Lemon 1726773bda96SJonathan Lemon start = pci_resource_start(pdev, 0) + r->offset; 1727773bda96SJonathan Lemon ptp_ocp_set_mem_resource(&res[0], start, r->size); 1728773bda96SJonathan Lemon ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1729773bda96SJonathan Lemon 1730773bda96SJonathan Lemon info = r->extra; 1731773bda96SJonathan Lemon id = pci_dev_id(pdev) << 1; 1732773bda96SJonathan Lemon id += info->pci_offset; 1733773bda96SJonathan Lemon 1734773bda96SJonathan Lemon p = platform_device_register_resndata(&pdev->dev, info->name, id, 1735773bda96SJonathan Lemon res, 2, info->data, 1736773bda96SJonathan Lemon info->data_size); 1737773bda96SJonathan Lemon if (IS_ERR(p)) 1738773bda96SJonathan Lemon return PTR_ERR(p); 1739773bda96SJonathan Lemon 1740773bda96SJonathan Lemon bp_assign_entry(bp, r, p); 1741773bda96SJonathan Lemon 1742773bda96SJonathan Lemon return 0; 1743773bda96SJonathan Lemon } 1744773bda96SJonathan Lemon 1745773bda96SJonathan Lemon static struct platform_device * 1746773bda96SJonathan Lemon ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id) 1747773bda96SJonathan Lemon { 17481618df6aSJonathan Lemon struct ptp_ocp_i2c_info *info; 1749773bda96SJonathan Lemon struct resource res[2]; 17508119c9eeSJonathan Lemon resource_size_t start; 1751773bda96SJonathan Lemon 17521618df6aSJonathan Lemon info = r->extra; 1753773bda96SJonathan Lemon start = pci_resource_start(pdev, 0) + r->offset; 1754773bda96SJonathan Lemon ptp_ocp_set_mem_resource(&res[0], start, r->size); 1755773bda96SJonathan Lemon ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1756773bda96SJonathan Lemon 17571618df6aSJonathan Lemon return platform_device_register_resndata(&pdev->dev, info->name, 17581618df6aSJonathan Lemon id, res, 2, 17591618df6aSJonathan Lemon info->data, info->data_size); 1760773bda96SJonathan Lemon } 1761773bda96SJonathan Lemon 1762773bda96SJonathan Lemon static int 1763773bda96SJonathan Lemon ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r) 1764773bda96SJonathan Lemon { 1765773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 17661618df6aSJonathan Lemon struct ptp_ocp_i2c_info *info; 1767773bda96SJonathan Lemon struct platform_device *p; 1768773bda96SJonathan Lemon struct clk_hw *clk; 1769773bda96SJonathan Lemon char buf[32]; 1770773bda96SJonathan Lemon int id; 1771773bda96SJonathan Lemon 17721618df6aSJonathan Lemon info = r->extra; 1773773bda96SJonathan Lemon id = pci_dev_id(bp->pdev); 1774773bda96SJonathan Lemon 1775773bda96SJonathan Lemon sprintf(buf, "AXI.%d", id); 17761618df6aSJonathan Lemon clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0, 17771618df6aSJonathan Lemon info->fixed_rate); 1778773bda96SJonathan Lemon if (IS_ERR(clk)) 1779773bda96SJonathan Lemon return PTR_ERR(clk); 1780773bda96SJonathan Lemon bp->i2c_clk = clk; 1781773bda96SJonathan Lemon 17821618df6aSJonathan Lemon sprintf(buf, "%s.%d", info->name, id); 1783773bda96SJonathan Lemon devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf); 1784773bda96SJonathan Lemon p = ptp_ocp_i2c_bus(bp->pdev, r, id); 1785773bda96SJonathan Lemon if (IS_ERR(p)) 1786773bda96SJonathan Lemon return PTR_ERR(p); 1787773bda96SJonathan Lemon 1788773bda96SJonathan Lemon bp_assign_entry(bp, r, p); 1789773bda96SJonathan Lemon 1790773bda96SJonathan Lemon return 0; 1791773bda96SJonathan Lemon } 1792773bda96SJonathan Lemon 1793b325af3cSJonathan Lemon /* The expectation is that this is triggered only on error. */ 1794b325af3cSJonathan Lemon static irqreturn_t 1795b325af3cSJonathan Lemon ptp_ocp_signal_irq(int irq, void *priv) 1796b325af3cSJonathan Lemon { 1797b325af3cSJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1798b325af3cSJonathan Lemon struct signal_reg __iomem *reg = ext->mem; 1799b325af3cSJonathan Lemon struct ptp_ocp *bp = ext->bp; 1800b325af3cSJonathan Lemon u32 enable, status; 1801b325af3cSJonathan Lemon int gen; 1802b325af3cSJonathan Lemon 1803b325af3cSJonathan Lemon gen = ext->info->index - 1; 1804b325af3cSJonathan Lemon 1805b325af3cSJonathan Lemon enable = ioread32(®->enable); 1806b325af3cSJonathan Lemon status = ioread32(®->status); 1807b325af3cSJonathan Lemon 1808b325af3cSJonathan Lemon /* disable generator on error */ 1809b325af3cSJonathan Lemon if (status || !enable) { 1810b325af3cSJonathan Lemon iowrite32(0, ®->intr_mask); 1811b325af3cSJonathan Lemon iowrite32(0, ®->enable); 1812b325af3cSJonathan Lemon bp->signal[gen].running = false; 1813b325af3cSJonathan Lemon } 1814b325af3cSJonathan Lemon 1815b325af3cSJonathan Lemon iowrite32(0, ®->intr); /* ack interrupt */ 1816b325af3cSJonathan Lemon 1817b325af3cSJonathan Lemon return IRQ_HANDLED; 1818b325af3cSJonathan Lemon } 1819b325af3cSJonathan Lemon 1820b325af3cSJonathan Lemon static int 1821b325af3cSJonathan Lemon ptp_ocp_signal_set(struct ptp_ocp *bp, int gen, struct ptp_ocp_signal *s) 1822b325af3cSJonathan Lemon { 1823b325af3cSJonathan Lemon struct ptp_system_timestamp sts; 1824b325af3cSJonathan Lemon struct timespec64 ts; 1825b325af3cSJonathan Lemon ktime_t start_ns; 1826b325af3cSJonathan Lemon int err; 1827b325af3cSJonathan Lemon 1828b325af3cSJonathan Lemon if (!s->period) 1829b325af3cSJonathan Lemon return 0; 1830b325af3cSJonathan Lemon 1831b325af3cSJonathan Lemon if (!s->pulse) 1832b325af3cSJonathan Lemon s->pulse = ktime_divns(s->period * s->duty, 100); 1833b325af3cSJonathan Lemon 1834b325af3cSJonathan Lemon err = ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts); 1835b325af3cSJonathan Lemon if (err) 1836b325af3cSJonathan Lemon return err; 1837b325af3cSJonathan Lemon 1838b325af3cSJonathan Lemon start_ns = ktime_set(ts.tv_sec, ts.tv_nsec) + NSEC_PER_MSEC; 1839b325af3cSJonathan Lemon if (!s->start) { 1840b325af3cSJonathan Lemon /* roundup() does not work on 32-bit systems */ 18414bd46bb0SJonathan Lemon s->start = DIV64_U64_ROUND_UP(start_ns, s->period); 1842b325af3cSJonathan Lemon s->start = ktime_add(s->start, s->phase); 1843b325af3cSJonathan Lemon } 1844b325af3cSJonathan Lemon 1845b325af3cSJonathan Lemon if (s->duty < 1 || s->duty > 99) 1846b325af3cSJonathan Lemon return -EINVAL; 1847b325af3cSJonathan Lemon 1848b325af3cSJonathan Lemon if (s->pulse < 1 || s->pulse > s->period) 1849b325af3cSJonathan Lemon return -EINVAL; 1850b325af3cSJonathan Lemon 1851b325af3cSJonathan Lemon if (s->start < start_ns) 1852b325af3cSJonathan Lemon return -EINVAL; 1853b325af3cSJonathan Lemon 1854b325af3cSJonathan Lemon bp->signal[gen] = *s; 1855b325af3cSJonathan Lemon 1856b325af3cSJonathan Lemon return 0; 1857b325af3cSJonathan Lemon } 1858b325af3cSJonathan Lemon 1859b325af3cSJonathan Lemon static int 18601aa66a3aSJonathan Lemon ptp_ocp_signal_from_perout(struct ptp_ocp *bp, int gen, 18611aa66a3aSJonathan Lemon struct ptp_perout_request *req) 18621aa66a3aSJonathan Lemon { 18631aa66a3aSJonathan Lemon struct ptp_ocp_signal s = { }; 18641aa66a3aSJonathan Lemon 18651aa66a3aSJonathan Lemon s.polarity = bp->signal[gen].polarity; 18661aa66a3aSJonathan Lemon s.period = ktime_set(req->period.sec, req->period.nsec); 18671aa66a3aSJonathan Lemon if (!s.period) 18681aa66a3aSJonathan Lemon return 0; 18691aa66a3aSJonathan Lemon 18701aa66a3aSJonathan Lemon if (req->flags & PTP_PEROUT_DUTY_CYCLE) { 18711aa66a3aSJonathan Lemon s.pulse = ktime_set(req->on.sec, req->on.nsec); 18721aa66a3aSJonathan Lemon s.duty = ktime_divns(s.pulse * 100, s.period); 18731aa66a3aSJonathan Lemon } 18741aa66a3aSJonathan Lemon 18751aa66a3aSJonathan Lemon if (req->flags & PTP_PEROUT_PHASE) 18761aa66a3aSJonathan Lemon s.phase = ktime_set(req->phase.sec, req->phase.nsec); 18771aa66a3aSJonathan Lemon else 18781aa66a3aSJonathan Lemon s.start = ktime_set(req->start.sec, req->start.nsec); 18791aa66a3aSJonathan Lemon 18801aa66a3aSJonathan Lemon return ptp_ocp_signal_set(bp, gen, &s); 18811aa66a3aSJonathan Lemon } 18821aa66a3aSJonathan Lemon 18831aa66a3aSJonathan Lemon static int 1884b325af3cSJonathan Lemon ptp_ocp_signal_enable(void *priv, u32 req, bool enable) 1885b325af3cSJonathan Lemon { 1886b325af3cSJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1887b325af3cSJonathan Lemon struct signal_reg __iomem *reg = ext->mem; 1888b325af3cSJonathan Lemon struct ptp_ocp *bp = ext->bp; 1889b325af3cSJonathan Lemon struct timespec64 ts; 1890b325af3cSJonathan Lemon int gen; 1891b325af3cSJonathan Lemon 1892b325af3cSJonathan Lemon gen = ext->info->index - 1; 1893b325af3cSJonathan Lemon 1894b325af3cSJonathan Lemon iowrite32(0, ®->intr_mask); 1895b325af3cSJonathan Lemon iowrite32(0, ®->enable); 1896b325af3cSJonathan Lemon bp->signal[gen].running = false; 1897b325af3cSJonathan Lemon if (!enable) 1898b325af3cSJonathan Lemon return 0; 1899b325af3cSJonathan Lemon 1900b325af3cSJonathan Lemon ts = ktime_to_timespec64(bp->signal[gen].start); 1901b325af3cSJonathan Lemon iowrite32(ts.tv_sec, ®->start_sec); 1902b325af3cSJonathan Lemon iowrite32(ts.tv_nsec, ®->start_ns); 1903b325af3cSJonathan Lemon 1904b325af3cSJonathan Lemon ts = ktime_to_timespec64(bp->signal[gen].period); 1905b325af3cSJonathan Lemon iowrite32(ts.tv_sec, ®->period_sec); 1906b325af3cSJonathan Lemon iowrite32(ts.tv_nsec, ®->period_ns); 1907b325af3cSJonathan Lemon 1908b325af3cSJonathan Lemon ts = ktime_to_timespec64(bp->signal[gen].pulse); 1909b325af3cSJonathan Lemon iowrite32(ts.tv_sec, ®->pulse_sec); 1910b325af3cSJonathan Lemon iowrite32(ts.tv_nsec, ®->pulse_ns); 1911b325af3cSJonathan Lemon 1912b325af3cSJonathan Lemon iowrite32(bp->signal[gen].polarity, ®->polarity); 1913b325af3cSJonathan Lemon iowrite32(0, ®->repeat_count); 1914b325af3cSJonathan Lemon 1915b325af3cSJonathan Lemon iowrite32(0, ®->intr); /* clear interrupt state */ 1916b325af3cSJonathan Lemon iowrite32(1, ®->intr_mask); /* enable interrupt */ 1917b325af3cSJonathan Lemon iowrite32(3, ®->enable); /* valid & enable */ 1918b325af3cSJonathan Lemon 1919b325af3cSJonathan Lemon bp->signal[gen].running = true; 1920b325af3cSJonathan Lemon 1921b325af3cSJonathan Lemon return 0; 1922b325af3cSJonathan Lemon } 1923b325af3cSJonathan Lemon 1924773bda96SJonathan Lemon static irqreturn_t 1925773bda96SJonathan Lemon ptp_ocp_ts_irq(int irq, void *priv) 1926773bda96SJonathan Lemon { 1927773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1928773bda96SJonathan Lemon struct ts_reg __iomem *reg = ext->mem; 1929773bda96SJonathan Lemon struct ptp_clock_event ev; 1930773bda96SJonathan Lemon u32 sec, nsec; 1931773bda96SJonathan Lemon 1932a62a56d0SJonathan Lemon if (ext == ext->bp->pps) { 1933a62a56d0SJonathan Lemon if (ext->bp->pps_req_map & OCP_REQ_PPS) { 1934a62a56d0SJonathan Lemon ev.type = PTP_CLOCK_PPS; 1935a62a56d0SJonathan Lemon ptp_clock_event(ext->bp->ptp, &ev); 1936a62a56d0SJonathan Lemon } 1937a62a56d0SJonathan Lemon 1938a62a56d0SJonathan Lemon if ((ext->bp->pps_req_map & ~OCP_REQ_PPS) == 0) 1939a62a56d0SJonathan Lemon goto out; 1940a62a56d0SJonathan Lemon } 1941a62a56d0SJonathan Lemon 1942773bda96SJonathan Lemon /* XXX should fix API - this converts s/ns -> ts -> s/ns */ 1943773bda96SJonathan Lemon sec = ioread32(®->time_sec); 1944773bda96SJonathan Lemon nsec = ioread32(®->time_ns); 1945773bda96SJonathan Lemon 1946773bda96SJonathan Lemon ev.type = PTP_CLOCK_EXTTS; 1947773bda96SJonathan Lemon ev.index = ext->info->index; 19481acffc6eSJonathan Lemon ev.timestamp = sec * NSEC_PER_SEC + nsec; 1949773bda96SJonathan Lemon 1950773bda96SJonathan Lemon ptp_clock_event(ext->bp->ptp, &ev); 1951773bda96SJonathan Lemon 1952a62a56d0SJonathan Lemon out: 1953773bda96SJonathan Lemon iowrite32(1, ®->intr); /* write 1 to ack */ 1954773bda96SJonathan Lemon 1955773bda96SJonathan Lemon return IRQ_HANDLED; 1956773bda96SJonathan Lemon } 1957773bda96SJonathan Lemon 1958773bda96SJonathan Lemon static int 1959a62a56d0SJonathan Lemon ptp_ocp_ts_enable(void *priv, u32 req, bool enable) 1960773bda96SJonathan Lemon { 1961773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1962773bda96SJonathan Lemon struct ts_reg __iomem *reg = ext->mem; 1963a62a56d0SJonathan Lemon struct ptp_ocp *bp = ext->bp; 1964a62a56d0SJonathan Lemon 1965a62a56d0SJonathan Lemon if (ext == bp->pps) { 1966a62a56d0SJonathan Lemon u32 old_map = bp->pps_req_map; 1967a62a56d0SJonathan Lemon 1968a62a56d0SJonathan Lemon if (enable) 1969a62a56d0SJonathan Lemon bp->pps_req_map |= req; 1970a62a56d0SJonathan Lemon else 1971a62a56d0SJonathan Lemon bp->pps_req_map &= ~req; 1972a62a56d0SJonathan Lemon 1973a62a56d0SJonathan Lemon /* if no state change, just return */ 1974a62a56d0SJonathan Lemon if ((!!old_map ^ !!bp->pps_req_map) == 0) 1975a62a56d0SJonathan Lemon return 0; 1976a62a56d0SJonathan Lemon } 1977773bda96SJonathan Lemon 1978773bda96SJonathan Lemon if (enable) { 1979773bda96SJonathan Lemon iowrite32(1, ®->enable); 1980773bda96SJonathan Lemon iowrite32(1, ®->intr_mask); 1981773bda96SJonathan Lemon iowrite32(1, ®->intr); 1982773bda96SJonathan Lemon } else { 1983773bda96SJonathan Lemon iowrite32(0, ®->intr_mask); 1984773bda96SJonathan Lemon iowrite32(0, ®->enable); 1985773bda96SJonathan Lemon } 1986773bda96SJonathan Lemon 1987773bda96SJonathan Lemon return 0; 1988773bda96SJonathan Lemon } 1989773bda96SJonathan Lemon 1990773bda96SJonathan Lemon static void 1991773bda96SJonathan Lemon ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext) 1992773bda96SJonathan Lemon { 1993a62a56d0SJonathan Lemon ext->info->enable(ext, ~0, false); 1994773bda96SJonathan Lemon pci_free_irq(ext->bp->pdev, ext->irq_vec, ext); 1995773bda96SJonathan Lemon kfree(ext); 1996773bda96SJonathan Lemon } 1997773bda96SJonathan Lemon 1998773bda96SJonathan Lemon static int 1999773bda96SJonathan Lemon ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r) 2000773bda96SJonathan Lemon { 2001773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 2002773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext; 2003773bda96SJonathan Lemon int err; 2004773bda96SJonathan Lemon 2005773bda96SJonathan Lemon ext = kzalloc(sizeof(*ext), GFP_KERNEL); 2006773bda96SJonathan Lemon if (!ext) 2007773bda96SJonathan Lemon return -ENOMEM; 2008773bda96SJonathan Lemon 2009773bda96SJonathan Lemon ext->mem = ptp_ocp_get_mem(bp, r); 2010c7521d3aSDan Carpenter if (IS_ERR(ext->mem)) { 2011c7521d3aSDan Carpenter err = PTR_ERR(ext->mem); 2012773bda96SJonathan Lemon goto out; 2013c7521d3aSDan Carpenter } 2014773bda96SJonathan Lemon 2015773bda96SJonathan Lemon ext->bp = bp; 2016773bda96SJonathan Lemon ext->info = r->extra; 2017773bda96SJonathan Lemon ext->irq_vec = r->irq_vec; 2018773bda96SJonathan Lemon 2019773bda96SJonathan Lemon err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL, 202056ec4403SJonathan Lemon ext, "ocp%d.%s", bp->id, r->name); 2021773bda96SJonathan Lemon if (err) { 2022773bda96SJonathan Lemon dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec); 2023773bda96SJonathan Lemon goto out; 2024773bda96SJonathan Lemon } 2025773bda96SJonathan Lemon 2026773bda96SJonathan Lemon bp_assign_entry(bp, r, ext); 2027773bda96SJonathan Lemon 2028773bda96SJonathan Lemon return 0; 2029773bda96SJonathan Lemon 2030773bda96SJonathan Lemon out: 2031773bda96SJonathan Lemon kfree(ext); 2032773bda96SJonathan Lemon return err; 2033773bda96SJonathan Lemon } 2034773bda96SJonathan Lemon 2035773bda96SJonathan Lemon static int 2036773bda96SJonathan Lemon ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r) 2037773bda96SJonathan Lemon { 2038773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 2039773bda96SJonathan Lemon struct uart_8250_port uart; 2040773bda96SJonathan Lemon 2041773bda96SJonathan Lemon /* Setting UPF_IOREMAP and leaving port.membase unspecified lets 2042773bda96SJonathan Lemon * the serial port device claim and release the pci resource. 2043773bda96SJonathan Lemon */ 2044773bda96SJonathan Lemon memset(&uart, 0, sizeof(uart)); 2045773bda96SJonathan Lemon uart.port.dev = &pdev->dev; 2046773bda96SJonathan Lemon uart.port.iotype = UPIO_MEM; 2047773bda96SJonathan Lemon uart.port.regshift = 2; 2048773bda96SJonathan Lemon uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset; 2049773bda96SJonathan Lemon uart.port.irq = pci_irq_vector(pdev, r->irq_vec); 2050773bda96SJonathan Lemon uart.port.uartclk = 50000000; 2051c17c4059SJonathan Lemon uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP | UPF_NO_THRE_TEST; 2052773bda96SJonathan Lemon uart.port.type = PORT_16550A; 2053773bda96SJonathan Lemon 2054773bda96SJonathan Lemon return serial8250_register_8250_port(&uart); 2055773bda96SJonathan Lemon } 2056773bda96SJonathan Lemon 2057773bda96SJonathan Lemon static int 2058773bda96SJonathan Lemon ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r) 2059773bda96SJonathan Lemon { 2060895ac5a5SVadim Fedorenko struct ptp_ocp_serial_port *p = (struct ptp_ocp_serial_port *)r->extra; 2061895ac5a5SVadim Fedorenko struct ptp_ocp_serial_port port = {}; 2062773bda96SJonathan Lemon 2063895ac5a5SVadim Fedorenko port.line = ptp_ocp_serial_line(bp, r); 2064895ac5a5SVadim Fedorenko if (port.line < 0) 2065895ac5a5SVadim Fedorenko return port.line; 2066895ac5a5SVadim Fedorenko 2067895ac5a5SVadim Fedorenko if (p) 2068895ac5a5SVadim Fedorenko port.baud = p->baud; 2069773bda96SJonathan Lemon 2070773bda96SJonathan Lemon bp_assign_entry(bp, r, port); 2071773bda96SJonathan Lemon 2072773bda96SJonathan Lemon return 0; 2073773bda96SJonathan Lemon } 2074773bda96SJonathan Lemon 2075773bda96SJonathan Lemon static int 2076773bda96SJonathan Lemon ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r) 2077773bda96SJonathan Lemon { 2078773bda96SJonathan Lemon void __iomem *mem; 2079773bda96SJonathan Lemon 2080773bda96SJonathan Lemon mem = ptp_ocp_get_mem(bp, r); 2081c7521d3aSDan Carpenter if (IS_ERR(mem)) 2082c7521d3aSDan Carpenter return PTR_ERR(mem); 2083773bda96SJonathan Lemon 2084773bda96SJonathan Lemon bp_assign_entry(bp, r, mem); 2085773bda96SJonathan Lemon 2086773bda96SJonathan Lemon return 0; 2087773bda96SJonathan Lemon } 2088773bda96SJonathan Lemon 2089e3516bb4SJonathan Lemon static void 2090e3516bb4SJonathan Lemon ptp_ocp_nmea_out_init(struct ptp_ocp *bp) 2091e3516bb4SJonathan Lemon { 2092e3516bb4SJonathan Lemon if (!bp->nmea_out) 2093e3516bb4SJonathan Lemon return; 2094e3516bb4SJonathan Lemon 2095e3516bb4SJonathan Lemon iowrite32(0, &bp->nmea_out->ctrl); /* disable */ 2096e3516bb4SJonathan Lemon iowrite32(7, &bp->nmea_out->uart_baud); /* 115200 */ 2097e3516bb4SJonathan Lemon iowrite32(1, &bp->nmea_out->ctrl); /* enable */ 2098e3516bb4SJonathan Lemon } 2099e3516bb4SJonathan Lemon 2100a509a7c6SJonathan Lemon static void 2101b325af3cSJonathan Lemon _ptp_ocp_signal_init(struct ptp_ocp_signal *s, struct signal_reg __iomem *reg) 2102b325af3cSJonathan Lemon { 2103b325af3cSJonathan Lemon u32 val; 2104b325af3cSJonathan Lemon 2105b325af3cSJonathan Lemon iowrite32(0, ®->enable); /* disable */ 2106b325af3cSJonathan Lemon 2107b325af3cSJonathan Lemon val = ioread32(®->polarity); 2108b325af3cSJonathan Lemon s->polarity = val ? true : false; 2109b325af3cSJonathan Lemon s->duty = 50; 2110b325af3cSJonathan Lemon } 2111b325af3cSJonathan Lemon 2112b325af3cSJonathan Lemon static void 2113b325af3cSJonathan Lemon ptp_ocp_signal_init(struct ptp_ocp *bp) 2114b325af3cSJonathan Lemon { 2115b325af3cSJonathan Lemon int i; 2116b325af3cSJonathan Lemon 2117b325af3cSJonathan Lemon for (i = 0; i < 4; i++) 2118b325af3cSJonathan Lemon if (bp->signal_out[i]) 2119b325af3cSJonathan Lemon _ptp_ocp_signal_init(&bp->signal[i], 2120b325af3cSJonathan Lemon bp->signal_out[i]->mem); 2121b325af3cSJonathan Lemon } 2122b325af3cSJonathan Lemon 2123b325af3cSJonathan Lemon static void 2124c2239294SJonathan Lemon ptp_ocp_attr_group_del(struct ptp_ocp *bp) 2125c2239294SJonathan Lemon { 2126c2239294SJonathan Lemon sysfs_remove_groups(&bp->dev.kobj, bp->attr_group); 2127c2239294SJonathan Lemon kfree(bp->attr_group); 2128c2239294SJonathan Lemon } 2129c2239294SJonathan Lemon 2130c2239294SJonathan Lemon static int 2131c2239294SJonathan Lemon ptp_ocp_attr_group_add(struct ptp_ocp *bp, 2132c2239294SJonathan Lemon const struct ocp_attr_group *attr_tbl) 2133c2239294SJonathan Lemon { 2134c2239294SJonathan Lemon int count, i; 2135c2239294SJonathan Lemon int err; 2136c2239294SJonathan Lemon 2137c2239294SJonathan Lemon count = 0; 2138c2239294SJonathan Lemon for (i = 0; attr_tbl[i].cap; i++) 2139c2239294SJonathan Lemon if (attr_tbl[i].cap & bp->fw_cap) 2140c2239294SJonathan Lemon count++; 2141c2239294SJonathan Lemon 2142c2239294SJonathan Lemon bp->attr_group = kcalloc(count + 1, sizeof(struct attribute_group *), 2143c2239294SJonathan Lemon GFP_KERNEL); 2144c2239294SJonathan Lemon if (!bp->attr_group) 2145c2239294SJonathan Lemon return -ENOMEM; 2146c2239294SJonathan Lemon 2147c2239294SJonathan Lemon count = 0; 2148c2239294SJonathan Lemon for (i = 0; attr_tbl[i].cap; i++) 2149c2239294SJonathan Lemon if (attr_tbl[i].cap & bp->fw_cap) 2150c2239294SJonathan Lemon bp->attr_group[count++] = attr_tbl[i].group; 2151c2239294SJonathan Lemon 2152c2239294SJonathan Lemon err = sysfs_create_groups(&bp->dev.kobj, bp->attr_group); 2153c2239294SJonathan Lemon if (err) 2154c2239294SJonathan Lemon bp->attr_group[0] = NULL; 2155c2239294SJonathan Lemon 2156c2239294SJonathan Lemon return err; 2157c2239294SJonathan Lemon } 2158c2239294SJonathan Lemon 2159c2239294SJonathan Lemon static void 2160caab82cdSJonathan Lemon ptp_ocp_enable_fpga(u32 __iomem *reg, u32 bit, bool enable) 2161caab82cdSJonathan Lemon { 2162caab82cdSJonathan Lemon u32 ctrl; 2163caab82cdSJonathan Lemon bool on; 2164caab82cdSJonathan Lemon 2165caab82cdSJonathan Lemon ctrl = ioread32(reg); 2166caab82cdSJonathan Lemon on = ctrl & bit; 2167caab82cdSJonathan Lemon if (on ^ enable) { 2168caab82cdSJonathan Lemon ctrl &= ~bit; 2169caab82cdSJonathan Lemon ctrl |= enable ? bit : 0; 2170caab82cdSJonathan Lemon iowrite32(ctrl, reg); 2171caab82cdSJonathan Lemon } 2172caab82cdSJonathan Lemon } 2173caab82cdSJonathan Lemon 2174caab82cdSJonathan Lemon static void 2175caab82cdSJonathan Lemon ptp_ocp_irig_out(struct ptp_ocp *bp, bool enable) 2176caab82cdSJonathan Lemon { 2177caab82cdSJonathan Lemon return ptp_ocp_enable_fpga(&bp->irig_out->ctrl, 2178caab82cdSJonathan Lemon IRIG_M_CTRL_ENABLE, enable); 2179caab82cdSJonathan Lemon } 2180caab82cdSJonathan Lemon 2181caab82cdSJonathan Lemon static void 2182caab82cdSJonathan Lemon ptp_ocp_irig_in(struct ptp_ocp *bp, bool enable) 2183caab82cdSJonathan Lemon { 2184caab82cdSJonathan Lemon return ptp_ocp_enable_fpga(&bp->irig_in->ctrl, 2185caab82cdSJonathan Lemon IRIG_S_CTRL_ENABLE, enable); 2186caab82cdSJonathan Lemon } 2187caab82cdSJonathan Lemon 2188caab82cdSJonathan Lemon static void 2189caab82cdSJonathan Lemon ptp_ocp_dcf_out(struct ptp_ocp *bp, bool enable) 2190caab82cdSJonathan Lemon { 2191caab82cdSJonathan Lemon return ptp_ocp_enable_fpga(&bp->dcf_out->ctrl, 2192caab82cdSJonathan Lemon DCF_M_CTRL_ENABLE, enable); 2193caab82cdSJonathan Lemon } 2194caab82cdSJonathan Lemon 2195caab82cdSJonathan Lemon static void 2196caab82cdSJonathan Lemon ptp_ocp_dcf_in(struct ptp_ocp *bp, bool enable) 2197caab82cdSJonathan Lemon { 2198caab82cdSJonathan Lemon return ptp_ocp_enable_fpga(&bp->dcf_in->ctrl, 2199caab82cdSJonathan Lemon DCF_S_CTRL_ENABLE, enable); 2200caab82cdSJonathan Lemon } 2201caab82cdSJonathan Lemon 2202caab82cdSJonathan Lemon static void 2203caab82cdSJonathan Lemon __handle_signal_outputs(struct ptp_ocp *bp, u32 val) 2204caab82cdSJonathan Lemon { 2205caab82cdSJonathan Lemon ptp_ocp_irig_out(bp, val & 0x00100010); 2206caab82cdSJonathan Lemon ptp_ocp_dcf_out(bp, val & 0x00200020); 2207caab82cdSJonathan Lemon } 2208caab82cdSJonathan Lemon 2209caab82cdSJonathan Lemon static void 2210caab82cdSJonathan Lemon __handle_signal_inputs(struct ptp_ocp *bp, u32 val) 2211caab82cdSJonathan Lemon { 2212caab82cdSJonathan Lemon ptp_ocp_irig_in(bp, val & 0x00100010); 2213caab82cdSJonathan Lemon ptp_ocp_dcf_in(bp, val & 0x00200020); 2214caab82cdSJonathan Lemon } 2215caab82cdSJonathan Lemon 2216caab82cdSJonathan Lemon static u32 2217caab82cdSJonathan Lemon ptp_ocp_sma_fb_get(struct ptp_ocp *bp, int sma_nr) 2218caab82cdSJonathan Lemon { 2219caab82cdSJonathan Lemon u32 __iomem *gpio; 2220caab82cdSJonathan Lemon u32 shift; 2221caab82cdSJonathan Lemon 2222caab82cdSJonathan Lemon if (bp->sma[sma_nr - 1].fixed_fcn) 2223caab82cdSJonathan Lemon return (sma_nr - 1) & 1; 2224caab82cdSJonathan Lemon 2225caab82cdSJonathan Lemon if (bp->sma[sma_nr - 1].mode == SMA_MODE_IN) 2226caab82cdSJonathan Lemon gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1; 2227caab82cdSJonathan Lemon else 2228caab82cdSJonathan Lemon gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2; 2229caab82cdSJonathan Lemon shift = sma_nr & 1 ? 0 : 16; 2230caab82cdSJonathan Lemon 2231caab82cdSJonathan Lemon return (ioread32(gpio) >> shift) & 0xffff; 2232caab82cdSJonathan Lemon } 2233caab82cdSJonathan Lemon 2234caab82cdSJonathan Lemon static int 2235caab82cdSJonathan Lemon ptp_ocp_sma_fb_set_output(struct ptp_ocp *bp, int sma_nr, u32 val) 2236caab82cdSJonathan Lemon { 2237caab82cdSJonathan Lemon u32 reg, mask, shift; 2238caab82cdSJonathan Lemon unsigned long flags; 2239caab82cdSJonathan Lemon u32 __iomem *gpio; 2240caab82cdSJonathan Lemon 2241caab82cdSJonathan Lemon gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2; 2242caab82cdSJonathan Lemon shift = sma_nr & 1 ? 0 : 16; 2243caab82cdSJonathan Lemon 2244caab82cdSJonathan Lemon mask = 0xffff << (16 - shift); 2245caab82cdSJonathan Lemon 2246caab82cdSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 2247caab82cdSJonathan Lemon 2248caab82cdSJonathan Lemon reg = ioread32(gpio); 2249caab82cdSJonathan Lemon reg = (reg & mask) | (val << shift); 2250caab82cdSJonathan Lemon 2251caab82cdSJonathan Lemon __handle_signal_outputs(bp, reg); 2252caab82cdSJonathan Lemon 2253caab82cdSJonathan Lemon iowrite32(reg, gpio); 2254caab82cdSJonathan Lemon 2255caab82cdSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 2256caab82cdSJonathan Lemon 2257caab82cdSJonathan Lemon return 0; 2258caab82cdSJonathan Lemon } 2259caab82cdSJonathan Lemon 2260caab82cdSJonathan Lemon static int 2261caab82cdSJonathan Lemon ptp_ocp_sma_fb_set_inputs(struct ptp_ocp *bp, int sma_nr, u32 val) 2262caab82cdSJonathan Lemon { 2263caab82cdSJonathan Lemon u32 reg, mask, shift; 2264caab82cdSJonathan Lemon unsigned long flags; 2265caab82cdSJonathan Lemon u32 __iomem *gpio; 2266caab82cdSJonathan Lemon 2267caab82cdSJonathan Lemon gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1; 2268caab82cdSJonathan Lemon shift = sma_nr & 1 ? 0 : 16; 2269caab82cdSJonathan Lemon 2270caab82cdSJonathan Lemon mask = 0xffff << (16 - shift); 2271caab82cdSJonathan Lemon 2272caab82cdSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 2273caab82cdSJonathan Lemon 2274caab82cdSJonathan Lemon reg = ioread32(gpio); 2275caab82cdSJonathan Lemon reg = (reg & mask) | (val << shift); 2276caab82cdSJonathan Lemon 2277caab82cdSJonathan Lemon __handle_signal_inputs(bp, reg); 2278caab82cdSJonathan Lemon 2279caab82cdSJonathan Lemon iowrite32(reg, gpio); 2280caab82cdSJonathan Lemon 2281caab82cdSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 2282caab82cdSJonathan Lemon 2283caab82cdSJonathan Lemon return 0; 2284caab82cdSJonathan Lemon } 2285caab82cdSJonathan Lemon 2286caab82cdSJonathan Lemon static void 2287ee4cd725SJonathan Lemon ptp_ocp_sma_fb_init(struct ptp_ocp *bp) 2288a509a7c6SJonathan Lemon { 2289a509a7c6SJonathan Lemon u32 reg; 2290a509a7c6SJonathan Lemon int i; 2291a509a7c6SJonathan Lemon 2292a509a7c6SJonathan Lemon /* defaults */ 2293a509a7c6SJonathan Lemon bp->sma[0].mode = SMA_MODE_IN; 2294a509a7c6SJonathan Lemon bp->sma[1].mode = SMA_MODE_IN; 2295a509a7c6SJonathan Lemon bp->sma[2].mode = SMA_MODE_OUT; 2296a509a7c6SJonathan Lemon bp->sma[3].mode = SMA_MODE_OUT; 2297ee4cd725SJonathan Lemon for (i = 0; i < 4; i++) 2298ee4cd725SJonathan Lemon bp->sma[i].default_fcn = i & 1; 2299a509a7c6SJonathan Lemon 2300a509a7c6SJonathan Lemon /* If no SMA1 map, the pin functions and directions are fixed. */ 2301a509a7c6SJonathan Lemon if (!bp->sma_map1) { 2302a509a7c6SJonathan Lemon for (i = 0; i < 4; i++) { 2303a509a7c6SJonathan Lemon bp->sma[i].fixed_fcn = true; 2304a509a7c6SJonathan Lemon bp->sma[i].fixed_dir = true; 2305a509a7c6SJonathan Lemon } 2306a509a7c6SJonathan Lemon return; 2307a509a7c6SJonathan Lemon } 2308a509a7c6SJonathan Lemon 2309a509a7c6SJonathan Lemon /* If SMA2 GPIO output map is all 1, it is not present. 2310a509a7c6SJonathan Lemon * This indicates the firmware has fixed direction SMA pins. 2311a509a7c6SJonathan Lemon */ 2312a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map2->gpio2); 2313a509a7c6SJonathan Lemon if (reg == 0xffffffff) { 2314a509a7c6SJonathan Lemon for (i = 0; i < 4; i++) 2315a509a7c6SJonathan Lemon bp->sma[i].fixed_dir = true; 2316a509a7c6SJonathan Lemon } else { 2317a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map1->gpio1); 2318a509a7c6SJonathan Lemon bp->sma[0].mode = reg & BIT(15) ? SMA_MODE_IN : SMA_MODE_OUT; 2319a509a7c6SJonathan Lemon bp->sma[1].mode = reg & BIT(31) ? SMA_MODE_IN : SMA_MODE_OUT; 2320a509a7c6SJonathan Lemon 2321a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map1->gpio2); 2322a509a7c6SJonathan Lemon bp->sma[2].mode = reg & BIT(15) ? SMA_MODE_OUT : SMA_MODE_IN; 2323a509a7c6SJonathan Lemon bp->sma[3].mode = reg & BIT(31) ? SMA_MODE_OUT : SMA_MODE_IN; 2324a509a7c6SJonathan Lemon } 2325a509a7c6SJonathan Lemon } 2326a509a7c6SJonathan Lemon 2327ee4cd725SJonathan Lemon static const struct ocp_sma_op ocp_fb_sma_op = { 2328ee4cd725SJonathan Lemon .tbl = { ptp_ocp_sma_in, ptp_ocp_sma_out }, 2329ee4cd725SJonathan Lemon .init = ptp_ocp_sma_fb_init, 2330ee4cd725SJonathan Lemon .get = ptp_ocp_sma_fb_get, 2331ee4cd725SJonathan Lemon .set_inputs = ptp_ocp_sma_fb_set_inputs, 2332ee4cd725SJonathan Lemon .set_output = ptp_ocp_sma_fb_set_output, 2333ee4cd725SJonathan Lemon }; 2334ee4cd725SJonathan Lemon 23351aa66a3aSJonathan Lemon static int 23361aa66a3aSJonathan Lemon ptp_ocp_fb_set_pins(struct ptp_ocp *bp) 23371aa66a3aSJonathan Lemon { 23381aa66a3aSJonathan Lemon struct ptp_pin_desc *config; 23391aa66a3aSJonathan Lemon int i; 23401aa66a3aSJonathan Lemon 23419a7a1be6SAndy Shevchenko config = kcalloc(4, sizeof(*config), GFP_KERNEL); 23421aa66a3aSJonathan Lemon if (!config) 23431aa66a3aSJonathan Lemon return -ENOMEM; 23441aa66a3aSJonathan Lemon 23451aa66a3aSJonathan Lemon for (i = 0; i < 4; i++) { 23461aa66a3aSJonathan Lemon sprintf(config[i].name, "sma%d", i + 1); 23471aa66a3aSJonathan Lemon config[i].index = i; 23481aa66a3aSJonathan Lemon } 23491aa66a3aSJonathan Lemon 23501aa66a3aSJonathan Lemon bp->ptp_info.n_pins = 4; 23511aa66a3aSJonathan Lemon bp->ptp_info.pin_config = config; 23521aa66a3aSJonathan Lemon 23531aa66a3aSJonathan Lemon return 0; 23541aa66a3aSJonathan Lemon } 23551aa66a3aSJonathan Lemon 23565a728ac5SJonathan Lemon static void 23575a728ac5SJonathan Lemon ptp_ocp_fb_set_version(struct ptp_ocp *bp) 23585a728ac5SJonathan Lemon { 23595a728ac5SJonathan Lemon u64 cap = OCP_CAP_BASIC; 23605a728ac5SJonathan Lemon u32 version; 23615a728ac5SJonathan Lemon 23625a728ac5SJonathan Lemon version = ioread32(&bp->image->version); 23635a728ac5SJonathan Lemon 23645a728ac5SJonathan Lemon /* if lower 16 bits are empty, this is the fw loader. */ 23655a728ac5SJonathan Lemon if ((version & 0xffff) == 0) { 23665a728ac5SJonathan Lemon version = version >> 16; 23675a728ac5SJonathan Lemon bp->fw_loader = true; 23685a728ac5SJonathan Lemon } 23695a728ac5SJonathan Lemon 23705a728ac5SJonathan Lemon bp->fw_tag = version >> 15; 23715a728ac5SJonathan Lemon bp->fw_version = version & 0x7fff; 23725a728ac5SJonathan Lemon 23735a728ac5SJonathan Lemon if (bp->fw_tag) { 23745a728ac5SJonathan Lemon /* FPGA firmware */ 23755a728ac5SJonathan Lemon if (version >= 5) 23765a728ac5SJonathan Lemon cap |= OCP_CAP_SIGNAL | OCP_CAP_FREQ; 23775a728ac5SJonathan Lemon } else { 23785a728ac5SJonathan Lemon /* SOM firmware */ 23795a728ac5SJonathan Lemon if (version >= 19) 23805a728ac5SJonathan Lemon cap |= OCP_CAP_SIGNAL; 23815a728ac5SJonathan Lemon if (version >= 20) 23825a728ac5SJonathan Lemon cap |= OCP_CAP_FREQ; 23835a728ac5SJonathan Lemon } 23845a728ac5SJonathan Lemon 23855a728ac5SJonathan Lemon bp->fw_cap = cap; 23865a728ac5SJonathan Lemon } 23875a728ac5SJonathan Lemon 2388773bda96SJonathan Lemon /* FB specific board initializers; last "resource" registered. */ 2389773bda96SJonathan Lemon static int 2390773bda96SJonathan Lemon ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r) 2391773bda96SJonathan Lemon { 23925a728ac5SJonathan Lemon int err; 2393b325af3cSJonathan Lemon 2394773bda96SJonathan Lemon bp->flash_start = 1024 * 4096; 23950cfcdd1eSJonathan Lemon bp->eeprom_map = fb_eeprom_map; 2396b0ca789aSJonathan Lemon bp->fw_version = ioread32(&bp->image->version); 2397caab82cdSJonathan Lemon bp->sma_op = &ocp_fb_sma_op; 2398773bda96SJonathan Lemon 23995a728ac5SJonathan Lemon ptp_ocp_fb_set_version(bp); 2400b325af3cSJonathan Lemon 2401065efcc5SJonathan Lemon ptp_ocp_tod_init(bp); 2402e3516bb4SJonathan Lemon ptp_ocp_nmea_out_init(bp); 2403a509a7c6SJonathan Lemon ptp_ocp_sma_init(bp); 2404b325af3cSJonathan Lemon ptp_ocp_signal_init(bp); 2405065efcc5SJonathan Lemon 2406c2239294SJonathan Lemon err = ptp_ocp_attr_group_add(bp, fb_timecard_groups); 2407c2239294SJonathan Lemon if (err) 2408c2239294SJonathan Lemon return err; 2409c2239294SJonathan Lemon 24101aa66a3aSJonathan Lemon err = ptp_ocp_fb_set_pins(bp); 24111aa66a3aSJonathan Lemon if (err) 24121aa66a3aSJonathan Lemon return err; 24131aa66a3aSJonathan Lemon 2414773bda96SJonathan Lemon return ptp_ocp_init_clock(bp); 2415773bda96SJonathan Lemon } 2416773bda96SJonathan Lemon 241756ec4403SJonathan Lemon static bool 241856ec4403SJonathan Lemon ptp_ocp_allow_irq(struct ptp_ocp *bp, struct ocp_resource *r) 241956ec4403SJonathan Lemon { 242056ec4403SJonathan Lemon bool allow = !r->irq_vec || r->irq_vec < bp->n_irqs; 242156ec4403SJonathan Lemon 242256ec4403SJonathan Lemon if (!allow) 242356ec4403SJonathan Lemon dev_err(&bp->pdev->dev, "irq %d out of range, skipping %s\n", 242456ec4403SJonathan Lemon r->irq_vec, r->name); 242556ec4403SJonathan Lemon return allow; 242656ec4403SJonathan Lemon } 242756ec4403SJonathan Lemon 2428773bda96SJonathan Lemon static int 2429773bda96SJonathan Lemon ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data) 2430773bda96SJonathan Lemon { 2431773bda96SJonathan Lemon struct ocp_resource *r, *table; 2432773bda96SJonathan Lemon int err = 0; 2433773bda96SJonathan Lemon 2434773bda96SJonathan Lemon table = (struct ocp_resource *)driver_data; 2435773bda96SJonathan Lemon for (r = table; r->setup; r++) { 243656ec4403SJonathan Lemon if (!ptp_ocp_allow_irq(bp, r)) 243756ec4403SJonathan Lemon continue; 2438773bda96SJonathan Lemon err = r->setup(bp, r); 2439bceff290SJonathan Lemon if (err) { 2440bceff290SJonathan Lemon dev_err(&bp->pdev->dev, 2441bceff290SJonathan Lemon "Could not register %s: err %d\n", 2442bceff290SJonathan Lemon r->name, err); 2443773bda96SJonathan Lemon break; 2444773bda96SJonathan Lemon } 2445bceff290SJonathan Lemon } 2446773bda96SJonathan Lemon return err; 2447773bda96SJonathan Lemon } 2448773bda96SJonathan Lemon 244969dbe107SVadim Fedorenko static void 245069dbe107SVadim Fedorenko ptp_ocp_art_sma_init(struct ptp_ocp *bp) 245169dbe107SVadim Fedorenko { 245269dbe107SVadim Fedorenko u32 reg; 245369dbe107SVadim Fedorenko int i; 245469dbe107SVadim Fedorenko 245569dbe107SVadim Fedorenko /* defaults */ 245669dbe107SVadim Fedorenko bp->sma[0].mode = SMA_MODE_IN; 245769dbe107SVadim Fedorenko bp->sma[1].mode = SMA_MODE_IN; 245869dbe107SVadim Fedorenko bp->sma[2].mode = SMA_MODE_OUT; 245969dbe107SVadim Fedorenko bp->sma[3].mode = SMA_MODE_OUT; 246069dbe107SVadim Fedorenko 246169dbe107SVadim Fedorenko bp->sma[0].default_fcn = 0x08; /* IN: 10Mhz */ 246269dbe107SVadim Fedorenko bp->sma[1].default_fcn = 0x01; /* IN: PPS1 */ 246369dbe107SVadim Fedorenko bp->sma[2].default_fcn = 0x10; /* OUT: 10Mhz */ 246469dbe107SVadim Fedorenko bp->sma[3].default_fcn = 0x02; /* OUT: PHC */ 246569dbe107SVadim Fedorenko 246669dbe107SVadim Fedorenko /* If no SMA map, the pin functions and directions are fixed. */ 246769dbe107SVadim Fedorenko if (!bp->art_sma) { 246869dbe107SVadim Fedorenko for (i = 0; i < 4; i++) { 246969dbe107SVadim Fedorenko bp->sma[i].fixed_fcn = true; 247069dbe107SVadim Fedorenko bp->sma[i].fixed_dir = true; 247169dbe107SVadim Fedorenko } 247269dbe107SVadim Fedorenko return; 247369dbe107SVadim Fedorenko } 247469dbe107SVadim Fedorenko 247569dbe107SVadim Fedorenko for (i = 0; i < 4; i++) { 247669dbe107SVadim Fedorenko reg = ioread32(&bp->art_sma->map[i].gpio); 247769dbe107SVadim Fedorenko 247869dbe107SVadim Fedorenko switch (reg & 0xff) { 247969dbe107SVadim Fedorenko case 0: 248069dbe107SVadim Fedorenko bp->sma[i].fixed_fcn = true; 248169dbe107SVadim Fedorenko bp->sma[i].fixed_dir = true; 248269dbe107SVadim Fedorenko break; 248369dbe107SVadim Fedorenko case 1: 248469dbe107SVadim Fedorenko case 8: 248569dbe107SVadim Fedorenko bp->sma[i].mode = SMA_MODE_IN; 248669dbe107SVadim Fedorenko break; 248769dbe107SVadim Fedorenko default: 248869dbe107SVadim Fedorenko bp->sma[i].mode = SMA_MODE_OUT; 248969dbe107SVadim Fedorenko break; 249069dbe107SVadim Fedorenko } 249169dbe107SVadim Fedorenko } 249269dbe107SVadim Fedorenko } 249369dbe107SVadim Fedorenko 249469dbe107SVadim Fedorenko static u32 249569dbe107SVadim Fedorenko ptp_ocp_art_sma_get(struct ptp_ocp *bp, int sma_nr) 249669dbe107SVadim Fedorenko { 249769dbe107SVadim Fedorenko if (bp->sma[sma_nr - 1].fixed_fcn) 249869dbe107SVadim Fedorenko return bp->sma[sma_nr - 1].default_fcn; 249969dbe107SVadim Fedorenko 250069dbe107SVadim Fedorenko return ioread32(&bp->art_sma->map[sma_nr - 1].gpio) & 0xff; 250169dbe107SVadim Fedorenko } 250269dbe107SVadim Fedorenko 250369dbe107SVadim Fedorenko /* note: store 0 is considered invalid. */ 250469dbe107SVadim Fedorenko static int 250569dbe107SVadim Fedorenko ptp_ocp_art_sma_set(struct ptp_ocp *bp, int sma_nr, u32 val) 250669dbe107SVadim Fedorenko { 250769dbe107SVadim Fedorenko unsigned long flags; 250869dbe107SVadim Fedorenko u32 __iomem *gpio; 250969dbe107SVadim Fedorenko int err = 0; 251069dbe107SVadim Fedorenko u32 reg; 251169dbe107SVadim Fedorenko 251269dbe107SVadim Fedorenko val &= SMA_SELECT_MASK; 251369dbe107SVadim Fedorenko if (hweight32(val) > 1) 251469dbe107SVadim Fedorenko return -EINVAL; 251569dbe107SVadim Fedorenko 251669dbe107SVadim Fedorenko gpio = &bp->art_sma->map[sma_nr - 1].gpio; 251769dbe107SVadim Fedorenko 251869dbe107SVadim Fedorenko spin_lock_irqsave(&bp->lock, flags); 251969dbe107SVadim Fedorenko reg = ioread32(gpio); 252069dbe107SVadim Fedorenko if (((reg >> 16) & val) == 0) { 252169dbe107SVadim Fedorenko err = -EOPNOTSUPP; 252269dbe107SVadim Fedorenko } else { 252369dbe107SVadim Fedorenko reg = (reg & 0xff00) | (val & 0xff); 252469dbe107SVadim Fedorenko iowrite32(reg, gpio); 252569dbe107SVadim Fedorenko } 252669dbe107SVadim Fedorenko spin_unlock_irqrestore(&bp->lock, flags); 252769dbe107SVadim Fedorenko 252869dbe107SVadim Fedorenko return err; 252969dbe107SVadim Fedorenko } 253069dbe107SVadim Fedorenko 253169dbe107SVadim Fedorenko static const struct ocp_sma_op ocp_art_sma_op = { 253269dbe107SVadim Fedorenko .tbl = { ptp_ocp_art_sma_in, ptp_ocp_art_sma_out }, 253369dbe107SVadim Fedorenko .init = ptp_ocp_art_sma_init, 253469dbe107SVadim Fedorenko .get = ptp_ocp_art_sma_get, 253569dbe107SVadim Fedorenko .set_inputs = ptp_ocp_art_sma_set, 253669dbe107SVadim Fedorenko .set_output = ptp_ocp_art_sma_set, 253769dbe107SVadim Fedorenko }; 253869dbe107SVadim Fedorenko 253969dbe107SVadim Fedorenko /* ART specific board initializers; last "resource" registered. */ 254069dbe107SVadim Fedorenko static int 254169dbe107SVadim Fedorenko ptp_ocp_art_board_init(struct ptp_ocp *bp, struct ocp_resource *r) 254269dbe107SVadim Fedorenko { 254369dbe107SVadim Fedorenko int err; 254469dbe107SVadim Fedorenko 254569dbe107SVadim Fedorenko bp->flash_start = 0x1000000; 254669dbe107SVadim Fedorenko bp->eeprom_map = art_eeprom_map; 254769dbe107SVadim Fedorenko bp->fw_cap = OCP_CAP_BASIC; 254869dbe107SVadim Fedorenko bp->fw_version = ioread32(&bp->reg->version); 254969dbe107SVadim Fedorenko bp->fw_tag = 2; 255069dbe107SVadim Fedorenko bp->sma_op = &ocp_art_sma_op; 255169dbe107SVadim Fedorenko 2552*9c44a7acSVadim Fedorenko /* Enable MAC serial port during initialisation */ 2553*9c44a7acSVadim Fedorenko iowrite32(1, &bp->board_config->mro50_serial_activate); 2554*9c44a7acSVadim Fedorenko 255569dbe107SVadim Fedorenko ptp_ocp_sma_init(bp); 255669dbe107SVadim Fedorenko 255769dbe107SVadim Fedorenko err = ptp_ocp_attr_group_add(bp, art_timecard_groups); 255869dbe107SVadim Fedorenko if (err) 255969dbe107SVadim Fedorenko return err; 256069dbe107SVadim Fedorenko 256169dbe107SVadim Fedorenko return ptp_ocp_init_clock(bp); 256269dbe107SVadim Fedorenko } 256369dbe107SVadim Fedorenko 2564e1daf0ecSJonathan Lemon static ssize_t 25653f3fe41cSJonathan Lemon ptp_ocp_show_output(const struct ocp_selector *tbl, u32 val, char *buf, 25663f3fe41cSJonathan Lemon int def_val) 2567e1daf0ecSJonathan Lemon { 2568e1daf0ecSJonathan Lemon const char *name; 2569e1daf0ecSJonathan Lemon ssize_t count; 2570e1daf0ecSJonathan Lemon 2571e1daf0ecSJonathan Lemon count = sysfs_emit(buf, "OUT: "); 2572aa56a7ffSJonathan Lemon name = ptp_ocp_select_name_from_val(tbl, val); 2573e1daf0ecSJonathan Lemon if (!name) 2574aa56a7ffSJonathan Lemon name = ptp_ocp_select_name_from_val(tbl, def_val); 2575e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s\n", name); 2576e1daf0ecSJonathan Lemon return count; 2577e1daf0ecSJonathan Lemon } 2578e1daf0ecSJonathan Lemon 2579e1daf0ecSJonathan Lemon static ssize_t 25803f3fe41cSJonathan Lemon ptp_ocp_show_inputs(const struct ocp_selector *tbl, u32 val, char *buf, 25813f3fe41cSJonathan Lemon int def_val) 2582e1daf0ecSJonathan Lemon { 2583e1daf0ecSJonathan Lemon const char *name; 2584e1daf0ecSJonathan Lemon ssize_t count; 2585e1daf0ecSJonathan Lemon int i; 2586e1daf0ecSJonathan Lemon 2587e1daf0ecSJonathan Lemon count = sysfs_emit(buf, "IN: "); 2588aa56a7ffSJonathan Lemon for (i = 0; tbl[i].name; i++) { 2589aa56a7ffSJonathan Lemon if (val & tbl[i].value) { 2590aa56a7ffSJonathan Lemon name = tbl[i].name; 2591e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", name); 2592e1daf0ecSJonathan Lemon } 2593e1daf0ecSJonathan Lemon } 2594b2c4f0acSJonathan Lemon if (!val && def_val >= 0) { 2595aa56a7ffSJonathan Lemon name = ptp_ocp_select_name_from_val(tbl, def_val); 2596b2c4f0acSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", name); 2597b2c4f0acSJonathan Lemon } 2598e1daf0ecSJonathan Lemon if (count) 2599e1daf0ecSJonathan Lemon count--; 2600e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "\n"); 2601e1daf0ecSJonathan Lemon return count; 2602e1daf0ecSJonathan Lemon } 2603e1daf0ecSJonathan Lemon 2604e1daf0ecSJonathan Lemon static int 26053f3fe41cSJonathan Lemon sma_parse_inputs(const struct ocp_selector * const tbl[], const char *buf, 2606aa56a7ffSJonathan Lemon enum ptp_ocp_sma_mode *mode) 2607e1daf0ecSJonathan Lemon { 2608e1daf0ecSJonathan Lemon int idx, count, dir; 2609e1daf0ecSJonathan Lemon char **argv; 2610e1daf0ecSJonathan Lemon int ret; 2611e1daf0ecSJonathan Lemon 2612e1daf0ecSJonathan Lemon argv = argv_split(GFP_KERNEL, buf, &count); 2613e1daf0ecSJonathan Lemon if (!argv) 2614e1daf0ecSJonathan Lemon return -ENOMEM; 2615e1daf0ecSJonathan Lemon 2616e1daf0ecSJonathan Lemon ret = -EINVAL; 2617e1daf0ecSJonathan Lemon if (!count) 2618e1daf0ecSJonathan Lemon goto out; 2619e1daf0ecSJonathan Lemon 2620e1daf0ecSJonathan Lemon idx = 0; 2621e1daf0ecSJonathan Lemon dir = *mode == SMA_MODE_IN ? 0 : 1; 2622a509a7c6SJonathan Lemon if (!strcasecmp("IN:", argv[0])) { 2623e1daf0ecSJonathan Lemon dir = 0; 2624e1daf0ecSJonathan Lemon idx++; 2625e1daf0ecSJonathan Lemon } 2626e1daf0ecSJonathan Lemon if (!strcasecmp("OUT:", argv[0])) { 2627e1daf0ecSJonathan Lemon dir = 1; 2628e1daf0ecSJonathan Lemon idx++; 2629e1daf0ecSJonathan Lemon } 2630e1daf0ecSJonathan Lemon *mode = dir == 0 ? SMA_MODE_IN : SMA_MODE_OUT; 2631e1daf0ecSJonathan Lemon 2632e1daf0ecSJonathan Lemon ret = 0; 2633e1daf0ecSJonathan Lemon for (; idx < count; idx++) 2634e1daf0ecSJonathan Lemon ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]); 2635e1daf0ecSJonathan Lemon if (ret < 0) 2636e1daf0ecSJonathan Lemon ret = -EINVAL; 2637e1daf0ecSJonathan Lemon 2638e1daf0ecSJonathan Lemon out: 2639e1daf0ecSJonathan Lemon argv_free(argv); 2640e1daf0ecSJonathan Lemon return ret; 2641e1daf0ecSJonathan Lemon } 2642e1daf0ecSJonathan Lemon 2643a509a7c6SJonathan Lemon static ssize_t 2644a509a7c6SJonathan Lemon ptp_ocp_sma_show(struct ptp_ocp *bp, int sma_nr, char *buf, 2645b2c4f0acSJonathan Lemon int default_in_val, int default_out_val) 2646a509a7c6SJonathan Lemon { 2647a509a7c6SJonathan Lemon struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1]; 26483f3fe41cSJonathan Lemon const struct ocp_selector * const *tbl; 2649a509a7c6SJonathan Lemon u32 val; 2650a509a7c6SJonathan Lemon 2651caab82cdSJonathan Lemon tbl = bp->sma_op->tbl; 2652caab82cdSJonathan Lemon val = ptp_ocp_sma_get(bp, sma_nr) & SMA_SELECT_MASK; 2653e1daf0ecSJonathan Lemon 2654b2c4f0acSJonathan Lemon if (sma->mode == SMA_MODE_IN) { 2655b2c4f0acSJonathan Lemon if (sma->disabled) 2656b2c4f0acSJonathan Lemon val = SMA_DISABLE; 2657aa56a7ffSJonathan Lemon return ptp_ocp_show_inputs(tbl[0], val, buf, default_in_val); 2658b2c4f0acSJonathan Lemon } 2659e1daf0ecSJonathan Lemon 2660aa56a7ffSJonathan Lemon return ptp_ocp_show_output(tbl[1], val, buf, default_out_val); 2661e1daf0ecSJonathan Lemon } 2662e1daf0ecSJonathan Lemon 2663e1daf0ecSJonathan Lemon static ssize_t 2664e1daf0ecSJonathan Lemon sma1_show(struct device *dev, struct device_attribute *attr, char *buf) 2665e1daf0ecSJonathan Lemon { 2666e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2667e1daf0ecSJonathan Lemon 2668a509a7c6SJonathan Lemon return ptp_ocp_sma_show(bp, 1, buf, 0, 1); 2669e1daf0ecSJonathan Lemon } 2670e1daf0ecSJonathan Lemon 2671e1daf0ecSJonathan Lemon static ssize_t 2672e1daf0ecSJonathan Lemon sma2_show(struct device *dev, struct device_attribute *attr, char *buf) 2673e1daf0ecSJonathan Lemon { 2674e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2675e1daf0ecSJonathan Lemon 2676a509a7c6SJonathan Lemon return ptp_ocp_sma_show(bp, 2, buf, -1, 1); 2677e1daf0ecSJonathan Lemon } 2678e1daf0ecSJonathan Lemon 2679e1daf0ecSJonathan Lemon static ssize_t 2680e1daf0ecSJonathan Lemon sma3_show(struct device *dev, struct device_attribute *attr, char *buf) 2681e1daf0ecSJonathan Lemon { 2682e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2683e1daf0ecSJonathan Lemon 2684a509a7c6SJonathan Lemon return ptp_ocp_sma_show(bp, 3, buf, -1, 0); 2685e1daf0ecSJonathan Lemon } 2686e1daf0ecSJonathan Lemon 2687e1daf0ecSJonathan Lemon static ssize_t 2688e1daf0ecSJonathan Lemon sma4_show(struct device *dev, struct device_attribute *attr, char *buf) 2689e1daf0ecSJonathan Lemon { 2690e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2691e1daf0ecSJonathan Lemon 2692a509a7c6SJonathan Lemon return ptp_ocp_sma_show(bp, 4, buf, -1, 1); 2693e1daf0ecSJonathan Lemon } 2694e1daf0ecSJonathan Lemon 2695a509a7c6SJonathan Lemon static int 2696a509a7c6SJonathan Lemon ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr) 2697e1daf0ecSJonathan Lemon { 2698a509a7c6SJonathan Lemon struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1]; 2699e1daf0ecSJonathan Lemon enum ptp_ocp_sma_mode mode; 2700e1daf0ecSJonathan Lemon int val; 2701e1daf0ecSJonathan Lemon 2702e1daf0ecSJonathan Lemon mode = sma->mode; 2703caab82cdSJonathan Lemon val = sma_parse_inputs(bp->sma_op->tbl, buf, &mode); 2704e1daf0ecSJonathan Lemon if (val < 0) 2705e1daf0ecSJonathan Lemon return val; 2706e1daf0ecSJonathan Lemon 2707b2c4f0acSJonathan Lemon if (sma->fixed_dir && (mode != sma->mode || val & SMA_DISABLE)) 2708e1daf0ecSJonathan Lemon return -EOPNOTSUPP; 2709e1daf0ecSJonathan Lemon 2710a509a7c6SJonathan Lemon if (sma->fixed_fcn) { 2711ee4cd725SJonathan Lemon if (val != sma->default_fcn) 2712e1daf0ecSJonathan Lemon return -EOPNOTSUPP; 2713a509a7c6SJonathan Lemon return 0; 2714e1daf0ecSJonathan Lemon } 2715e1daf0ecSJonathan Lemon 2716b2c4f0acSJonathan Lemon sma->disabled = !!(val & SMA_DISABLE); 2717b2c4f0acSJonathan Lemon 2718a509a7c6SJonathan Lemon if (mode != sma->mode) { 2719a509a7c6SJonathan Lemon if (mode == SMA_MODE_IN) 2720caab82cdSJonathan Lemon ptp_ocp_sma_set_output(bp, sma_nr, 0); 2721e1daf0ecSJonathan Lemon else 2722caab82cdSJonathan Lemon ptp_ocp_sma_set_inputs(bp, sma_nr, 0); 2723a509a7c6SJonathan Lemon sma->mode = mode; 2724a509a7c6SJonathan Lemon } 2725a509a7c6SJonathan Lemon 2726a509a7c6SJonathan Lemon if (!sma->fixed_dir) 2727a509a7c6SJonathan Lemon val |= SMA_ENABLE; /* add enable bit */ 2728a509a7c6SJonathan Lemon 2729b2c4f0acSJonathan Lemon if (sma->disabled) 2730b2c4f0acSJonathan Lemon val = 0; 2731b2c4f0acSJonathan Lemon 2732a509a7c6SJonathan Lemon if (mode == SMA_MODE_IN) 2733caab82cdSJonathan Lemon val = ptp_ocp_sma_set_inputs(bp, sma_nr, val); 2734a509a7c6SJonathan Lemon else 2735caab82cdSJonathan Lemon val = ptp_ocp_sma_set_output(bp, sma_nr, val); 2736e1daf0ecSJonathan Lemon 2737caab82cdSJonathan Lemon return val; 2738e1daf0ecSJonathan Lemon } 2739e1daf0ecSJonathan Lemon 2740e1daf0ecSJonathan Lemon static ssize_t 2741e1daf0ecSJonathan Lemon sma1_store(struct device *dev, struct device_attribute *attr, 2742e1daf0ecSJonathan Lemon const char *buf, size_t count) 2743e1daf0ecSJonathan Lemon { 2744e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2745e1daf0ecSJonathan Lemon int err; 2746e1daf0ecSJonathan Lemon 2747a509a7c6SJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 1); 2748e1daf0ecSJonathan Lemon return err ? err : count; 2749e1daf0ecSJonathan Lemon } 2750e1daf0ecSJonathan Lemon 2751e1daf0ecSJonathan Lemon static ssize_t 2752e1daf0ecSJonathan Lemon sma2_store(struct device *dev, struct device_attribute *attr, 2753e1daf0ecSJonathan Lemon const char *buf, size_t count) 2754e1daf0ecSJonathan Lemon { 2755e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2756e1daf0ecSJonathan Lemon int err; 2757e1daf0ecSJonathan Lemon 2758a509a7c6SJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 2); 2759e1daf0ecSJonathan Lemon return err ? err : count; 2760e1daf0ecSJonathan Lemon } 2761e1daf0ecSJonathan Lemon 2762e1daf0ecSJonathan Lemon static ssize_t 2763e1daf0ecSJonathan Lemon sma3_store(struct device *dev, struct device_attribute *attr, 2764e1daf0ecSJonathan Lemon const char *buf, size_t count) 2765e1daf0ecSJonathan Lemon { 2766e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2767e1daf0ecSJonathan Lemon int err; 2768e1daf0ecSJonathan Lemon 2769a509a7c6SJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 3); 2770e1daf0ecSJonathan Lemon return err ? err : count; 2771e1daf0ecSJonathan Lemon } 2772e1daf0ecSJonathan Lemon 2773e1daf0ecSJonathan Lemon static ssize_t 2774e1daf0ecSJonathan Lemon sma4_store(struct device *dev, struct device_attribute *attr, 2775e1daf0ecSJonathan Lemon const char *buf, size_t count) 2776e1daf0ecSJonathan Lemon { 2777e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2778e1daf0ecSJonathan Lemon int err; 2779e1daf0ecSJonathan Lemon 2780a509a7c6SJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 4); 2781e1daf0ecSJonathan Lemon return err ? err : count; 2782e1daf0ecSJonathan Lemon } 2783e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma1); 2784e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma2); 2785e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma3); 2786e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma4); 2787e1daf0ecSJonathan Lemon 2788e1daf0ecSJonathan Lemon static ssize_t 2789e1daf0ecSJonathan Lemon available_sma_inputs_show(struct device *dev, 2790e1daf0ecSJonathan Lemon struct device_attribute *attr, char *buf) 2791e1daf0ecSJonathan Lemon { 2792aa56a7ffSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2793aa56a7ffSJonathan Lemon 2794caab82cdSJonathan Lemon return ptp_ocp_select_table_show(bp->sma_op->tbl[0], buf); 2795e1daf0ecSJonathan Lemon } 2796e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_inputs); 2797e1daf0ecSJonathan Lemon 2798e1daf0ecSJonathan Lemon static ssize_t 2799e1daf0ecSJonathan Lemon available_sma_outputs_show(struct device *dev, 2800e1daf0ecSJonathan Lemon struct device_attribute *attr, char *buf) 2801e1daf0ecSJonathan Lemon { 2802aa56a7ffSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2803aa56a7ffSJonathan Lemon 2804caab82cdSJonathan Lemon return ptp_ocp_select_table_show(bp->sma_op->tbl[1], buf); 2805e1daf0ecSJonathan Lemon } 2806e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_outputs); 2807e1daf0ecSJonathan Lemon 2808b325af3cSJonathan Lemon #define EXT_ATTR_RO(_group, _name, _val) \ 2809b325af3cSJonathan Lemon struct dev_ext_attribute dev_attr_##_group##_val##_##_name = \ 2810b325af3cSJonathan Lemon { __ATTR_RO(_name), (void *)_val } 2811b325af3cSJonathan Lemon #define EXT_ATTR_RW(_group, _name, _val) \ 2812b325af3cSJonathan Lemon struct dev_ext_attribute dev_attr_##_group##_val##_##_name = \ 2813b325af3cSJonathan Lemon { __ATTR_RW(_name), (void *)_val } 2814b325af3cSJonathan Lemon #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr) 2815b325af3cSJonathan Lemon 2816b325af3cSJonathan Lemon /* period [duty [phase [polarity]]] */ 2817b325af3cSJonathan Lemon static ssize_t 2818b325af3cSJonathan Lemon signal_store(struct device *dev, struct device_attribute *attr, 2819b325af3cSJonathan Lemon const char *buf, size_t count) 2820b325af3cSJonathan Lemon { 2821b325af3cSJonathan Lemon struct dev_ext_attribute *ea = to_ext_attr(attr); 2822b325af3cSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2823b325af3cSJonathan Lemon struct ptp_ocp_signal s = { }; 2824b325af3cSJonathan Lemon int gen = (uintptr_t)ea->var; 2825b325af3cSJonathan Lemon int argc, err; 2826b325af3cSJonathan Lemon char **argv; 2827b325af3cSJonathan Lemon 2828b325af3cSJonathan Lemon argv = argv_split(GFP_KERNEL, buf, &argc); 2829b325af3cSJonathan Lemon if (!argv) 2830b325af3cSJonathan Lemon return -ENOMEM; 2831b325af3cSJonathan Lemon 2832b325af3cSJonathan Lemon err = -EINVAL; 2833b325af3cSJonathan Lemon s.duty = bp->signal[gen].duty; 2834b325af3cSJonathan Lemon s.phase = bp->signal[gen].phase; 2835b325af3cSJonathan Lemon s.period = bp->signal[gen].period; 2836b325af3cSJonathan Lemon s.polarity = bp->signal[gen].polarity; 2837b325af3cSJonathan Lemon 2838b325af3cSJonathan Lemon switch (argc) { 2839b325af3cSJonathan Lemon case 4: 2840b325af3cSJonathan Lemon argc--; 2841b325af3cSJonathan Lemon err = kstrtobool(argv[argc], &s.polarity); 2842b325af3cSJonathan Lemon if (err) 2843b325af3cSJonathan Lemon goto out; 2844b325af3cSJonathan Lemon fallthrough; 2845b325af3cSJonathan Lemon case 3: 2846b325af3cSJonathan Lemon argc--; 2847b325af3cSJonathan Lemon err = kstrtou64(argv[argc], 0, &s.phase); 2848b325af3cSJonathan Lemon if (err) 2849b325af3cSJonathan Lemon goto out; 2850b325af3cSJonathan Lemon fallthrough; 2851b325af3cSJonathan Lemon case 2: 2852b325af3cSJonathan Lemon argc--; 2853b325af3cSJonathan Lemon err = kstrtoint(argv[argc], 0, &s.duty); 2854b325af3cSJonathan Lemon if (err) 2855b325af3cSJonathan Lemon goto out; 2856b325af3cSJonathan Lemon fallthrough; 2857b325af3cSJonathan Lemon case 1: 2858b325af3cSJonathan Lemon argc--; 2859b325af3cSJonathan Lemon err = kstrtou64(argv[argc], 0, &s.period); 2860b325af3cSJonathan Lemon if (err) 2861b325af3cSJonathan Lemon goto out; 2862b325af3cSJonathan Lemon break; 2863b325af3cSJonathan Lemon default: 2864b325af3cSJonathan Lemon goto out; 2865b325af3cSJonathan Lemon } 2866b325af3cSJonathan Lemon 2867b325af3cSJonathan Lemon err = ptp_ocp_signal_set(bp, gen, &s); 2868b325af3cSJonathan Lemon if (err) 2869b325af3cSJonathan Lemon goto out; 2870b325af3cSJonathan Lemon 2871b325af3cSJonathan Lemon err = ptp_ocp_signal_enable(bp->signal_out[gen], gen, s.period != 0); 2872b325af3cSJonathan Lemon 2873b325af3cSJonathan Lemon out: 2874b325af3cSJonathan Lemon argv_free(argv); 2875b325af3cSJonathan Lemon return err ? err : count; 2876b325af3cSJonathan Lemon } 2877b325af3cSJonathan Lemon 2878b325af3cSJonathan Lemon static ssize_t 2879b325af3cSJonathan Lemon signal_show(struct device *dev, struct device_attribute *attr, char *buf) 2880b325af3cSJonathan Lemon { 2881b325af3cSJonathan Lemon struct dev_ext_attribute *ea = to_ext_attr(attr); 2882b325af3cSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2883b325af3cSJonathan Lemon struct ptp_ocp_signal *signal; 2884b325af3cSJonathan Lemon struct timespec64 ts; 2885b325af3cSJonathan Lemon ssize_t count; 2886b325af3cSJonathan Lemon int i; 2887b325af3cSJonathan Lemon 2888b325af3cSJonathan Lemon i = (uintptr_t)ea->var; 2889b325af3cSJonathan Lemon signal = &bp->signal[i]; 2890b325af3cSJonathan Lemon 2891b325af3cSJonathan Lemon count = sysfs_emit(buf, "%llu %d %llu %d", signal->period, 2892b325af3cSJonathan Lemon signal->duty, signal->phase, signal->polarity); 2893b325af3cSJonathan Lemon 2894b325af3cSJonathan Lemon ts = ktime_to_timespec64(signal->start); 2895b325af3cSJonathan Lemon count += sysfs_emit_at(buf, count, " %ptT TAI\n", &ts); 2896b325af3cSJonathan Lemon 2897b325af3cSJonathan Lemon return count; 2898b325af3cSJonathan Lemon } 2899b325af3cSJonathan Lemon static EXT_ATTR_RW(signal, signal, 0); 2900b325af3cSJonathan Lemon static EXT_ATTR_RW(signal, signal, 1); 2901b325af3cSJonathan Lemon static EXT_ATTR_RW(signal, signal, 2); 2902b325af3cSJonathan Lemon static EXT_ATTR_RW(signal, signal, 3); 2903b325af3cSJonathan Lemon 2904b325af3cSJonathan Lemon static ssize_t 2905b325af3cSJonathan Lemon duty_show(struct device *dev, struct device_attribute *attr, char *buf) 2906b325af3cSJonathan Lemon { 2907b325af3cSJonathan Lemon struct dev_ext_attribute *ea = to_ext_attr(attr); 2908b325af3cSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2909b325af3cSJonathan Lemon int i = (uintptr_t)ea->var; 2910b325af3cSJonathan Lemon 2911b325af3cSJonathan Lemon return sysfs_emit(buf, "%d\n", bp->signal[i].duty); 2912b325af3cSJonathan Lemon } 2913b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, duty, 0); 2914b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, duty, 1); 2915b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, duty, 2); 2916b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, duty, 3); 2917b325af3cSJonathan Lemon 2918b325af3cSJonathan Lemon static ssize_t 2919b325af3cSJonathan Lemon period_show(struct device *dev, struct device_attribute *attr, char *buf) 2920b325af3cSJonathan Lemon { 2921b325af3cSJonathan Lemon struct dev_ext_attribute *ea = to_ext_attr(attr); 2922b325af3cSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2923b325af3cSJonathan Lemon int i = (uintptr_t)ea->var; 2924b325af3cSJonathan Lemon 2925b325af3cSJonathan Lemon return sysfs_emit(buf, "%llu\n", bp->signal[i].period); 2926b325af3cSJonathan Lemon } 2927b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, period, 0); 2928b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, period, 1); 2929b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, period, 2); 2930b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, period, 3); 2931b325af3cSJonathan Lemon 2932b325af3cSJonathan Lemon static ssize_t 2933b325af3cSJonathan Lemon phase_show(struct device *dev, struct device_attribute *attr, char *buf) 2934b325af3cSJonathan Lemon { 2935b325af3cSJonathan Lemon struct dev_ext_attribute *ea = to_ext_attr(attr); 2936b325af3cSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2937b325af3cSJonathan Lemon int i = (uintptr_t)ea->var; 2938b325af3cSJonathan Lemon 2939b325af3cSJonathan Lemon return sysfs_emit(buf, "%llu\n", bp->signal[i].phase); 2940b325af3cSJonathan Lemon } 2941b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, phase, 0); 2942b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, phase, 1); 2943b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, phase, 2); 2944b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, phase, 3); 2945b325af3cSJonathan Lemon 2946b325af3cSJonathan Lemon static ssize_t 2947b325af3cSJonathan Lemon polarity_show(struct device *dev, struct device_attribute *attr, 2948b325af3cSJonathan Lemon char *buf) 2949b325af3cSJonathan Lemon { 2950b325af3cSJonathan Lemon struct dev_ext_attribute *ea = to_ext_attr(attr); 2951b325af3cSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2952b325af3cSJonathan Lemon int i = (uintptr_t)ea->var; 2953b325af3cSJonathan Lemon 2954b325af3cSJonathan Lemon return sysfs_emit(buf, "%d\n", bp->signal[i].polarity); 2955b325af3cSJonathan Lemon } 2956b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, polarity, 0); 2957b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, polarity, 1); 2958b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, polarity, 2); 2959b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, polarity, 3); 2960b325af3cSJonathan Lemon 2961b325af3cSJonathan Lemon static ssize_t 2962b325af3cSJonathan Lemon running_show(struct device *dev, struct device_attribute *attr, char *buf) 2963b325af3cSJonathan Lemon { 2964b325af3cSJonathan Lemon struct dev_ext_attribute *ea = to_ext_attr(attr); 2965b325af3cSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2966b325af3cSJonathan Lemon int i = (uintptr_t)ea->var; 2967b325af3cSJonathan Lemon 2968b325af3cSJonathan Lemon return sysfs_emit(buf, "%d\n", bp->signal[i].running); 2969b325af3cSJonathan Lemon } 2970b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, running, 0); 2971b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, running, 1); 2972b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, running, 2); 2973b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, running, 3); 2974b325af3cSJonathan Lemon 2975b325af3cSJonathan Lemon static ssize_t 2976b325af3cSJonathan Lemon start_show(struct device *dev, struct device_attribute *attr, char *buf) 2977b325af3cSJonathan Lemon { 2978b325af3cSJonathan Lemon struct dev_ext_attribute *ea = to_ext_attr(attr); 2979b325af3cSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2980b325af3cSJonathan Lemon int i = (uintptr_t)ea->var; 2981b325af3cSJonathan Lemon struct timespec64 ts; 2982b325af3cSJonathan Lemon 2983b325af3cSJonathan Lemon ts = ktime_to_timespec64(bp->signal[i].start); 2984b325af3cSJonathan Lemon return sysfs_emit(buf, "%llu.%lu\n", ts.tv_sec, ts.tv_nsec); 2985b325af3cSJonathan Lemon } 2986b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, start, 0); 2987b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, start, 1); 2988b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, start, 2); 2989b325af3cSJonathan Lemon static EXT_ATTR_RO(signal, start, 3); 2990b325af3cSJonathan Lemon 2991773bda96SJonathan Lemon static ssize_t 29922407f5d6SJonathan Lemon seconds_store(struct device *dev, struct device_attribute *attr, 29932407f5d6SJonathan Lemon const char *buf, size_t count) 29942407f5d6SJonathan Lemon { 29952407f5d6SJonathan Lemon struct dev_ext_attribute *ea = to_ext_attr(attr); 29962407f5d6SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 29972407f5d6SJonathan Lemon int idx = (uintptr_t)ea->var; 29982407f5d6SJonathan Lemon u32 val; 29992407f5d6SJonathan Lemon int err; 30002407f5d6SJonathan Lemon 30012407f5d6SJonathan Lemon err = kstrtou32(buf, 0, &val); 30022407f5d6SJonathan Lemon if (err) 30032407f5d6SJonathan Lemon return err; 30042407f5d6SJonathan Lemon if (val > 0xff) 30052407f5d6SJonathan Lemon return -EINVAL; 30062407f5d6SJonathan Lemon 30072407f5d6SJonathan Lemon if (val) 30082407f5d6SJonathan Lemon val = (val << 8) | 0x1; 30092407f5d6SJonathan Lemon 30102407f5d6SJonathan Lemon iowrite32(val, &bp->freq_in[idx]->ctrl); 30112407f5d6SJonathan Lemon 30122407f5d6SJonathan Lemon return count; 30132407f5d6SJonathan Lemon } 30142407f5d6SJonathan Lemon 30152407f5d6SJonathan Lemon static ssize_t 30162407f5d6SJonathan Lemon seconds_show(struct device *dev, struct device_attribute *attr, char *buf) 30172407f5d6SJonathan Lemon { 30182407f5d6SJonathan Lemon struct dev_ext_attribute *ea = to_ext_attr(attr); 30192407f5d6SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 30202407f5d6SJonathan Lemon int idx = (uintptr_t)ea->var; 30212407f5d6SJonathan Lemon u32 val; 30222407f5d6SJonathan Lemon 30232407f5d6SJonathan Lemon val = ioread32(&bp->freq_in[idx]->ctrl); 30242407f5d6SJonathan Lemon if (val & 1) 30252407f5d6SJonathan Lemon val = (val >> 8) & 0xff; 30262407f5d6SJonathan Lemon else 30272407f5d6SJonathan Lemon val = 0; 30282407f5d6SJonathan Lemon 30292407f5d6SJonathan Lemon return sysfs_emit(buf, "%u\n", val); 30302407f5d6SJonathan Lemon } 30312407f5d6SJonathan Lemon static EXT_ATTR_RW(freq, seconds, 0); 30322407f5d6SJonathan Lemon static EXT_ATTR_RW(freq, seconds, 1); 30332407f5d6SJonathan Lemon static EXT_ATTR_RW(freq, seconds, 2); 30342407f5d6SJonathan Lemon static EXT_ATTR_RW(freq, seconds, 3); 30352407f5d6SJonathan Lemon 30362407f5d6SJonathan Lemon static ssize_t 30372407f5d6SJonathan Lemon frequency_show(struct device *dev, struct device_attribute *attr, char *buf) 30382407f5d6SJonathan Lemon { 30392407f5d6SJonathan Lemon struct dev_ext_attribute *ea = to_ext_attr(attr); 30402407f5d6SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 30412407f5d6SJonathan Lemon int idx = (uintptr_t)ea->var; 30422407f5d6SJonathan Lemon u32 val; 30432407f5d6SJonathan Lemon 30442407f5d6SJonathan Lemon val = ioread32(&bp->freq_in[idx]->status); 30452407f5d6SJonathan Lemon if (val & FREQ_STATUS_ERROR) 30462407f5d6SJonathan Lemon return sysfs_emit(buf, "error\n"); 30472407f5d6SJonathan Lemon if (val & FREQ_STATUS_OVERRUN) 30482407f5d6SJonathan Lemon return sysfs_emit(buf, "overrun\n"); 30492407f5d6SJonathan Lemon if (val & FREQ_STATUS_VALID) 30502407f5d6SJonathan Lemon return sysfs_emit(buf, "%lu\n", val & FREQ_STATUS_MASK); 30512407f5d6SJonathan Lemon return 0; 30522407f5d6SJonathan Lemon } 30532407f5d6SJonathan Lemon static EXT_ATTR_RO(freq, frequency, 0); 30542407f5d6SJonathan Lemon static EXT_ATTR_RO(freq, frequency, 1); 30552407f5d6SJonathan Lemon static EXT_ATTR_RO(freq, frequency, 2); 30562407f5d6SJonathan Lemon static EXT_ATTR_RO(freq, frequency, 3); 30572407f5d6SJonathan Lemon 30582407f5d6SJonathan Lemon static ssize_t 3059773bda96SJonathan Lemon serialnum_show(struct device *dev, struct device_attribute *attr, char *buf) 3060773bda96SJonathan Lemon { 3061773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 3062773bda96SJonathan Lemon 30630cfcdd1eSJonathan Lemon if (!bp->has_eeprom_data) 30640cfcdd1eSJonathan Lemon ptp_ocp_read_eeprom(bp); 3065773bda96SJonathan Lemon 3066773bda96SJonathan Lemon return sysfs_emit(buf, "%pM\n", bp->serial); 3067773bda96SJonathan Lemon } 3068773bda96SJonathan Lemon static DEVICE_ATTR_RO(serialnum); 3069773bda96SJonathan Lemon 3070773bda96SJonathan Lemon static ssize_t 3071ef0cfb34SJonathan Lemon gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf) 3072773bda96SJonathan Lemon { 3073773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 3074773bda96SJonathan Lemon ssize_t ret; 3075773bda96SJonathan Lemon 3076ef0cfb34SJonathan Lemon if (bp->gnss_lost) 3077ef0cfb34SJonathan Lemon ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost); 3078773bda96SJonathan Lemon else 3079773bda96SJonathan Lemon ret = sysfs_emit(buf, "SYNC\n"); 3080773bda96SJonathan Lemon 3081773bda96SJonathan Lemon return ret; 3082773bda96SJonathan Lemon } 3083ef0cfb34SJonathan Lemon static DEVICE_ATTR_RO(gnss_sync); 3084773bda96SJonathan Lemon 3085773bda96SJonathan Lemon static ssize_t 308689260d87SJonathan Lemon utc_tai_offset_show(struct device *dev, 308789260d87SJonathan Lemon struct device_attribute *attr, char *buf) 308889260d87SJonathan Lemon { 308989260d87SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 309089260d87SJonathan Lemon 309189260d87SJonathan Lemon return sysfs_emit(buf, "%d\n", bp->utc_tai_offset); 309289260d87SJonathan Lemon } 309389260d87SJonathan Lemon 309489260d87SJonathan Lemon static ssize_t 309589260d87SJonathan Lemon utc_tai_offset_store(struct device *dev, 309689260d87SJonathan Lemon struct device_attribute *attr, 309789260d87SJonathan Lemon const char *buf, size_t count) 309889260d87SJonathan Lemon { 309989260d87SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 310089260d87SJonathan Lemon int err; 310189260d87SJonathan Lemon u32 val; 310289260d87SJonathan Lemon 310389260d87SJonathan Lemon err = kstrtou32(buf, 0, &val); 310489260d87SJonathan Lemon if (err) 310589260d87SJonathan Lemon return err; 310689260d87SJonathan Lemon 310789260d87SJonathan Lemon ptp_ocp_utc_distribute(bp, val); 310889260d87SJonathan Lemon 310989260d87SJonathan Lemon return count; 311089260d87SJonathan Lemon } 311189260d87SJonathan Lemon static DEVICE_ATTR_RW(utc_tai_offset); 311289260d87SJonathan Lemon 311389260d87SJonathan Lemon static ssize_t 31141acffc6eSJonathan Lemon ts_window_adjust_show(struct device *dev, 31151acffc6eSJonathan Lemon struct device_attribute *attr, char *buf) 31161acffc6eSJonathan Lemon { 31171acffc6eSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 31181acffc6eSJonathan Lemon 31191acffc6eSJonathan Lemon return sysfs_emit(buf, "%d\n", bp->ts_window_adjust); 31201acffc6eSJonathan Lemon } 31211acffc6eSJonathan Lemon 31221acffc6eSJonathan Lemon static ssize_t 31231acffc6eSJonathan Lemon ts_window_adjust_store(struct device *dev, 31241acffc6eSJonathan Lemon struct device_attribute *attr, 31251acffc6eSJonathan Lemon const char *buf, size_t count) 31261acffc6eSJonathan Lemon { 31271acffc6eSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 31281acffc6eSJonathan Lemon int err; 31291acffc6eSJonathan Lemon u32 val; 31301acffc6eSJonathan Lemon 31311acffc6eSJonathan Lemon err = kstrtou32(buf, 0, &val); 31321acffc6eSJonathan Lemon if (err) 31331acffc6eSJonathan Lemon return err; 31341acffc6eSJonathan Lemon 31351acffc6eSJonathan Lemon bp->ts_window_adjust = val; 31361acffc6eSJonathan Lemon 31371acffc6eSJonathan Lemon return count; 31381acffc6eSJonathan Lemon } 31391acffc6eSJonathan Lemon static DEVICE_ATTR_RW(ts_window_adjust); 31401acffc6eSJonathan Lemon 31411acffc6eSJonathan Lemon static ssize_t 3142d14ee252SJonathan Lemon irig_b_mode_show(struct device *dev, struct device_attribute *attr, char *buf) 3143d14ee252SJonathan Lemon { 3144d14ee252SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 3145d14ee252SJonathan Lemon u32 val; 3146d14ee252SJonathan Lemon 3147d14ee252SJonathan Lemon val = ioread32(&bp->irig_out->ctrl); 3148d14ee252SJonathan Lemon val = (val >> 16) & 0x07; 3149d14ee252SJonathan Lemon return sysfs_emit(buf, "%d\n", val); 3150d14ee252SJonathan Lemon } 3151d14ee252SJonathan Lemon 3152d14ee252SJonathan Lemon static ssize_t 3153d14ee252SJonathan Lemon irig_b_mode_store(struct device *dev, 3154d14ee252SJonathan Lemon struct device_attribute *attr, 3155d14ee252SJonathan Lemon const char *buf, size_t count) 3156d14ee252SJonathan Lemon { 3157d14ee252SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 3158d14ee252SJonathan Lemon unsigned long flags; 3159d14ee252SJonathan Lemon int err; 3160d14ee252SJonathan Lemon u32 reg; 3161d14ee252SJonathan Lemon u8 val; 3162d14ee252SJonathan Lemon 3163d14ee252SJonathan Lemon err = kstrtou8(buf, 0, &val); 3164d14ee252SJonathan Lemon if (err) 3165d14ee252SJonathan Lemon return err; 3166d14ee252SJonathan Lemon if (val > 7) 3167d14ee252SJonathan Lemon return -EINVAL; 3168d14ee252SJonathan Lemon 3169d14ee252SJonathan Lemon reg = ((val & 0x7) << 16); 3170d14ee252SJonathan Lemon 3171d14ee252SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 3172d14ee252SJonathan Lemon iowrite32(0, &bp->irig_out->ctrl); /* disable */ 3173d14ee252SJonathan Lemon iowrite32(reg, &bp->irig_out->ctrl); /* change mode */ 3174d14ee252SJonathan Lemon iowrite32(reg | IRIG_M_CTRL_ENABLE, &bp->irig_out->ctrl); 3175d14ee252SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 3176d14ee252SJonathan Lemon 3177d14ee252SJonathan Lemon return count; 3178d14ee252SJonathan Lemon } 3179d14ee252SJonathan Lemon static DEVICE_ATTR_RW(irig_b_mode); 3180d14ee252SJonathan Lemon 3181d14ee252SJonathan Lemon static ssize_t 3182773bda96SJonathan Lemon clock_source_show(struct device *dev, struct device_attribute *attr, char *buf) 3183773bda96SJonathan Lemon { 3184773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 3185773bda96SJonathan Lemon const char *p; 3186773bda96SJonathan Lemon u32 select; 3187773bda96SJonathan Lemon 3188773bda96SJonathan Lemon select = ioread32(&bp->reg->select); 3189e1daf0ecSJonathan Lemon p = ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16); 3190773bda96SJonathan Lemon 3191773bda96SJonathan Lemon return sysfs_emit(buf, "%s\n", p); 3192773bda96SJonathan Lemon } 3193773bda96SJonathan Lemon 3194773bda96SJonathan Lemon static ssize_t 3195773bda96SJonathan Lemon clock_source_store(struct device *dev, struct device_attribute *attr, 3196773bda96SJonathan Lemon const char *buf, size_t count) 3197773bda96SJonathan Lemon { 3198773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 3199773bda96SJonathan Lemon unsigned long flags; 3200773bda96SJonathan Lemon int val; 3201773bda96SJonathan Lemon 3202e1daf0ecSJonathan Lemon val = ptp_ocp_select_val_from_name(ptp_ocp_clock, buf); 3203773bda96SJonathan Lemon if (val < 0) 3204773bda96SJonathan Lemon return val; 3205773bda96SJonathan Lemon 3206773bda96SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 3207773bda96SJonathan Lemon iowrite32(val, &bp->reg->select); 3208773bda96SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 3209773bda96SJonathan Lemon 3210773bda96SJonathan Lemon return count; 3211773bda96SJonathan Lemon } 3212773bda96SJonathan Lemon static DEVICE_ATTR_RW(clock_source); 3213773bda96SJonathan Lemon 3214773bda96SJonathan Lemon static ssize_t 3215773bda96SJonathan Lemon available_clock_sources_show(struct device *dev, 3216773bda96SJonathan Lemon struct device_attribute *attr, char *buf) 3217773bda96SJonathan Lemon { 3218e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_clock, buf); 3219773bda96SJonathan Lemon } 3220773bda96SJonathan Lemon static DEVICE_ATTR_RO(available_clock_sources); 3221773bda96SJonathan Lemon 32222f23f486SVadim Fedorenko static ssize_t 32232f23f486SVadim Fedorenko clock_status_drift_show(struct device *dev, 32242f23f486SVadim Fedorenko struct device_attribute *attr, char *buf) 32252f23f486SVadim Fedorenko { 32262f23f486SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 32272f23f486SVadim Fedorenko u32 val; 32282f23f486SVadim Fedorenko int res; 32292f23f486SVadim Fedorenko 32302f23f486SVadim Fedorenko val = ioread32(&bp->reg->status_drift); 32312f23f486SVadim Fedorenko res = (val & ~INT_MAX) ? -1 : 1; 32322f23f486SVadim Fedorenko res *= (val & INT_MAX); 32332f23f486SVadim Fedorenko return sysfs_emit(buf, "%d\n", res); 32342f23f486SVadim Fedorenko } 32352f23f486SVadim Fedorenko static DEVICE_ATTR_RO(clock_status_drift); 32362f23f486SVadim Fedorenko 32372f23f486SVadim Fedorenko static ssize_t 32382f23f486SVadim Fedorenko clock_status_offset_show(struct device *dev, 32392f23f486SVadim Fedorenko struct device_attribute *attr, char *buf) 32402f23f486SVadim Fedorenko { 32412f23f486SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 32422f23f486SVadim Fedorenko u32 val; 32432f23f486SVadim Fedorenko int res; 32442f23f486SVadim Fedorenko 32452f23f486SVadim Fedorenko val = ioread32(&bp->reg->status_offset); 32462f23f486SVadim Fedorenko res = (val & ~INT_MAX) ? -1 : 1; 32472f23f486SVadim Fedorenko res *= (val & INT_MAX); 32482f23f486SVadim Fedorenko return sysfs_emit(buf, "%d\n", res); 32492f23f486SVadim Fedorenko } 32502f23f486SVadim Fedorenko static DEVICE_ATTR_RO(clock_status_offset); 32512f23f486SVadim Fedorenko 325244a412d1SVadim Fedorenko static ssize_t 325344a412d1SVadim Fedorenko tod_correction_show(struct device *dev, 325444a412d1SVadim Fedorenko struct device_attribute *attr, char *buf) 325544a412d1SVadim Fedorenko { 325644a412d1SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 325744a412d1SVadim Fedorenko u32 val; 325844a412d1SVadim Fedorenko int res; 325944a412d1SVadim Fedorenko 326044a412d1SVadim Fedorenko val = ioread32(&bp->tod->adj_sec); 326144a412d1SVadim Fedorenko res = (val & ~INT_MAX) ? -1 : 1; 326244a412d1SVadim Fedorenko res *= (val & INT_MAX); 326344a412d1SVadim Fedorenko return sysfs_emit(buf, "%d\n", res); 326444a412d1SVadim Fedorenko } 326544a412d1SVadim Fedorenko 326644a412d1SVadim Fedorenko static ssize_t 326744a412d1SVadim Fedorenko tod_correction_store(struct device *dev, struct device_attribute *attr, 326844a412d1SVadim Fedorenko const char *buf, size_t count) 326944a412d1SVadim Fedorenko { 327044a412d1SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 327144a412d1SVadim Fedorenko unsigned long flags; 327244a412d1SVadim Fedorenko int err, res; 327344a412d1SVadim Fedorenko u32 val = 0; 327444a412d1SVadim Fedorenko 327544a412d1SVadim Fedorenko err = kstrtos32(buf, 0, &res); 327644a412d1SVadim Fedorenko if (err) 327744a412d1SVadim Fedorenko return err; 327844a412d1SVadim Fedorenko if (res < 0) { 327944a412d1SVadim Fedorenko res *= -1; 328044a412d1SVadim Fedorenko val |= BIT(31); 328144a412d1SVadim Fedorenko } 328244a412d1SVadim Fedorenko val |= res; 328344a412d1SVadim Fedorenko 328444a412d1SVadim Fedorenko spin_lock_irqsave(&bp->lock, flags); 328544a412d1SVadim Fedorenko iowrite32(val, &bp->tod->adj_sec); 328644a412d1SVadim Fedorenko spin_unlock_irqrestore(&bp->lock, flags); 328744a412d1SVadim Fedorenko 328844a412d1SVadim Fedorenko return count; 328944a412d1SVadim Fedorenko } 329044a412d1SVadim Fedorenko static DEVICE_ATTR_RW(tod_correction); 329144a412d1SVadim Fedorenko 3292b325af3cSJonathan Lemon #define _DEVICE_SIGNAL_GROUP_ATTRS(_nr) \ 3293b325af3cSJonathan Lemon static struct attribute *fb_timecard_signal##_nr##_attrs[] = { \ 3294b325af3cSJonathan Lemon &dev_attr_signal##_nr##_signal.attr.attr, \ 3295b325af3cSJonathan Lemon &dev_attr_signal##_nr##_duty.attr.attr, \ 3296b325af3cSJonathan Lemon &dev_attr_signal##_nr##_phase.attr.attr, \ 3297b325af3cSJonathan Lemon &dev_attr_signal##_nr##_period.attr.attr, \ 3298b325af3cSJonathan Lemon &dev_attr_signal##_nr##_polarity.attr.attr, \ 3299b325af3cSJonathan Lemon &dev_attr_signal##_nr##_running.attr.attr, \ 3300b325af3cSJonathan Lemon &dev_attr_signal##_nr##_start.attr.attr, \ 3301b325af3cSJonathan Lemon NULL, \ 3302b325af3cSJonathan Lemon } 3303b325af3cSJonathan Lemon 3304b325af3cSJonathan Lemon #define DEVICE_SIGNAL_GROUP(_name, _nr) \ 3305b325af3cSJonathan Lemon _DEVICE_SIGNAL_GROUP_ATTRS(_nr); \ 3306b325af3cSJonathan Lemon static const struct attribute_group \ 3307b325af3cSJonathan Lemon fb_timecard_signal##_nr##_group = { \ 3308b325af3cSJonathan Lemon .name = #_name, \ 3309b325af3cSJonathan Lemon .attrs = fb_timecard_signal##_nr##_attrs, \ 3310b325af3cSJonathan Lemon } 3311b325af3cSJonathan Lemon 3312b325af3cSJonathan Lemon DEVICE_SIGNAL_GROUP(gen1, 0); 3313b325af3cSJonathan Lemon DEVICE_SIGNAL_GROUP(gen2, 1); 3314b325af3cSJonathan Lemon DEVICE_SIGNAL_GROUP(gen3, 2); 3315b325af3cSJonathan Lemon DEVICE_SIGNAL_GROUP(gen4, 3); 3316b325af3cSJonathan Lemon 33172407f5d6SJonathan Lemon #define _DEVICE_FREQ_GROUP_ATTRS(_nr) \ 33182407f5d6SJonathan Lemon static struct attribute *fb_timecard_freq##_nr##_attrs[] = { \ 33192407f5d6SJonathan Lemon &dev_attr_freq##_nr##_seconds.attr.attr, \ 33202407f5d6SJonathan Lemon &dev_attr_freq##_nr##_frequency.attr.attr, \ 33212407f5d6SJonathan Lemon NULL, \ 33222407f5d6SJonathan Lemon } 33232407f5d6SJonathan Lemon 33242407f5d6SJonathan Lemon #define DEVICE_FREQ_GROUP(_name, _nr) \ 33252407f5d6SJonathan Lemon _DEVICE_FREQ_GROUP_ATTRS(_nr); \ 33262407f5d6SJonathan Lemon static const struct attribute_group \ 33272407f5d6SJonathan Lemon fb_timecard_freq##_nr##_group = { \ 33282407f5d6SJonathan Lemon .name = #_name, \ 33292407f5d6SJonathan Lemon .attrs = fb_timecard_freq##_nr##_attrs, \ 33302407f5d6SJonathan Lemon } 33312407f5d6SJonathan Lemon 33322407f5d6SJonathan Lemon DEVICE_FREQ_GROUP(freq1, 0); 33332407f5d6SJonathan Lemon DEVICE_FREQ_GROUP(freq2, 1); 33342407f5d6SJonathan Lemon DEVICE_FREQ_GROUP(freq3, 2); 33352407f5d6SJonathan Lemon DEVICE_FREQ_GROUP(freq4, 3); 33362407f5d6SJonathan Lemon 3337c205d53cSJonathan Lemon static struct attribute *fb_timecard_attrs[] = { 3338773bda96SJonathan Lemon &dev_attr_serialnum.attr, 3339ef0cfb34SJonathan Lemon &dev_attr_gnss_sync.attr, 3340773bda96SJonathan Lemon &dev_attr_clock_source.attr, 3341773bda96SJonathan Lemon &dev_attr_available_clock_sources.attr, 3342e1daf0ecSJonathan Lemon &dev_attr_sma1.attr, 3343e1daf0ecSJonathan Lemon &dev_attr_sma2.attr, 3344e1daf0ecSJonathan Lemon &dev_attr_sma3.attr, 3345e1daf0ecSJonathan Lemon &dev_attr_sma4.attr, 3346e1daf0ecSJonathan Lemon &dev_attr_available_sma_inputs.attr, 3347e1daf0ecSJonathan Lemon &dev_attr_available_sma_outputs.attr, 33482f23f486SVadim Fedorenko &dev_attr_clock_status_drift.attr, 33492f23f486SVadim Fedorenko &dev_attr_clock_status_offset.attr, 3350d14ee252SJonathan Lemon &dev_attr_irig_b_mode.attr, 335189260d87SJonathan Lemon &dev_attr_utc_tai_offset.attr, 33521acffc6eSJonathan Lemon &dev_attr_ts_window_adjust.attr, 335344a412d1SVadim Fedorenko &dev_attr_tod_correction.attr, 3354773bda96SJonathan Lemon NULL, 3355773bda96SJonathan Lemon }; 3356c205d53cSJonathan Lemon static const struct attribute_group fb_timecard_group = { 3357c205d53cSJonathan Lemon .attrs = fb_timecard_attrs, 3358c205d53cSJonathan Lemon }; 3359c205d53cSJonathan Lemon static const struct ocp_attr_group fb_timecard_groups[] = { 3360c205d53cSJonathan Lemon { .cap = OCP_CAP_BASIC, .group = &fb_timecard_group }, 3361b325af3cSJonathan Lemon { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal0_group }, 3362b325af3cSJonathan Lemon { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal1_group }, 3363b325af3cSJonathan Lemon { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal2_group }, 3364b325af3cSJonathan Lemon { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal3_group }, 33652407f5d6SJonathan Lemon { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq0_group }, 33662407f5d6SJonathan Lemon { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq1_group }, 33672407f5d6SJonathan Lemon { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq2_group }, 33682407f5d6SJonathan Lemon { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq3_group }, 3369c205d53cSJonathan Lemon { }, 3370c205d53cSJonathan Lemon }; 3371773bda96SJonathan Lemon 337269dbe107SVadim Fedorenko static struct attribute *art_timecard_attrs[] = { 337369dbe107SVadim Fedorenko &dev_attr_serialnum.attr, 337469dbe107SVadim Fedorenko &dev_attr_clock_source.attr, 337569dbe107SVadim Fedorenko &dev_attr_available_clock_sources.attr, 337669dbe107SVadim Fedorenko &dev_attr_utc_tai_offset.attr, 337769dbe107SVadim Fedorenko &dev_attr_ts_window_adjust.attr, 337869dbe107SVadim Fedorenko &dev_attr_sma1.attr, 337969dbe107SVadim Fedorenko &dev_attr_sma2.attr, 338069dbe107SVadim Fedorenko &dev_attr_sma3.attr, 338169dbe107SVadim Fedorenko &dev_attr_sma4.attr, 338269dbe107SVadim Fedorenko &dev_attr_available_sma_inputs.attr, 338369dbe107SVadim Fedorenko &dev_attr_available_sma_outputs.attr, 338469dbe107SVadim Fedorenko NULL, 338569dbe107SVadim Fedorenko }; 338669dbe107SVadim Fedorenko 338769dbe107SVadim Fedorenko static const struct attribute_group art_timecard_group = { 338869dbe107SVadim Fedorenko .attrs = art_timecard_attrs, 338969dbe107SVadim Fedorenko }; 339069dbe107SVadim Fedorenko 339169dbe107SVadim Fedorenko static const struct ocp_attr_group art_timecard_groups[] = { 339269dbe107SVadim Fedorenko { .cap = OCP_CAP_BASIC, .group = &art_timecard_group }, 339369dbe107SVadim Fedorenko { }, 339469dbe107SVadim Fedorenko }; 339569dbe107SVadim Fedorenko 3396a509a7c6SJonathan Lemon static void 3397a509a7c6SJonathan Lemon gpio_input_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit, 3398a509a7c6SJonathan Lemon const char *def) 3399f67bf662SJonathan Lemon { 3400a509a7c6SJonathan Lemon int i; 3401f67bf662SJonathan Lemon 3402a509a7c6SJonathan Lemon for (i = 0; i < 4; i++) { 3403a509a7c6SJonathan Lemon if (bp->sma[i].mode != SMA_MODE_IN) 3404a509a7c6SJonathan Lemon continue; 3405a509a7c6SJonathan Lemon if (map[i][0] & (1 << bit)) { 3406a509a7c6SJonathan Lemon sprintf(buf, "sma%d", i + 1); 3407a509a7c6SJonathan Lemon return; 3408a509a7c6SJonathan Lemon } 3409a509a7c6SJonathan Lemon } 3410a509a7c6SJonathan Lemon if (!def) 3411a509a7c6SJonathan Lemon def = "----"; 3412a509a7c6SJonathan Lemon strcpy(buf, def); 3413f67bf662SJonathan Lemon } 3414f67bf662SJonathan Lemon 3415f67bf662SJonathan Lemon static void 3416a509a7c6SJonathan Lemon gpio_output_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit) 3417f67bf662SJonathan Lemon { 3418f67bf662SJonathan Lemon char *ans = buf; 3419a509a7c6SJonathan Lemon int i; 3420f67bf662SJonathan Lemon 3421a509a7c6SJonathan Lemon strcpy(ans, "----"); 3422a509a7c6SJonathan Lemon for (i = 0; i < 4; i++) { 3423a509a7c6SJonathan Lemon if (bp->sma[i].mode != SMA_MODE_OUT) 3424a509a7c6SJonathan Lemon continue; 3425a509a7c6SJonathan Lemon if (map[i][1] & (1 << bit)) 3426a509a7c6SJonathan Lemon ans += sprintf(ans, "sma%d ", i + 1); 3427a509a7c6SJonathan Lemon } 3428f67bf662SJonathan Lemon } 3429f67bf662SJonathan Lemon 3430b325af3cSJonathan Lemon static void 3431b325af3cSJonathan Lemon _signal_summary_show(struct seq_file *s, struct ptp_ocp *bp, int nr) 3432b325af3cSJonathan Lemon { 3433b325af3cSJonathan Lemon struct signal_reg __iomem *reg = bp->signal_out[nr]->mem; 3434b325af3cSJonathan Lemon struct ptp_ocp_signal *signal = &bp->signal[nr]; 3435b325af3cSJonathan Lemon char label[8]; 3436b325af3cSJonathan Lemon bool on; 3437b325af3cSJonathan Lemon u32 val; 3438b325af3cSJonathan Lemon 3439b325af3cSJonathan Lemon if (!signal) 3440b325af3cSJonathan Lemon return; 3441b325af3cSJonathan Lemon 3442b325af3cSJonathan Lemon on = signal->running; 344305fc65f3SJonathan Lemon sprintf(label, "GEN%d", nr + 1); 3444b325af3cSJonathan Lemon seq_printf(s, "%7s: %s, period:%llu duty:%d%% phase:%llu pol:%d", 3445b325af3cSJonathan Lemon label, on ? " ON" : "OFF", 3446b325af3cSJonathan Lemon signal->period, signal->duty, signal->phase, 3447b325af3cSJonathan Lemon signal->polarity); 3448b325af3cSJonathan Lemon 3449b325af3cSJonathan Lemon val = ioread32(®->enable); 3450b325af3cSJonathan Lemon seq_printf(s, " [%x", val); 3451b325af3cSJonathan Lemon val = ioread32(®->status); 3452b325af3cSJonathan Lemon seq_printf(s, " %x]", val); 3453b325af3cSJonathan Lemon 3454b325af3cSJonathan Lemon seq_printf(s, " start:%llu\n", signal->start); 3455b325af3cSJonathan Lemon } 3456b325af3cSJonathan Lemon 34572407f5d6SJonathan Lemon static void 34582407f5d6SJonathan Lemon _frequency_summary_show(struct seq_file *s, int nr, 34592407f5d6SJonathan Lemon struct frequency_reg __iomem *reg) 34602407f5d6SJonathan Lemon { 34612407f5d6SJonathan Lemon char label[8]; 34622407f5d6SJonathan Lemon bool on; 34632407f5d6SJonathan Lemon u32 val; 34642407f5d6SJonathan Lemon 34652407f5d6SJonathan Lemon if (!reg) 34662407f5d6SJonathan Lemon return; 34672407f5d6SJonathan Lemon 346805fc65f3SJonathan Lemon sprintf(label, "FREQ%d", nr + 1); 34692407f5d6SJonathan Lemon val = ioread32(®->ctrl); 34702407f5d6SJonathan Lemon on = val & 1; 34712407f5d6SJonathan Lemon val = (val >> 8) & 0xff; 34722407f5d6SJonathan Lemon seq_printf(s, "%7s: %s, sec:%u", 34732407f5d6SJonathan Lemon label, 34742407f5d6SJonathan Lemon on ? " ON" : "OFF", 34752407f5d6SJonathan Lemon val); 34762407f5d6SJonathan Lemon 34772407f5d6SJonathan Lemon val = ioread32(®->status); 34782407f5d6SJonathan Lemon if (val & FREQ_STATUS_ERROR) 34792407f5d6SJonathan Lemon seq_printf(s, ", error"); 34802407f5d6SJonathan Lemon if (val & FREQ_STATUS_OVERRUN) 34812407f5d6SJonathan Lemon seq_printf(s, ", overrun"); 34822407f5d6SJonathan Lemon if (val & FREQ_STATUS_VALID) 34832407f5d6SJonathan Lemon seq_printf(s, ", freq %lu Hz", val & FREQ_STATUS_MASK); 34842407f5d6SJonathan Lemon seq_printf(s, " reg:%x\n", val); 34852407f5d6SJonathan Lemon } 34862407f5d6SJonathan Lemon 3487f67bf662SJonathan Lemon static int 3488f67bf662SJonathan Lemon ptp_ocp_summary_show(struct seq_file *s, void *data) 3489f67bf662SJonathan Lemon { 3490f67bf662SJonathan Lemon struct device *dev = s->private; 3491f67bf662SJonathan Lemon struct ptp_system_timestamp sts; 3492f67bf662SJonathan Lemon struct ts_reg __iomem *ts_reg; 3493b88fdbbaSJonathan Lemon char *buf, *src, *mac_src; 3494f67bf662SJonathan Lemon struct timespec64 ts; 3495f67bf662SJonathan Lemon struct ptp_ocp *bp; 34962b341f75SJonathan Lemon u16 sma_val[4][2]; 34972b341f75SJonathan Lemon u32 ctrl, val; 3498a62a56d0SJonathan Lemon bool on, map; 3499b325af3cSJonathan Lemon int i; 3500f67bf662SJonathan Lemon 3501f67bf662SJonathan Lemon buf = (char *)__get_free_page(GFP_KERNEL); 3502f67bf662SJonathan Lemon if (!buf) 3503f67bf662SJonathan Lemon return -ENOMEM; 3504f67bf662SJonathan Lemon 3505f67bf662SJonathan Lemon bp = dev_get_drvdata(dev); 3506f67bf662SJonathan Lemon 3507f67bf662SJonathan Lemon seq_printf(s, "%7s: /dev/ptp%d\n", "PTP", ptp_clock_index(bp->ptp)); 3508895ac5a5SVadim Fedorenko if (bp->gnss_port.line != -1) 3509895ac5a5SVadim Fedorenko seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS1", 3510895ac5a5SVadim Fedorenko bp->gnss_port.line); 3511895ac5a5SVadim Fedorenko if (bp->gnss2_port.line != -1) 3512895ac5a5SVadim Fedorenko seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS2", 3513895ac5a5SVadim Fedorenko bp->gnss2_port.line); 3514895ac5a5SVadim Fedorenko if (bp->mac_port.line != -1) 3515895ac5a5SVadim Fedorenko seq_printf(s, "%7s: /dev/ttyS%d\n", "MAC", bp->mac_port.line); 3516895ac5a5SVadim Fedorenko if (bp->nmea_port.line != -1) 3517895ac5a5SVadim Fedorenko seq_printf(s, "%7s: /dev/ttyS%d\n", "NMEA", bp->nmea_port.line); 3518f67bf662SJonathan Lemon 3519a509a7c6SJonathan Lemon memset(sma_val, 0xff, sizeof(sma_val)); 3520a509a7c6SJonathan Lemon if (bp->sma_map1) { 3521a509a7c6SJonathan Lemon u32 reg; 3522a509a7c6SJonathan Lemon 3523a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map1->gpio1); 3524a509a7c6SJonathan Lemon sma_val[0][0] = reg & 0xffff; 3525a509a7c6SJonathan Lemon sma_val[1][0] = reg >> 16; 3526a509a7c6SJonathan Lemon 3527a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map1->gpio2); 3528a509a7c6SJonathan Lemon sma_val[2][1] = reg & 0xffff; 3529a509a7c6SJonathan Lemon sma_val[3][1] = reg >> 16; 3530a509a7c6SJonathan Lemon 3531a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map2->gpio1); 3532a509a7c6SJonathan Lemon sma_val[2][0] = reg & 0xffff; 3533a509a7c6SJonathan Lemon sma_val[3][0] = reg >> 16; 3534a509a7c6SJonathan Lemon 3535a509a7c6SJonathan Lemon reg = ioread32(&bp->sma_map2->gpio2); 3536a509a7c6SJonathan Lemon sma_val[0][1] = reg & 0xffff; 3537a509a7c6SJonathan Lemon sma_val[1][1] = reg >> 16; 3538a509a7c6SJonathan Lemon } 3539a509a7c6SJonathan Lemon 3540f67bf662SJonathan Lemon sma1_show(dev, NULL, buf); 3541a509a7c6SJonathan Lemon seq_printf(s, " sma1: %04x,%04x %s", 3542a509a7c6SJonathan Lemon sma_val[0][0], sma_val[0][1], buf); 3543f67bf662SJonathan Lemon 3544f67bf662SJonathan Lemon sma2_show(dev, NULL, buf); 3545a509a7c6SJonathan Lemon seq_printf(s, " sma2: %04x,%04x %s", 3546a509a7c6SJonathan Lemon sma_val[1][0], sma_val[1][1], buf); 3547f67bf662SJonathan Lemon 3548f67bf662SJonathan Lemon sma3_show(dev, NULL, buf); 3549a509a7c6SJonathan Lemon seq_printf(s, " sma3: %04x,%04x %s", 3550a509a7c6SJonathan Lemon sma_val[2][0], sma_val[2][1], buf); 3551f67bf662SJonathan Lemon 3552f67bf662SJonathan Lemon sma4_show(dev, NULL, buf); 3553a509a7c6SJonathan Lemon seq_printf(s, " sma4: %04x,%04x %s", 3554a509a7c6SJonathan Lemon sma_val[3][0], sma_val[3][1], buf); 3555f67bf662SJonathan Lemon 3556f67bf662SJonathan Lemon if (bp->ts0) { 3557f67bf662SJonathan Lemon ts_reg = bp->ts0->mem; 3558f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 3559be69087cSJonathan Lemon src = "GNSS1"; 3560f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS0", 3561f67bf662SJonathan Lemon on ? " ON" : "OFF", src); 3562f67bf662SJonathan Lemon } 3563f67bf662SJonathan Lemon 3564f67bf662SJonathan Lemon if (bp->ts1) { 3565f67bf662SJonathan Lemon ts_reg = bp->ts1->mem; 3566f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 3567a509a7c6SJonathan Lemon gpio_input_map(buf, bp, sma_val, 2, NULL); 3568f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS1", 3569a509a7c6SJonathan Lemon on ? " ON" : "OFF", buf); 3570f67bf662SJonathan Lemon } 3571f67bf662SJonathan Lemon 3572f67bf662SJonathan Lemon if (bp->ts2) { 3573f67bf662SJonathan Lemon ts_reg = bp->ts2->mem; 3574f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 3575a509a7c6SJonathan Lemon gpio_input_map(buf, bp, sma_val, 3, NULL); 3576f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS2", 3577a509a7c6SJonathan Lemon on ? " ON" : "OFF", buf); 3578f67bf662SJonathan Lemon } 3579f67bf662SJonathan Lemon 35800fa3ff7eSJonathan Lemon if (bp->ts3) { 35810fa3ff7eSJonathan Lemon ts_reg = bp->ts3->mem; 35820fa3ff7eSJonathan Lemon on = ioread32(&ts_reg->enable); 35830fa3ff7eSJonathan Lemon gpio_input_map(buf, bp, sma_val, 6, NULL); 35840fa3ff7eSJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS3", 35850fa3ff7eSJonathan Lemon on ? " ON" : "OFF", buf); 35860fa3ff7eSJonathan Lemon } 35870fa3ff7eSJonathan Lemon 35880fa3ff7eSJonathan Lemon if (bp->ts4) { 35890fa3ff7eSJonathan Lemon ts_reg = bp->ts4->mem; 35900fa3ff7eSJonathan Lemon on = ioread32(&ts_reg->enable); 35910fa3ff7eSJonathan Lemon gpio_input_map(buf, bp, sma_val, 7, NULL); 35920fa3ff7eSJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS4", 35930fa3ff7eSJonathan Lemon on ? " ON" : "OFF", buf); 35940fa3ff7eSJonathan Lemon } 35950fa3ff7eSJonathan Lemon 3596a62a56d0SJonathan Lemon if (bp->pps) { 3597a62a56d0SJonathan Lemon ts_reg = bp->pps->mem; 3598a62a56d0SJonathan Lemon src = "PHC"; 3599a62a56d0SJonathan Lemon on = ioread32(&ts_reg->enable); 3600a62a56d0SJonathan Lemon map = !!(bp->pps_req_map & OCP_REQ_TIMESTAMP); 36010fa3ff7eSJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS5", 36021a575cdeSNathan Chancellor on && map ? " ON" : "OFF", src); 3603a62a56d0SJonathan Lemon 3604a62a56d0SJonathan Lemon map = !!(bp->pps_req_map & OCP_REQ_PPS); 3605a62a56d0SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "PPS", 36061a575cdeSNathan Chancellor on && map ? " ON" : "OFF", src); 3607a62a56d0SJonathan Lemon } 3608a62a56d0SJonathan Lemon 3609b325af3cSJonathan Lemon if (bp->fw_cap & OCP_CAP_SIGNAL) 3610b325af3cSJonathan Lemon for (i = 0; i < 4; i++) 3611b325af3cSJonathan Lemon _signal_summary_show(s, bp, i); 3612b325af3cSJonathan Lemon 36132407f5d6SJonathan Lemon if (bp->fw_cap & OCP_CAP_FREQ) 36142407f5d6SJonathan Lemon for (i = 0; i < 4; i++) 36152407f5d6SJonathan Lemon _frequency_summary_show(s, i, bp->freq_in[i]); 36162407f5d6SJonathan Lemon 3617f67bf662SJonathan Lemon if (bp->irig_out) { 3618f67bf662SJonathan Lemon ctrl = ioread32(&bp->irig_out->ctrl); 3619f67bf662SJonathan Lemon on = ctrl & IRIG_M_CTRL_ENABLE; 3620f67bf662SJonathan Lemon val = ioread32(&bp->irig_out->status); 3621a509a7c6SJonathan Lemon gpio_output_map(buf, bp, sma_val, 4); 3622f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, mode %d, out: %s\n", "IRIG", 3623f67bf662SJonathan Lemon on ? " ON" : "OFF", val, (ctrl >> 16), buf); 3624f67bf662SJonathan Lemon } 3625f67bf662SJonathan Lemon 3626f67bf662SJonathan Lemon if (bp->irig_in) { 3627f67bf662SJonathan Lemon on = ioread32(&bp->irig_in->ctrl) & IRIG_S_CTRL_ENABLE; 3628f67bf662SJonathan Lemon val = ioread32(&bp->irig_in->status); 3629a509a7c6SJonathan Lemon gpio_input_map(buf, bp, sma_val, 4, NULL); 3630f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, src: %s\n", "IRIG in", 3631a509a7c6SJonathan Lemon on ? " ON" : "OFF", val, buf); 3632f67bf662SJonathan Lemon } 3633f67bf662SJonathan Lemon 3634f67bf662SJonathan Lemon if (bp->dcf_out) { 3635f67bf662SJonathan Lemon on = ioread32(&bp->dcf_out->ctrl) & DCF_M_CTRL_ENABLE; 3636f67bf662SJonathan Lemon val = ioread32(&bp->dcf_out->status); 3637a509a7c6SJonathan Lemon gpio_output_map(buf, bp, sma_val, 5); 3638f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, out: %s\n", "DCF", 3639f67bf662SJonathan Lemon on ? " ON" : "OFF", val, buf); 3640f67bf662SJonathan Lemon } 3641f67bf662SJonathan Lemon 3642f67bf662SJonathan Lemon if (bp->dcf_in) { 3643f67bf662SJonathan Lemon on = ioread32(&bp->dcf_in->ctrl) & DCF_S_CTRL_ENABLE; 3644f67bf662SJonathan Lemon val = ioread32(&bp->dcf_in->status); 3645a509a7c6SJonathan Lemon gpio_input_map(buf, bp, sma_val, 5, NULL); 3646f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, src: %s\n", "DCF in", 3647a509a7c6SJonathan Lemon on ? " ON" : "OFF", val, buf); 3648f67bf662SJonathan Lemon } 3649f67bf662SJonathan Lemon 3650e3516bb4SJonathan Lemon if (bp->nmea_out) { 3651e3516bb4SJonathan Lemon on = ioread32(&bp->nmea_out->ctrl) & 1; 3652e3516bb4SJonathan Lemon val = ioread32(&bp->nmea_out->status); 3653e3516bb4SJonathan Lemon seq_printf(s, "%7s: %s, error: %d\n", "NMEA", 3654e3516bb4SJonathan Lemon on ? " ON" : "OFF", val); 3655e3516bb4SJonathan Lemon } 3656e3516bb4SJonathan Lemon 3657f67bf662SJonathan Lemon /* compute src for PPS1, used below. */ 3658f67bf662SJonathan Lemon if (bp->pps_select) { 3659f67bf662SJonathan Lemon val = ioread32(&bp->pps_select->gpio1); 3660a509a7c6SJonathan Lemon src = &buf[80]; 3661b88fdbbaSJonathan Lemon mac_src = "GNSS1"; 3662b88fdbbaSJonathan Lemon if (val & 0x01) { 3663a509a7c6SJonathan Lemon gpio_input_map(src, bp, sma_val, 0, NULL); 3664b88fdbbaSJonathan Lemon mac_src = src; 3665b88fdbbaSJonathan Lemon } else if (val & 0x02) { 3666f67bf662SJonathan Lemon src = "MAC"; 3667b88fdbbaSJonathan Lemon } else if (val & 0x04) { 3668be69087cSJonathan Lemon src = "GNSS1"; 3669b88fdbbaSJonathan Lemon } else { 3670f67bf662SJonathan Lemon src = "----"; 3671b88fdbbaSJonathan Lemon mac_src = src; 3672b88fdbbaSJonathan Lemon } 3673f67bf662SJonathan Lemon } else { 3674f67bf662SJonathan Lemon src = "?"; 3675b88fdbbaSJonathan Lemon mac_src = src; 3676f67bf662SJonathan Lemon } 3677b88fdbbaSJonathan Lemon seq_printf(s, "MAC PPS1 src: %s\n", mac_src); 3678b88fdbbaSJonathan Lemon 3679b88fdbbaSJonathan Lemon gpio_input_map(buf, bp, sma_val, 1, "GNSS2"); 3680b88fdbbaSJonathan Lemon seq_printf(s, "MAC PPS2 src: %s\n", buf); 3681f67bf662SJonathan Lemon 3682f67bf662SJonathan Lemon /* assumes automatic switchover/selection */ 3683f67bf662SJonathan Lemon val = ioread32(&bp->reg->select); 3684f67bf662SJonathan Lemon switch (val >> 16) { 3685f67bf662SJonathan Lemon case 0: 3686f67bf662SJonathan Lemon sprintf(buf, "----"); 3687f67bf662SJonathan Lemon break; 3688f67bf662SJonathan Lemon case 2: 3689f67bf662SJonathan Lemon sprintf(buf, "IRIG"); 3690f67bf662SJonathan Lemon break; 3691f67bf662SJonathan Lemon case 3: 3692f67bf662SJonathan Lemon sprintf(buf, "%s via PPS1", src); 3693f67bf662SJonathan Lemon break; 3694f67bf662SJonathan Lemon case 6: 3695f67bf662SJonathan Lemon sprintf(buf, "DCF"); 3696f67bf662SJonathan Lemon break; 3697f67bf662SJonathan Lemon default: 3698f67bf662SJonathan Lemon strcpy(buf, "unknown"); 3699f67bf662SJonathan Lemon break; 3700f67bf662SJonathan Lemon } 3701f67bf662SJonathan Lemon val = ioread32(&bp->reg->status); 3702f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, state: %s\n", "PHC src", buf, 3703f67bf662SJonathan Lemon val & OCP_STATUS_IN_SYNC ? "sync" : "unsynced"); 3704f67bf662SJonathan Lemon 3705f67bf662SJonathan Lemon if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts)) { 3706f67bf662SJonathan Lemon struct timespec64 sys_ts; 3707f67bf662SJonathan Lemon s64 pre_ns, post_ns, ns; 3708f67bf662SJonathan Lemon 3709f67bf662SJonathan Lemon pre_ns = timespec64_to_ns(&sts.pre_ts); 3710f67bf662SJonathan Lemon post_ns = timespec64_to_ns(&sts.post_ts); 3711f67bf662SJonathan Lemon ns = (pre_ns + post_ns) / 2; 3712f67bf662SJonathan Lemon ns += (s64)bp->utc_tai_offset * NSEC_PER_SEC; 3713f67bf662SJonathan Lemon sys_ts = ns_to_timespec64(ns); 3714f67bf662SJonathan Lemon 3715f67bf662SJonathan Lemon seq_printf(s, "%7s: %lld.%ld == %ptT TAI\n", "PHC", 3716f67bf662SJonathan Lemon ts.tv_sec, ts.tv_nsec, &ts); 3717f67bf662SJonathan Lemon seq_printf(s, "%7s: %lld.%ld == %ptT UTC offset %d\n", "SYS", 3718f67bf662SJonathan Lemon sys_ts.tv_sec, sys_ts.tv_nsec, &sys_ts, 3719f67bf662SJonathan Lemon bp->utc_tai_offset); 3720f67bf662SJonathan Lemon seq_printf(s, "%7s: PHC:SYS offset: %lld window: %lld\n", "", 3721f67bf662SJonathan Lemon timespec64_to_ns(&ts) - ns, 3722f67bf662SJonathan Lemon post_ns - pre_ns); 3723f67bf662SJonathan Lemon } 3724f67bf662SJonathan Lemon 3725f67bf662SJonathan Lemon free_page((unsigned long)buf); 3726f67bf662SJonathan Lemon return 0; 3727f67bf662SJonathan Lemon } 3728f67bf662SJonathan Lemon DEFINE_SHOW_ATTRIBUTE(ptp_ocp_summary); 3729f67bf662SJonathan Lemon 37309f492c4cSVadim Fedorenko static int 37319f492c4cSVadim Fedorenko ptp_ocp_tod_status_show(struct seq_file *s, void *data) 37329f492c4cSVadim Fedorenko { 37339f492c4cSVadim Fedorenko struct device *dev = s->private; 37349f492c4cSVadim Fedorenko struct ptp_ocp *bp; 37359f492c4cSVadim Fedorenko u32 val; 37369f492c4cSVadim Fedorenko int idx; 37379f492c4cSVadim Fedorenko 37389f492c4cSVadim Fedorenko bp = dev_get_drvdata(dev); 37399f492c4cSVadim Fedorenko 37409f492c4cSVadim Fedorenko val = ioread32(&bp->tod->ctrl); 37419f492c4cSVadim Fedorenko if (!(val & TOD_CTRL_ENABLE)) { 37429f492c4cSVadim Fedorenko seq_printf(s, "TOD Slave disabled\n"); 37439f492c4cSVadim Fedorenko return 0; 37449f492c4cSVadim Fedorenko } 37459f492c4cSVadim Fedorenko seq_printf(s, "TOD Slave enabled, Control Register 0x%08X\n", val); 37469f492c4cSVadim Fedorenko 37479f492c4cSVadim Fedorenko idx = val & TOD_CTRL_PROTOCOL ? 4 : 0; 37489f492c4cSVadim Fedorenko idx += (val >> 16) & 3; 37499f492c4cSVadim Fedorenko seq_printf(s, "Protocol %s\n", ptp_ocp_tod_proto_name(idx)); 37509f492c4cSVadim Fedorenko 37519f492c4cSVadim Fedorenko idx = (val >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK; 37529f492c4cSVadim Fedorenko seq_printf(s, "GNSS %s\n", ptp_ocp_tod_gnss_name(idx)); 37539f492c4cSVadim Fedorenko 37549f492c4cSVadim Fedorenko val = ioread32(&bp->tod->version); 37559f492c4cSVadim Fedorenko seq_printf(s, "TOD Version %d.%d.%d\n", 37569f492c4cSVadim Fedorenko val >> 24, (val >> 16) & 0xff, val & 0xffff); 37579f492c4cSVadim Fedorenko 37589f492c4cSVadim Fedorenko val = ioread32(&bp->tod->status); 37599f492c4cSVadim Fedorenko seq_printf(s, "Status register: 0x%08X\n", val); 37609f492c4cSVadim Fedorenko 37619f492c4cSVadim Fedorenko val = ioread32(&bp->tod->adj_sec); 37629f492c4cSVadim Fedorenko idx = (val & ~INT_MAX) ? -1 : 1; 37639f492c4cSVadim Fedorenko idx *= (val & INT_MAX); 37649f492c4cSVadim Fedorenko seq_printf(s, "Correction seconds: %d\n", idx); 37659f492c4cSVadim Fedorenko 37669f492c4cSVadim Fedorenko val = ioread32(&bp->tod->utc_status); 37679f492c4cSVadim Fedorenko seq_printf(s, "UTC status register: 0x%08X\n", val); 37681132bb29SAndy Shevchenko seq_printf(s, "UTC offset: %ld valid:%d\n", 37699f492c4cSVadim Fedorenko val & TOD_STATUS_UTC_MASK, val & TOD_STATUS_UTC_VALID ? 1 : 0); 37709f492c4cSVadim Fedorenko seq_printf(s, "Leap second info valid:%d, Leap second announce %d\n", 37719f492c4cSVadim Fedorenko val & TOD_STATUS_LEAP_VALID ? 1 : 0, 37729f492c4cSVadim Fedorenko val & TOD_STATUS_LEAP_ANNOUNCE ? 1 : 0); 37739f492c4cSVadim Fedorenko 37749f492c4cSVadim Fedorenko val = ioread32(&bp->tod->leap); 37759f492c4cSVadim Fedorenko seq_printf(s, "Time to next leap second (in sec): %d\n", (s32) val); 37769f492c4cSVadim Fedorenko 37779f492c4cSVadim Fedorenko return 0; 37789f492c4cSVadim Fedorenko } 37799f492c4cSVadim Fedorenko DEFINE_SHOW_ATTRIBUTE(ptp_ocp_tod_status); 37809f492c4cSVadim Fedorenko 3781f67bf662SJonathan Lemon static struct dentry *ptp_ocp_debugfs_root; 3782f67bf662SJonathan Lemon 3783f67bf662SJonathan Lemon static void 3784f67bf662SJonathan Lemon ptp_ocp_debugfs_add_device(struct ptp_ocp *bp) 3785f67bf662SJonathan Lemon { 3786f67bf662SJonathan Lemon struct dentry *d; 3787f67bf662SJonathan Lemon 3788f67bf662SJonathan Lemon d = debugfs_create_dir(dev_name(&bp->dev), ptp_ocp_debugfs_root); 3789f67bf662SJonathan Lemon bp->debug_root = d; 3790f67bf662SJonathan Lemon debugfs_create_file("summary", 0444, bp->debug_root, 3791f67bf662SJonathan Lemon &bp->dev, &ptp_ocp_summary_fops); 37929f492c4cSVadim Fedorenko if (bp->tod) 37939f492c4cSVadim Fedorenko debugfs_create_file("tod_status", 0444, bp->debug_root, 37949f492c4cSVadim Fedorenko &bp->dev, &ptp_ocp_tod_status_fops); 3795f67bf662SJonathan Lemon } 3796f67bf662SJonathan Lemon 3797f67bf662SJonathan Lemon static void 3798f67bf662SJonathan Lemon ptp_ocp_debugfs_remove_device(struct ptp_ocp *bp) 3799f67bf662SJonathan Lemon { 3800f67bf662SJonathan Lemon debugfs_remove_recursive(bp->debug_root); 3801f67bf662SJonathan Lemon } 3802f67bf662SJonathan Lemon 3803f67bf662SJonathan Lemon static void 3804f67bf662SJonathan Lemon ptp_ocp_debugfs_init(void) 3805f67bf662SJonathan Lemon { 3806f67bf662SJonathan Lemon ptp_ocp_debugfs_root = debugfs_create_dir("timecard", NULL); 3807f67bf662SJonathan Lemon } 3808f67bf662SJonathan Lemon 3809f67bf662SJonathan Lemon static void 3810f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(void) 3811f67bf662SJonathan Lemon { 3812f67bf662SJonathan Lemon debugfs_remove_recursive(ptp_ocp_debugfs_root); 3813f67bf662SJonathan Lemon } 3814f67bf662SJonathan Lemon 3815773bda96SJonathan Lemon static void 3816773bda96SJonathan Lemon ptp_ocp_dev_release(struct device *dev) 3817773bda96SJonathan Lemon { 3818773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 3819773bda96SJonathan Lemon 3820773bda96SJonathan Lemon mutex_lock(&ptp_ocp_lock); 3821773bda96SJonathan Lemon idr_remove(&ptp_ocp_idr, bp->id); 3822773bda96SJonathan Lemon mutex_unlock(&ptp_ocp_lock); 3823773bda96SJonathan Lemon } 3824773bda96SJonathan Lemon 3825773bda96SJonathan Lemon static int 3826773bda96SJonathan Lemon ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev) 3827773bda96SJonathan Lemon { 3828773bda96SJonathan Lemon int err; 3829773bda96SJonathan Lemon 3830773bda96SJonathan Lemon mutex_lock(&ptp_ocp_lock); 3831773bda96SJonathan Lemon err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL); 3832773bda96SJonathan Lemon mutex_unlock(&ptp_ocp_lock); 3833773bda96SJonathan Lemon if (err < 0) { 3834773bda96SJonathan Lemon dev_err(&pdev->dev, "idr_alloc failed: %d\n", err); 3835773bda96SJonathan Lemon return err; 3836773bda96SJonathan Lemon } 3837773bda96SJonathan Lemon bp->id = err; 3838773bda96SJonathan Lemon 3839773bda96SJonathan Lemon bp->ptp_info = ptp_ocp_clock_info; 3840773bda96SJonathan Lemon spin_lock_init(&bp->lock); 3841895ac5a5SVadim Fedorenko bp->gnss_port.line = -1; 3842895ac5a5SVadim Fedorenko bp->gnss2_port.line = -1; 3843895ac5a5SVadim Fedorenko bp->mac_port.line = -1; 3844895ac5a5SVadim Fedorenko bp->nmea_port.line = -1; 3845773bda96SJonathan Lemon bp->pdev = pdev; 3846773bda96SJonathan Lemon 3847773bda96SJonathan Lemon device_initialize(&bp->dev); 3848773bda96SJonathan Lemon dev_set_name(&bp->dev, "ocp%d", bp->id); 3849773bda96SJonathan Lemon bp->dev.class = &timecard_class; 3850773bda96SJonathan Lemon bp->dev.parent = &pdev->dev; 3851773bda96SJonathan Lemon bp->dev.release = ptp_ocp_dev_release; 3852773bda96SJonathan Lemon dev_set_drvdata(&bp->dev, bp); 3853773bda96SJonathan Lemon 3854773bda96SJonathan Lemon err = device_add(&bp->dev); 3855773bda96SJonathan Lemon if (err) { 3856773bda96SJonathan Lemon dev_err(&bp->dev, "device add failed: %d\n", err); 3857773bda96SJonathan Lemon goto out; 3858773bda96SJonathan Lemon } 3859773bda96SJonathan Lemon 3860773bda96SJonathan Lemon pci_set_drvdata(pdev, bp); 3861773bda96SJonathan Lemon 3862773bda96SJonathan Lemon return 0; 3863773bda96SJonathan Lemon 3864773bda96SJonathan Lemon out: 3865773bda96SJonathan Lemon ptp_ocp_dev_release(&bp->dev); 3866d12f23faSJonathan Lemon put_device(&bp->dev); 3867773bda96SJonathan Lemon return err; 3868773bda96SJonathan Lemon } 3869773bda96SJonathan Lemon 3870773bda96SJonathan Lemon static void 3871773bda96SJonathan Lemon ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link) 3872773bda96SJonathan Lemon { 3873773bda96SJonathan Lemon struct device *dev = &bp->dev; 3874773bda96SJonathan Lemon 3875773bda96SJonathan Lemon if (sysfs_create_link(&dev->kobj, &child->kobj, link)) 3876773bda96SJonathan Lemon dev_err(dev, "%s symlink failed\n", link); 3877773bda96SJonathan Lemon } 3878773bda96SJonathan Lemon 3879773bda96SJonathan Lemon static void 3880773bda96SJonathan Lemon ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link) 3881773bda96SJonathan Lemon { 3882773bda96SJonathan Lemon struct device *dev, *child; 3883773bda96SJonathan Lemon 3884773bda96SJonathan Lemon dev = &bp->pdev->dev; 3885773bda96SJonathan Lemon 3886773bda96SJonathan Lemon child = device_find_child_by_name(dev, name); 3887773bda96SJonathan Lemon if (!child) { 3888773bda96SJonathan Lemon dev_err(dev, "Could not find device %s\n", name); 3889773bda96SJonathan Lemon return; 3890773bda96SJonathan Lemon } 3891773bda96SJonathan Lemon 3892773bda96SJonathan Lemon ptp_ocp_symlink(bp, child, link); 3893773bda96SJonathan Lemon put_device(child); 3894773bda96SJonathan Lemon } 3895773bda96SJonathan Lemon 3896773bda96SJonathan Lemon static int 3897773bda96SJonathan Lemon ptp_ocp_complete(struct ptp_ocp *bp) 3898773bda96SJonathan Lemon { 3899773bda96SJonathan Lemon struct pps_device *pps; 3900773bda96SJonathan Lemon char buf[32]; 3901773bda96SJonathan Lemon 3902895ac5a5SVadim Fedorenko if (bp->gnss_port.line != -1) { 3903895ac5a5SVadim Fedorenko sprintf(buf, "ttyS%d", bp->gnss_port.line); 3904ef0cfb34SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyGNSS"); 3905773bda96SJonathan Lemon } 3906895ac5a5SVadim Fedorenko if (bp->gnss2_port.line != -1) { 3907895ac5a5SVadim Fedorenko sprintf(buf, "ttyS%d", bp->gnss2_port.line); 390871d7e085SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyGNSS2"); 390971d7e085SJonathan Lemon } 3910895ac5a5SVadim Fedorenko if (bp->mac_port.line != -1) { 3911895ac5a5SVadim Fedorenko sprintf(buf, "ttyS%d", bp->mac_port.line); 3912773bda96SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyMAC"); 3913773bda96SJonathan Lemon } 3914895ac5a5SVadim Fedorenko if (bp->nmea_port.line != -1) { 3915895ac5a5SVadim Fedorenko sprintf(buf, "ttyS%d", bp->nmea_port.line); 3916e3516bb4SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyNMEA"); 3917e3516bb4SJonathan Lemon } 3918773bda96SJonathan Lemon sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp)); 3919773bda96SJonathan Lemon ptp_ocp_link_child(bp, buf, "ptp"); 3920773bda96SJonathan Lemon 3921773bda96SJonathan Lemon pps = pps_lookup_dev(bp->ptp); 3922773bda96SJonathan Lemon if (pps) 3923773bda96SJonathan Lemon ptp_ocp_symlink(bp, pps->dev, "pps"); 3924773bda96SJonathan Lemon 3925f67bf662SJonathan Lemon ptp_ocp_debugfs_add_device(bp); 3926f67bf662SJonathan Lemon 3927773bda96SJonathan Lemon return 0; 3928773bda96SJonathan Lemon } 3929773bda96SJonathan Lemon 3930773bda96SJonathan Lemon static void 3931065efcc5SJonathan Lemon ptp_ocp_phc_info(struct ptp_ocp *bp) 3932065efcc5SJonathan Lemon { 3933065efcc5SJonathan Lemon struct timespec64 ts; 3934065efcc5SJonathan Lemon u32 version, select; 3935065efcc5SJonathan Lemon bool sync; 3936065efcc5SJonathan Lemon 3937065efcc5SJonathan Lemon version = ioread32(&bp->reg->version); 3938065efcc5SJonathan Lemon select = ioread32(&bp->reg->select); 3939065efcc5SJonathan Lemon dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n", 3940065efcc5SJonathan Lemon version >> 24, (version >> 16) & 0xff, version & 0xffff, 3941065efcc5SJonathan Lemon ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16), 3942065efcc5SJonathan Lemon ptp_clock_index(bp->ptp)); 3943065efcc5SJonathan Lemon 3944065efcc5SJonathan Lemon sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC; 3945065efcc5SJonathan Lemon if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL)) 3946065efcc5SJonathan Lemon dev_info(&bp->pdev->dev, "Time: %lld.%ld, %s\n", 3947065efcc5SJonathan Lemon ts.tv_sec, ts.tv_nsec, 3948065efcc5SJonathan Lemon sync ? "in-sync" : "UNSYNCED"); 3949065efcc5SJonathan Lemon } 3950065efcc5SJonathan Lemon 3951065efcc5SJonathan Lemon static void 3952065efcc5SJonathan Lemon ptp_ocp_serial_info(struct device *dev, const char *name, int port, int baud) 3953065efcc5SJonathan Lemon { 3954065efcc5SJonathan Lemon if (port != -1) 3955065efcc5SJonathan Lemon dev_info(dev, "%5s: /dev/ttyS%-2d @ %6d\n", name, port, baud); 3956065efcc5SJonathan Lemon } 3957065efcc5SJonathan Lemon 3958065efcc5SJonathan Lemon static void 3959065efcc5SJonathan Lemon ptp_ocp_info(struct ptp_ocp *bp) 3960773bda96SJonathan Lemon { 3961e3516bb4SJonathan Lemon static int nmea_baud[] = { 3962e3516bb4SJonathan Lemon 1200, 2400, 4800, 9600, 19200, 38400, 3963e3516bb4SJonathan Lemon 57600, 115200, 230400, 460800, 921600, 3964e3516bb4SJonathan Lemon 1000000, 2000000 3965e3516bb4SJonathan Lemon }; 3966773bda96SJonathan Lemon struct device *dev = &bp->pdev->dev; 3967e3516bb4SJonathan Lemon u32 reg; 3968773bda96SJonathan Lemon 3969065efcc5SJonathan Lemon ptp_ocp_phc_info(bp); 3970065efcc5SJonathan Lemon 3971895ac5a5SVadim Fedorenko ptp_ocp_serial_info(dev, "GNSS", bp->gnss_port.line, 3972895ac5a5SVadim Fedorenko bp->gnss_port.baud); 3973895ac5a5SVadim Fedorenko ptp_ocp_serial_info(dev, "GNSS2", bp->gnss2_port.line, 3974895ac5a5SVadim Fedorenko bp->gnss2_port.baud); 3975895ac5a5SVadim Fedorenko ptp_ocp_serial_info(dev, "MAC", bp->mac_port.line, bp->mac_port.baud); 3976895ac5a5SVadim Fedorenko if (bp->nmea_out && bp->nmea_port.line != -1) { 3977895ac5a5SVadim Fedorenko bp->nmea_port.baud = -1; 3978e3516bb4SJonathan Lemon 3979e3516bb4SJonathan Lemon reg = ioread32(&bp->nmea_out->uart_baud); 3980e3516bb4SJonathan Lemon if (reg < ARRAY_SIZE(nmea_baud)) 3981895ac5a5SVadim Fedorenko bp->nmea_port.baud = nmea_baud[reg]; 3982895ac5a5SVadim Fedorenko 3983895ac5a5SVadim Fedorenko ptp_ocp_serial_info(dev, "NMEA", bp->nmea_port.line, 3984895ac5a5SVadim Fedorenko bp->nmea_port.baud); 3985e3516bb4SJonathan Lemon } 3986773bda96SJonathan Lemon } 3987773bda96SJonathan Lemon 3988773bda96SJonathan Lemon static void 3989773bda96SJonathan Lemon ptp_ocp_detach_sysfs(struct ptp_ocp *bp) 3990773bda96SJonathan Lemon { 3991773bda96SJonathan Lemon struct device *dev = &bp->dev; 3992773bda96SJonathan Lemon 3993ef0cfb34SJonathan Lemon sysfs_remove_link(&dev->kobj, "ttyGNSS"); 399484cdf5bcSVadim Fedorenko sysfs_remove_link(&dev->kobj, "ttyGNSS2"); 3995773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "ttyMAC"); 3996773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "ptp"); 3997773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "pps"); 3998773bda96SJonathan Lemon } 3999773bda96SJonathan Lemon 4000773bda96SJonathan Lemon static void 4001773bda96SJonathan Lemon ptp_ocp_detach(struct ptp_ocp *bp) 4002773bda96SJonathan Lemon { 4003b325af3cSJonathan Lemon int i; 4004b325af3cSJonathan Lemon 4005f67bf662SJonathan Lemon ptp_ocp_debugfs_remove_device(bp); 4006773bda96SJonathan Lemon ptp_ocp_detach_sysfs(bp); 4007c2239294SJonathan Lemon ptp_ocp_attr_group_del(bp); 4008773bda96SJonathan Lemon if (timer_pending(&bp->watchdog)) 4009773bda96SJonathan Lemon del_timer_sync(&bp->watchdog); 4010773bda96SJonathan Lemon if (bp->ts0) 4011773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->ts0); 4012773bda96SJonathan Lemon if (bp->ts1) 4013773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->ts1); 4014dcf61469SJonathan Lemon if (bp->ts2) 4015dcf61469SJonathan Lemon ptp_ocp_unregister_ext(bp->ts2); 40160fa3ff7eSJonathan Lemon if (bp->ts3) 40170fa3ff7eSJonathan Lemon ptp_ocp_unregister_ext(bp->ts3); 40180fa3ff7eSJonathan Lemon if (bp->ts4) 40190fa3ff7eSJonathan Lemon ptp_ocp_unregister_ext(bp->ts4); 4020773bda96SJonathan Lemon if (bp->pps) 4021773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->pps); 4022b325af3cSJonathan Lemon for (i = 0; i < 4; i++) 4023b325af3cSJonathan Lemon if (bp->signal_out[i]) 4024b325af3cSJonathan Lemon ptp_ocp_unregister_ext(bp->signal_out[i]); 4025895ac5a5SVadim Fedorenko if (bp->gnss_port.line != -1) 4026895ac5a5SVadim Fedorenko serial8250_unregister_port(bp->gnss_port.line); 4027895ac5a5SVadim Fedorenko if (bp->gnss2_port.line != -1) 4028895ac5a5SVadim Fedorenko serial8250_unregister_port(bp->gnss2_port.line); 4029895ac5a5SVadim Fedorenko if (bp->mac_port.line != -1) 4030895ac5a5SVadim Fedorenko serial8250_unregister_port(bp->mac_port.line); 4031895ac5a5SVadim Fedorenko if (bp->nmea_port.line != -1) 4032895ac5a5SVadim Fedorenko serial8250_unregister_port(bp->nmea_port.line); 4033773bda96SJonathan Lemon platform_device_unregister(bp->spi_flash); 4034773bda96SJonathan Lemon platform_device_unregister(bp->i2c_ctrl); 4035773bda96SJonathan Lemon if (bp->i2c_clk) 4036773bda96SJonathan Lemon clk_hw_unregister_fixed_rate(bp->i2c_clk); 4037773bda96SJonathan Lemon if (bp->n_irqs) 4038773bda96SJonathan Lemon pci_free_irq_vectors(bp->pdev); 4039773bda96SJonathan Lemon if (bp->ptp) 4040773bda96SJonathan Lemon ptp_clock_unregister(bp->ptp); 40411aa66a3aSJonathan Lemon kfree(bp->ptp_info.pin_config); 4042773bda96SJonathan Lemon device_unregister(&bp->dev); 4043773bda96SJonathan Lemon } 4044773bda96SJonathan Lemon 4045a7e1abadSJonathan Lemon static int 4046a7e1abadSJonathan Lemon ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id) 4047a7e1abadSJonathan Lemon { 4048773bda96SJonathan Lemon struct devlink *devlink; 4049a7e1abadSJonathan Lemon struct ptp_ocp *bp; 4050a7e1abadSJonathan Lemon int err; 4051a7e1abadSJonathan Lemon 4052919d13a7SLeon Romanovsky devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp), &pdev->dev); 4053773bda96SJonathan Lemon if (!devlink) { 4054773bda96SJonathan Lemon dev_err(&pdev->dev, "devlink_alloc failed\n"); 4055a7e1abadSJonathan Lemon return -ENOMEM; 4056773bda96SJonathan Lemon } 4057773bda96SJonathan Lemon 4058a7e1abadSJonathan Lemon err = pci_enable_device(pdev); 4059a7e1abadSJonathan Lemon if (err) { 4060a7e1abadSJonathan Lemon dev_err(&pdev->dev, "pci_enable_device\n"); 40614587369bSJonathan Lemon goto out_free; 4062a7e1abadSJonathan Lemon } 4063a7e1abadSJonathan Lemon 4064773bda96SJonathan Lemon bp = devlink_priv(devlink); 4065773bda96SJonathan Lemon err = ptp_ocp_device_init(bp, pdev); 4066773bda96SJonathan Lemon if (err) 4067d9fdbf13SJonathan Lemon goto out_disable; 4068a7e1abadSJonathan Lemon 4069773bda96SJonathan Lemon /* compat mode. 4070773bda96SJonathan Lemon * Older FPGA firmware only returns 2 irq's. 4071773bda96SJonathan Lemon * allow this - if not all of the IRQ's are returned, skip the 4072773bda96SJonathan Lemon * extra devices and just register the clock. 4073773bda96SJonathan Lemon */ 40740fa3ff7eSJonathan Lemon err = pci_alloc_irq_vectors(pdev, 1, 17, PCI_IRQ_MSI | PCI_IRQ_MSIX); 4075773bda96SJonathan Lemon if (err < 0) { 4076773bda96SJonathan Lemon dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err); 4077773bda96SJonathan Lemon goto out; 4078a7e1abadSJonathan Lemon } 4079773bda96SJonathan Lemon bp->n_irqs = err; 4080773bda96SJonathan Lemon pci_set_master(pdev); 4081a7e1abadSJonathan Lemon 4082773bda96SJonathan Lemon err = ptp_ocp_register_resources(bp, id->driver_data); 4083a7e1abadSJonathan Lemon if (err) 4084a7e1abadSJonathan Lemon goto out; 4085a7e1abadSJonathan Lemon 4086a7e1abadSJonathan Lemon bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev); 4087a7e1abadSJonathan Lemon if (IS_ERR(bp->ptp)) { 4088a7e1abadSJonathan Lemon err = PTR_ERR(bp->ptp); 4089773bda96SJonathan Lemon dev_err(&pdev->dev, "ptp_clock_register: %d\n", err); 4090773bda96SJonathan Lemon bp->ptp = NULL; 4091a7e1abadSJonathan Lemon goto out; 4092a7e1abadSJonathan Lemon } 4093a7e1abadSJonathan Lemon 4094773bda96SJonathan Lemon err = ptp_ocp_complete(bp); 4095773bda96SJonathan Lemon if (err) 4096773bda96SJonathan Lemon goto out; 4097773bda96SJonathan Lemon 4098a7e1abadSJonathan Lemon ptp_ocp_info(bp); 4099c89f78e9SLeon Romanovsky devlink_register(devlink); 4100a7e1abadSJonathan Lemon return 0; 4101a7e1abadSJonathan Lemon 4102a7e1abadSJonathan Lemon out: 4103773bda96SJonathan Lemon ptp_ocp_detach(bp); 4104d9fdbf13SJonathan Lemon out_disable: 4105d9fdbf13SJonathan Lemon pci_disable_device(pdev); 41064587369bSJonathan Lemon out_free: 4107773bda96SJonathan Lemon devlink_free(devlink); 4108a7e1abadSJonathan Lemon return err; 4109a7e1abadSJonathan Lemon } 4110a7e1abadSJonathan Lemon 4111a7e1abadSJonathan Lemon static void 4112a7e1abadSJonathan Lemon ptp_ocp_remove(struct pci_dev *pdev) 4113a7e1abadSJonathan Lemon { 4114a7e1abadSJonathan Lemon struct ptp_ocp *bp = pci_get_drvdata(pdev); 4115773bda96SJonathan Lemon struct devlink *devlink = priv_to_devlink(bp); 4116a7e1abadSJonathan Lemon 4117c89f78e9SLeon Romanovsky devlink_unregister(devlink); 4118773bda96SJonathan Lemon ptp_ocp_detach(bp); 4119d9fdbf13SJonathan Lemon pci_disable_device(pdev); 4120773bda96SJonathan Lemon 4121773bda96SJonathan Lemon devlink_free(devlink); 4122a7e1abadSJonathan Lemon } 4123a7e1abadSJonathan Lemon 4124a7e1abadSJonathan Lemon static struct pci_driver ptp_ocp_driver = { 4125a7e1abadSJonathan Lemon .name = KBUILD_MODNAME, 4126a7e1abadSJonathan Lemon .id_table = ptp_ocp_pcidev_id, 4127a7e1abadSJonathan Lemon .probe = ptp_ocp_probe, 4128a7e1abadSJonathan Lemon .remove = ptp_ocp_remove, 4129a7e1abadSJonathan Lemon }; 4130a7e1abadSJonathan Lemon 4131773bda96SJonathan Lemon static int 4132773bda96SJonathan Lemon ptp_ocp_i2c_notifier_call(struct notifier_block *nb, 4133773bda96SJonathan Lemon unsigned long action, void *data) 4134773bda96SJonathan Lemon { 4135773bda96SJonathan Lemon struct device *dev, *child = data; 4136773bda96SJonathan Lemon struct ptp_ocp *bp; 4137773bda96SJonathan Lemon bool add; 4138773bda96SJonathan Lemon 4139773bda96SJonathan Lemon switch (action) { 4140773bda96SJonathan Lemon case BUS_NOTIFY_ADD_DEVICE: 4141773bda96SJonathan Lemon case BUS_NOTIFY_DEL_DEVICE: 4142773bda96SJonathan Lemon add = action == BUS_NOTIFY_ADD_DEVICE; 4143773bda96SJonathan Lemon break; 4144773bda96SJonathan Lemon default: 4145773bda96SJonathan Lemon return 0; 4146773bda96SJonathan Lemon } 4147773bda96SJonathan Lemon 4148773bda96SJonathan Lemon if (!i2c_verify_adapter(child)) 4149773bda96SJonathan Lemon return 0; 4150773bda96SJonathan Lemon 4151773bda96SJonathan Lemon dev = child; 4152773bda96SJonathan Lemon while ((dev = dev->parent)) 4153773bda96SJonathan Lemon if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME)) 4154773bda96SJonathan Lemon goto found; 4155773bda96SJonathan Lemon return 0; 4156773bda96SJonathan Lemon 4157773bda96SJonathan Lemon found: 4158773bda96SJonathan Lemon bp = dev_get_drvdata(dev); 4159773bda96SJonathan Lemon if (add) 4160773bda96SJonathan Lemon ptp_ocp_symlink(bp, child, "i2c"); 4161773bda96SJonathan Lemon else 4162773bda96SJonathan Lemon sysfs_remove_link(&bp->dev.kobj, "i2c"); 4163773bda96SJonathan Lemon 4164773bda96SJonathan Lemon return 0; 4165773bda96SJonathan Lemon } 4166773bda96SJonathan Lemon 4167773bda96SJonathan Lemon static struct notifier_block ptp_ocp_i2c_notifier = { 4168773bda96SJonathan Lemon .notifier_call = ptp_ocp_i2c_notifier_call, 4169773bda96SJonathan Lemon }; 4170773bda96SJonathan Lemon 4171a7e1abadSJonathan Lemon static int __init 4172a7e1abadSJonathan Lemon ptp_ocp_init(void) 4173a7e1abadSJonathan Lemon { 4174773bda96SJonathan Lemon const char *what; 4175a7e1abadSJonathan Lemon int err; 4176a7e1abadSJonathan Lemon 4177f67bf662SJonathan Lemon ptp_ocp_debugfs_init(); 4178f67bf662SJonathan Lemon 4179773bda96SJonathan Lemon what = "timecard class"; 4180773bda96SJonathan Lemon err = class_register(&timecard_class); 4181773bda96SJonathan Lemon if (err) 4182773bda96SJonathan Lemon goto out; 4183773bda96SJonathan Lemon 4184773bda96SJonathan Lemon what = "i2c notifier"; 4185773bda96SJonathan Lemon err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 4186773bda96SJonathan Lemon if (err) 4187773bda96SJonathan Lemon goto out_notifier; 4188773bda96SJonathan Lemon 4189773bda96SJonathan Lemon what = "ptp_ocp driver"; 4190a7e1abadSJonathan Lemon err = pci_register_driver(&ptp_ocp_driver); 4191773bda96SJonathan Lemon if (err) 4192773bda96SJonathan Lemon goto out_register; 4193773bda96SJonathan Lemon 4194773bda96SJonathan Lemon return 0; 4195773bda96SJonathan Lemon 4196773bda96SJonathan Lemon out_register: 4197773bda96SJonathan Lemon bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 4198773bda96SJonathan Lemon out_notifier: 4199773bda96SJonathan Lemon class_unregister(&timecard_class); 4200773bda96SJonathan Lemon out: 4201f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(); 4202773bda96SJonathan Lemon pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err); 4203a7e1abadSJonathan Lemon return err; 4204a7e1abadSJonathan Lemon } 4205a7e1abadSJonathan Lemon 4206a7e1abadSJonathan Lemon static void __exit 4207a7e1abadSJonathan Lemon ptp_ocp_fini(void) 4208a7e1abadSJonathan Lemon { 4209773bda96SJonathan Lemon bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 4210a7e1abadSJonathan Lemon pci_unregister_driver(&ptp_ocp_driver); 4211773bda96SJonathan Lemon class_unregister(&timecard_class); 4212f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(); 4213a7e1abadSJonathan Lemon } 4214a7e1abadSJonathan Lemon 4215a7e1abadSJonathan Lemon module_init(ptp_ocp_init); 4216a7e1abadSJonathan Lemon module_exit(ptp_ocp_fini); 4217a7e1abadSJonathan Lemon 4218a7e1abadSJonathan Lemon MODULE_DESCRIPTION("OpenCompute TimeCard driver"); 4219a7e1abadSJonathan Lemon MODULE_LICENSE("GPL v2"); 4220