1a7e1abadSJonathan Lemon // SPDX-License-Identifier: GPL-2.0-only 2a7e1abadSJonathan Lemon /* Copyright (c) 2020 Facebook */ 3a7e1abadSJonathan Lemon 4a7e1abadSJonathan Lemon #include <linux/err.h> 5a7e1abadSJonathan Lemon #include <linux/kernel.h> 6a7e1abadSJonathan Lemon #include <linux/module.h> 7f67bf662SJonathan Lemon #include <linux/debugfs.h> 8a7e1abadSJonathan Lemon #include <linux/init.h> 9a7e1abadSJonathan Lemon #include <linux/pci.h> 10773bda96SJonathan Lemon #include <linux/serial_8250.h> 11773bda96SJonathan Lemon #include <linux/clkdev.h> 12773bda96SJonathan Lemon #include <linux/clk-provider.h> 13773bda96SJonathan Lemon #include <linux/platform_device.h> 14*0cfcdd1eSJonathan Lemon #include <linux/platform_data/i2c-xiic.h> 15a7e1abadSJonathan Lemon #include <linux/ptp_clock_kernel.h> 16773bda96SJonathan Lemon #include <linux/spi/spi.h> 17773bda96SJonathan Lemon #include <linux/spi/xilinx_spi.h> 18773bda96SJonathan Lemon #include <net/devlink.h> 19773bda96SJonathan Lemon #include <linux/i2c.h> 20773bda96SJonathan Lemon #include <linux/mtd/mtd.h> 21*0cfcdd1eSJonathan Lemon #include <linux/nvmem-consumer.h> 22a7e1abadSJonathan Lemon 23773bda96SJonathan Lemon #ifndef PCI_VENDOR_ID_FACEBOOK 24773bda96SJonathan Lemon #define PCI_VENDOR_ID_FACEBOOK 0x1d9b 25773bda96SJonathan Lemon #endif 26773bda96SJonathan Lemon 27773bda96SJonathan Lemon #ifndef PCI_DEVICE_ID_FACEBOOK_TIMECARD 28773bda96SJonathan Lemon #define PCI_DEVICE_ID_FACEBOOK_TIMECARD 0x0400 29773bda96SJonathan Lemon #endif 30773bda96SJonathan Lemon 31773bda96SJonathan Lemon static struct class timecard_class = { 32773bda96SJonathan Lemon .owner = THIS_MODULE, 33773bda96SJonathan Lemon .name = "timecard", 34a7e1abadSJonathan Lemon }; 35a7e1abadSJonathan Lemon 36a7e1abadSJonathan Lemon struct ocp_reg { 37a7e1abadSJonathan Lemon u32 ctrl; 38a7e1abadSJonathan Lemon u32 status; 39a7e1abadSJonathan Lemon u32 select; 40a7e1abadSJonathan Lemon u32 version; 41a7e1abadSJonathan Lemon u32 time_ns; 42a7e1abadSJonathan Lemon u32 time_sec; 43a7e1abadSJonathan Lemon u32 __pad0[2]; 44a7e1abadSJonathan Lemon u32 adjust_ns; 45a7e1abadSJonathan Lemon u32 adjust_sec; 46a7e1abadSJonathan Lemon u32 __pad1[2]; 47a7e1abadSJonathan Lemon u32 offset_ns; 48a7e1abadSJonathan Lemon u32 offset_window_ns; 49773bda96SJonathan Lemon u32 __pad2[2]; 50773bda96SJonathan Lemon u32 drift_ns; 51773bda96SJonathan Lemon u32 drift_window_ns; 52773bda96SJonathan Lemon u32 __pad3[6]; 53773bda96SJonathan Lemon u32 servo_offset_p; 54773bda96SJonathan Lemon u32 servo_offset_i; 55773bda96SJonathan Lemon u32 servo_drift_p; 56773bda96SJonathan Lemon u32 servo_drift_i; 572f23f486SVadim Fedorenko u32 status_offset; 582f23f486SVadim Fedorenko u32 status_drift; 59a7e1abadSJonathan Lemon }; 60a7e1abadSJonathan Lemon 61a7e1abadSJonathan Lemon #define OCP_CTRL_ENABLE BIT(0) 62a7e1abadSJonathan Lemon #define OCP_CTRL_ADJUST_TIME BIT(1) 63a7e1abadSJonathan Lemon #define OCP_CTRL_ADJUST_OFFSET BIT(2) 64773bda96SJonathan Lemon #define OCP_CTRL_ADJUST_DRIFT BIT(3) 65773bda96SJonathan Lemon #define OCP_CTRL_ADJUST_SERVO BIT(8) 66a7e1abadSJonathan Lemon #define OCP_CTRL_READ_TIME_REQ BIT(30) 67a7e1abadSJonathan Lemon #define OCP_CTRL_READ_TIME_DONE BIT(31) 68a7e1abadSJonathan Lemon 69a7e1abadSJonathan Lemon #define OCP_STATUS_IN_SYNC BIT(0) 70773bda96SJonathan Lemon #define OCP_STATUS_IN_HOLDOVER BIT(1) 71a7e1abadSJonathan Lemon 72a7e1abadSJonathan Lemon #define OCP_SELECT_CLK_NONE 0 73773bda96SJonathan Lemon #define OCP_SELECT_CLK_REG 0xfe 74a7e1abadSJonathan Lemon 75a7e1abadSJonathan Lemon struct tod_reg { 76a7e1abadSJonathan Lemon u32 ctrl; 77a7e1abadSJonathan Lemon u32 status; 78a7e1abadSJonathan Lemon u32 uart_polarity; 79a7e1abadSJonathan Lemon u32 version; 80065efcc5SJonathan Lemon u32 adj_sec; 81a7e1abadSJonathan Lemon u32 __pad0[3]; 82a7e1abadSJonathan Lemon u32 uart_baud; 83a7e1abadSJonathan Lemon u32 __pad1[3]; 84a7e1abadSJonathan Lemon u32 utc_status; 85a7e1abadSJonathan Lemon u32 leap; 86a7e1abadSJonathan Lemon }; 87a7e1abadSJonathan Lemon 88a7e1abadSJonathan Lemon #define TOD_CTRL_PROTOCOL BIT(28) 89a7e1abadSJonathan Lemon #define TOD_CTRL_DISABLE_FMT_A BIT(17) 90a7e1abadSJonathan Lemon #define TOD_CTRL_DISABLE_FMT_B BIT(16) 91a7e1abadSJonathan Lemon #define TOD_CTRL_ENABLE BIT(0) 92a7e1abadSJonathan Lemon #define TOD_CTRL_GNSS_MASK ((1U << 4) - 1) 93a7e1abadSJonathan Lemon #define TOD_CTRL_GNSS_SHIFT 24 94a7e1abadSJonathan Lemon 95a7e1abadSJonathan Lemon #define TOD_STATUS_UTC_MASK 0xff 96a7e1abadSJonathan Lemon #define TOD_STATUS_UTC_VALID BIT(8) 979f492c4cSVadim Fedorenko #define TOD_STATUS_LEAP_ANNOUNCE BIT(12) 98a7e1abadSJonathan Lemon #define TOD_STATUS_LEAP_VALID BIT(16) 99a7e1abadSJonathan Lemon 100773bda96SJonathan Lemon struct ts_reg { 101773bda96SJonathan Lemon u32 enable; 102773bda96SJonathan Lemon u32 error; 103773bda96SJonathan Lemon u32 polarity; 104773bda96SJonathan Lemon u32 version; 105773bda96SJonathan Lemon u32 __pad0[4]; 106773bda96SJonathan Lemon u32 cable_delay; 107773bda96SJonathan Lemon u32 __pad1[3]; 108773bda96SJonathan Lemon u32 intr; 109773bda96SJonathan Lemon u32 intr_mask; 110773bda96SJonathan Lemon u32 event_count; 111773bda96SJonathan Lemon u32 __pad2[1]; 112773bda96SJonathan Lemon u32 ts_count; 113773bda96SJonathan Lemon u32 time_ns; 114773bda96SJonathan Lemon u32 time_sec; 115773bda96SJonathan Lemon u32 data_width; 116773bda96SJonathan Lemon u32 data; 117773bda96SJonathan Lemon }; 118773bda96SJonathan Lemon 119773bda96SJonathan Lemon struct pps_reg { 120773bda96SJonathan Lemon u32 ctrl; 121773bda96SJonathan Lemon u32 status; 1220d43d4f2SJonathan Lemon u32 __pad0[6]; 1230d43d4f2SJonathan Lemon u32 cable_delay; 124773bda96SJonathan Lemon }; 125773bda96SJonathan Lemon 126773bda96SJonathan Lemon #define PPS_STATUS_FILTER_ERR BIT(0) 127773bda96SJonathan Lemon #define PPS_STATUS_SUPERV_ERR BIT(1) 128773bda96SJonathan Lemon 129773bda96SJonathan Lemon struct img_reg { 130773bda96SJonathan Lemon u32 version; 131773bda96SJonathan Lemon }; 132773bda96SJonathan Lemon 133e1daf0ecSJonathan Lemon struct gpio_reg { 134e1daf0ecSJonathan Lemon u32 gpio1; 135e1daf0ecSJonathan Lemon u32 __pad0; 136e1daf0ecSJonathan Lemon u32 gpio2; 137e1daf0ecSJonathan Lemon u32 __pad1; 138e1daf0ecSJonathan Lemon }; 139e1daf0ecSJonathan Lemon 1406baf2925SJonathan Lemon struct irig_master_reg { 1416baf2925SJonathan Lemon u32 ctrl; 1426baf2925SJonathan Lemon u32 status; 1436baf2925SJonathan Lemon u32 __pad0; 1446baf2925SJonathan Lemon u32 version; 1456baf2925SJonathan Lemon u32 adj_sec; 1466baf2925SJonathan Lemon u32 mode_ctrl; 1476baf2925SJonathan Lemon }; 1486baf2925SJonathan Lemon 1496baf2925SJonathan Lemon #define IRIG_M_CTRL_ENABLE BIT(0) 1506baf2925SJonathan Lemon 1516baf2925SJonathan Lemon struct irig_slave_reg { 1526baf2925SJonathan Lemon u32 ctrl; 1536baf2925SJonathan Lemon u32 status; 1546baf2925SJonathan Lemon u32 __pad0; 1556baf2925SJonathan Lemon u32 version; 1566baf2925SJonathan Lemon u32 adj_sec; 1576baf2925SJonathan Lemon u32 mode_ctrl; 1586baf2925SJonathan Lemon }; 1596baf2925SJonathan Lemon 1606baf2925SJonathan Lemon #define IRIG_S_CTRL_ENABLE BIT(0) 1616baf2925SJonathan Lemon 1626baf2925SJonathan Lemon struct dcf_master_reg { 1636baf2925SJonathan Lemon u32 ctrl; 1646baf2925SJonathan Lemon u32 status; 1656baf2925SJonathan Lemon u32 __pad0; 1666baf2925SJonathan Lemon u32 version; 1676baf2925SJonathan Lemon u32 adj_sec; 1686baf2925SJonathan Lemon }; 1696baf2925SJonathan Lemon 1706baf2925SJonathan Lemon #define DCF_M_CTRL_ENABLE BIT(0) 1716baf2925SJonathan Lemon 1726baf2925SJonathan Lemon struct dcf_slave_reg { 1736baf2925SJonathan Lemon u32 ctrl; 1746baf2925SJonathan Lemon u32 status; 1756baf2925SJonathan Lemon u32 __pad0; 1766baf2925SJonathan Lemon u32 version; 1776baf2925SJonathan Lemon u32 adj_sec; 1786baf2925SJonathan Lemon }; 1796baf2925SJonathan Lemon 1806baf2925SJonathan Lemon #define DCF_S_CTRL_ENABLE BIT(0) 1816baf2925SJonathan Lemon 182773bda96SJonathan Lemon struct ptp_ocp_flash_info { 183773bda96SJonathan Lemon const char *name; 184773bda96SJonathan Lemon int pci_offset; 185773bda96SJonathan Lemon int data_size; 186773bda96SJonathan Lemon void *data; 187773bda96SJonathan Lemon }; 188773bda96SJonathan Lemon 1891618df6aSJonathan Lemon struct ptp_ocp_i2c_info { 1901618df6aSJonathan Lemon const char *name; 1911618df6aSJonathan Lemon unsigned long fixed_rate; 1921618df6aSJonathan Lemon size_t data_size; 1931618df6aSJonathan Lemon void *data; 1941618df6aSJonathan Lemon }; 1951618df6aSJonathan Lemon 196773bda96SJonathan Lemon struct ptp_ocp_ext_info { 197773bda96SJonathan Lemon int index; 198773bda96SJonathan Lemon irqreturn_t (*irq_fcn)(int irq, void *priv); 199a62a56d0SJonathan Lemon int (*enable)(void *priv, u32 req, bool enable); 200773bda96SJonathan Lemon }; 201773bda96SJonathan Lemon 202773bda96SJonathan Lemon struct ptp_ocp_ext_src { 203773bda96SJonathan Lemon void __iomem *mem; 204773bda96SJonathan Lemon struct ptp_ocp *bp; 205773bda96SJonathan Lemon struct ptp_ocp_ext_info *info; 206773bda96SJonathan Lemon int irq_vec; 207773bda96SJonathan Lemon }; 208773bda96SJonathan Lemon 209*0cfcdd1eSJonathan Lemon #define OCP_BOARD_ID_LEN 13 210*0cfcdd1eSJonathan Lemon #define OCP_SERIAL_LEN 6 211*0cfcdd1eSJonathan Lemon 212a7e1abadSJonathan Lemon struct ptp_ocp { 213a7e1abadSJonathan Lemon struct pci_dev *pdev; 214773bda96SJonathan Lemon struct device dev; 215a7e1abadSJonathan Lemon spinlock_t lock; 216a7e1abadSJonathan Lemon struct ocp_reg __iomem *reg; 217a7e1abadSJonathan Lemon struct tod_reg __iomem *tod; 2180d43d4f2SJonathan Lemon struct pps_reg __iomem *pps_to_ext; 2190d43d4f2SJonathan Lemon struct pps_reg __iomem *pps_to_clk; 220f67bf662SJonathan Lemon struct gpio_reg __iomem *pps_select; 221e1daf0ecSJonathan Lemon struct gpio_reg __iomem *sma; 2226baf2925SJonathan Lemon struct irig_master_reg __iomem *irig_out; 2236baf2925SJonathan Lemon struct irig_slave_reg __iomem *irig_in; 2246baf2925SJonathan Lemon struct dcf_master_reg __iomem *dcf_out; 2256baf2925SJonathan Lemon struct dcf_slave_reg __iomem *dcf_in; 226e3516bb4SJonathan Lemon struct tod_reg __iomem *nmea_out; 227773bda96SJonathan Lemon struct ptp_ocp_ext_src *pps; 228773bda96SJonathan Lemon struct ptp_ocp_ext_src *ts0; 229773bda96SJonathan Lemon struct ptp_ocp_ext_src *ts1; 230dcf61469SJonathan Lemon struct ptp_ocp_ext_src *ts2; 231773bda96SJonathan Lemon struct img_reg __iomem *image; 232a7e1abadSJonathan Lemon struct ptp_clock *ptp; 233a7e1abadSJonathan Lemon struct ptp_clock_info ptp_info; 234773bda96SJonathan Lemon struct platform_device *i2c_ctrl; 235773bda96SJonathan Lemon struct platform_device *spi_flash; 236773bda96SJonathan Lemon struct clk_hw *i2c_clk; 237773bda96SJonathan Lemon struct timer_list watchdog; 238*0cfcdd1eSJonathan Lemon const struct ptp_ocp_eeprom_map *eeprom_map; 239f67bf662SJonathan Lemon struct dentry *debug_root; 240ef0cfb34SJonathan Lemon time64_t gnss_lost; 241773bda96SJonathan Lemon int id; 242773bda96SJonathan Lemon int n_irqs; 243ef0cfb34SJonathan Lemon int gnss_port; 24471d7e085SJonathan Lemon int gnss2_port; 245773bda96SJonathan Lemon int mac_port; /* miniature atomic clock */ 246e3516bb4SJonathan Lemon int nmea_port; 247*0cfcdd1eSJonathan Lemon u8 board_id[OCP_BOARD_ID_LEN]; 248*0cfcdd1eSJonathan Lemon u8 serial[OCP_SERIAL_LEN]; 249*0cfcdd1eSJonathan Lemon bool has_eeprom_data; 250a62a56d0SJonathan Lemon u32 pps_req_map; 25189260d87SJonathan Lemon int flash_start; 25289260d87SJonathan Lemon u32 utc_tai_offset; 2531acffc6eSJonathan Lemon u32 ts_window_adjust; 254a7e1abadSJonathan Lemon }; 255a7e1abadSJonathan Lemon 256a62a56d0SJonathan Lemon #define OCP_REQ_TIMESTAMP BIT(0) 257a62a56d0SJonathan Lemon #define OCP_REQ_PPS BIT(1) 258a62a56d0SJonathan Lemon 259773bda96SJonathan Lemon struct ocp_resource { 260773bda96SJonathan Lemon unsigned long offset; 261773bda96SJonathan Lemon int size; 262773bda96SJonathan Lemon int irq_vec; 263773bda96SJonathan Lemon int (*setup)(struct ptp_ocp *bp, struct ocp_resource *r); 264773bda96SJonathan Lemon void *extra; 265773bda96SJonathan Lemon unsigned long bp_offset; 26656ec4403SJonathan Lemon const char * const name; 267773bda96SJonathan Lemon }; 268773bda96SJonathan Lemon 269773bda96SJonathan Lemon static int ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r); 270773bda96SJonathan Lemon static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r); 271773bda96SJonathan Lemon static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r); 272773bda96SJonathan Lemon static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r); 273773bda96SJonathan Lemon static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r); 274773bda96SJonathan Lemon static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r); 275773bda96SJonathan Lemon static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv); 276a62a56d0SJonathan Lemon static int ptp_ocp_ts_enable(void *priv, u32 req, bool enable); 277773bda96SJonathan Lemon 278*0cfcdd1eSJonathan Lemon struct ptp_ocp_eeprom_map { 279*0cfcdd1eSJonathan Lemon u16 off; 280*0cfcdd1eSJonathan Lemon u16 len; 281*0cfcdd1eSJonathan Lemon u32 bp_offset; 282*0cfcdd1eSJonathan Lemon const void * const tag; 283*0cfcdd1eSJonathan Lemon }; 284*0cfcdd1eSJonathan Lemon 285*0cfcdd1eSJonathan Lemon #define EEPROM_ENTRY(addr, member) \ 286*0cfcdd1eSJonathan Lemon .off = addr, \ 287*0cfcdd1eSJonathan Lemon .len = sizeof_field(struct ptp_ocp, member), \ 288*0cfcdd1eSJonathan Lemon .bp_offset = offsetof(struct ptp_ocp, member) 289*0cfcdd1eSJonathan Lemon 290*0cfcdd1eSJonathan Lemon #define BP_MAP_ENTRY_ADDR(bp, map) ({ \ 291*0cfcdd1eSJonathan Lemon (void *)((uintptr_t)(bp) + (map)->bp_offset); \ 292*0cfcdd1eSJonathan Lemon }) 293*0cfcdd1eSJonathan Lemon 294*0cfcdd1eSJonathan Lemon static struct ptp_ocp_eeprom_map fb_eeprom_map[] = { 295*0cfcdd1eSJonathan Lemon { EEPROM_ENTRY(0x43, board_id) }, 296*0cfcdd1eSJonathan Lemon { EEPROM_ENTRY(0x00, serial), .tag = "mac" }, 297*0cfcdd1eSJonathan Lemon { } 298*0cfcdd1eSJonathan Lemon }; 299*0cfcdd1eSJonathan Lemon 300773bda96SJonathan Lemon #define bp_assign_entry(bp, res, val) ({ \ 301773bda96SJonathan Lemon uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset; \ 302773bda96SJonathan Lemon *(typeof(val) *)addr = val; \ 303773bda96SJonathan Lemon }) 304773bda96SJonathan Lemon 305773bda96SJonathan Lemon #define OCP_RES_LOCATION(member) \ 30656ec4403SJonathan Lemon .name = #member, .bp_offset = offsetof(struct ptp_ocp, member) 307773bda96SJonathan Lemon 308773bda96SJonathan Lemon #define OCP_MEM_RESOURCE(member) \ 309773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_mem 310773bda96SJonathan Lemon 311773bda96SJonathan Lemon #define OCP_SERIAL_RESOURCE(member) \ 312773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_serial 313773bda96SJonathan Lemon 314773bda96SJonathan Lemon #define OCP_I2C_RESOURCE(member) \ 315773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_i2c 316773bda96SJonathan Lemon 317773bda96SJonathan Lemon #define OCP_SPI_RESOURCE(member) \ 318773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_spi 319773bda96SJonathan Lemon 320773bda96SJonathan Lemon #define OCP_EXT_RESOURCE(member) \ 321773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_ext 322773bda96SJonathan Lemon 323773bda96SJonathan Lemon /* This is the MSI vector mapping used. 324a62a56d0SJonathan Lemon * 0: TS3 (and PPS) 325773bda96SJonathan Lemon * 1: TS0 326773bda96SJonathan Lemon * 2: TS1 32771d7e085SJonathan Lemon * 3: GNSS 32871d7e085SJonathan Lemon * 4: GNSS2 329773bda96SJonathan Lemon * 5: MAC 330dcf61469SJonathan Lemon * 6: TS2 3311447149dSJonathan Lemon * 7: I2C controller 332e3516bb4SJonathan Lemon * 8: HWICAP (notused) 333773bda96SJonathan Lemon * 9: SPI Flash 334e3516bb4SJonathan Lemon * 10: NMEA 335773bda96SJonathan Lemon */ 336773bda96SJonathan Lemon 337773bda96SJonathan Lemon static struct ocp_resource ocp_fb_resource[] = { 338773bda96SJonathan Lemon { 339773bda96SJonathan Lemon OCP_MEM_RESOURCE(reg), 340773bda96SJonathan Lemon .offset = 0x01000000, .size = 0x10000, 341773bda96SJonathan Lemon }, 342773bda96SJonathan Lemon { 343773bda96SJonathan Lemon OCP_EXT_RESOURCE(ts0), 344773bda96SJonathan Lemon .offset = 0x01010000, .size = 0x10000, .irq_vec = 1, 345773bda96SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 34656ec4403SJonathan Lemon .index = 0, 347773bda96SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 348773bda96SJonathan Lemon .enable = ptp_ocp_ts_enable, 349773bda96SJonathan Lemon }, 350773bda96SJonathan Lemon }, 351773bda96SJonathan Lemon { 352773bda96SJonathan Lemon OCP_EXT_RESOURCE(ts1), 353773bda96SJonathan Lemon .offset = 0x01020000, .size = 0x10000, .irq_vec = 2, 354773bda96SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 35556ec4403SJonathan Lemon .index = 1, 356773bda96SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 357773bda96SJonathan Lemon .enable = ptp_ocp_ts_enable, 358773bda96SJonathan Lemon }, 359773bda96SJonathan Lemon }, 360773bda96SJonathan Lemon { 361dcf61469SJonathan Lemon OCP_EXT_RESOURCE(ts2), 362dcf61469SJonathan Lemon .offset = 0x01060000, .size = 0x10000, .irq_vec = 6, 363dcf61469SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 364dcf61469SJonathan Lemon .index = 2, 365dcf61469SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 366dcf61469SJonathan Lemon .enable = ptp_ocp_ts_enable, 367dcf61469SJonathan Lemon }, 368dcf61469SJonathan Lemon }, 369dcf61469SJonathan Lemon { 370a62a56d0SJonathan Lemon OCP_EXT_RESOURCE(pps), 371a62a56d0SJonathan Lemon .offset = 0x010C0000, .size = 0x10000, .irq_vec = 0, 372a62a56d0SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 373a62a56d0SJonathan Lemon .index = 3, 374a62a56d0SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 375a62a56d0SJonathan Lemon .enable = ptp_ocp_ts_enable, 376a62a56d0SJonathan Lemon }, 377a62a56d0SJonathan Lemon }, 378a62a56d0SJonathan Lemon { 3790d43d4f2SJonathan Lemon OCP_MEM_RESOURCE(pps_to_ext), 3800d43d4f2SJonathan Lemon .offset = 0x01030000, .size = 0x10000, 3810d43d4f2SJonathan Lemon }, 3820d43d4f2SJonathan Lemon { 3830d43d4f2SJonathan Lemon OCP_MEM_RESOURCE(pps_to_clk), 384773bda96SJonathan Lemon .offset = 0x01040000, .size = 0x10000, 385773bda96SJonathan Lemon }, 386773bda96SJonathan Lemon { 387773bda96SJonathan Lemon OCP_MEM_RESOURCE(tod), 388773bda96SJonathan Lemon .offset = 0x01050000, .size = 0x10000, 389773bda96SJonathan Lemon }, 390773bda96SJonathan Lemon { 3916baf2925SJonathan Lemon OCP_MEM_RESOURCE(irig_in), 3926baf2925SJonathan Lemon .offset = 0x01070000, .size = 0x10000, 3936baf2925SJonathan Lemon }, 3946baf2925SJonathan Lemon { 3956baf2925SJonathan Lemon OCP_MEM_RESOURCE(irig_out), 3966baf2925SJonathan Lemon .offset = 0x01080000, .size = 0x10000, 3976baf2925SJonathan Lemon }, 3986baf2925SJonathan Lemon { 3996baf2925SJonathan Lemon OCP_MEM_RESOURCE(dcf_in), 4006baf2925SJonathan Lemon .offset = 0x01090000, .size = 0x10000, 4016baf2925SJonathan Lemon }, 4026baf2925SJonathan Lemon { 4036baf2925SJonathan Lemon OCP_MEM_RESOURCE(dcf_out), 4046baf2925SJonathan Lemon .offset = 0x010A0000, .size = 0x10000, 4056baf2925SJonathan Lemon }, 4066baf2925SJonathan Lemon { 407e3516bb4SJonathan Lemon OCP_MEM_RESOURCE(nmea_out), 408e3516bb4SJonathan Lemon .offset = 0x010B0000, .size = 0x10000, 409e3516bb4SJonathan Lemon }, 410e3516bb4SJonathan Lemon { 411773bda96SJonathan Lemon OCP_MEM_RESOURCE(image), 412773bda96SJonathan Lemon .offset = 0x00020000, .size = 0x1000, 413773bda96SJonathan Lemon }, 414773bda96SJonathan Lemon { 415f67bf662SJonathan Lemon OCP_MEM_RESOURCE(pps_select), 416f67bf662SJonathan Lemon .offset = 0x00130000, .size = 0x1000, 417f67bf662SJonathan Lemon }, 418f67bf662SJonathan Lemon { 419e1daf0ecSJonathan Lemon OCP_MEM_RESOURCE(sma), 420e1daf0ecSJonathan Lemon .offset = 0x00140000, .size = 0x1000, 421e1daf0ecSJonathan Lemon }, 422e1daf0ecSJonathan Lemon { 423773bda96SJonathan Lemon OCP_I2C_RESOURCE(i2c_ctrl), 424773bda96SJonathan Lemon .offset = 0x00150000, .size = 0x10000, .irq_vec = 7, 4251618df6aSJonathan Lemon .extra = &(struct ptp_ocp_i2c_info) { 4261618df6aSJonathan Lemon .name = "xiic-i2c", 4271618df6aSJonathan Lemon .fixed_rate = 50000000, 428*0cfcdd1eSJonathan Lemon .data_size = sizeof(struct xiic_i2c_platform_data), 429*0cfcdd1eSJonathan Lemon .data = &(struct xiic_i2c_platform_data) { 430*0cfcdd1eSJonathan Lemon .num_devices = 2, 431*0cfcdd1eSJonathan Lemon .devices = (struct i2c_board_info[]) { 432*0cfcdd1eSJonathan Lemon { I2C_BOARD_INFO("24c02", 0x50) }, 433*0cfcdd1eSJonathan Lemon { I2C_BOARD_INFO("24mac402", 0x58), 434*0cfcdd1eSJonathan Lemon .platform_data = "mac" }, 435*0cfcdd1eSJonathan Lemon }, 436*0cfcdd1eSJonathan Lemon }, 4371618df6aSJonathan Lemon }, 438773bda96SJonathan Lemon }, 439773bda96SJonathan Lemon { 440ef0cfb34SJonathan Lemon OCP_SERIAL_RESOURCE(gnss_port), 441773bda96SJonathan Lemon .offset = 0x00160000 + 0x1000, .irq_vec = 3, 442773bda96SJonathan Lemon }, 443773bda96SJonathan Lemon { 44471d7e085SJonathan Lemon OCP_SERIAL_RESOURCE(gnss2_port), 44571d7e085SJonathan Lemon .offset = 0x00170000 + 0x1000, .irq_vec = 4, 44671d7e085SJonathan Lemon }, 44771d7e085SJonathan Lemon { 448773bda96SJonathan Lemon OCP_SERIAL_RESOURCE(mac_port), 449773bda96SJonathan Lemon .offset = 0x00180000 + 0x1000, .irq_vec = 5, 450773bda96SJonathan Lemon }, 451773bda96SJonathan Lemon { 452e3516bb4SJonathan Lemon OCP_SERIAL_RESOURCE(nmea_port), 453e3516bb4SJonathan Lemon .offset = 0x00190000 + 0x1000, .irq_vec = 10, 454e3516bb4SJonathan Lemon }, 455e3516bb4SJonathan Lemon { 456773bda96SJonathan Lemon OCP_SPI_RESOURCE(spi_flash), 457773bda96SJonathan Lemon .offset = 0x00310000, .size = 0x10000, .irq_vec = 9, 458773bda96SJonathan Lemon .extra = &(struct ptp_ocp_flash_info) { 459773bda96SJonathan Lemon .name = "xilinx_spi", .pci_offset = 0, 460773bda96SJonathan Lemon .data_size = sizeof(struct xspi_platform_data), 461773bda96SJonathan Lemon .data = &(struct xspi_platform_data) { 462773bda96SJonathan Lemon .num_chipselect = 1, 463773bda96SJonathan Lemon .bits_per_word = 8, 464773bda96SJonathan Lemon .num_devices = 1, 465773bda96SJonathan Lemon .devices = &(struct spi_board_info) { 466773bda96SJonathan Lemon .modalias = "spi-nor", 467773bda96SJonathan Lemon }, 468773bda96SJonathan Lemon }, 469773bda96SJonathan Lemon }, 470773bda96SJonathan Lemon }, 471773bda96SJonathan Lemon { 472773bda96SJonathan Lemon .setup = ptp_ocp_fb_board_init, 473773bda96SJonathan Lemon }, 474773bda96SJonathan Lemon { } 475773bda96SJonathan Lemon }; 476773bda96SJonathan Lemon 477773bda96SJonathan Lemon static const struct pci_device_id ptp_ocp_pcidev_id[] = { 478773bda96SJonathan Lemon { PCI_DEVICE_DATA(FACEBOOK, TIMECARD, &ocp_fb_resource) }, 479773bda96SJonathan Lemon { 0 } 480773bda96SJonathan Lemon }; 481773bda96SJonathan Lemon MODULE_DEVICE_TABLE(pci, ptp_ocp_pcidev_id); 482773bda96SJonathan Lemon 483773bda96SJonathan Lemon static DEFINE_MUTEX(ptp_ocp_lock); 484773bda96SJonathan Lemon static DEFINE_IDR(ptp_ocp_idr); 485773bda96SJonathan Lemon 486e1daf0ecSJonathan Lemon struct ocp_selector { 487773bda96SJonathan Lemon const char *name; 488773bda96SJonathan Lemon int value; 489e1daf0ecSJonathan Lemon }; 490e1daf0ecSJonathan Lemon 491e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_clock[] = { 492773bda96SJonathan Lemon { .name = "NONE", .value = 0 }, 493773bda96SJonathan Lemon { .name = "TOD", .value = 1 }, 494773bda96SJonathan Lemon { .name = "IRIG", .value = 2 }, 495773bda96SJonathan Lemon { .name = "PPS", .value = 3 }, 496773bda96SJonathan Lemon { .name = "PTP", .value = 4 }, 497773bda96SJonathan Lemon { .name = "RTC", .value = 5 }, 498773bda96SJonathan Lemon { .name = "DCF", .value = 6 }, 499773bda96SJonathan Lemon { .name = "REGS", .value = 0xfe }, 500773bda96SJonathan Lemon { .name = "EXT", .value = 0xff }, 501e1daf0ecSJonathan Lemon { } 502e1daf0ecSJonathan Lemon }; 503e1daf0ecSJonathan Lemon 504e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_sma_in[] = { 505e1daf0ecSJonathan Lemon { .name = "10Mhz", .value = 0x00 }, 506e1daf0ecSJonathan Lemon { .name = "PPS1", .value = 0x01 }, 507e1daf0ecSJonathan Lemon { .name = "PPS2", .value = 0x02 }, 508e1daf0ecSJonathan Lemon { .name = "TS1", .value = 0x04 }, 509e1daf0ecSJonathan Lemon { .name = "TS2", .value = 0x08 }, 5106baf2925SJonathan Lemon { .name = "IRIG", .value = 0x10 }, 5116baf2925SJonathan Lemon { .name = "DCF", .value = 0x20 }, 512e1daf0ecSJonathan Lemon { } 513e1daf0ecSJonathan Lemon }; 514e1daf0ecSJonathan Lemon 515e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_sma_out[] = { 516e1daf0ecSJonathan Lemon { .name = "10Mhz", .value = 0x00 }, 517e1daf0ecSJonathan Lemon { .name = "PHC", .value = 0x01 }, 518e1daf0ecSJonathan Lemon { .name = "MAC", .value = 0x02 }, 519e1daf0ecSJonathan Lemon { .name = "GNSS", .value = 0x04 }, 520e1daf0ecSJonathan Lemon { .name = "GNSS2", .value = 0x08 }, 5216baf2925SJonathan Lemon { .name = "IRIG", .value = 0x10 }, 5226baf2925SJonathan Lemon { .name = "DCF", .value = 0x20 }, 523e1daf0ecSJonathan Lemon { } 524773bda96SJonathan Lemon }; 525773bda96SJonathan Lemon 526773bda96SJonathan Lemon static const char * 527e1daf0ecSJonathan Lemon ptp_ocp_select_name_from_val(struct ocp_selector *tbl, int val) 528773bda96SJonathan Lemon { 529773bda96SJonathan Lemon int i; 530773bda96SJonathan Lemon 531e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) 532e1daf0ecSJonathan Lemon if (tbl[i].value == val) 533e1daf0ecSJonathan Lemon return tbl[i].name; 534773bda96SJonathan Lemon return NULL; 535773bda96SJonathan Lemon } 536773bda96SJonathan Lemon 537773bda96SJonathan Lemon static int 538e1daf0ecSJonathan Lemon ptp_ocp_select_val_from_name(struct ocp_selector *tbl, const char *name) 539773bda96SJonathan Lemon { 540e1daf0ecSJonathan Lemon const char *select; 541773bda96SJonathan Lemon int i; 542773bda96SJonathan Lemon 543e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) { 544e1daf0ecSJonathan Lemon select = tbl[i].name; 545e1daf0ecSJonathan Lemon if (!strncasecmp(name, select, strlen(select))) 546e1daf0ecSJonathan Lemon return tbl[i].value; 547773bda96SJonathan Lemon } 548773bda96SJonathan Lemon return -EINVAL; 549773bda96SJonathan Lemon } 550773bda96SJonathan Lemon 551e1daf0ecSJonathan Lemon static ssize_t 552e1daf0ecSJonathan Lemon ptp_ocp_select_table_show(struct ocp_selector *tbl, char *buf) 553e1daf0ecSJonathan Lemon { 554e1daf0ecSJonathan Lemon ssize_t count; 555e1daf0ecSJonathan Lemon int i; 556e1daf0ecSJonathan Lemon 557e1daf0ecSJonathan Lemon count = 0; 558e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) 559e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", tbl[i].name); 560e1daf0ecSJonathan Lemon if (count) 561e1daf0ecSJonathan Lemon count--; 562e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "\n"); 563e1daf0ecSJonathan Lemon return count; 564e1daf0ecSJonathan Lemon } 565e1daf0ecSJonathan Lemon 566a7e1abadSJonathan Lemon static int 567a7e1abadSJonathan Lemon __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts, 568a7e1abadSJonathan Lemon struct ptp_system_timestamp *sts) 569a7e1abadSJonathan Lemon { 570a7e1abadSJonathan Lemon u32 ctrl, time_sec, time_ns; 571a7e1abadSJonathan Lemon int i; 572a7e1abadSJonathan Lemon 573a7e1abadSJonathan Lemon ptp_read_system_prets(sts); 5741acffc6eSJonathan Lemon 5751acffc6eSJonathan Lemon ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE; 576a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 577a7e1abadSJonathan Lemon 578a7e1abadSJonathan Lemon for (i = 0; i < 100; i++) { 579a7e1abadSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 580a7e1abadSJonathan Lemon if (ctrl & OCP_CTRL_READ_TIME_DONE) 581a7e1abadSJonathan Lemon break; 582a7e1abadSJonathan Lemon } 583a7e1abadSJonathan Lemon ptp_read_system_postts(sts); 584a7e1abadSJonathan Lemon 5851acffc6eSJonathan Lemon if (sts && bp->ts_window_adjust) { 5861acffc6eSJonathan Lemon s64 ns = timespec64_to_ns(&sts->post_ts); 5871acffc6eSJonathan Lemon 5881acffc6eSJonathan Lemon sts->post_ts = ns_to_timespec64(ns - bp->ts_window_adjust); 5891acffc6eSJonathan Lemon } 5901acffc6eSJonathan Lemon 591a7e1abadSJonathan Lemon time_ns = ioread32(&bp->reg->time_ns); 592a7e1abadSJonathan Lemon time_sec = ioread32(&bp->reg->time_sec); 593a7e1abadSJonathan Lemon 594a7e1abadSJonathan Lemon ts->tv_sec = time_sec; 595a7e1abadSJonathan Lemon ts->tv_nsec = time_ns; 596a7e1abadSJonathan Lemon 597a7e1abadSJonathan Lemon return ctrl & OCP_CTRL_READ_TIME_DONE ? 0 : -ETIMEDOUT; 598a7e1abadSJonathan Lemon } 599a7e1abadSJonathan Lemon 600a7e1abadSJonathan Lemon static int 601a7e1abadSJonathan Lemon ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts, 602a7e1abadSJonathan Lemon struct ptp_system_timestamp *sts) 603a7e1abadSJonathan Lemon { 604a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 605a7e1abadSJonathan Lemon unsigned long flags; 606a7e1abadSJonathan Lemon int err; 607a7e1abadSJonathan Lemon 608a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 609a7e1abadSJonathan Lemon err = __ptp_ocp_gettime_locked(bp, ts, sts); 610a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 611a7e1abadSJonathan Lemon 612a7e1abadSJonathan Lemon return err; 613a7e1abadSJonathan Lemon } 614a7e1abadSJonathan Lemon 615a7e1abadSJonathan Lemon static void 616a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts) 617a7e1abadSJonathan Lemon { 618a7e1abadSJonathan Lemon u32 ctrl, time_sec, time_ns; 619a7e1abadSJonathan Lemon u32 select; 620a7e1abadSJonathan Lemon 621a7e1abadSJonathan Lemon time_ns = ts->tv_nsec; 622a7e1abadSJonathan Lemon time_sec = ts->tv_sec; 623a7e1abadSJonathan Lemon 624a7e1abadSJonathan Lemon select = ioread32(&bp->reg->select); 625a7e1abadSJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 626a7e1abadSJonathan Lemon 627a7e1abadSJonathan Lemon iowrite32(time_ns, &bp->reg->adjust_ns); 628a7e1abadSJonathan Lemon iowrite32(time_sec, &bp->reg->adjust_sec); 629a7e1abadSJonathan Lemon 6301acffc6eSJonathan Lemon ctrl = OCP_CTRL_ADJUST_TIME | OCP_CTRL_ENABLE; 631a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 632a7e1abadSJonathan Lemon 633a7e1abadSJonathan Lemon /* restore clock selection */ 634a7e1abadSJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 635a7e1abadSJonathan Lemon } 636a7e1abadSJonathan Lemon 637a7e1abadSJonathan Lemon static int 638a7e1abadSJonathan Lemon ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts) 639a7e1abadSJonathan Lemon { 640a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 641a7e1abadSJonathan Lemon unsigned long flags; 642a7e1abadSJonathan Lemon 643a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 644a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(bp, ts); 645a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 646a7e1abadSJonathan Lemon 647a7e1abadSJonathan Lemon return 0; 648a7e1abadSJonathan Lemon } 649a7e1abadSJonathan Lemon 6506d59d4faSJonathan Lemon static void 65190f8f4c0SJonathan Lemon __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val) 6526d59d4faSJonathan Lemon { 6536d59d4faSJonathan Lemon u32 select, ctrl; 6546d59d4faSJonathan Lemon 6556d59d4faSJonathan Lemon select = ioread32(&bp->reg->select); 6566d59d4faSJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 6576d59d4faSJonathan Lemon 6586d59d4faSJonathan Lemon iowrite32(adj_val, &bp->reg->offset_ns); 65990f8f4c0SJonathan Lemon iowrite32(NSEC_PER_SEC, &bp->reg->offset_window_ns); 6606d59d4faSJonathan Lemon 6616d59d4faSJonathan Lemon ctrl = OCP_CTRL_ADJUST_OFFSET | OCP_CTRL_ENABLE; 6626d59d4faSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 6636d59d4faSJonathan Lemon 6646d59d4faSJonathan Lemon /* restore clock selection */ 6656d59d4faSJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 6666d59d4faSJonathan Lemon } 6676d59d4faSJonathan Lemon 66890f8f4c0SJonathan Lemon static void 66990f8f4c0SJonathan Lemon ptp_ocp_adjtime_coarse(struct ptp_ocp *bp, u64 delta_ns) 67090f8f4c0SJonathan Lemon { 67190f8f4c0SJonathan Lemon struct timespec64 ts; 67290f8f4c0SJonathan Lemon unsigned long flags; 67390f8f4c0SJonathan Lemon int err; 67490f8f4c0SJonathan Lemon 67590f8f4c0SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 67690f8f4c0SJonathan Lemon err = __ptp_ocp_gettime_locked(bp, &ts, NULL); 67790f8f4c0SJonathan Lemon if (likely(!err)) { 67890f8f4c0SJonathan Lemon timespec64_add_ns(&ts, delta_ns); 67990f8f4c0SJonathan Lemon __ptp_ocp_settime_locked(bp, &ts); 68090f8f4c0SJonathan Lemon } 68190f8f4c0SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 68290f8f4c0SJonathan Lemon } 68390f8f4c0SJonathan Lemon 684a7e1abadSJonathan Lemon static int 685a7e1abadSJonathan Lemon ptp_ocp_adjtime(struct ptp_clock_info *ptp_info, s64 delta_ns) 686a7e1abadSJonathan Lemon { 687a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 688a7e1abadSJonathan Lemon unsigned long flags; 6896d59d4faSJonathan Lemon u32 adj_ns, sign; 690a7e1abadSJonathan Lemon 69190f8f4c0SJonathan Lemon if (delta_ns > NSEC_PER_SEC || -delta_ns > NSEC_PER_SEC) { 69290f8f4c0SJonathan Lemon ptp_ocp_adjtime_coarse(bp, delta_ns); 69390f8f4c0SJonathan Lemon return 0; 69490f8f4c0SJonathan Lemon } 69590f8f4c0SJonathan Lemon 6966d59d4faSJonathan Lemon sign = delta_ns < 0 ? BIT(31) : 0; 6976d59d4faSJonathan Lemon adj_ns = sign ? -delta_ns : delta_ns; 698a7e1abadSJonathan Lemon 699a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 7006d59d4faSJonathan Lemon __ptp_ocp_adjtime_locked(bp, sign | adj_ns); 701a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 702a7e1abadSJonathan Lemon 7036d59d4faSJonathan Lemon return 0; 704a7e1abadSJonathan Lemon } 705a7e1abadSJonathan Lemon 706a7e1abadSJonathan Lemon static int 707a7e1abadSJonathan Lemon ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm) 708a7e1abadSJonathan Lemon { 709a7e1abadSJonathan Lemon if (scaled_ppm == 0) 710a7e1abadSJonathan Lemon return 0; 711a7e1abadSJonathan Lemon 712a7e1abadSJonathan Lemon return -EOPNOTSUPP; 713a7e1abadSJonathan Lemon } 714a7e1abadSJonathan Lemon 715773bda96SJonathan Lemon static int 7166d59d4faSJonathan Lemon ptp_ocp_null_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns) 717773bda96SJonathan Lemon { 718773bda96SJonathan Lemon return -EOPNOTSUPP; 719773bda96SJonathan Lemon } 720773bda96SJonathan Lemon 721773bda96SJonathan Lemon static int 722773bda96SJonathan Lemon ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq, 723773bda96SJonathan Lemon int on) 724773bda96SJonathan Lemon { 725773bda96SJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 726773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = NULL; 727a62a56d0SJonathan Lemon u32 req; 728773bda96SJonathan Lemon int err; 729773bda96SJonathan Lemon 730773bda96SJonathan Lemon switch (rq->type) { 731773bda96SJonathan Lemon case PTP_CLK_REQ_EXTTS: 732a62a56d0SJonathan Lemon req = OCP_REQ_TIMESTAMP; 733773bda96SJonathan Lemon switch (rq->extts.index) { 734773bda96SJonathan Lemon case 0: 735773bda96SJonathan Lemon ext = bp->ts0; 736773bda96SJonathan Lemon break; 737773bda96SJonathan Lemon case 1: 738773bda96SJonathan Lemon ext = bp->ts1; 739773bda96SJonathan Lemon break; 740dcf61469SJonathan Lemon case 2: 741dcf61469SJonathan Lemon ext = bp->ts2; 742dcf61469SJonathan Lemon break; 743a62a56d0SJonathan Lemon case 3: 744a62a56d0SJonathan Lemon ext = bp->pps; 745a62a56d0SJonathan Lemon break; 746773bda96SJonathan Lemon } 747773bda96SJonathan Lemon break; 748773bda96SJonathan Lemon case PTP_CLK_REQ_PPS: 749a62a56d0SJonathan Lemon req = OCP_REQ_PPS; 750773bda96SJonathan Lemon ext = bp->pps; 751773bda96SJonathan Lemon break; 752a62a56d0SJonathan Lemon case PTP_CLK_REQ_PEROUT: 753a62a56d0SJonathan Lemon if (on && 754a62a56d0SJonathan Lemon (rq->perout.period.sec != 1 || rq->perout.period.nsec != 0)) 755a62a56d0SJonathan Lemon return -EINVAL; 756a62a56d0SJonathan Lemon /* This is a request for 1PPS on an output SMA. 757a62a56d0SJonathan Lemon * Allow, but assume manual configuration. 758a62a56d0SJonathan Lemon */ 759a62a56d0SJonathan Lemon return 0; 760773bda96SJonathan Lemon default: 761773bda96SJonathan Lemon return -EOPNOTSUPP; 762773bda96SJonathan Lemon } 763773bda96SJonathan Lemon 764773bda96SJonathan Lemon err = -ENXIO; 765773bda96SJonathan Lemon if (ext) 766a62a56d0SJonathan Lemon err = ext->info->enable(ext, req, on); 767773bda96SJonathan Lemon 768773bda96SJonathan Lemon return err; 769773bda96SJonathan Lemon } 770773bda96SJonathan Lemon 771a7e1abadSJonathan Lemon static const struct ptp_clock_info ptp_ocp_clock_info = { 772a7e1abadSJonathan Lemon .owner = THIS_MODULE, 773a7e1abadSJonathan Lemon .name = KBUILD_MODNAME, 774a7e1abadSJonathan Lemon .max_adj = 100000000, 775a7e1abadSJonathan Lemon .gettimex64 = ptp_ocp_gettimex, 776a7e1abadSJonathan Lemon .settime64 = ptp_ocp_settime, 777a7e1abadSJonathan Lemon .adjtime = ptp_ocp_adjtime, 778a7e1abadSJonathan Lemon .adjfine = ptp_ocp_null_adjfine, 7796d59d4faSJonathan Lemon .adjphase = ptp_ocp_null_adjphase, 780773bda96SJonathan Lemon .enable = ptp_ocp_enable, 781773bda96SJonathan Lemon .pps = true, 782a62a56d0SJonathan Lemon .n_ext_ts = 4, 783a62a56d0SJonathan Lemon .n_per_out = 1, 784a7e1abadSJonathan Lemon }; 785a7e1abadSJonathan Lemon 786773bda96SJonathan Lemon static void 787773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp) 788773bda96SJonathan Lemon { 789773bda96SJonathan Lemon u32 ctrl, select; 790773bda96SJonathan Lemon 791773bda96SJonathan Lemon select = ioread32(&bp->reg->select); 792773bda96SJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 793773bda96SJonathan Lemon 794773bda96SJonathan Lemon iowrite32(0, &bp->reg->drift_ns); 795773bda96SJonathan Lemon 7961acffc6eSJonathan Lemon ctrl = OCP_CTRL_ADJUST_DRIFT | OCP_CTRL_ENABLE; 797773bda96SJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 798773bda96SJonathan Lemon 799773bda96SJonathan Lemon /* restore clock selection */ 800773bda96SJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 801773bda96SJonathan Lemon } 802773bda96SJonathan Lemon 803773bda96SJonathan Lemon static void 804e68462a0SVadim Fedorenko ptp_ocp_utc_distribute(struct ptp_ocp *bp, u32 val) 805e68462a0SVadim Fedorenko { 806e68462a0SVadim Fedorenko unsigned long flags; 807e68462a0SVadim Fedorenko 808e68462a0SVadim Fedorenko spin_lock_irqsave(&bp->lock, flags); 809e68462a0SVadim Fedorenko 810e68462a0SVadim Fedorenko bp->utc_tai_offset = val; 811e68462a0SVadim Fedorenko 812e68462a0SVadim Fedorenko if (bp->irig_out) 813e68462a0SVadim Fedorenko iowrite32(val, &bp->irig_out->adj_sec); 814e68462a0SVadim Fedorenko if (bp->dcf_out) 815e68462a0SVadim Fedorenko iowrite32(val, &bp->dcf_out->adj_sec); 816e68462a0SVadim Fedorenko if (bp->nmea_out) 817e68462a0SVadim Fedorenko iowrite32(val, &bp->nmea_out->adj_sec); 818e68462a0SVadim Fedorenko 819e68462a0SVadim Fedorenko spin_unlock_irqrestore(&bp->lock, flags); 820e68462a0SVadim Fedorenko } 821e68462a0SVadim Fedorenko 822e68462a0SVadim Fedorenko static void 823773bda96SJonathan Lemon ptp_ocp_watchdog(struct timer_list *t) 824773bda96SJonathan Lemon { 825773bda96SJonathan Lemon struct ptp_ocp *bp = from_timer(bp, t, watchdog); 826773bda96SJonathan Lemon unsigned long flags; 827e68462a0SVadim Fedorenko u32 status, utc_offset; 828773bda96SJonathan Lemon 8290d43d4f2SJonathan Lemon status = ioread32(&bp->pps_to_clk->status); 830773bda96SJonathan Lemon 831773bda96SJonathan Lemon if (status & PPS_STATUS_SUPERV_ERR) { 8320d43d4f2SJonathan Lemon iowrite32(status, &bp->pps_to_clk->status); 833ef0cfb34SJonathan Lemon if (!bp->gnss_lost) { 834773bda96SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 835773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(bp); 836773bda96SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 837ef0cfb34SJonathan Lemon bp->gnss_lost = ktime_get_real_seconds(); 838773bda96SJonathan Lemon } 839773bda96SJonathan Lemon 840ef0cfb34SJonathan Lemon } else if (bp->gnss_lost) { 841ef0cfb34SJonathan Lemon bp->gnss_lost = 0; 842773bda96SJonathan Lemon } 843773bda96SJonathan Lemon 844e68462a0SVadim Fedorenko /* if GNSS provides correct data we can rely on 845e68462a0SVadim Fedorenko * it to get leap second information 846e68462a0SVadim Fedorenko */ 847e68462a0SVadim Fedorenko if (bp->tod) { 848e68462a0SVadim Fedorenko status = ioread32(&bp->tod->utc_status); 849e68462a0SVadim Fedorenko utc_offset = status & TOD_STATUS_UTC_MASK; 850e68462a0SVadim Fedorenko if (status & TOD_STATUS_UTC_VALID && 851e68462a0SVadim Fedorenko utc_offset != bp->utc_tai_offset) 852e68462a0SVadim Fedorenko ptp_ocp_utc_distribute(bp, utc_offset); 853e68462a0SVadim Fedorenko } 854e68462a0SVadim Fedorenko 855773bda96SJonathan Lemon mod_timer(&bp->watchdog, jiffies + HZ); 856773bda96SJonathan Lemon } 857773bda96SJonathan Lemon 8581acffc6eSJonathan Lemon static void 8591acffc6eSJonathan Lemon ptp_ocp_estimate_pci_timing(struct ptp_ocp *bp) 8601acffc6eSJonathan Lemon { 8611acffc6eSJonathan Lemon ktime_t start, end; 8621acffc6eSJonathan Lemon ktime_t delay; 8631acffc6eSJonathan Lemon u32 ctrl; 8641acffc6eSJonathan Lemon 8651acffc6eSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 8661acffc6eSJonathan Lemon ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE; 8671acffc6eSJonathan Lemon 8681acffc6eSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 8691acffc6eSJonathan Lemon 8701acffc6eSJonathan Lemon start = ktime_get_ns(); 8711acffc6eSJonathan Lemon 8721acffc6eSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 8731acffc6eSJonathan Lemon 8741acffc6eSJonathan Lemon end = ktime_get_ns(); 8751acffc6eSJonathan Lemon 8761acffc6eSJonathan Lemon delay = end - start; 8771acffc6eSJonathan Lemon bp->ts_window_adjust = (delay >> 5) * 3; 8781acffc6eSJonathan Lemon } 8791acffc6eSJonathan Lemon 880a7e1abadSJonathan Lemon static int 881773bda96SJonathan Lemon ptp_ocp_init_clock(struct ptp_ocp *bp) 882a7e1abadSJonathan Lemon { 883a7e1abadSJonathan Lemon struct timespec64 ts; 884a7e1abadSJonathan Lemon bool sync; 885a7e1abadSJonathan Lemon u32 ctrl; 886a7e1abadSJonathan Lemon 8871acffc6eSJonathan Lemon ctrl = OCP_CTRL_ENABLE; 888a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 889a7e1abadSJonathan Lemon 890773bda96SJonathan Lemon /* NO DRIFT Correction */ 891773bda96SJonathan Lemon /* offset_p:i 1/8, offset_i: 1/16, drift_p: 0, drift_i: 0 */ 892773bda96SJonathan Lemon iowrite32(0x2000, &bp->reg->servo_offset_p); 893773bda96SJonathan Lemon iowrite32(0x1000, &bp->reg->servo_offset_i); 894773bda96SJonathan Lemon iowrite32(0, &bp->reg->servo_drift_p); 895773bda96SJonathan Lemon iowrite32(0, &bp->reg->servo_drift_i); 896773bda96SJonathan Lemon 897773bda96SJonathan Lemon /* latch servo values */ 898773bda96SJonathan Lemon ctrl |= OCP_CTRL_ADJUST_SERVO; 899773bda96SJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 900773bda96SJonathan Lemon 901a7e1abadSJonathan Lemon if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) { 902a7e1abadSJonathan Lemon dev_err(&bp->pdev->dev, "clock not enabled\n"); 903a7e1abadSJonathan Lemon return -ENODEV; 904a7e1abadSJonathan Lemon } 905a7e1abadSJonathan Lemon 9061acffc6eSJonathan Lemon ptp_ocp_estimate_pci_timing(bp); 9071acffc6eSJonathan Lemon 908a7e1abadSJonathan Lemon sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC; 909a7e1abadSJonathan Lemon if (!sync) { 910065efcc5SJonathan Lemon ktime_get_clocktai_ts64(&ts); 911a7e1abadSJonathan Lemon ptp_ocp_settime(&bp->ptp_info, &ts); 912a7e1abadSJonathan Lemon } 913a7e1abadSJonathan Lemon 914065efcc5SJonathan Lemon /* If there is a clock supervisor, then enable the watchdog */ 915065efcc5SJonathan Lemon if (bp->pps_to_clk) { 916773bda96SJonathan Lemon timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0); 917773bda96SJonathan Lemon mod_timer(&bp->watchdog, jiffies + HZ); 918065efcc5SJonathan Lemon } 919773bda96SJonathan Lemon 920a7e1abadSJonathan Lemon return 0; 921a7e1abadSJonathan Lemon } 922a7e1abadSJonathan Lemon 923a7e1abadSJonathan Lemon static void 924065efcc5SJonathan Lemon ptp_ocp_tod_init(struct ptp_ocp *bp) 925065efcc5SJonathan Lemon { 926065efcc5SJonathan Lemon u32 ctrl, reg; 927065efcc5SJonathan Lemon 928065efcc5SJonathan Lemon ctrl = ioread32(&bp->tod->ctrl); 929065efcc5SJonathan Lemon ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE; 930065efcc5SJonathan Lemon ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B); 931065efcc5SJonathan Lemon iowrite32(ctrl, &bp->tod->ctrl); 932065efcc5SJonathan Lemon 933065efcc5SJonathan Lemon reg = ioread32(&bp->tod->utc_status); 934065efcc5SJonathan Lemon if (reg & TOD_STATUS_UTC_VALID) 935065efcc5SJonathan Lemon ptp_ocp_utc_distribute(bp, reg & TOD_STATUS_UTC_MASK); 936065efcc5SJonathan Lemon } 937065efcc5SJonathan Lemon 9389f492c4cSVadim Fedorenko static const char * 9399f492c4cSVadim Fedorenko ptp_ocp_tod_proto_name(const int idx) 940a7e1abadSJonathan Lemon { 941a7e1abadSJonathan Lemon static const char * const proto_name[] = { 942a7e1abadSJonathan Lemon "NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none", 943a7e1abadSJonathan Lemon "UBX", "UBX_UTC", "UBX_LS", "UBX_none" 944a7e1abadSJonathan Lemon }; 9459f492c4cSVadim Fedorenko return proto_name[idx]; 9469f492c4cSVadim Fedorenko } 9479f492c4cSVadim Fedorenko 9489f492c4cSVadim Fedorenko static const char * 9499f492c4cSVadim Fedorenko ptp_ocp_tod_gnss_name(int idx) 9509f492c4cSVadim Fedorenko { 951a7e1abadSJonathan Lemon static const char * const gnss_name[] = { 952a7e1abadSJonathan Lemon "ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU", 9539f492c4cSVadim Fedorenko "Unknown" 954a7e1abadSJonathan Lemon }; 95572f00505SDan Carpenter if (idx >= ARRAY_SIZE(gnss_name)) 9569f492c4cSVadim Fedorenko idx = ARRAY_SIZE(gnss_name) - 1; 9579f492c4cSVadim Fedorenko return gnss_name[idx]; 958a7e1abadSJonathan Lemon } 959a7e1abadSJonathan Lemon 960*0cfcdd1eSJonathan Lemon struct ptp_ocp_nvmem_match_info { 961*0cfcdd1eSJonathan Lemon struct ptp_ocp *bp; 962*0cfcdd1eSJonathan Lemon const void * const tag; 963773bda96SJonathan Lemon }; 964773bda96SJonathan Lemon 965*0cfcdd1eSJonathan Lemon static int 966*0cfcdd1eSJonathan Lemon ptp_ocp_nvmem_match(struct device *dev, const void *data) 967*0cfcdd1eSJonathan Lemon { 968*0cfcdd1eSJonathan Lemon const struct ptp_ocp_nvmem_match_info *info = data; 969*0cfcdd1eSJonathan Lemon 970*0cfcdd1eSJonathan Lemon dev = dev->parent; 971*0cfcdd1eSJonathan Lemon if (!i2c_verify_client(dev) || info->tag != dev->platform_data) 972*0cfcdd1eSJonathan Lemon return 0; 973*0cfcdd1eSJonathan Lemon 974*0cfcdd1eSJonathan Lemon while ((dev = dev->parent)) 975*0cfcdd1eSJonathan Lemon if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME)) 976*0cfcdd1eSJonathan Lemon return info->bp == dev_get_drvdata(dev); 977773bda96SJonathan Lemon return 0; 978773bda96SJonathan Lemon } 979773bda96SJonathan Lemon 980*0cfcdd1eSJonathan Lemon static inline struct nvmem_device * 981*0cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_get(struct ptp_ocp *bp, const void * const tag) 982773bda96SJonathan Lemon { 983*0cfcdd1eSJonathan Lemon struct ptp_ocp_nvmem_match_info info = { .bp = bp, .tag = tag }; 984*0cfcdd1eSJonathan Lemon 985*0cfcdd1eSJonathan Lemon return nvmem_device_find(&info, ptp_ocp_nvmem_match); 986*0cfcdd1eSJonathan Lemon } 987*0cfcdd1eSJonathan Lemon 988*0cfcdd1eSJonathan Lemon static inline void 989*0cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_put(struct nvmem_device **nvmemp) 990*0cfcdd1eSJonathan Lemon { 991*0cfcdd1eSJonathan Lemon if (*nvmemp != NULL) { 992*0cfcdd1eSJonathan Lemon nvmem_device_put(*nvmemp); 993*0cfcdd1eSJonathan Lemon *nvmemp = NULL; 994*0cfcdd1eSJonathan Lemon } 995*0cfcdd1eSJonathan Lemon } 996*0cfcdd1eSJonathan Lemon 997*0cfcdd1eSJonathan Lemon static void 998*0cfcdd1eSJonathan Lemon ptp_ocp_read_eeprom(struct ptp_ocp *bp) 999*0cfcdd1eSJonathan Lemon { 1000*0cfcdd1eSJonathan Lemon const struct ptp_ocp_eeprom_map *map; 1001*0cfcdd1eSJonathan Lemon struct nvmem_device *nvmem; 1002*0cfcdd1eSJonathan Lemon const void *tag; 1003*0cfcdd1eSJonathan Lemon int ret; 1004773bda96SJonathan Lemon 10051447149dSJonathan Lemon if (!bp->i2c_ctrl) 10061447149dSJonathan Lemon return; 10071447149dSJonathan Lemon 1008*0cfcdd1eSJonathan Lemon tag = NULL; 1009*0cfcdd1eSJonathan Lemon nvmem = NULL; 1010773bda96SJonathan Lemon 1011*0cfcdd1eSJonathan Lemon for (map = bp->eeprom_map; map->len; map++) { 1012*0cfcdd1eSJonathan Lemon if (map->tag != tag) { 1013*0cfcdd1eSJonathan Lemon tag = map->tag; 1014*0cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_put(&nvmem); 1015*0cfcdd1eSJonathan Lemon } 1016*0cfcdd1eSJonathan Lemon if (!nvmem) { 1017*0cfcdd1eSJonathan Lemon nvmem = ptp_ocp_nvmem_device_get(bp, tag); 1018*0cfcdd1eSJonathan Lemon if (!nvmem) 1019773bda96SJonathan Lemon goto out; 1020773bda96SJonathan Lemon } 1021*0cfcdd1eSJonathan Lemon ret = nvmem_device_read(nvmem, map->off, map->len, 1022*0cfcdd1eSJonathan Lemon BP_MAP_ENTRY_ADDR(bp, map)); 1023*0cfcdd1eSJonathan Lemon if (ret != map->len) 1024*0cfcdd1eSJonathan Lemon goto read_fail; 1025773bda96SJonathan Lemon } 1026773bda96SJonathan Lemon 1027*0cfcdd1eSJonathan Lemon bp->has_eeprom_data = true; 1028773bda96SJonathan Lemon 1029773bda96SJonathan Lemon out: 1030*0cfcdd1eSJonathan Lemon ptp_ocp_nvmem_device_put(&nvmem); 1031*0cfcdd1eSJonathan Lemon return; 1032*0cfcdd1eSJonathan Lemon 1033*0cfcdd1eSJonathan Lemon read_fail: 1034*0cfcdd1eSJonathan Lemon dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", ret); 1035*0cfcdd1eSJonathan Lemon goto out; 1036*0cfcdd1eSJonathan Lemon } 1037*0cfcdd1eSJonathan Lemon 1038*0cfcdd1eSJonathan Lemon static int 1039*0cfcdd1eSJonathan Lemon ptp_ocp_firstchild(struct device *dev, void *data) 1040*0cfcdd1eSJonathan Lemon { 1041*0cfcdd1eSJonathan Lemon return 1; 1042773bda96SJonathan Lemon } 1043773bda96SJonathan Lemon 1044773bda96SJonathan Lemon static struct device * 1045773bda96SJonathan Lemon ptp_ocp_find_flash(struct ptp_ocp *bp) 1046773bda96SJonathan Lemon { 1047773bda96SJonathan Lemon struct device *dev, *last; 1048773bda96SJonathan Lemon 1049773bda96SJonathan Lemon last = NULL; 1050773bda96SJonathan Lemon dev = &bp->spi_flash->dev; 1051773bda96SJonathan Lemon 1052773bda96SJonathan Lemon while ((dev = device_find_child(dev, NULL, ptp_ocp_firstchild))) { 1053773bda96SJonathan Lemon if (!strcmp("mtd", dev_bus_name(dev))) 1054773bda96SJonathan Lemon break; 1055773bda96SJonathan Lemon put_device(last); 1056773bda96SJonathan Lemon last = dev; 1057773bda96SJonathan Lemon } 1058773bda96SJonathan Lemon put_device(last); 1059773bda96SJonathan Lemon 1060773bda96SJonathan Lemon return dev; 1061773bda96SJonathan Lemon } 1062773bda96SJonathan Lemon 1063773bda96SJonathan Lemon static int 1064773bda96SJonathan Lemon ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev, 1065773bda96SJonathan Lemon const struct firmware *fw) 1066773bda96SJonathan Lemon { 1067773bda96SJonathan Lemon struct mtd_info *mtd = dev_get_drvdata(dev); 1068773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 1069773bda96SJonathan Lemon size_t off, len, resid, wrote; 1070773bda96SJonathan Lemon struct erase_info erase; 1071773bda96SJonathan Lemon size_t base, blksz; 10727c807572SJonathan Lemon int err = 0; 1073773bda96SJonathan Lemon 1074773bda96SJonathan Lemon off = 0; 1075773bda96SJonathan Lemon base = bp->flash_start; 1076773bda96SJonathan Lemon blksz = 4096; 1077773bda96SJonathan Lemon resid = fw->size; 1078773bda96SJonathan Lemon 1079773bda96SJonathan Lemon while (resid) { 1080773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, "Flashing", 1081773bda96SJonathan Lemon NULL, off, fw->size); 1082773bda96SJonathan Lemon 1083773bda96SJonathan Lemon len = min_t(size_t, resid, blksz); 1084773bda96SJonathan Lemon erase.addr = base + off; 1085773bda96SJonathan Lemon erase.len = blksz; 1086773bda96SJonathan Lemon 1087773bda96SJonathan Lemon err = mtd_erase(mtd, &erase); 1088773bda96SJonathan Lemon if (err) 1089773bda96SJonathan Lemon goto out; 1090773bda96SJonathan Lemon 1091773bda96SJonathan Lemon err = mtd_write(mtd, base + off, len, &wrote, &fw->data[off]); 1092773bda96SJonathan Lemon if (err) 1093773bda96SJonathan Lemon goto out; 1094773bda96SJonathan Lemon 1095773bda96SJonathan Lemon off += blksz; 1096773bda96SJonathan Lemon resid -= len; 1097773bda96SJonathan Lemon } 1098773bda96SJonathan Lemon out: 1099773bda96SJonathan Lemon return err; 1100773bda96SJonathan Lemon } 1101773bda96SJonathan Lemon 1102773bda96SJonathan Lemon static int 1103773bda96SJonathan Lemon ptp_ocp_devlink_flash_update(struct devlink *devlink, 1104773bda96SJonathan Lemon struct devlink_flash_update_params *params, 1105773bda96SJonathan Lemon struct netlink_ext_ack *extack) 1106773bda96SJonathan Lemon { 1107773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 1108773bda96SJonathan Lemon struct device *dev; 1109773bda96SJonathan Lemon const char *msg; 1110773bda96SJonathan Lemon int err; 1111773bda96SJonathan Lemon 1112773bda96SJonathan Lemon dev = ptp_ocp_find_flash(bp); 1113773bda96SJonathan Lemon if (!dev) { 1114773bda96SJonathan Lemon dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n"); 1115773bda96SJonathan Lemon return -ENODEV; 1116773bda96SJonathan Lemon } 1117773bda96SJonathan Lemon 1118773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, "Preparing to flash", 1119773bda96SJonathan Lemon NULL, 0, 0); 1120773bda96SJonathan Lemon 1121773bda96SJonathan Lemon err = ptp_ocp_devlink_flash(devlink, dev, params->fw); 1122773bda96SJonathan Lemon 1123773bda96SJonathan Lemon msg = err ? "Flash error" : "Flash complete"; 1124773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0); 1125773bda96SJonathan Lemon 1126773bda96SJonathan Lemon put_device(dev); 1127773bda96SJonathan Lemon return err; 1128773bda96SJonathan Lemon } 1129773bda96SJonathan Lemon 1130773bda96SJonathan Lemon static int 1131773bda96SJonathan Lemon ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req, 1132773bda96SJonathan Lemon struct netlink_ext_ack *extack) 1133773bda96SJonathan Lemon { 1134773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 1135773bda96SJonathan Lemon char buf[32]; 1136773bda96SJonathan Lemon int err; 1137773bda96SJonathan Lemon 1138773bda96SJonathan Lemon err = devlink_info_driver_name_put(req, KBUILD_MODNAME); 1139773bda96SJonathan Lemon if (err) 1140773bda96SJonathan Lemon return err; 1141773bda96SJonathan Lemon 1142773bda96SJonathan Lemon if (bp->image) { 1143773bda96SJonathan Lemon u32 ver = ioread32(&bp->image->version); 1144773bda96SJonathan Lemon 1145773bda96SJonathan Lemon if (ver & 0xffff) { 1146773bda96SJonathan Lemon sprintf(buf, "%d", ver); 1147773bda96SJonathan Lemon err = devlink_info_version_running_put(req, 11481a052da9SJonathan Lemon "fw", 1149773bda96SJonathan Lemon buf); 1150773bda96SJonathan Lemon } else { 1151773bda96SJonathan Lemon sprintf(buf, "%d", ver >> 16); 1152773bda96SJonathan Lemon err = devlink_info_version_running_put(req, 11531a052da9SJonathan Lemon "loader", 1154773bda96SJonathan Lemon buf); 1155773bda96SJonathan Lemon } 1156773bda96SJonathan Lemon if (err) 1157773bda96SJonathan Lemon return err; 1158773bda96SJonathan Lemon } 1159773bda96SJonathan Lemon 1160*0cfcdd1eSJonathan Lemon if (!bp->has_eeprom_data) { 1161*0cfcdd1eSJonathan Lemon ptp_ocp_read_eeprom(bp); 1162*0cfcdd1eSJonathan Lemon if (!bp->has_eeprom_data) 1163*0cfcdd1eSJonathan Lemon return 0; 1164*0cfcdd1eSJonathan Lemon } 1165773bda96SJonathan Lemon 1166773bda96SJonathan Lemon sprintf(buf, "%pM", bp->serial); 1167773bda96SJonathan Lemon err = devlink_info_serial_number_put(req, buf); 1168773bda96SJonathan Lemon if (err) 1169773bda96SJonathan Lemon return err; 1170*0cfcdd1eSJonathan Lemon 1171*0cfcdd1eSJonathan Lemon err = devlink_info_version_fixed_put(req, 1172*0cfcdd1eSJonathan Lemon DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, 1173*0cfcdd1eSJonathan Lemon bp->board_id); 1174*0cfcdd1eSJonathan Lemon if (err) 1175*0cfcdd1eSJonathan Lemon return err; 1176773bda96SJonathan Lemon 1177773bda96SJonathan Lemon return 0; 1178773bda96SJonathan Lemon } 1179773bda96SJonathan Lemon 1180773bda96SJonathan Lemon static const struct devlink_ops ptp_ocp_devlink_ops = { 1181773bda96SJonathan Lemon .flash_update = ptp_ocp_devlink_flash_update, 1182773bda96SJonathan Lemon .info_get = ptp_ocp_devlink_info_get, 1183773bda96SJonathan Lemon }; 1184773bda96SJonathan Lemon 1185773bda96SJonathan Lemon static void __iomem * 1186773bda96SJonathan Lemon __ptp_ocp_get_mem(struct ptp_ocp *bp, unsigned long start, int size) 1187773bda96SJonathan Lemon { 1188773bda96SJonathan Lemon struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp"); 1189773bda96SJonathan Lemon 1190773bda96SJonathan Lemon return devm_ioremap_resource(&bp->pdev->dev, &res); 1191773bda96SJonathan Lemon } 1192773bda96SJonathan Lemon 1193773bda96SJonathan Lemon static void __iomem * 1194773bda96SJonathan Lemon ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r) 1195773bda96SJonathan Lemon { 1196773bda96SJonathan Lemon unsigned long start; 1197773bda96SJonathan Lemon 1198773bda96SJonathan Lemon start = pci_resource_start(bp->pdev, 0) + r->offset; 1199773bda96SJonathan Lemon return __ptp_ocp_get_mem(bp, start, r->size); 1200773bda96SJonathan Lemon } 1201773bda96SJonathan Lemon 1202773bda96SJonathan Lemon static void 1203773bda96SJonathan Lemon ptp_ocp_set_irq_resource(struct resource *res, int irq) 1204773bda96SJonathan Lemon { 1205773bda96SJonathan Lemon struct resource r = DEFINE_RES_IRQ(irq); 1206773bda96SJonathan Lemon *res = r; 1207773bda96SJonathan Lemon } 1208773bda96SJonathan Lemon 1209773bda96SJonathan Lemon static void 1210773bda96SJonathan Lemon ptp_ocp_set_mem_resource(struct resource *res, unsigned long start, int size) 1211773bda96SJonathan Lemon { 1212773bda96SJonathan Lemon struct resource r = DEFINE_RES_MEM(start, size); 1213773bda96SJonathan Lemon *res = r; 1214773bda96SJonathan Lemon } 1215773bda96SJonathan Lemon 1216773bda96SJonathan Lemon static int 1217773bda96SJonathan Lemon ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r) 1218773bda96SJonathan Lemon { 1219773bda96SJonathan Lemon struct ptp_ocp_flash_info *info; 1220773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1221773bda96SJonathan Lemon struct platform_device *p; 1222773bda96SJonathan Lemon struct resource res[2]; 1223773bda96SJonathan Lemon unsigned long start; 1224773bda96SJonathan Lemon int id; 1225773bda96SJonathan Lemon 1226773bda96SJonathan Lemon start = pci_resource_start(pdev, 0) + r->offset; 1227773bda96SJonathan Lemon ptp_ocp_set_mem_resource(&res[0], start, r->size); 1228773bda96SJonathan Lemon ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1229773bda96SJonathan Lemon 1230773bda96SJonathan Lemon info = r->extra; 1231773bda96SJonathan Lemon id = pci_dev_id(pdev) << 1; 1232773bda96SJonathan Lemon id += info->pci_offset; 1233773bda96SJonathan Lemon 1234773bda96SJonathan Lemon p = platform_device_register_resndata(&pdev->dev, info->name, id, 1235773bda96SJonathan Lemon res, 2, info->data, 1236773bda96SJonathan Lemon info->data_size); 1237773bda96SJonathan Lemon if (IS_ERR(p)) 1238773bda96SJonathan Lemon return PTR_ERR(p); 1239773bda96SJonathan Lemon 1240773bda96SJonathan Lemon bp_assign_entry(bp, r, p); 1241773bda96SJonathan Lemon 1242773bda96SJonathan Lemon return 0; 1243773bda96SJonathan Lemon } 1244773bda96SJonathan Lemon 1245773bda96SJonathan Lemon static struct platform_device * 1246773bda96SJonathan Lemon ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id) 1247773bda96SJonathan Lemon { 12481618df6aSJonathan Lemon struct ptp_ocp_i2c_info *info; 1249773bda96SJonathan Lemon struct resource res[2]; 1250773bda96SJonathan Lemon unsigned long start; 1251773bda96SJonathan Lemon 12521618df6aSJonathan Lemon info = r->extra; 1253773bda96SJonathan Lemon start = pci_resource_start(pdev, 0) + r->offset; 1254773bda96SJonathan Lemon ptp_ocp_set_mem_resource(&res[0], start, r->size); 1255773bda96SJonathan Lemon ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1256773bda96SJonathan Lemon 12571618df6aSJonathan Lemon return platform_device_register_resndata(&pdev->dev, info->name, 12581618df6aSJonathan Lemon id, res, 2, 12591618df6aSJonathan Lemon info->data, info->data_size); 1260773bda96SJonathan Lemon } 1261773bda96SJonathan Lemon 1262773bda96SJonathan Lemon static int 1263773bda96SJonathan Lemon ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r) 1264773bda96SJonathan Lemon { 1265773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 12661618df6aSJonathan Lemon struct ptp_ocp_i2c_info *info; 1267773bda96SJonathan Lemon struct platform_device *p; 1268773bda96SJonathan Lemon struct clk_hw *clk; 1269773bda96SJonathan Lemon char buf[32]; 1270773bda96SJonathan Lemon int id; 1271773bda96SJonathan Lemon 12721618df6aSJonathan Lemon info = r->extra; 1273773bda96SJonathan Lemon id = pci_dev_id(bp->pdev); 1274773bda96SJonathan Lemon 1275773bda96SJonathan Lemon sprintf(buf, "AXI.%d", id); 12761618df6aSJonathan Lemon clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0, 12771618df6aSJonathan Lemon info->fixed_rate); 1278773bda96SJonathan Lemon if (IS_ERR(clk)) 1279773bda96SJonathan Lemon return PTR_ERR(clk); 1280773bda96SJonathan Lemon bp->i2c_clk = clk; 1281773bda96SJonathan Lemon 12821618df6aSJonathan Lemon sprintf(buf, "%s.%d", info->name, id); 1283773bda96SJonathan Lemon devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf); 1284773bda96SJonathan Lemon p = ptp_ocp_i2c_bus(bp->pdev, r, id); 1285773bda96SJonathan Lemon if (IS_ERR(p)) 1286773bda96SJonathan Lemon return PTR_ERR(p); 1287773bda96SJonathan Lemon 1288773bda96SJonathan Lemon bp_assign_entry(bp, r, p); 1289773bda96SJonathan Lemon 1290773bda96SJonathan Lemon return 0; 1291773bda96SJonathan Lemon } 1292773bda96SJonathan Lemon 1293773bda96SJonathan Lemon static irqreturn_t 1294773bda96SJonathan Lemon ptp_ocp_ts_irq(int irq, void *priv) 1295773bda96SJonathan Lemon { 1296773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1297773bda96SJonathan Lemon struct ts_reg __iomem *reg = ext->mem; 1298773bda96SJonathan Lemon struct ptp_clock_event ev; 1299773bda96SJonathan Lemon u32 sec, nsec; 1300773bda96SJonathan Lemon 1301a62a56d0SJonathan Lemon if (ext == ext->bp->pps) { 1302a62a56d0SJonathan Lemon if (ext->bp->pps_req_map & OCP_REQ_PPS) { 1303a62a56d0SJonathan Lemon ev.type = PTP_CLOCK_PPS; 1304a62a56d0SJonathan Lemon ptp_clock_event(ext->bp->ptp, &ev); 1305a62a56d0SJonathan Lemon } 1306a62a56d0SJonathan Lemon 1307a62a56d0SJonathan Lemon if ((ext->bp->pps_req_map & ~OCP_REQ_PPS) == 0) 1308a62a56d0SJonathan Lemon goto out; 1309a62a56d0SJonathan Lemon } 1310a62a56d0SJonathan Lemon 1311773bda96SJonathan Lemon /* XXX should fix API - this converts s/ns -> ts -> s/ns */ 1312773bda96SJonathan Lemon sec = ioread32(®->time_sec); 1313773bda96SJonathan Lemon nsec = ioread32(®->time_ns); 1314773bda96SJonathan Lemon 1315773bda96SJonathan Lemon ev.type = PTP_CLOCK_EXTTS; 1316773bda96SJonathan Lemon ev.index = ext->info->index; 13171acffc6eSJonathan Lemon ev.timestamp = sec * NSEC_PER_SEC + nsec; 1318773bda96SJonathan Lemon 1319773bda96SJonathan Lemon ptp_clock_event(ext->bp->ptp, &ev); 1320773bda96SJonathan Lemon 1321a62a56d0SJonathan Lemon out: 1322773bda96SJonathan Lemon iowrite32(1, ®->intr); /* write 1 to ack */ 1323773bda96SJonathan Lemon 1324773bda96SJonathan Lemon return IRQ_HANDLED; 1325773bda96SJonathan Lemon } 1326773bda96SJonathan Lemon 1327773bda96SJonathan Lemon static int 1328a62a56d0SJonathan Lemon ptp_ocp_ts_enable(void *priv, u32 req, bool enable) 1329773bda96SJonathan Lemon { 1330773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1331773bda96SJonathan Lemon struct ts_reg __iomem *reg = ext->mem; 1332a62a56d0SJonathan Lemon struct ptp_ocp *bp = ext->bp; 1333a62a56d0SJonathan Lemon 1334a62a56d0SJonathan Lemon if (ext == bp->pps) { 1335a62a56d0SJonathan Lemon u32 old_map = bp->pps_req_map; 1336a62a56d0SJonathan Lemon 1337a62a56d0SJonathan Lemon if (enable) 1338a62a56d0SJonathan Lemon bp->pps_req_map |= req; 1339a62a56d0SJonathan Lemon else 1340a62a56d0SJonathan Lemon bp->pps_req_map &= ~req; 1341a62a56d0SJonathan Lemon 1342a62a56d0SJonathan Lemon /* if no state change, just return */ 1343a62a56d0SJonathan Lemon if ((!!old_map ^ !!bp->pps_req_map) == 0) 1344a62a56d0SJonathan Lemon return 0; 1345a62a56d0SJonathan Lemon } 1346773bda96SJonathan Lemon 1347773bda96SJonathan Lemon if (enable) { 1348773bda96SJonathan Lemon iowrite32(1, ®->enable); 1349773bda96SJonathan Lemon iowrite32(1, ®->intr_mask); 1350773bda96SJonathan Lemon iowrite32(1, ®->intr); 1351773bda96SJonathan Lemon } else { 1352773bda96SJonathan Lemon iowrite32(0, ®->intr_mask); 1353773bda96SJonathan Lemon iowrite32(0, ®->enable); 1354773bda96SJonathan Lemon } 1355773bda96SJonathan Lemon 1356773bda96SJonathan Lemon return 0; 1357773bda96SJonathan Lemon } 1358773bda96SJonathan Lemon 1359773bda96SJonathan Lemon static void 1360773bda96SJonathan Lemon ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext) 1361773bda96SJonathan Lemon { 1362a62a56d0SJonathan Lemon ext->info->enable(ext, ~0, false); 1363773bda96SJonathan Lemon pci_free_irq(ext->bp->pdev, ext->irq_vec, ext); 1364773bda96SJonathan Lemon kfree(ext); 1365773bda96SJonathan Lemon } 1366773bda96SJonathan Lemon 1367773bda96SJonathan Lemon static int 1368773bda96SJonathan Lemon ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r) 1369773bda96SJonathan Lemon { 1370773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1371773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext; 1372773bda96SJonathan Lemon int err; 1373773bda96SJonathan Lemon 1374773bda96SJonathan Lemon ext = kzalloc(sizeof(*ext), GFP_KERNEL); 1375773bda96SJonathan Lemon if (!ext) 1376773bda96SJonathan Lemon return -ENOMEM; 1377773bda96SJonathan Lemon 1378773bda96SJonathan Lemon ext->mem = ptp_ocp_get_mem(bp, r); 1379c7521d3aSDan Carpenter if (IS_ERR(ext->mem)) { 1380c7521d3aSDan Carpenter err = PTR_ERR(ext->mem); 1381773bda96SJonathan Lemon goto out; 1382c7521d3aSDan Carpenter } 1383773bda96SJonathan Lemon 1384773bda96SJonathan Lemon ext->bp = bp; 1385773bda96SJonathan Lemon ext->info = r->extra; 1386773bda96SJonathan Lemon ext->irq_vec = r->irq_vec; 1387773bda96SJonathan Lemon 1388773bda96SJonathan Lemon err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL, 138956ec4403SJonathan Lemon ext, "ocp%d.%s", bp->id, r->name); 1390773bda96SJonathan Lemon if (err) { 1391773bda96SJonathan Lemon dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec); 1392773bda96SJonathan Lemon goto out; 1393773bda96SJonathan Lemon } 1394773bda96SJonathan Lemon 1395773bda96SJonathan Lemon bp_assign_entry(bp, r, ext); 1396773bda96SJonathan Lemon 1397773bda96SJonathan Lemon return 0; 1398773bda96SJonathan Lemon 1399773bda96SJonathan Lemon out: 1400773bda96SJonathan Lemon kfree(ext); 1401773bda96SJonathan Lemon return err; 1402773bda96SJonathan Lemon } 1403773bda96SJonathan Lemon 1404773bda96SJonathan Lemon static int 1405773bda96SJonathan Lemon ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r) 1406773bda96SJonathan Lemon { 1407773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1408773bda96SJonathan Lemon struct uart_8250_port uart; 1409773bda96SJonathan Lemon 1410773bda96SJonathan Lemon /* Setting UPF_IOREMAP and leaving port.membase unspecified lets 1411773bda96SJonathan Lemon * the serial port device claim and release the pci resource. 1412773bda96SJonathan Lemon */ 1413773bda96SJonathan Lemon memset(&uart, 0, sizeof(uart)); 1414773bda96SJonathan Lemon uart.port.dev = &pdev->dev; 1415773bda96SJonathan Lemon uart.port.iotype = UPIO_MEM; 1416773bda96SJonathan Lemon uart.port.regshift = 2; 1417773bda96SJonathan Lemon uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset; 1418773bda96SJonathan Lemon uart.port.irq = pci_irq_vector(pdev, r->irq_vec); 1419773bda96SJonathan Lemon uart.port.uartclk = 50000000; 1420773bda96SJonathan Lemon uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP; 1421773bda96SJonathan Lemon uart.port.type = PORT_16550A; 1422773bda96SJonathan Lemon 1423773bda96SJonathan Lemon return serial8250_register_8250_port(&uart); 1424773bda96SJonathan Lemon } 1425773bda96SJonathan Lemon 1426773bda96SJonathan Lemon static int 1427773bda96SJonathan Lemon ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r) 1428773bda96SJonathan Lemon { 1429773bda96SJonathan Lemon int port; 1430773bda96SJonathan Lemon 1431773bda96SJonathan Lemon port = ptp_ocp_serial_line(bp, r); 1432773bda96SJonathan Lemon if (port < 0) 1433773bda96SJonathan Lemon return port; 1434773bda96SJonathan Lemon 1435773bda96SJonathan Lemon bp_assign_entry(bp, r, port); 1436773bda96SJonathan Lemon 1437773bda96SJonathan Lemon return 0; 1438773bda96SJonathan Lemon } 1439773bda96SJonathan Lemon 1440773bda96SJonathan Lemon static int 1441773bda96SJonathan Lemon ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r) 1442773bda96SJonathan Lemon { 1443773bda96SJonathan Lemon void __iomem *mem; 1444773bda96SJonathan Lemon 1445773bda96SJonathan Lemon mem = ptp_ocp_get_mem(bp, r); 1446c7521d3aSDan Carpenter if (IS_ERR(mem)) 1447c7521d3aSDan Carpenter return PTR_ERR(mem); 1448773bda96SJonathan Lemon 1449773bda96SJonathan Lemon bp_assign_entry(bp, r, mem); 1450773bda96SJonathan Lemon 1451773bda96SJonathan Lemon return 0; 1452773bda96SJonathan Lemon } 1453773bda96SJonathan Lemon 1454e3516bb4SJonathan Lemon static void 1455e3516bb4SJonathan Lemon ptp_ocp_nmea_out_init(struct ptp_ocp *bp) 1456e3516bb4SJonathan Lemon { 1457e3516bb4SJonathan Lemon if (!bp->nmea_out) 1458e3516bb4SJonathan Lemon return; 1459e3516bb4SJonathan Lemon 1460e3516bb4SJonathan Lemon iowrite32(0, &bp->nmea_out->ctrl); /* disable */ 1461e3516bb4SJonathan Lemon iowrite32(7, &bp->nmea_out->uart_baud); /* 115200 */ 1462e3516bb4SJonathan Lemon iowrite32(1, &bp->nmea_out->ctrl); /* enable */ 1463e3516bb4SJonathan Lemon } 1464e3516bb4SJonathan Lemon 1465773bda96SJonathan Lemon /* FB specific board initializers; last "resource" registered. */ 1466773bda96SJonathan Lemon static int 1467773bda96SJonathan Lemon ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r) 1468773bda96SJonathan Lemon { 1469773bda96SJonathan Lemon bp->flash_start = 1024 * 4096; 1470*0cfcdd1eSJonathan Lemon bp->eeprom_map = fb_eeprom_map; 1471773bda96SJonathan Lemon 1472065efcc5SJonathan Lemon ptp_ocp_tod_init(bp); 1473e3516bb4SJonathan Lemon ptp_ocp_nmea_out_init(bp); 1474065efcc5SJonathan Lemon 1475773bda96SJonathan Lemon return ptp_ocp_init_clock(bp); 1476773bda96SJonathan Lemon } 1477773bda96SJonathan Lemon 147856ec4403SJonathan Lemon static bool 147956ec4403SJonathan Lemon ptp_ocp_allow_irq(struct ptp_ocp *bp, struct ocp_resource *r) 148056ec4403SJonathan Lemon { 148156ec4403SJonathan Lemon bool allow = !r->irq_vec || r->irq_vec < bp->n_irqs; 148256ec4403SJonathan Lemon 148356ec4403SJonathan Lemon if (!allow) 148456ec4403SJonathan Lemon dev_err(&bp->pdev->dev, "irq %d out of range, skipping %s\n", 148556ec4403SJonathan Lemon r->irq_vec, r->name); 148656ec4403SJonathan Lemon return allow; 148756ec4403SJonathan Lemon } 148856ec4403SJonathan Lemon 1489773bda96SJonathan Lemon static int 1490773bda96SJonathan Lemon ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data) 1491773bda96SJonathan Lemon { 1492773bda96SJonathan Lemon struct ocp_resource *r, *table; 1493773bda96SJonathan Lemon int err = 0; 1494773bda96SJonathan Lemon 1495773bda96SJonathan Lemon table = (struct ocp_resource *)driver_data; 1496773bda96SJonathan Lemon for (r = table; r->setup; r++) { 149756ec4403SJonathan Lemon if (!ptp_ocp_allow_irq(bp, r)) 149856ec4403SJonathan Lemon continue; 1499773bda96SJonathan Lemon err = r->setup(bp, r); 1500bceff290SJonathan Lemon if (err) { 1501bceff290SJonathan Lemon dev_err(&bp->pdev->dev, 1502bceff290SJonathan Lemon "Could not register %s: err %d\n", 1503bceff290SJonathan Lemon r->name, err); 1504773bda96SJonathan Lemon break; 1505773bda96SJonathan Lemon } 1506bceff290SJonathan Lemon } 1507773bda96SJonathan Lemon return err; 1508773bda96SJonathan Lemon } 1509773bda96SJonathan Lemon 15106baf2925SJonathan Lemon static void 15116baf2925SJonathan Lemon ptp_ocp_enable_fpga(u32 __iomem *reg, u32 bit, bool enable) 15126baf2925SJonathan Lemon { 15136baf2925SJonathan Lemon u32 ctrl; 15146baf2925SJonathan Lemon bool on; 15156baf2925SJonathan Lemon 15166baf2925SJonathan Lemon ctrl = ioread32(reg); 15176baf2925SJonathan Lemon on = ctrl & bit; 15186baf2925SJonathan Lemon if (on ^ enable) { 15196baf2925SJonathan Lemon ctrl &= ~bit; 15206baf2925SJonathan Lemon ctrl |= enable ? bit : 0; 15216baf2925SJonathan Lemon iowrite32(ctrl, reg); 15226baf2925SJonathan Lemon } 15236baf2925SJonathan Lemon } 15246baf2925SJonathan Lemon 15256baf2925SJonathan Lemon static void 15266baf2925SJonathan Lemon ptp_ocp_irig_out(struct ptp_ocp *bp, bool enable) 15276baf2925SJonathan Lemon { 15286baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->irig_out->ctrl, 15296baf2925SJonathan Lemon IRIG_M_CTRL_ENABLE, enable); 15306baf2925SJonathan Lemon } 15316baf2925SJonathan Lemon 15326baf2925SJonathan Lemon static void 15336baf2925SJonathan Lemon ptp_ocp_irig_in(struct ptp_ocp *bp, bool enable) 15346baf2925SJonathan Lemon { 15356baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->irig_in->ctrl, 15366baf2925SJonathan Lemon IRIG_S_CTRL_ENABLE, enable); 15376baf2925SJonathan Lemon } 15386baf2925SJonathan Lemon 15396baf2925SJonathan Lemon static void 15406baf2925SJonathan Lemon ptp_ocp_dcf_out(struct ptp_ocp *bp, bool enable) 15416baf2925SJonathan Lemon { 15426baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->dcf_out->ctrl, 15436baf2925SJonathan Lemon DCF_M_CTRL_ENABLE, enable); 15446baf2925SJonathan Lemon } 15456baf2925SJonathan Lemon 15466baf2925SJonathan Lemon static void 15476baf2925SJonathan Lemon ptp_ocp_dcf_in(struct ptp_ocp *bp, bool enable) 15486baf2925SJonathan Lemon { 15496baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->dcf_in->ctrl, 15506baf2925SJonathan Lemon DCF_S_CTRL_ENABLE, enable); 15516baf2925SJonathan Lemon } 15526baf2925SJonathan Lemon 15536baf2925SJonathan Lemon static void 15546baf2925SJonathan Lemon __handle_signal_outputs(struct ptp_ocp *bp, u32 val) 15556baf2925SJonathan Lemon { 15566baf2925SJonathan Lemon ptp_ocp_irig_out(bp, val & 0x00100010); 15576baf2925SJonathan Lemon ptp_ocp_dcf_out(bp, val & 0x00200020); 15586baf2925SJonathan Lemon } 15596baf2925SJonathan Lemon 15606baf2925SJonathan Lemon static void 15616baf2925SJonathan Lemon __handle_signal_inputs(struct ptp_ocp *bp, u32 val) 15626baf2925SJonathan Lemon { 15636baf2925SJonathan Lemon ptp_ocp_irig_in(bp, val & 0x00100010); 15646baf2925SJonathan Lemon ptp_ocp_dcf_in(bp, val & 0x00200020); 15656baf2925SJonathan Lemon } 15666baf2925SJonathan Lemon 1567e1daf0ecSJonathan Lemon /* 1568e1daf0ecSJonathan Lemon * ANT0 == gps (in) 1569e1daf0ecSJonathan Lemon * ANT1 == sma1 (in) 1570e1daf0ecSJonathan Lemon * ANT2 == sma2 (in) 1571e1daf0ecSJonathan Lemon * ANT3 == sma3 (out) 1572e1daf0ecSJonathan Lemon * ANT4 == sma4 (out) 1573e1daf0ecSJonathan Lemon */ 1574e1daf0ecSJonathan Lemon 1575e1daf0ecSJonathan Lemon enum ptp_ocp_sma_mode { 1576e1daf0ecSJonathan Lemon SMA_MODE_IN, 1577e1daf0ecSJonathan Lemon SMA_MODE_OUT, 1578e1daf0ecSJonathan Lemon }; 1579e1daf0ecSJonathan Lemon 1580e1daf0ecSJonathan Lemon static struct ptp_ocp_sma_connector { 1581e1daf0ecSJonathan Lemon enum ptp_ocp_sma_mode mode; 1582e1daf0ecSJonathan Lemon bool fixed_mode; 1583e1daf0ecSJonathan Lemon u16 default_out_idx; 1584e1daf0ecSJonathan Lemon } ptp_ocp_sma_map[4] = { 1585e1daf0ecSJonathan Lemon { 1586e1daf0ecSJonathan Lemon .mode = SMA_MODE_IN, 1587e1daf0ecSJonathan Lemon .fixed_mode = true, 1588e1daf0ecSJonathan Lemon }, 1589e1daf0ecSJonathan Lemon { 1590e1daf0ecSJonathan Lemon .mode = SMA_MODE_IN, 1591e1daf0ecSJonathan Lemon .fixed_mode = true, 1592e1daf0ecSJonathan Lemon }, 1593e1daf0ecSJonathan Lemon { 1594e1daf0ecSJonathan Lemon .mode = SMA_MODE_OUT, 1595e1daf0ecSJonathan Lemon .fixed_mode = true, 1596e1daf0ecSJonathan Lemon .default_out_idx = 0, /* 10Mhz */ 1597e1daf0ecSJonathan Lemon }, 1598e1daf0ecSJonathan Lemon { 1599e1daf0ecSJonathan Lemon .mode = SMA_MODE_OUT, 1600e1daf0ecSJonathan Lemon .fixed_mode = true, 1601e1daf0ecSJonathan Lemon .default_out_idx = 1, /* PHC */ 1602e1daf0ecSJonathan Lemon }, 1603e1daf0ecSJonathan Lemon }; 1604e1daf0ecSJonathan Lemon 1605e1daf0ecSJonathan Lemon static ssize_t 1606e1daf0ecSJonathan Lemon ptp_ocp_show_output(u32 val, char *buf, int default_idx) 1607e1daf0ecSJonathan Lemon { 1608e1daf0ecSJonathan Lemon const char *name; 1609e1daf0ecSJonathan Lemon ssize_t count; 1610e1daf0ecSJonathan Lemon 1611e1daf0ecSJonathan Lemon count = sysfs_emit(buf, "OUT: "); 1612e1daf0ecSJonathan Lemon name = ptp_ocp_select_name_from_val(ptp_ocp_sma_out, val); 1613e1daf0ecSJonathan Lemon if (!name) 1614e1daf0ecSJonathan Lemon name = ptp_ocp_sma_out[default_idx].name; 1615e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s\n", name); 1616e1daf0ecSJonathan Lemon return count; 1617e1daf0ecSJonathan Lemon } 1618e1daf0ecSJonathan Lemon 1619e1daf0ecSJonathan Lemon static ssize_t 1620e1daf0ecSJonathan Lemon ptp_ocp_show_inputs(u32 val, char *buf, const char *zero_in) 1621e1daf0ecSJonathan Lemon { 1622e1daf0ecSJonathan Lemon const char *name; 1623e1daf0ecSJonathan Lemon ssize_t count; 1624e1daf0ecSJonathan Lemon int i; 1625e1daf0ecSJonathan Lemon 1626e1daf0ecSJonathan Lemon count = sysfs_emit(buf, "IN: "); 1627e1daf0ecSJonathan Lemon for (i = 0; i < ARRAY_SIZE(ptp_ocp_sma_in); i++) { 1628e1daf0ecSJonathan Lemon if (val & ptp_ocp_sma_in[i].value) { 1629e1daf0ecSJonathan Lemon name = ptp_ocp_sma_in[i].name; 1630e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", name); 1631e1daf0ecSJonathan Lemon } 1632e1daf0ecSJonathan Lemon } 1633e1daf0ecSJonathan Lemon if (!val && zero_in) 1634e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", zero_in); 1635e1daf0ecSJonathan Lemon if (count) 1636e1daf0ecSJonathan Lemon count--; 1637e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "\n"); 1638e1daf0ecSJonathan Lemon return count; 1639e1daf0ecSJonathan Lemon } 1640e1daf0ecSJonathan Lemon 1641e1daf0ecSJonathan Lemon static int 1642e1daf0ecSJonathan Lemon sma_parse_inputs(const char *buf, enum ptp_ocp_sma_mode *mode) 1643e1daf0ecSJonathan Lemon { 1644e1daf0ecSJonathan Lemon struct ocp_selector *tbl[] = { ptp_ocp_sma_in, ptp_ocp_sma_out }; 1645e1daf0ecSJonathan Lemon int idx, count, dir; 1646e1daf0ecSJonathan Lemon char **argv; 1647e1daf0ecSJonathan Lemon int ret; 1648e1daf0ecSJonathan Lemon 1649e1daf0ecSJonathan Lemon argv = argv_split(GFP_KERNEL, buf, &count); 1650e1daf0ecSJonathan Lemon if (!argv) 1651e1daf0ecSJonathan Lemon return -ENOMEM; 1652e1daf0ecSJonathan Lemon 1653e1daf0ecSJonathan Lemon ret = -EINVAL; 1654e1daf0ecSJonathan Lemon if (!count) 1655e1daf0ecSJonathan Lemon goto out; 1656e1daf0ecSJonathan Lemon 1657e1daf0ecSJonathan Lemon idx = 0; 1658e1daf0ecSJonathan Lemon dir = *mode == SMA_MODE_IN ? 0 : 1; 1659e1daf0ecSJonathan Lemon if (!strcasecmp("IN:", argv[idx])) { 1660e1daf0ecSJonathan Lemon dir = 0; 1661e1daf0ecSJonathan Lemon idx++; 1662e1daf0ecSJonathan Lemon } 1663e1daf0ecSJonathan Lemon if (!strcasecmp("OUT:", argv[0])) { 1664e1daf0ecSJonathan Lemon dir = 1; 1665e1daf0ecSJonathan Lemon idx++; 1666e1daf0ecSJonathan Lemon } 1667e1daf0ecSJonathan Lemon *mode = dir == 0 ? SMA_MODE_IN : SMA_MODE_OUT; 1668e1daf0ecSJonathan Lemon 1669e1daf0ecSJonathan Lemon ret = 0; 1670e1daf0ecSJonathan Lemon for (; idx < count; idx++) 1671e1daf0ecSJonathan Lemon ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]); 1672e1daf0ecSJonathan Lemon if (ret < 0) 1673e1daf0ecSJonathan Lemon ret = -EINVAL; 1674e1daf0ecSJonathan Lemon 1675e1daf0ecSJonathan Lemon out: 1676e1daf0ecSJonathan Lemon argv_free(argv); 1677e1daf0ecSJonathan Lemon return ret; 1678e1daf0ecSJonathan Lemon } 1679e1daf0ecSJonathan Lemon 1680e1daf0ecSJonathan Lemon static ssize_t 1681e1daf0ecSJonathan Lemon ptp_ocp_sma_show(struct ptp_ocp *bp, int sma_nr, u32 val, char *buf, 1682e1daf0ecSJonathan Lemon const char *zero_in) 1683e1daf0ecSJonathan Lemon { 1684e1daf0ecSJonathan Lemon struct ptp_ocp_sma_connector *sma = &ptp_ocp_sma_map[sma_nr - 1]; 1685e1daf0ecSJonathan Lemon 1686e1daf0ecSJonathan Lemon if (sma->mode == SMA_MODE_IN) 1687e1daf0ecSJonathan Lemon return ptp_ocp_show_inputs(val, buf, zero_in); 1688e1daf0ecSJonathan Lemon 1689e1daf0ecSJonathan Lemon return ptp_ocp_show_output(val, buf, sma->default_out_idx); 1690e1daf0ecSJonathan Lemon } 1691e1daf0ecSJonathan Lemon 1692e1daf0ecSJonathan Lemon static ssize_t 1693e1daf0ecSJonathan Lemon sma1_show(struct device *dev, struct device_attribute *attr, char *buf) 1694e1daf0ecSJonathan Lemon { 1695e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1696e1daf0ecSJonathan Lemon u32 val; 1697e1daf0ecSJonathan Lemon 1698e1daf0ecSJonathan Lemon val = ioread32(&bp->sma->gpio1) & 0x3f; 1699e1daf0ecSJonathan Lemon return ptp_ocp_sma_show(bp, 1, val, buf, ptp_ocp_sma_in[0].name); 1700e1daf0ecSJonathan Lemon } 1701e1daf0ecSJonathan Lemon 1702e1daf0ecSJonathan Lemon static ssize_t 1703e1daf0ecSJonathan Lemon sma2_show(struct device *dev, struct device_attribute *attr, char *buf) 1704e1daf0ecSJonathan Lemon { 1705e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1706e1daf0ecSJonathan Lemon u32 val; 1707e1daf0ecSJonathan Lemon 1708e1daf0ecSJonathan Lemon val = (ioread32(&bp->sma->gpio1) >> 16) & 0x3f; 1709e1daf0ecSJonathan Lemon return ptp_ocp_sma_show(bp, 2, val, buf, NULL); 1710e1daf0ecSJonathan Lemon } 1711e1daf0ecSJonathan Lemon 1712e1daf0ecSJonathan Lemon static ssize_t 1713e1daf0ecSJonathan Lemon sma3_show(struct device *dev, struct device_attribute *attr, char *buf) 1714e1daf0ecSJonathan Lemon { 1715e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1716e1daf0ecSJonathan Lemon u32 val; 1717e1daf0ecSJonathan Lemon 1718e1daf0ecSJonathan Lemon val = ioread32(&bp->sma->gpio2) & 0x3f; 1719e1daf0ecSJonathan Lemon return ptp_ocp_sma_show(bp, 3, val, buf, NULL); 1720e1daf0ecSJonathan Lemon } 1721e1daf0ecSJonathan Lemon 1722e1daf0ecSJonathan Lemon static ssize_t 1723e1daf0ecSJonathan Lemon sma4_show(struct device *dev, struct device_attribute *attr, char *buf) 1724e1daf0ecSJonathan Lemon { 1725e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1726e1daf0ecSJonathan Lemon u32 val; 1727e1daf0ecSJonathan Lemon 1728e1daf0ecSJonathan Lemon val = (ioread32(&bp->sma->gpio2) >> 16) & 0x3f; 1729e1daf0ecSJonathan Lemon return ptp_ocp_sma_show(bp, 4, val, buf, NULL); 1730e1daf0ecSJonathan Lemon } 1731e1daf0ecSJonathan Lemon 1732e1daf0ecSJonathan Lemon static void 1733e1daf0ecSJonathan Lemon ptp_ocp_sma_store_output(struct ptp_ocp *bp, u32 val, u32 shift) 1734e1daf0ecSJonathan Lemon { 1735e1daf0ecSJonathan Lemon unsigned long flags; 1736e1daf0ecSJonathan Lemon u32 gpio, mask; 1737e1daf0ecSJonathan Lemon 1738e1daf0ecSJonathan Lemon mask = 0xffff << (16 - shift); 1739e1daf0ecSJonathan Lemon 1740e1daf0ecSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1741e1daf0ecSJonathan Lemon 1742e1daf0ecSJonathan Lemon gpio = ioread32(&bp->sma->gpio2); 1743e1daf0ecSJonathan Lemon gpio = (gpio & mask) | (val << shift); 17446baf2925SJonathan Lemon 17456baf2925SJonathan Lemon __handle_signal_outputs(bp, gpio); 17466baf2925SJonathan Lemon 1747e1daf0ecSJonathan Lemon iowrite32(gpio, &bp->sma->gpio2); 1748e1daf0ecSJonathan Lemon 1749e1daf0ecSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1750e1daf0ecSJonathan Lemon } 1751e1daf0ecSJonathan Lemon 1752e1daf0ecSJonathan Lemon static void 1753e1daf0ecSJonathan Lemon ptp_ocp_sma_store_inputs(struct ptp_ocp *bp, u32 val, u32 shift) 1754e1daf0ecSJonathan Lemon { 1755e1daf0ecSJonathan Lemon unsigned long flags; 1756e1daf0ecSJonathan Lemon u32 gpio, mask; 1757e1daf0ecSJonathan Lemon 1758e1daf0ecSJonathan Lemon mask = 0xffff << (16 - shift); 1759e1daf0ecSJonathan Lemon 1760e1daf0ecSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1761e1daf0ecSJonathan Lemon 1762e1daf0ecSJonathan Lemon gpio = ioread32(&bp->sma->gpio1); 1763e1daf0ecSJonathan Lemon gpio = (gpio & mask) | (val << shift); 17646baf2925SJonathan Lemon 17656baf2925SJonathan Lemon __handle_signal_inputs(bp, gpio); 17666baf2925SJonathan Lemon 1767e1daf0ecSJonathan Lemon iowrite32(gpio, &bp->sma->gpio1); 1768e1daf0ecSJonathan Lemon 1769e1daf0ecSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1770e1daf0ecSJonathan Lemon } 1771e1daf0ecSJonathan Lemon 1772e1daf0ecSJonathan Lemon static ssize_t 1773e1daf0ecSJonathan Lemon ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr, u32 shift) 1774e1daf0ecSJonathan Lemon { 1775e1daf0ecSJonathan Lemon struct ptp_ocp_sma_connector *sma = &ptp_ocp_sma_map[sma_nr - 1]; 1776e1daf0ecSJonathan Lemon enum ptp_ocp_sma_mode mode; 1777e1daf0ecSJonathan Lemon int val; 1778e1daf0ecSJonathan Lemon 1779e1daf0ecSJonathan Lemon mode = sma->mode; 1780e1daf0ecSJonathan Lemon val = sma_parse_inputs(buf, &mode); 1781e1daf0ecSJonathan Lemon if (val < 0) 1782e1daf0ecSJonathan Lemon return val; 1783e1daf0ecSJonathan Lemon 1784e1daf0ecSJonathan Lemon if (mode != sma->mode && sma->fixed_mode) 1785e1daf0ecSJonathan Lemon return -EOPNOTSUPP; 1786e1daf0ecSJonathan Lemon 1787e1daf0ecSJonathan Lemon if (mode != sma->mode) { 1788e1daf0ecSJonathan Lemon pr_err("Mode changes not supported yet.\n"); 1789e1daf0ecSJonathan Lemon return -EOPNOTSUPP; 1790e1daf0ecSJonathan Lemon } 1791e1daf0ecSJonathan Lemon 1792e1daf0ecSJonathan Lemon if (sma->mode == SMA_MODE_IN) 1793e1daf0ecSJonathan Lemon ptp_ocp_sma_store_inputs(bp, val, shift); 1794e1daf0ecSJonathan Lemon else 1795e1daf0ecSJonathan Lemon ptp_ocp_sma_store_output(bp, val, shift); 1796e1daf0ecSJonathan Lemon 1797e1daf0ecSJonathan Lemon return 0; 1798e1daf0ecSJonathan Lemon } 1799e1daf0ecSJonathan Lemon 1800e1daf0ecSJonathan Lemon static ssize_t 1801e1daf0ecSJonathan Lemon sma1_store(struct device *dev, struct device_attribute *attr, 1802e1daf0ecSJonathan Lemon const char *buf, size_t count) 1803e1daf0ecSJonathan Lemon { 1804e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1805e1daf0ecSJonathan Lemon int err; 1806e1daf0ecSJonathan Lemon 1807e1daf0ecSJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 1, 0); 1808e1daf0ecSJonathan Lemon return err ? err : count; 1809e1daf0ecSJonathan Lemon } 1810e1daf0ecSJonathan Lemon 1811e1daf0ecSJonathan Lemon static ssize_t 1812e1daf0ecSJonathan Lemon sma2_store(struct device *dev, struct device_attribute *attr, 1813e1daf0ecSJonathan Lemon const char *buf, size_t count) 1814e1daf0ecSJonathan Lemon { 1815e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1816e1daf0ecSJonathan Lemon int err; 1817e1daf0ecSJonathan Lemon 1818e1daf0ecSJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 2, 16); 1819e1daf0ecSJonathan Lemon return err ? err : count; 1820e1daf0ecSJonathan Lemon } 1821e1daf0ecSJonathan Lemon 1822e1daf0ecSJonathan Lemon static ssize_t 1823e1daf0ecSJonathan Lemon sma3_store(struct device *dev, struct device_attribute *attr, 1824e1daf0ecSJonathan Lemon const char *buf, size_t count) 1825e1daf0ecSJonathan Lemon { 1826e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1827e1daf0ecSJonathan Lemon int err; 1828e1daf0ecSJonathan Lemon 1829e1daf0ecSJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 3, 0); 1830e1daf0ecSJonathan Lemon return err ? err : count; 1831e1daf0ecSJonathan Lemon } 1832e1daf0ecSJonathan Lemon 1833e1daf0ecSJonathan Lemon static ssize_t 1834e1daf0ecSJonathan Lemon sma4_store(struct device *dev, struct device_attribute *attr, 1835e1daf0ecSJonathan Lemon const char *buf, size_t count) 1836e1daf0ecSJonathan Lemon { 1837e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1838e1daf0ecSJonathan Lemon int err; 1839e1daf0ecSJonathan Lemon 1840e1daf0ecSJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 4, 16); 1841e1daf0ecSJonathan Lemon return err ? err : count; 1842e1daf0ecSJonathan Lemon } 1843e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma1); 1844e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma2); 1845e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma3); 1846e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma4); 1847e1daf0ecSJonathan Lemon 1848e1daf0ecSJonathan Lemon static ssize_t 1849e1daf0ecSJonathan Lemon available_sma_inputs_show(struct device *dev, 1850e1daf0ecSJonathan Lemon struct device_attribute *attr, char *buf) 1851e1daf0ecSJonathan Lemon { 1852e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_sma_in, buf); 1853e1daf0ecSJonathan Lemon } 1854e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_inputs); 1855e1daf0ecSJonathan Lemon 1856e1daf0ecSJonathan Lemon static ssize_t 1857e1daf0ecSJonathan Lemon available_sma_outputs_show(struct device *dev, 1858e1daf0ecSJonathan Lemon struct device_attribute *attr, char *buf) 1859e1daf0ecSJonathan Lemon { 1860e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_sma_out, buf); 1861e1daf0ecSJonathan Lemon } 1862e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_outputs); 1863e1daf0ecSJonathan Lemon 1864773bda96SJonathan Lemon static ssize_t 1865773bda96SJonathan Lemon serialnum_show(struct device *dev, struct device_attribute *attr, char *buf) 1866773bda96SJonathan Lemon { 1867773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1868773bda96SJonathan Lemon 1869*0cfcdd1eSJonathan Lemon if (!bp->has_eeprom_data) 1870*0cfcdd1eSJonathan Lemon ptp_ocp_read_eeprom(bp); 1871773bda96SJonathan Lemon 1872773bda96SJonathan Lemon return sysfs_emit(buf, "%pM\n", bp->serial); 1873773bda96SJonathan Lemon } 1874773bda96SJonathan Lemon static DEVICE_ATTR_RO(serialnum); 1875773bda96SJonathan Lemon 1876773bda96SJonathan Lemon static ssize_t 1877ef0cfb34SJonathan Lemon gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf) 1878773bda96SJonathan Lemon { 1879773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1880773bda96SJonathan Lemon ssize_t ret; 1881773bda96SJonathan Lemon 1882ef0cfb34SJonathan Lemon if (bp->gnss_lost) 1883ef0cfb34SJonathan Lemon ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost); 1884773bda96SJonathan Lemon else 1885773bda96SJonathan Lemon ret = sysfs_emit(buf, "SYNC\n"); 1886773bda96SJonathan Lemon 1887773bda96SJonathan Lemon return ret; 1888773bda96SJonathan Lemon } 1889ef0cfb34SJonathan Lemon static DEVICE_ATTR_RO(gnss_sync); 1890773bda96SJonathan Lemon 1891773bda96SJonathan Lemon static ssize_t 189289260d87SJonathan Lemon utc_tai_offset_show(struct device *dev, 189389260d87SJonathan Lemon struct device_attribute *attr, char *buf) 189489260d87SJonathan Lemon { 189589260d87SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 189689260d87SJonathan Lemon 189789260d87SJonathan Lemon return sysfs_emit(buf, "%d\n", bp->utc_tai_offset); 189889260d87SJonathan Lemon } 189989260d87SJonathan Lemon 190089260d87SJonathan Lemon static ssize_t 190189260d87SJonathan Lemon utc_tai_offset_store(struct device *dev, 190289260d87SJonathan Lemon struct device_attribute *attr, 190389260d87SJonathan Lemon const char *buf, size_t count) 190489260d87SJonathan Lemon { 190589260d87SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 190689260d87SJonathan Lemon int err; 190789260d87SJonathan Lemon u32 val; 190889260d87SJonathan Lemon 190989260d87SJonathan Lemon err = kstrtou32(buf, 0, &val); 191089260d87SJonathan Lemon if (err) 191189260d87SJonathan Lemon return err; 191289260d87SJonathan Lemon 191389260d87SJonathan Lemon ptp_ocp_utc_distribute(bp, val); 191489260d87SJonathan Lemon 191589260d87SJonathan Lemon return count; 191689260d87SJonathan Lemon } 191789260d87SJonathan Lemon static DEVICE_ATTR_RW(utc_tai_offset); 191889260d87SJonathan Lemon 191989260d87SJonathan Lemon static ssize_t 19201acffc6eSJonathan Lemon ts_window_adjust_show(struct device *dev, 19211acffc6eSJonathan Lemon struct device_attribute *attr, char *buf) 19221acffc6eSJonathan Lemon { 19231acffc6eSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 19241acffc6eSJonathan Lemon 19251acffc6eSJonathan Lemon return sysfs_emit(buf, "%d\n", bp->ts_window_adjust); 19261acffc6eSJonathan Lemon } 19271acffc6eSJonathan Lemon 19281acffc6eSJonathan Lemon static ssize_t 19291acffc6eSJonathan Lemon ts_window_adjust_store(struct device *dev, 19301acffc6eSJonathan Lemon struct device_attribute *attr, 19311acffc6eSJonathan Lemon const char *buf, size_t count) 19321acffc6eSJonathan Lemon { 19331acffc6eSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 19341acffc6eSJonathan Lemon int err; 19351acffc6eSJonathan Lemon u32 val; 19361acffc6eSJonathan Lemon 19371acffc6eSJonathan Lemon err = kstrtou32(buf, 0, &val); 19381acffc6eSJonathan Lemon if (err) 19391acffc6eSJonathan Lemon return err; 19401acffc6eSJonathan Lemon 19411acffc6eSJonathan Lemon bp->ts_window_adjust = val; 19421acffc6eSJonathan Lemon 19431acffc6eSJonathan Lemon return count; 19441acffc6eSJonathan Lemon } 19451acffc6eSJonathan Lemon static DEVICE_ATTR_RW(ts_window_adjust); 19461acffc6eSJonathan Lemon 19471acffc6eSJonathan Lemon static ssize_t 1948d14ee252SJonathan Lemon irig_b_mode_show(struct device *dev, struct device_attribute *attr, char *buf) 1949d14ee252SJonathan Lemon { 1950d14ee252SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1951d14ee252SJonathan Lemon u32 val; 1952d14ee252SJonathan Lemon 1953d14ee252SJonathan Lemon val = ioread32(&bp->irig_out->ctrl); 1954d14ee252SJonathan Lemon val = (val >> 16) & 0x07; 1955d14ee252SJonathan Lemon return sysfs_emit(buf, "%d\n", val); 1956d14ee252SJonathan Lemon } 1957d14ee252SJonathan Lemon 1958d14ee252SJonathan Lemon static ssize_t 1959d14ee252SJonathan Lemon irig_b_mode_store(struct device *dev, 1960d14ee252SJonathan Lemon struct device_attribute *attr, 1961d14ee252SJonathan Lemon const char *buf, size_t count) 1962d14ee252SJonathan Lemon { 1963d14ee252SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1964d14ee252SJonathan Lemon unsigned long flags; 1965d14ee252SJonathan Lemon int err; 1966d14ee252SJonathan Lemon u32 reg; 1967d14ee252SJonathan Lemon u8 val; 1968d14ee252SJonathan Lemon 1969d14ee252SJonathan Lemon err = kstrtou8(buf, 0, &val); 1970d14ee252SJonathan Lemon if (err) 1971d14ee252SJonathan Lemon return err; 1972d14ee252SJonathan Lemon if (val > 7) 1973d14ee252SJonathan Lemon return -EINVAL; 1974d14ee252SJonathan Lemon 1975d14ee252SJonathan Lemon reg = ((val & 0x7) << 16); 1976d14ee252SJonathan Lemon 1977d14ee252SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1978d14ee252SJonathan Lemon iowrite32(0, &bp->irig_out->ctrl); /* disable */ 1979d14ee252SJonathan Lemon iowrite32(reg, &bp->irig_out->ctrl); /* change mode */ 1980d14ee252SJonathan Lemon iowrite32(reg | IRIG_M_CTRL_ENABLE, &bp->irig_out->ctrl); 1981d14ee252SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1982d14ee252SJonathan Lemon 1983d14ee252SJonathan Lemon return count; 1984d14ee252SJonathan Lemon } 1985d14ee252SJonathan Lemon static DEVICE_ATTR_RW(irig_b_mode); 1986d14ee252SJonathan Lemon 1987d14ee252SJonathan Lemon static ssize_t 1988773bda96SJonathan Lemon clock_source_show(struct device *dev, struct device_attribute *attr, char *buf) 1989773bda96SJonathan Lemon { 1990773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1991773bda96SJonathan Lemon const char *p; 1992773bda96SJonathan Lemon u32 select; 1993773bda96SJonathan Lemon 1994773bda96SJonathan Lemon select = ioread32(&bp->reg->select); 1995e1daf0ecSJonathan Lemon p = ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16); 1996773bda96SJonathan Lemon 1997773bda96SJonathan Lemon return sysfs_emit(buf, "%s\n", p); 1998773bda96SJonathan Lemon } 1999773bda96SJonathan Lemon 2000773bda96SJonathan Lemon static ssize_t 2001773bda96SJonathan Lemon clock_source_store(struct device *dev, struct device_attribute *attr, 2002773bda96SJonathan Lemon const char *buf, size_t count) 2003773bda96SJonathan Lemon { 2004773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2005773bda96SJonathan Lemon unsigned long flags; 2006773bda96SJonathan Lemon int val; 2007773bda96SJonathan Lemon 2008e1daf0ecSJonathan Lemon val = ptp_ocp_select_val_from_name(ptp_ocp_clock, buf); 2009773bda96SJonathan Lemon if (val < 0) 2010773bda96SJonathan Lemon return val; 2011773bda96SJonathan Lemon 2012773bda96SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 2013773bda96SJonathan Lemon iowrite32(val, &bp->reg->select); 2014773bda96SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 2015773bda96SJonathan Lemon 2016773bda96SJonathan Lemon return count; 2017773bda96SJonathan Lemon } 2018773bda96SJonathan Lemon static DEVICE_ATTR_RW(clock_source); 2019773bda96SJonathan Lemon 2020773bda96SJonathan Lemon static ssize_t 2021773bda96SJonathan Lemon available_clock_sources_show(struct device *dev, 2022773bda96SJonathan Lemon struct device_attribute *attr, char *buf) 2023773bda96SJonathan Lemon { 2024e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_clock, buf); 2025773bda96SJonathan Lemon } 2026773bda96SJonathan Lemon static DEVICE_ATTR_RO(available_clock_sources); 2027773bda96SJonathan Lemon 20282f23f486SVadim Fedorenko static ssize_t 20292f23f486SVadim Fedorenko clock_status_drift_show(struct device *dev, 20302f23f486SVadim Fedorenko struct device_attribute *attr, char *buf) 20312f23f486SVadim Fedorenko { 20322f23f486SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 20332f23f486SVadim Fedorenko u32 val; 20342f23f486SVadim Fedorenko int res; 20352f23f486SVadim Fedorenko 20362f23f486SVadim Fedorenko val = ioread32(&bp->reg->status_drift); 20372f23f486SVadim Fedorenko res = (val & ~INT_MAX) ? -1 : 1; 20382f23f486SVadim Fedorenko res *= (val & INT_MAX); 20392f23f486SVadim Fedorenko return sysfs_emit(buf, "%d\n", res); 20402f23f486SVadim Fedorenko } 20412f23f486SVadim Fedorenko static DEVICE_ATTR_RO(clock_status_drift); 20422f23f486SVadim Fedorenko 20432f23f486SVadim Fedorenko static ssize_t 20442f23f486SVadim Fedorenko clock_status_offset_show(struct device *dev, 20452f23f486SVadim Fedorenko struct device_attribute *attr, char *buf) 20462f23f486SVadim Fedorenko { 20472f23f486SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 20482f23f486SVadim Fedorenko u32 val; 20492f23f486SVadim Fedorenko int res; 20502f23f486SVadim Fedorenko 20512f23f486SVadim Fedorenko val = ioread32(&bp->reg->status_offset); 20522f23f486SVadim Fedorenko res = (val & ~INT_MAX) ? -1 : 1; 20532f23f486SVadim Fedorenko res *= (val & INT_MAX); 20542f23f486SVadim Fedorenko return sysfs_emit(buf, "%d\n", res); 20552f23f486SVadim Fedorenko } 20562f23f486SVadim Fedorenko static DEVICE_ATTR_RO(clock_status_offset); 20572f23f486SVadim Fedorenko 205844a412d1SVadim Fedorenko static ssize_t 205944a412d1SVadim Fedorenko tod_correction_show(struct device *dev, 206044a412d1SVadim Fedorenko struct device_attribute *attr, char *buf) 206144a412d1SVadim Fedorenko { 206244a412d1SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 206344a412d1SVadim Fedorenko u32 val; 206444a412d1SVadim Fedorenko int res; 206544a412d1SVadim Fedorenko 206644a412d1SVadim Fedorenko val = ioread32(&bp->tod->adj_sec); 206744a412d1SVadim Fedorenko res = (val & ~INT_MAX) ? -1 : 1; 206844a412d1SVadim Fedorenko res *= (val & INT_MAX); 206944a412d1SVadim Fedorenko return sysfs_emit(buf, "%d\n", res); 207044a412d1SVadim Fedorenko } 207144a412d1SVadim Fedorenko 207244a412d1SVadim Fedorenko static ssize_t 207344a412d1SVadim Fedorenko tod_correction_store(struct device *dev, struct device_attribute *attr, 207444a412d1SVadim Fedorenko const char *buf, size_t count) 207544a412d1SVadim Fedorenko { 207644a412d1SVadim Fedorenko struct ptp_ocp *bp = dev_get_drvdata(dev); 207744a412d1SVadim Fedorenko unsigned long flags; 207844a412d1SVadim Fedorenko int err, res; 207944a412d1SVadim Fedorenko u32 val = 0; 208044a412d1SVadim Fedorenko 208144a412d1SVadim Fedorenko err = kstrtos32(buf, 0, &res); 208244a412d1SVadim Fedorenko if (err) 208344a412d1SVadim Fedorenko return err; 208444a412d1SVadim Fedorenko if (res < 0) { 208544a412d1SVadim Fedorenko res *= -1; 208644a412d1SVadim Fedorenko val |= BIT(31); 208744a412d1SVadim Fedorenko } 208844a412d1SVadim Fedorenko val |= res; 208944a412d1SVadim Fedorenko 209044a412d1SVadim Fedorenko spin_lock_irqsave(&bp->lock, flags); 209144a412d1SVadim Fedorenko iowrite32(val, &bp->tod->adj_sec); 209244a412d1SVadim Fedorenko spin_unlock_irqrestore(&bp->lock, flags); 209344a412d1SVadim Fedorenko 209444a412d1SVadim Fedorenko return count; 209544a412d1SVadim Fedorenko } 209644a412d1SVadim Fedorenko static DEVICE_ATTR_RW(tod_correction); 209744a412d1SVadim Fedorenko 2098773bda96SJonathan Lemon static struct attribute *timecard_attrs[] = { 2099773bda96SJonathan Lemon &dev_attr_serialnum.attr, 2100ef0cfb34SJonathan Lemon &dev_attr_gnss_sync.attr, 2101773bda96SJonathan Lemon &dev_attr_clock_source.attr, 2102773bda96SJonathan Lemon &dev_attr_available_clock_sources.attr, 2103e1daf0ecSJonathan Lemon &dev_attr_sma1.attr, 2104e1daf0ecSJonathan Lemon &dev_attr_sma2.attr, 2105e1daf0ecSJonathan Lemon &dev_attr_sma3.attr, 2106e1daf0ecSJonathan Lemon &dev_attr_sma4.attr, 2107e1daf0ecSJonathan Lemon &dev_attr_available_sma_inputs.attr, 2108e1daf0ecSJonathan Lemon &dev_attr_available_sma_outputs.attr, 21092f23f486SVadim Fedorenko &dev_attr_clock_status_drift.attr, 21102f23f486SVadim Fedorenko &dev_attr_clock_status_offset.attr, 2111d14ee252SJonathan Lemon &dev_attr_irig_b_mode.attr, 211289260d87SJonathan Lemon &dev_attr_utc_tai_offset.attr, 21131acffc6eSJonathan Lemon &dev_attr_ts_window_adjust.attr, 211444a412d1SVadim Fedorenko &dev_attr_tod_correction.attr, 2115773bda96SJonathan Lemon NULL, 2116773bda96SJonathan Lemon }; 2117773bda96SJonathan Lemon ATTRIBUTE_GROUPS(timecard); 2118773bda96SJonathan Lemon 2119f67bf662SJonathan Lemon static const char * 2120f67bf662SJonathan Lemon gpio_map(u32 gpio, u32 bit, const char *pri, const char *sec, const char *def) 2121f67bf662SJonathan Lemon { 2122f67bf662SJonathan Lemon const char *ans; 2123f67bf662SJonathan Lemon 2124f67bf662SJonathan Lemon if (gpio & (1 << bit)) 2125f67bf662SJonathan Lemon ans = pri; 2126f67bf662SJonathan Lemon else if (gpio & (1 << (bit + 16))) 2127f67bf662SJonathan Lemon ans = sec; 2128f67bf662SJonathan Lemon else 2129f67bf662SJonathan Lemon ans = def; 2130f67bf662SJonathan Lemon return ans; 2131f67bf662SJonathan Lemon } 2132f67bf662SJonathan Lemon 2133f67bf662SJonathan Lemon static void 2134f67bf662SJonathan Lemon gpio_multi_map(char *buf, u32 gpio, u32 bit, 2135f67bf662SJonathan Lemon const char *pri, const char *sec, const char *def) 2136f67bf662SJonathan Lemon { 2137f67bf662SJonathan Lemon char *ans = buf; 2138f67bf662SJonathan Lemon 2139f67bf662SJonathan Lemon strcpy(ans, def); 2140f67bf662SJonathan Lemon if (gpio & (1 << bit)) 2141f67bf662SJonathan Lemon ans += sprintf(ans, "%s ", pri); 2142f67bf662SJonathan Lemon if (gpio & (1 << (bit + 16))) 2143f67bf662SJonathan Lemon ans += sprintf(ans, "%s ", sec); 2144f67bf662SJonathan Lemon } 2145f67bf662SJonathan Lemon 2146f67bf662SJonathan Lemon static int 2147f67bf662SJonathan Lemon ptp_ocp_summary_show(struct seq_file *s, void *data) 2148f67bf662SJonathan Lemon { 2149f67bf662SJonathan Lemon struct device *dev = s->private; 2150f67bf662SJonathan Lemon struct ptp_system_timestamp sts; 2151f67bf662SJonathan Lemon u32 sma_in, sma_out, ctrl, val; 2152f67bf662SJonathan Lemon struct ts_reg __iomem *ts_reg; 2153f67bf662SJonathan Lemon struct timespec64 ts; 2154f67bf662SJonathan Lemon struct ptp_ocp *bp; 2155f67bf662SJonathan Lemon const char *src; 2156a62a56d0SJonathan Lemon bool on, map; 2157f67bf662SJonathan Lemon char *buf; 2158f67bf662SJonathan Lemon 2159f67bf662SJonathan Lemon buf = (char *)__get_free_page(GFP_KERNEL); 2160f67bf662SJonathan Lemon if (!buf) 2161f67bf662SJonathan Lemon return -ENOMEM; 2162f67bf662SJonathan Lemon 2163f67bf662SJonathan Lemon bp = dev_get_drvdata(dev); 2164f67bf662SJonathan Lemon sma_in = ioread32(&bp->sma->gpio1); 2165f67bf662SJonathan Lemon sma_out = ioread32(&bp->sma->gpio2); 2166f67bf662SJonathan Lemon 2167f67bf662SJonathan Lemon seq_printf(s, "%7s: /dev/ptp%d\n", "PTP", ptp_clock_index(bp->ptp)); 216861fd7ac2SJonathan Lemon if (bp->gnss_port != -1) 216961fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS1", bp->gnss_port); 217061fd7ac2SJonathan Lemon if (bp->gnss2_port != -1) 217161fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS2", bp->gnss2_port); 217261fd7ac2SJonathan Lemon if (bp->mac_port != -1) 217361fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "MAC", bp->mac_port); 217461fd7ac2SJonathan Lemon if (bp->nmea_port != -1) 217561fd7ac2SJonathan Lemon seq_printf(s, "%7s: /dev/ttyS%d\n", "NMEA", bp->nmea_port); 2176f67bf662SJonathan Lemon 2177f67bf662SJonathan Lemon sma1_show(dev, NULL, buf); 2178f67bf662SJonathan Lemon seq_printf(s, " sma1: %s", buf); 2179f67bf662SJonathan Lemon 2180f67bf662SJonathan Lemon sma2_show(dev, NULL, buf); 2181f67bf662SJonathan Lemon seq_printf(s, " sma2: %s", buf); 2182f67bf662SJonathan Lemon 2183f67bf662SJonathan Lemon sma3_show(dev, NULL, buf); 2184f67bf662SJonathan Lemon seq_printf(s, " sma3: %s", buf); 2185f67bf662SJonathan Lemon 2186f67bf662SJonathan Lemon sma4_show(dev, NULL, buf); 2187f67bf662SJonathan Lemon seq_printf(s, " sma4: %s", buf); 2188f67bf662SJonathan Lemon 2189f67bf662SJonathan Lemon if (bp->ts0) { 2190f67bf662SJonathan Lemon ts_reg = bp->ts0->mem; 2191f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 2192f67bf662SJonathan Lemon src = "GNSS"; 2193f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS0", 2194f67bf662SJonathan Lemon on ? " ON" : "OFF", src); 2195f67bf662SJonathan Lemon } 2196f67bf662SJonathan Lemon 2197f67bf662SJonathan Lemon if (bp->ts1) { 2198f67bf662SJonathan Lemon ts_reg = bp->ts1->mem; 2199f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 2200f67bf662SJonathan Lemon src = gpio_map(sma_in, 2, "sma1", "sma2", "----"); 2201f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS1", 2202f67bf662SJonathan Lemon on ? " ON" : "OFF", src); 2203f67bf662SJonathan Lemon } 2204f67bf662SJonathan Lemon 2205f67bf662SJonathan Lemon if (bp->ts2) { 2206f67bf662SJonathan Lemon ts_reg = bp->ts2->mem; 2207f67bf662SJonathan Lemon on = ioread32(&ts_reg->enable); 2208f67bf662SJonathan Lemon src = gpio_map(sma_in, 3, "sma1", "sma2", "----"); 2209f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS2", 2210f67bf662SJonathan Lemon on ? " ON" : "OFF", src); 2211f67bf662SJonathan Lemon } 2212f67bf662SJonathan Lemon 2213a62a56d0SJonathan Lemon if (bp->pps) { 2214a62a56d0SJonathan Lemon ts_reg = bp->pps->mem; 2215a62a56d0SJonathan Lemon src = "PHC"; 2216a62a56d0SJonathan Lemon on = ioread32(&ts_reg->enable); 2217a62a56d0SJonathan Lemon map = !!(bp->pps_req_map & OCP_REQ_TIMESTAMP); 2218a62a56d0SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "TS3", 22191a575cdeSNathan Chancellor on && map ? " ON" : "OFF", src); 2220a62a56d0SJonathan Lemon 2221a62a56d0SJonathan Lemon map = !!(bp->pps_req_map & OCP_REQ_PPS); 2222a62a56d0SJonathan Lemon seq_printf(s, "%7s: %s, src: %s\n", "PPS", 22231a575cdeSNathan Chancellor on && map ? " ON" : "OFF", src); 2224a62a56d0SJonathan Lemon } 2225a62a56d0SJonathan Lemon 2226f67bf662SJonathan Lemon if (bp->irig_out) { 2227f67bf662SJonathan Lemon ctrl = ioread32(&bp->irig_out->ctrl); 2228f67bf662SJonathan Lemon on = ctrl & IRIG_M_CTRL_ENABLE; 2229f67bf662SJonathan Lemon val = ioread32(&bp->irig_out->status); 2230f67bf662SJonathan Lemon gpio_multi_map(buf, sma_out, 4, "sma3", "sma4", "----"); 2231f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, mode %d, out: %s\n", "IRIG", 2232f67bf662SJonathan Lemon on ? " ON" : "OFF", val, (ctrl >> 16), buf); 2233f67bf662SJonathan Lemon } 2234f67bf662SJonathan Lemon 2235f67bf662SJonathan Lemon if (bp->irig_in) { 2236f67bf662SJonathan Lemon on = ioread32(&bp->irig_in->ctrl) & IRIG_S_CTRL_ENABLE; 2237f67bf662SJonathan Lemon val = ioread32(&bp->irig_in->status); 2238f67bf662SJonathan Lemon src = gpio_map(sma_in, 4, "sma1", "sma2", "----"); 2239f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, src: %s\n", "IRIG in", 2240f67bf662SJonathan Lemon on ? " ON" : "OFF", val, src); 2241f67bf662SJonathan Lemon } 2242f67bf662SJonathan Lemon 2243f67bf662SJonathan Lemon if (bp->dcf_out) { 2244f67bf662SJonathan Lemon on = ioread32(&bp->dcf_out->ctrl) & DCF_M_CTRL_ENABLE; 2245f67bf662SJonathan Lemon val = ioread32(&bp->dcf_out->status); 2246f67bf662SJonathan Lemon gpio_multi_map(buf, sma_out, 5, "sma3", "sma4", "----"); 2247f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, out: %s\n", "DCF", 2248f67bf662SJonathan Lemon on ? " ON" : "OFF", val, buf); 2249f67bf662SJonathan Lemon } 2250f67bf662SJonathan Lemon 2251f67bf662SJonathan Lemon if (bp->dcf_in) { 2252f67bf662SJonathan Lemon on = ioread32(&bp->dcf_in->ctrl) & DCF_S_CTRL_ENABLE; 2253f67bf662SJonathan Lemon val = ioread32(&bp->dcf_in->status); 2254f67bf662SJonathan Lemon src = gpio_map(sma_in, 5, "sma1", "sma2", "----"); 2255f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, error: %d, src: %s\n", "DCF in", 2256f67bf662SJonathan Lemon on ? " ON" : "OFF", val, src); 2257f67bf662SJonathan Lemon } 2258f67bf662SJonathan Lemon 2259e3516bb4SJonathan Lemon if (bp->nmea_out) { 2260e3516bb4SJonathan Lemon on = ioread32(&bp->nmea_out->ctrl) & 1; 2261e3516bb4SJonathan Lemon val = ioread32(&bp->nmea_out->status); 2262e3516bb4SJonathan Lemon seq_printf(s, "%7s: %s, error: %d\n", "NMEA", 2263e3516bb4SJonathan Lemon on ? " ON" : "OFF", val); 2264e3516bb4SJonathan Lemon } 2265e3516bb4SJonathan Lemon 2266f67bf662SJonathan Lemon /* compute src for PPS1, used below. */ 2267f67bf662SJonathan Lemon if (bp->pps_select) { 2268f67bf662SJonathan Lemon val = ioread32(&bp->pps_select->gpio1); 2269f67bf662SJonathan Lemon if (val & 0x01) 2270f67bf662SJonathan Lemon src = gpio_map(sma_in, 0, "sma1", "sma2", "----"); 2271f67bf662SJonathan Lemon else if (val & 0x02) 2272f67bf662SJonathan Lemon src = "MAC"; 2273f67bf662SJonathan Lemon else if (val & 0x04) 2274f67bf662SJonathan Lemon src = "GNSS"; 2275f67bf662SJonathan Lemon else 2276f67bf662SJonathan Lemon src = "----"; 2277f67bf662SJonathan Lemon } else { 2278f67bf662SJonathan Lemon src = "?"; 2279f67bf662SJonathan Lemon } 2280f67bf662SJonathan Lemon 2281f67bf662SJonathan Lemon /* assumes automatic switchover/selection */ 2282f67bf662SJonathan Lemon val = ioread32(&bp->reg->select); 2283f67bf662SJonathan Lemon switch (val >> 16) { 2284f67bf662SJonathan Lemon case 0: 2285f67bf662SJonathan Lemon sprintf(buf, "----"); 2286f67bf662SJonathan Lemon break; 2287f67bf662SJonathan Lemon case 2: 2288f67bf662SJonathan Lemon sprintf(buf, "IRIG"); 2289f67bf662SJonathan Lemon break; 2290f67bf662SJonathan Lemon case 3: 2291f67bf662SJonathan Lemon sprintf(buf, "%s via PPS1", src); 2292f67bf662SJonathan Lemon break; 2293f67bf662SJonathan Lemon case 6: 2294f67bf662SJonathan Lemon sprintf(buf, "DCF"); 2295f67bf662SJonathan Lemon break; 2296f67bf662SJonathan Lemon default: 2297f67bf662SJonathan Lemon strcpy(buf, "unknown"); 2298f67bf662SJonathan Lemon break; 2299f67bf662SJonathan Lemon } 2300f67bf662SJonathan Lemon val = ioread32(&bp->reg->status); 2301f67bf662SJonathan Lemon seq_printf(s, "%7s: %s, state: %s\n", "PHC src", buf, 2302f67bf662SJonathan Lemon val & OCP_STATUS_IN_SYNC ? "sync" : "unsynced"); 2303f67bf662SJonathan Lemon 2304f67bf662SJonathan Lemon /* reuses PPS1 src from earlier */ 2305f67bf662SJonathan Lemon seq_printf(s, "MAC PPS1 src: %s\n", src); 2306f67bf662SJonathan Lemon 2307f67bf662SJonathan Lemon src = gpio_map(sma_in, 1, "sma1", "sma2", "GNSS2"); 2308f67bf662SJonathan Lemon seq_printf(s, "MAC PPS2 src: %s\n", src); 2309f67bf662SJonathan Lemon 2310f67bf662SJonathan Lemon if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts)) { 2311f67bf662SJonathan Lemon struct timespec64 sys_ts; 2312f67bf662SJonathan Lemon s64 pre_ns, post_ns, ns; 2313f67bf662SJonathan Lemon 2314f67bf662SJonathan Lemon pre_ns = timespec64_to_ns(&sts.pre_ts); 2315f67bf662SJonathan Lemon post_ns = timespec64_to_ns(&sts.post_ts); 2316f67bf662SJonathan Lemon ns = (pre_ns + post_ns) / 2; 2317f67bf662SJonathan Lemon ns += (s64)bp->utc_tai_offset * NSEC_PER_SEC; 2318f67bf662SJonathan Lemon sys_ts = ns_to_timespec64(ns); 2319f67bf662SJonathan Lemon 2320f67bf662SJonathan Lemon seq_printf(s, "%7s: %lld.%ld == %ptT TAI\n", "PHC", 2321f67bf662SJonathan Lemon ts.tv_sec, ts.tv_nsec, &ts); 2322f67bf662SJonathan Lemon seq_printf(s, "%7s: %lld.%ld == %ptT UTC offset %d\n", "SYS", 2323f67bf662SJonathan Lemon sys_ts.tv_sec, sys_ts.tv_nsec, &sys_ts, 2324f67bf662SJonathan Lemon bp->utc_tai_offset); 2325f67bf662SJonathan Lemon seq_printf(s, "%7s: PHC:SYS offset: %lld window: %lld\n", "", 2326f67bf662SJonathan Lemon timespec64_to_ns(&ts) - ns, 2327f67bf662SJonathan Lemon post_ns - pre_ns); 2328f67bf662SJonathan Lemon } 2329f67bf662SJonathan Lemon 2330f67bf662SJonathan Lemon free_page((unsigned long)buf); 2331f67bf662SJonathan Lemon return 0; 2332f67bf662SJonathan Lemon } 2333f67bf662SJonathan Lemon DEFINE_SHOW_ATTRIBUTE(ptp_ocp_summary); 2334f67bf662SJonathan Lemon 23359f492c4cSVadim Fedorenko static int 23369f492c4cSVadim Fedorenko ptp_ocp_tod_status_show(struct seq_file *s, void *data) 23379f492c4cSVadim Fedorenko { 23389f492c4cSVadim Fedorenko struct device *dev = s->private; 23399f492c4cSVadim Fedorenko struct ptp_ocp *bp; 23409f492c4cSVadim Fedorenko u32 val; 23419f492c4cSVadim Fedorenko int idx; 23429f492c4cSVadim Fedorenko 23439f492c4cSVadim Fedorenko bp = dev_get_drvdata(dev); 23449f492c4cSVadim Fedorenko 23459f492c4cSVadim Fedorenko val = ioread32(&bp->tod->ctrl); 23469f492c4cSVadim Fedorenko if (!(val & TOD_CTRL_ENABLE)) { 23479f492c4cSVadim Fedorenko seq_printf(s, "TOD Slave disabled\n"); 23489f492c4cSVadim Fedorenko return 0; 23499f492c4cSVadim Fedorenko } 23509f492c4cSVadim Fedorenko seq_printf(s, "TOD Slave enabled, Control Register 0x%08X\n", val); 23519f492c4cSVadim Fedorenko 23529f492c4cSVadim Fedorenko idx = val & TOD_CTRL_PROTOCOL ? 4 : 0; 23539f492c4cSVadim Fedorenko idx += (val >> 16) & 3; 23549f492c4cSVadim Fedorenko seq_printf(s, "Protocol %s\n", ptp_ocp_tod_proto_name(idx)); 23559f492c4cSVadim Fedorenko 23569f492c4cSVadim Fedorenko idx = (val >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK; 23579f492c4cSVadim Fedorenko seq_printf(s, "GNSS %s\n", ptp_ocp_tod_gnss_name(idx)); 23589f492c4cSVadim Fedorenko 23599f492c4cSVadim Fedorenko val = ioread32(&bp->tod->version); 23609f492c4cSVadim Fedorenko seq_printf(s, "TOD Version %d.%d.%d\n", 23619f492c4cSVadim Fedorenko val >> 24, (val >> 16) & 0xff, val & 0xffff); 23629f492c4cSVadim Fedorenko 23639f492c4cSVadim Fedorenko val = ioread32(&bp->tod->status); 23649f492c4cSVadim Fedorenko seq_printf(s, "Status register: 0x%08X\n", val); 23659f492c4cSVadim Fedorenko 23669f492c4cSVadim Fedorenko val = ioread32(&bp->tod->adj_sec); 23679f492c4cSVadim Fedorenko idx = (val & ~INT_MAX) ? -1 : 1; 23689f492c4cSVadim Fedorenko idx *= (val & INT_MAX); 23699f492c4cSVadim Fedorenko seq_printf(s, "Correction seconds: %d\n", idx); 23709f492c4cSVadim Fedorenko 23719f492c4cSVadim Fedorenko val = ioread32(&bp->tod->utc_status); 23729f492c4cSVadim Fedorenko seq_printf(s, "UTC status register: 0x%08X\n", val); 23739f492c4cSVadim Fedorenko seq_printf(s, "UTC offset: %d valid:%d\n", 23749f492c4cSVadim Fedorenko val & TOD_STATUS_UTC_MASK, val & TOD_STATUS_UTC_VALID ? 1 : 0); 23759f492c4cSVadim Fedorenko seq_printf(s, "Leap second info valid:%d, Leap second announce %d\n", 23769f492c4cSVadim Fedorenko val & TOD_STATUS_LEAP_VALID ? 1 : 0, 23779f492c4cSVadim Fedorenko val & TOD_STATUS_LEAP_ANNOUNCE ? 1 : 0); 23789f492c4cSVadim Fedorenko 23799f492c4cSVadim Fedorenko val = ioread32(&bp->tod->leap); 23809f492c4cSVadim Fedorenko seq_printf(s, "Time to next leap second (in sec): %d\n", (s32) val); 23819f492c4cSVadim Fedorenko 23829f492c4cSVadim Fedorenko return 0; 23839f492c4cSVadim Fedorenko } 23849f492c4cSVadim Fedorenko DEFINE_SHOW_ATTRIBUTE(ptp_ocp_tod_status); 23859f492c4cSVadim Fedorenko 2386f67bf662SJonathan Lemon static struct dentry *ptp_ocp_debugfs_root; 2387f67bf662SJonathan Lemon 2388f67bf662SJonathan Lemon static void 2389f67bf662SJonathan Lemon ptp_ocp_debugfs_add_device(struct ptp_ocp *bp) 2390f67bf662SJonathan Lemon { 2391f67bf662SJonathan Lemon struct dentry *d; 2392f67bf662SJonathan Lemon 2393f67bf662SJonathan Lemon d = debugfs_create_dir(dev_name(&bp->dev), ptp_ocp_debugfs_root); 2394f67bf662SJonathan Lemon bp->debug_root = d; 2395f67bf662SJonathan Lemon debugfs_create_file("summary", 0444, bp->debug_root, 2396f67bf662SJonathan Lemon &bp->dev, &ptp_ocp_summary_fops); 23979f492c4cSVadim Fedorenko if (bp->tod) 23989f492c4cSVadim Fedorenko debugfs_create_file("tod_status", 0444, bp->debug_root, 23999f492c4cSVadim Fedorenko &bp->dev, &ptp_ocp_tod_status_fops); 2400f67bf662SJonathan Lemon } 2401f67bf662SJonathan Lemon 2402f67bf662SJonathan Lemon static void 2403f67bf662SJonathan Lemon ptp_ocp_debugfs_remove_device(struct ptp_ocp *bp) 2404f67bf662SJonathan Lemon { 2405f67bf662SJonathan Lemon debugfs_remove_recursive(bp->debug_root); 2406f67bf662SJonathan Lemon } 2407f67bf662SJonathan Lemon 2408f67bf662SJonathan Lemon static void 2409f67bf662SJonathan Lemon ptp_ocp_debugfs_init(void) 2410f67bf662SJonathan Lemon { 2411f67bf662SJonathan Lemon ptp_ocp_debugfs_root = debugfs_create_dir("timecard", NULL); 2412f67bf662SJonathan Lemon } 2413f67bf662SJonathan Lemon 2414f67bf662SJonathan Lemon static void 2415f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(void) 2416f67bf662SJonathan Lemon { 2417f67bf662SJonathan Lemon debugfs_remove_recursive(ptp_ocp_debugfs_root); 2418f67bf662SJonathan Lemon } 2419f67bf662SJonathan Lemon 2420773bda96SJonathan Lemon static void 2421773bda96SJonathan Lemon ptp_ocp_dev_release(struct device *dev) 2422773bda96SJonathan Lemon { 2423773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 2424773bda96SJonathan Lemon 2425773bda96SJonathan Lemon mutex_lock(&ptp_ocp_lock); 2426773bda96SJonathan Lemon idr_remove(&ptp_ocp_idr, bp->id); 2427773bda96SJonathan Lemon mutex_unlock(&ptp_ocp_lock); 2428773bda96SJonathan Lemon } 2429773bda96SJonathan Lemon 2430773bda96SJonathan Lemon static int 2431773bda96SJonathan Lemon ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev) 2432773bda96SJonathan Lemon { 2433773bda96SJonathan Lemon int err; 2434773bda96SJonathan Lemon 2435773bda96SJonathan Lemon mutex_lock(&ptp_ocp_lock); 2436773bda96SJonathan Lemon err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL); 2437773bda96SJonathan Lemon mutex_unlock(&ptp_ocp_lock); 2438773bda96SJonathan Lemon if (err < 0) { 2439773bda96SJonathan Lemon dev_err(&pdev->dev, "idr_alloc failed: %d\n", err); 2440773bda96SJonathan Lemon return err; 2441773bda96SJonathan Lemon } 2442773bda96SJonathan Lemon bp->id = err; 2443773bda96SJonathan Lemon 2444773bda96SJonathan Lemon bp->ptp_info = ptp_ocp_clock_info; 2445773bda96SJonathan Lemon spin_lock_init(&bp->lock); 2446ef0cfb34SJonathan Lemon bp->gnss_port = -1; 244771d7e085SJonathan Lemon bp->gnss2_port = -1; 2448773bda96SJonathan Lemon bp->mac_port = -1; 2449e3516bb4SJonathan Lemon bp->nmea_port = -1; 2450773bda96SJonathan Lemon bp->pdev = pdev; 2451773bda96SJonathan Lemon 2452773bda96SJonathan Lemon device_initialize(&bp->dev); 2453773bda96SJonathan Lemon dev_set_name(&bp->dev, "ocp%d", bp->id); 2454773bda96SJonathan Lemon bp->dev.class = &timecard_class; 2455773bda96SJonathan Lemon bp->dev.parent = &pdev->dev; 2456773bda96SJonathan Lemon bp->dev.release = ptp_ocp_dev_release; 2457773bda96SJonathan Lemon dev_set_drvdata(&bp->dev, bp); 2458773bda96SJonathan Lemon 2459773bda96SJonathan Lemon err = device_add(&bp->dev); 2460773bda96SJonathan Lemon if (err) { 2461773bda96SJonathan Lemon dev_err(&bp->dev, "device add failed: %d\n", err); 2462773bda96SJonathan Lemon goto out; 2463773bda96SJonathan Lemon } 2464773bda96SJonathan Lemon 2465773bda96SJonathan Lemon pci_set_drvdata(pdev, bp); 2466773bda96SJonathan Lemon 2467773bda96SJonathan Lemon return 0; 2468773bda96SJonathan Lemon 2469773bda96SJonathan Lemon out: 2470773bda96SJonathan Lemon ptp_ocp_dev_release(&bp->dev); 2471d12f23faSJonathan Lemon put_device(&bp->dev); 2472773bda96SJonathan Lemon return err; 2473773bda96SJonathan Lemon } 2474773bda96SJonathan Lemon 2475773bda96SJonathan Lemon static void 2476773bda96SJonathan Lemon ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link) 2477773bda96SJonathan Lemon { 2478773bda96SJonathan Lemon struct device *dev = &bp->dev; 2479773bda96SJonathan Lemon 2480773bda96SJonathan Lemon if (sysfs_create_link(&dev->kobj, &child->kobj, link)) 2481773bda96SJonathan Lemon dev_err(dev, "%s symlink failed\n", link); 2482773bda96SJonathan Lemon } 2483773bda96SJonathan Lemon 2484773bda96SJonathan Lemon static void 2485773bda96SJonathan Lemon ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link) 2486773bda96SJonathan Lemon { 2487773bda96SJonathan Lemon struct device *dev, *child; 2488773bda96SJonathan Lemon 2489773bda96SJonathan Lemon dev = &bp->pdev->dev; 2490773bda96SJonathan Lemon 2491773bda96SJonathan Lemon child = device_find_child_by_name(dev, name); 2492773bda96SJonathan Lemon if (!child) { 2493773bda96SJonathan Lemon dev_err(dev, "Could not find device %s\n", name); 2494773bda96SJonathan Lemon return; 2495773bda96SJonathan Lemon } 2496773bda96SJonathan Lemon 2497773bda96SJonathan Lemon ptp_ocp_symlink(bp, child, link); 2498773bda96SJonathan Lemon put_device(child); 2499773bda96SJonathan Lemon } 2500773bda96SJonathan Lemon 2501773bda96SJonathan Lemon static int 2502773bda96SJonathan Lemon ptp_ocp_complete(struct ptp_ocp *bp) 2503773bda96SJonathan Lemon { 2504773bda96SJonathan Lemon struct pps_device *pps; 2505773bda96SJonathan Lemon char buf[32]; 2506773bda96SJonathan Lemon 2507ef0cfb34SJonathan Lemon if (bp->gnss_port != -1) { 2508ef0cfb34SJonathan Lemon sprintf(buf, "ttyS%d", bp->gnss_port); 2509ef0cfb34SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyGNSS"); 2510773bda96SJonathan Lemon } 251171d7e085SJonathan Lemon if (bp->gnss2_port != -1) { 251271d7e085SJonathan Lemon sprintf(buf, "ttyS%d", bp->gnss2_port); 251371d7e085SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyGNSS2"); 251471d7e085SJonathan Lemon } 2515773bda96SJonathan Lemon if (bp->mac_port != -1) { 2516773bda96SJonathan Lemon sprintf(buf, "ttyS%d", bp->mac_port); 2517773bda96SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyMAC"); 2518773bda96SJonathan Lemon } 2519e3516bb4SJonathan Lemon if (bp->nmea_port != -1) { 2520e3516bb4SJonathan Lemon sprintf(buf, "ttyS%d", bp->nmea_port); 2521e3516bb4SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyNMEA"); 2522e3516bb4SJonathan Lemon } 2523773bda96SJonathan Lemon sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp)); 2524773bda96SJonathan Lemon ptp_ocp_link_child(bp, buf, "ptp"); 2525773bda96SJonathan Lemon 2526773bda96SJonathan Lemon pps = pps_lookup_dev(bp->ptp); 2527773bda96SJonathan Lemon if (pps) 2528773bda96SJonathan Lemon ptp_ocp_symlink(bp, pps->dev, "pps"); 2529773bda96SJonathan Lemon 2530773bda96SJonathan Lemon if (device_add_groups(&bp->dev, timecard_groups)) 2531773bda96SJonathan Lemon pr_err("device add groups failed\n"); 2532773bda96SJonathan Lemon 2533f67bf662SJonathan Lemon ptp_ocp_debugfs_add_device(bp); 2534f67bf662SJonathan Lemon 2535773bda96SJonathan Lemon return 0; 2536773bda96SJonathan Lemon } 2537773bda96SJonathan Lemon 2538773bda96SJonathan Lemon static void 2539065efcc5SJonathan Lemon ptp_ocp_phc_info(struct ptp_ocp *bp) 2540065efcc5SJonathan Lemon { 2541065efcc5SJonathan Lemon struct timespec64 ts; 2542065efcc5SJonathan Lemon u32 version, select; 2543065efcc5SJonathan Lemon bool sync; 2544065efcc5SJonathan Lemon 2545065efcc5SJonathan Lemon version = ioread32(&bp->reg->version); 2546065efcc5SJonathan Lemon select = ioread32(&bp->reg->select); 2547065efcc5SJonathan Lemon dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n", 2548065efcc5SJonathan Lemon version >> 24, (version >> 16) & 0xff, version & 0xffff, 2549065efcc5SJonathan Lemon ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16), 2550065efcc5SJonathan Lemon ptp_clock_index(bp->ptp)); 2551065efcc5SJonathan Lemon 2552065efcc5SJonathan Lemon sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC; 2553065efcc5SJonathan Lemon if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL)) 2554065efcc5SJonathan Lemon dev_info(&bp->pdev->dev, "Time: %lld.%ld, %s\n", 2555065efcc5SJonathan Lemon ts.tv_sec, ts.tv_nsec, 2556065efcc5SJonathan Lemon sync ? "in-sync" : "UNSYNCED"); 2557065efcc5SJonathan Lemon } 2558065efcc5SJonathan Lemon 2559065efcc5SJonathan Lemon static void 2560065efcc5SJonathan Lemon ptp_ocp_serial_info(struct device *dev, const char *name, int port, int baud) 2561065efcc5SJonathan Lemon { 2562065efcc5SJonathan Lemon if (port != -1) 2563065efcc5SJonathan Lemon dev_info(dev, "%5s: /dev/ttyS%-2d @ %6d\n", name, port, baud); 2564065efcc5SJonathan Lemon } 2565065efcc5SJonathan Lemon 2566065efcc5SJonathan Lemon static void 2567065efcc5SJonathan Lemon ptp_ocp_info(struct ptp_ocp *bp) 2568773bda96SJonathan Lemon { 2569e3516bb4SJonathan Lemon static int nmea_baud[] = { 2570e3516bb4SJonathan Lemon 1200, 2400, 4800, 9600, 19200, 38400, 2571e3516bb4SJonathan Lemon 57600, 115200, 230400, 460800, 921600, 2572e3516bb4SJonathan Lemon 1000000, 2000000 2573e3516bb4SJonathan Lemon }; 2574773bda96SJonathan Lemon struct device *dev = &bp->pdev->dev; 2575e3516bb4SJonathan Lemon u32 reg; 2576773bda96SJonathan Lemon 2577065efcc5SJonathan Lemon ptp_ocp_phc_info(bp); 2578065efcc5SJonathan Lemon 2579773bda96SJonathan Lemon if (bp->image) { 2580773bda96SJonathan Lemon u32 ver = ioread32(&bp->image->version); 2581773bda96SJonathan Lemon 2582773bda96SJonathan Lemon dev_info(dev, "version %x\n", ver); 2583773bda96SJonathan Lemon if (ver & 0xffff) 2584773bda96SJonathan Lemon dev_info(dev, "regular image, version %d\n", 2585773bda96SJonathan Lemon ver & 0xffff); 2586773bda96SJonathan Lemon else 2587773bda96SJonathan Lemon dev_info(dev, "golden image, version %d\n", 2588773bda96SJonathan Lemon ver >> 16); 2589773bda96SJonathan Lemon } 2590065efcc5SJonathan Lemon ptp_ocp_serial_info(dev, "GNSS", bp->gnss_port, 115200); 259171d7e085SJonathan Lemon ptp_ocp_serial_info(dev, "GNSS2", bp->gnss2_port, 115200); 2592065efcc5SJonathan Lemon ptp_ocp_serial_info(dev, "MAC", bp->mac_port, 57600); 2593e3516bb4SJonathan Lemon if (bp->nmea_out && bp->nmea_port != -1) { 2594e3516bb4SJonathan Lemon int baud = -1; 2595e3516bb4SJonathan Lemon 2596e3516bb4SJonathan Lemon reg = ioread32(&bp->nmea_out->uart_baud); 2597e3516bb4SJonathan Lemon if (reg < ARRAY_SIZE(nmea_baud)) 2598e3516bb4SJonathan Lemon baud = nmea_baud[reg]; 2599e3516bb4SJonathan Lemon ptp_ocp_serial_info(dev, "NMEA", bp->nmea_port, baud); 2600e3516bb4SJonathan Lemon } 2601773bda96SJonathan Lemon } 2602773bda96SJonathan Lemon 2603773bda96SJonathan Lemon static void 2604773bda96SJonathan Lemon ptp_ocp_detach_sysfs(struct ptp_ocp *bp) 2605773bda96SJonathan Lemon { 2606773bda96SJonathan Lemon struct device *dev = &bp->dev; 2607773bda96SJonathan Lemon 2608ef0cfb34SJonathan Lemon sysfs_remove_link(&dev->kobj, "ttyGNSS"); 2609773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "ttyMAC"); 2610773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "ptp"); 2611773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "pps"); 2612773bda96SJonathan Lemon device_remove_groups(dev, timecard_groups); 2613773bda96SJonathan Lemon } 2614773bda96SJonathan Lemon 2615773bda96SJonathan Lemon static void 2616773bda96SJonathan Lemon ptp_ocp_detach(struct ptp_ocp *bp) 2617773bda96SJonathan Lemon { 2618f67bf662SJonathan Lemon ptp_ocp_debugfs_remove_device(bp); 2619773bda96SJonathan Lemon ptp_ocp_detach_sysfs(bp); 2620773bda96SJonathan Lemon if (timer_pending(&bp->watchdog)) 2621773bda96SJonathan Lemon del_timer_sync(&bp->watchdog); 2622773bda96SJonathan Lemon if (bp->ts0) 2623773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->ts0); 2624773bda96SJonathan Lemon if (bp->ts1) 2625773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->ts1); 2626dcf61469SJonathan Lemon if (bp->ts2) 2627dcf61469SJonathan Lemon ptp_ocp_unregister_ext(bp->ts2); 2628773bda96SJonathan Lemon if (bp->pps) 2629773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->pps); 2630ef0cfb34SJonathan Lemon if (bp->gnss_port != -1) 2631ef0cfb34SJonathan Lemon serial8250_unregister_port(bp->gnss_port); 263271d7e085SJonathan Lemon if (bp->gnss2_port != -1) 263371d7e085SJonathan Lemon serial8250_unregister_port(bp->gnss2_port); 2634773bda96SJonathan Lemon if (bp->mac_port != -1) 2635773bda96SJonathan Lemon serial8250_unregister_port(bp->mac_port); 2636e3516bb4SJonathan Lemon if (bp->nmea_port != -1) 2637e3516bb4SJonathan Lemon serial8250_unregister_port(bp->nmea_port); 2638773bda96SJonathan Lemon if (bp->spi_flash) 2639773bda96SJonathan Lemon platform_device_unregister(bp->spi_flash); 2640773bda96SJonathan Lemon if (bp->i2c_ctrl) 2641773bda96SJonathan Lemon platform_device_unregister(bp->i2c_ctrl); 2642773bda96SJonathan Lemon if (bp->i2c_clk) 2643773bda96SJonathan Lemon clk_hw_unregister_fixed_rate(bp->i2c_clk); 2644773bda96SJonathan Lemon if (bp->n_irqs) 2645773bda96SJonathan Lemon pci_free_irq_vectors(bp->pdev); 2646773bda96SJonathan Lemon if (bp->ptp) 2647773bda96SJonathan Lemon ptp_clock_unregister(bp->ptp); 2648773bda96SJonathan Lemon device_unregister(&bp->dev); 2649773bda96SJonathan Lemon } 2650773bda96SJonathan Lemon 2651a7e1abadSJonathan Lemon static int 2652a7e1abadSJonathan Lemon ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id) 2653a7e1abadSJonathan Lemon { 2654773bda96SJonathan Lemon struct devlink *devlink; 2655a7e1abadSJonathan Lemon struct ptp_ocp *bp; 2656a7e1abadSJonathan Lemon int err; 2657a7e1abadSJonathan Lemon 2658919d13a7SLeon Romanovsky devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp), &pdev->dev); 2659773bda96SJonathan Lemon if (!devlink) { 2660773bda96SJonathan Lemon dev_err(&pdev->dev, "devlink_alloc failed\n"); 2661a7e1abadSJonathan Lemon return -ENOMEM; 2662773bda96SJonathan Lemon } 2663773bda96SJonathan Lemon 2664a7e1abadSJonathan Lemon err = pci_enable_device(pdev); 2665a7e1abadSJonathan Lemon if (err) { 2666a7e1abadSJonathan Lemon dev_err(&pdev->dev, "pci_enable_device\n"); 26674587369bSJonathan Lemon goto out_free; 2668a7e1abadSJonathan Lemon } 2669a7e1abadSJonathan Lemon 2670773bda96SJonathan Lemon bp = devlink_priv(devlink); 2671773bda96SJonathan Lemon err = ptp_ocp_device_init(bp, pdev); 2672773bda96SJonathan Lemon if (err) 2673d9fdbf13SJonathan Lemon goto out_disable; 2674a7e1abadSJonathan Lemon 2675773bda96SJonathan Lemon /* compat mode. 2676773bda96SJonathan Lemon * Older FPGA firmware only returns 2 irq's. 2677773bda96SJonathan Lemon * allow this - if not all of the IRQ's are returned, skip the 2678773bda96SJonathan Lemon * extra devices and just register the clock. 2679773bda96SJonathan Lemon */ 2680e3516bb4SJonathan Lemon err = pci_alloc_irq_vectors(pdev, 1, 11, PCI_IRQ_MSI | PCI_IRQ_MSIX); 2681773bda96SJonathan Lemon if (err < 0) { 2682773bda96SJonathan Lemon dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err); 2683773bda96SJonathan Lemon goto out; 2684a7e1abadSJonathan Lemon } 2685773bda96SJonathan Lemon bp->n_irqs = err; 2686773bda96SJonathan Lemon pci_set_master(pdev); 2687a7e1abadSJonathan Lemon 2688773bda96SJonathan Lemon err = ptp_ocp_register_resources(bp, id->driver_data); 2689a7e1abadSJonathan Lemon if (err) 2690a7e1abadSJonathan Lemon goto out; 2691a7e1abadSJonathan Lemon 2692a7e1abadSJonathan Lemon bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev); 2693a7e1abadSJonathan Lemon if (IS_ERR(bp->ptp)) { 2694a7e1abadSJonathan Lemon err = PTR_ERR(bp->ptp); 2695773bda96SJonathan Lemon dev_err(&pdev->dev, "ptp_clock_register: %d\n", err); 2696773bda96SJonathan Lemon bp->ptp = NULL; 2697a7e1abadSJonathan Lemon goto out; 2698a7e1abadSJonathan Lemon } 2699a7e1abadSJonathan Lemon 2700773bda96SJonathan Lemon err = ptp_ocp_complete(bp); 2701773bda96SJonathan Lemon if (err) 2702773bda96SJonathan Lemon goto out; 2703773bda96SJonathan Lemon 2704a7e1abadSJonathan Lemon ptp_ocp_info(bp); 2705c89f78e9SLeon Romanovsky devlink_register(devlink); 2706a7e1abadSJonathan Lemon return 0; 2707a7e1abadSJonathan Lemon 2708a7e1abadSJonathan Lemon out: 2709773bda96SJonathan Lemon ptp_ocp_detach(bp); 2710773bda96SJonathan Lemon pci_set_drvdata(pdev, NULL); 2711d9fdbf13SJonathan Lemon out_disable: 2712d9fdbf13SJonathan Lemon pci_disable_device(pdev); 27134587369bSJonathan Lemon out_free: 2714773bda96SJonathan Lemon devlink_free(devlink); 2715a7e1abadSJonathan Lemon return err; 2716a7e1abadSJonathan Lemon } 2717a7e1abadSJonathan Lemon 2718a7e1abadSJonathan Lemon static void 2719a7e1abadSJonathan Lemon ptp_ocp_remove(struct pci_dev *pdev) 2720a7e1abadSJonathan Lemon { 2721a7e1abadSJonathan Lemon struct ptp_ocp *bp = pci_get_drvdata(pdev); 2722773bda96SJonathan Lemon struct devlink *devlink = priv_to_devlink(bp); 2723a7e1abadSJonathan Lemon 2724c89f78e9SLeon Romanovsky devlink_unregister(devlink); 2725773bda96SJonathan Lemon ptp_ocp_detach(bp); 2726a7e1abadSJonathan Lemon pci_set_drvdata(pdev, NULL); 2727d9fdbf13SJonathan Lemon pci_disable_device(pdev); 2728773bda96SJonathan Lemon 2729773bda96SJonathan Lemon devlink_free(devlink); 2730a7e1abadSJonathan Lemon } 2731a7e1abadSJonathan Lemon 2732a7e1abadSJonathan Lemon static struct pci_driver ptp_ocp_driver = { 2733a7e1abadSJonathan Lemon .name = KBUILD_MODNAME, 2734a7e1abadSJonathan Lemon .id_table = ptp_ocp_pcidev_id, 2735a7e1abadSJonathan Lemon .probe = ptp_ocp_probe, 2736a7e1abadSJonathan Lemon .remove = ptp_ocp_remove, 2737a7e1abadSJonathan Lemon }; 2738a7e1abadSJonathan Lemon 2739773bda96SJonathan Lemon static int 2740773bda96SJonathan Lemon ptp_ocp_i2c_notifier_call(struct notifier_block *nb, 2741773bda96SJonathan Lemon unsigned long action, void *data) 2742773bda96SJonathan Lemon { 2743773bda96SJonathan Lemon struct device *dev, *child = data; 2744773bda96SJonathan Lemon struct ptp_ocp *bp; 2745773bda96SJonathan Lemon bool add; 2746773bda96SJonathan Lemon 2747773bda96SJonathan Lemon switch (action) { 2748773bda96SJonathan Lemon case BUS_NOTIFY_ADD_DEVICE: 2749773bda96SJonathan Lemon case BUS_NOTIFY_DEL_DEVICE: 2750773bda96SJonathan Lemon add = action == BUS_NOTIFY_ADD_DEVICE; 2751773bda96SJonathan Lemon break; 2752773bda96SJonathan Lemon default: 2753773bda96SJonathan Lemon return 0; 2754773bda96SJonathan Lemon } 2755773bda96SJonathan Lemon 2756773bda96SJonathan Lemon if (!i2c_verify_adapter(child)) 2757773bda96SJonathan Lemon return 0; 2758773bda96SJonathan Lemon 2759773bda96SJonathan Lemon dev = child; 2760773bda96SJonathan Lemon while ((dev = dev->parent)) 2761773bda96SJonathan Lemon if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME)) 2762773bda96SJonathan Lemon goto found; 2763773bda96SJonathan Lemon return 0; 2764773bda96SJonathan Lemon 2765773bda96SJonathan Lemon found: 2766773bda96SJonathan Lemon bp = dev_get_drvdata(dev); 2767773bda96SJonathan Lemon if (add) 2768773bda96SJonathan Lemon ptp_ocp_symlink(bp, child, "i2c"); 2769773bda96SJonathan Lemon else 2770773bda96SJonathan Lemon sysfs_remove_link(&bp->dev.kobj, "i2c"); 2771773bda96SJonathan Lemon 2772773bda96SJonathan Lemon return 0; 2773773bda96SJonathan Lemon } 2774773bda96SJonathan Lemon 2775773bda96SJonathan Lemon static struct notifier_block ptp_ocp_i2c_notifier = { 2776773bda96SJonathan Lemon .notifier_call = ptp_ocp_i2c_notifier_call, 2777773bda96SJonathan Lemon }; 2778773bda96SJonathan Lemon 2779a7e1abadSJonathan Lemon static int __init 2780a7e1abadSJonathan Lemon ptp_ocp_init(void) 2781a7e1abadSJonathan Lemon { 2782773bda96SJonathan Lemon const char *what; 2783a7e1abadSJonathan Lemon int err; 2784a7e1abadSJonathan Lemon 2785f67bf662SJonathan Lemon ptp_ocp_debugfs_init(); 2786f67bf662SJonathan Lemon 2787773bda96SJonathan Lemon what = "timecard class"; 2788773bda96SJonathan Lemon err = class_register(&timecard_class); 2789773bda96SJonathan Lemon if (err) 2790773bda96SJonathan Lemon goto out; 2791773bda96SJonathan Lemon 2792773bda96SJonathan Lemon what = "i2c notifier"; 2793773bda96SJonathan Lemon err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2794773bda96SJonathan Lemon if (err) 2795773bda96SJonathan Lemon goto out_notifier; 2796773bda96SJonathan Lemon 2797773bda96SJonathan Lemon what = "ptp_ocp driver"; 2798a7e1abadSJonathan Lemon err = pci_register_driver(&ptp_ocp_driver); 2799773bda96SJonathan Lemon if (err) 2800773bda96SJonathan Lemon goto out_register; 2801773bda96SJonathan Lemon 2802773bda96SJonathan Lemon return 0; 2803773bda96SJonathan Lemon 2804773bda96SJonathan Lemon out_register: 2805773bda96SJonathan Lemon bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2806773bda96SJonathan Lemon out_notifier: 2807773bda96SJonathan Lemon class_unregister(&timecard_class); 2808773bda96SJonathan Lemon out: 2809f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(); 2810773bda96SJonathan Lemon pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err); 2811a7e1abadSJonathan Lemon return err; 2812a7e1abadSJonathan Lemon } 2813a7e1abadSJonathan Lemon 2814a7e1abadSJonathan Lemon static void __exit 2815a7e1abadSJonathan Lemon ptp_ocp_fini(void) 2816a7e1abadSJonathan Lemon { 2817773bda96SJonathan Lemon bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2818a7e1abadSJonathan Lemon pci_unregister_driver(&ptp_ocp_driver); 2819773bda96SJonathan Lemon class_unregister(&timecard_class); 2820f67bf662SJonathan Lemon ptp_ocp_debugfs_fini(); 2821a7e1abadSJonathan Lemon } 2822a7e1abadSJonathan Lemon 2823a7e1abadSJonathan Lemon module_init(ptp_ocp_init); 2824a7e1abadSJonathan Lemon module_exit(ptp_ocp_fini); 2825a7e1abadSJonathan Lemon 2826a7e1abadSJonathan Lemon MODULE_DESCRIPTION("OpenCompute TimeCard driver"); 2827a7e1abadSJonathan Lemon MODULE_LICENSE("GPL v2"); 2828