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> 7a7e1abadSJonathan Lemon #include <linux/init.h> 8a7e1abadSJonathan Lemon #include <linux/pci.h> 9773bda96SJonathan Lemon #include <linux/serial_8250.h> 10773bda96SJonathan Lemon #include <linux/clkdev.h> 11773bda96SJonathan Lemon #include <linux/clk-provider.h> 12773bda96SJonathan Lemon #include <linux/platform_device.h> 13a7e1abadSJonathan Lemon #include <linux/ptp_clock_kernel.h> 14773bda96SJonathan Lemon #include <linux/spi/spi.h> 15773bda96SJonathan Lemon #include <linux/spi/xilinx_spi.h> 16773bda96SJonathan Lemon #include <net/devlink.h> 17773bda96SJonathan Lemon #include <linux/i2c.h> 18773bda96SJonathan Lemon #include <linux/mtd/mtd.h> 19a7e1abadSJonathan Lemon 20773bda96SJonathan Lemon #ifndef PCI_VENDOR_ID_FACEBOOK 21773bda96SJonathan Lemon #define PCI_VENDOR_ID_FACEBOOK 0x1d9b 22773bda96SJonathan Lemon #endif 23773bda96SJonathan Lemon 24773bda96SJonathan Lemon #ifndef PCI_DEVICE_ID_FACEBOOK_TIMECARD 25773bda96SJonathan Lemon #define PCI_DEVICE_ID_FACEBOOK_TIMECARD 0x0400 26773bda96SJonathan Lemon #endif 27773bda96SJonathan Lemon 28773bda96SJonathan Lemon static struct class timecard_class = { 29773bda96SJonathan Lemon .owner = THIS_MODULE, 30773bda96SJonathan Lemon .name = "timecard", 31a7e1abadSJonathan Lemon }; 32a7e1abadSJonathan Lemon 33a7e1abadSJonathan Lemon struct ocp_reg { 34a7e1abadSJonathan Lemon u32 ctrl; 35a7e1abadSJonathan Lemon u32 status; 36a7e1abadSJonathan Lemon u32 select; 37a7e1abadSJonathan Lemon u32 version; 38a7e1abadSJonathan Lemon u32 time_ns; 39a7e1abadSJonathan Lemon u32 time_sec; 40a7e1abadSJonathan Lemon u32 __pad0[2]; 41a7e1abadSJonathan Lemon u32 adjust_ns; 42a7e1abadSJonathan Lemon u32 adjust_sec; 43a7e1abadSJonathan Lemon u32 __pad1[2]; 44a7e1abadSJonathan Lemon u32 offset_ns; 45a7e1abadSJonathan Lemon u32 offset_window_ns; 46773bda96SJonathan Lemon u32 __pad2[2]; 47773bda96SJonathan Lemon u32 drift_ns; 48773bda96SJonathan Lemon u32 drift_window_ns; 49773bda96SJonathan Lemon u32 __pad3[6]; 50773bda96SJonathan Lemon u32 servo_offset_p; 51773bda96SJonathan Lemon u32 servo_offset_i; 52773bda96SJonathan Lemon u32 servo_drift_p; 53773bda96SJonathan Lemon u32 servo_drift_i; 54a7e1abadSJonathan Lemon }; 55a7e1abadSJonathan Lemon 56a7e1abadSJonathan Lemon #define OCP_CTRL_ENABLE BIT(0) 57a7e1abadSJonathan Lemon #define OCP_CTRL_ADJUST_TIME BIT(1) 58a7e1abadSJonathan Lemon #define OCP_CTRL_ADJUST_OFFSET BIT(2) 59773bda96SJonathan Lemon #define OCP_CTRL_ADJUST_DRIFT BIT(3) 60773bda96SJonathan Lemon #define OCP_CTRL_ADJUST_SERVO BIT(8) 61a7e1abadSJonathan Lemon #define OCP_CTRL_READ_TIME_REQ BIT(30) 62a7e1abadSJonathan Lemon #define OCP_CTRL_READ_TIME_DONE BIT(31) 63a7e1abadSJonathan Lemon 64a7e1abadSJonathan Lemon #define OCP_STATUS_IN_SYNC BIT(0) 65773bda96SJonathan Lemon #define OCP_STATUS_IN_HOLDOVER BIT(1) 66a7e1abadSJonathan Lemon 67a7e1abadSJonathan Lemon #define OCP_SELECT_CLK_NONE 0 68773bda96SJonathan Lemon #define OCP_SELECT_CLK_REG 0xfe 69a7e1abadSJonathan Lemon 70a7e1abadSJonathan Lemon struct tod_reg { 71a7e1abadSJonathan Lemon u32 ctrl; 72a7e1abadSJonathan Lemon u32 status; 73a7e1abadSJonathan Lemon u32 uart_polarity; 74a7e1abadSJonathan Lemon u32 version; 75a7e1abadSJonathan Lemon u32 correction_sec; 76a7e1abadSJonathan Lemon u32 __pad0[3]; 77a7e1abadSJonathan Lemon u32 uart_baud; 78a7e1abadSJonathan Lemon u32 __pad1[3]; 79a7e1abadSJonathan Lemon u32 utc_status; 80a7e1abadSJonathan Lemon u32 leap; 81a7e1abadSJonathan Lemon }; 82a7e1abadSJonathan Lemon 83a7e1abadSJonathan Lemon #define TOD_CTRL_PROTOCOL BIT(28) 84a7e1abadSJonathan Lemon #define TOD_CTRL_DISABLE_FMT_A BIT(17) 85a7e1abadSJonathan Lemon #define TOD_CTRL_DISABLE_FMT_B BIT(16) 86a7e1abadSJonathan Lemon #define TOD_CTRL_ENABLE BIT(0) 87a7e1abadSJonathan Lemon #define TOD_CTRL_GNSS_MASK ((1U << 4) - 1) 88a7e1abadSJonathan Lemon #define TOD_CTRL_GNSS_SHIFT 24 89a7e1abadSJonathan Lemon 90a7e1abadSJonathan Lemon #define TOD_STATUS_UTC_MASK 0xff 91a7e1abadSJonathan Lemon #define TOD_STATUS_UTC_VALID BIT(8) 92a7e1abadSJonathan Lemon #define TOD_STATUS_LEAP_VALID BIT(16) 93a7e1abadSJonathan Lemon 94773bda96SJonathan Lemon struct ts_reg { 95773bda96SJonathan Lemon u32 enable; 96773bda96SJonathan Lemon u32 error; 97773bda96SJonathan Lemon u32 polarity; 98773bda96SJonathan Lemon u32 version; 99773bda96SJonathan Lemon u32 __pad0[4]; 100773bda96SJonathan Lemon u32 cable_delay; 101773bda96SJonathan Lemon u32 __pad1[3]; 102773bda96SJonathan Lemon u32 intr; 103773bda96SJonathan Lemon u32 intr_mask; 104773bda96SJonathan Lemon u32 event_count; 105773bda96SJonathan Lemon u32 __pad2[1]; 106773bda96SJonathan Lemon u32 ts_count; 107773bda96SJonathan Lemon u32 time_ns; 108773bda96SJonathan Lemon u32 time_sec; 109773bda96SJonathan Lemon u32 data_width; 110773bda96SJonathan Lemon u32 data; 111773bda96SJonathan Lemon }; 112773bda96SJonathan Lemon 113773bda96SJonathan Lemon struct pps_reg { 114773bda96SJonathan Lemon u32 ctrl; 115773bda96SJonathan Lemon u32 status; 1160d43d4f2SJonathan Lemon u32 __pad0[6]; 1170d43d4f2SJonathan Lemon u32 cable_delay; 118773bda96SJonathan Lemon }; 119773bda96SJonathan Lemon 120773bda96SJonathan Lemon #define PPS_STATUS_FILTER_ERR BIT(0) 121773bda96SJonathan Lemon #define PPS_STATUS_SUPERV_ERR BIT(1) 122773bda96SJonathan Lemon 123773bda96SJonathan Lemon struct img_reg { 124773bda96SJonathan Lemon u32 version; 125773bda96SJonathan Lemon }; 126773bda96SJonathan Lemon 127e1daf0ecSJonathan Lemon struct gpio_reg { 128e1daf0ecSJonathan Lemon u32 gpio1; 129e1daf0ecSJonathan Lemon u32 __pad0; 130e1daf0ecSJonathan Lemon u32 gpio2; 131e1daf0ecSJonathan Lemon u32 __pad1; 132e1daf0ecSJonathan Lemon }; 133e1daf0ecSJonathan Lemon 134*6baf2925SJonathan Lemon struct irig_master_reg { 135*6baf2925SJonathan Lemon u32 ctrl; 136*6baf2925SJonathan Lemon u32 status; 137*6baf2925SJonathan Lemon u32 __pad0; 138*6baf2925SJonathan Lemon u32 version; 139*6baf2925SJonathan Lemon u32 adj_sec; 140*6baf2925SJonathan Lemon u32 mode_ctrl; 141*6baf2925SJonathan Lemon }; 142*6baf2925SJonathan Lemon 143*6baf2925SJonathan Lemon #define IRIG_M_CTRL_ENABLE BIT(0) 144*6baf2925SJonathan Lemon 145*6baf2925SJonathan Lemon struct irig_slave_reg { 146*6baf2925SJonathan Lemon u32 ctrl; 147*6baf2925SJonathan Lemon u32 status; 148*6baf2925SJonathan Lemon u32 __pad0; 149*6baf2925SJonathan Lemon u32 version; 150*6baf2925SJonathan Lemon u32 adj_sec; 151*6baf2925SJonathan Lemon u32 mode_ctrl; 152*6baf2925SJonathan Lemon }; 153*6baf2925SJonathan Lemon 154*6baf2925SJonathan Lemon #define IRIG_S_CTRL_ENABLE BIT(0) 155*6baf2925SJonathan Lemon 156*6baf2925SJonathan Lemon struct dcf_master_reg { 157*6baf2925SJonathan Lemon u32 ctrl; 158*6baf2925SJonathan Lemon u32 status; 159*6baf2925SJonathan Lemon u32 __pad0; 160*6baf2925SJonathan Lemon u32 version; 161*6baf2925SJonathan Lemon u32 adj_sec; 162*6baf2925SJonathan Lemon }; 163*6baf2925SJonathan Lemon 164*6baf2925SJonathan Lemon #define DCF_M_CTRL_ENABLE BIT(0) 165*6baf2925SJonathan Lemon 166*6baf2925SJonathan Lemon struct dcf_slave_reg { 167*6baf2925SJonathan Lemon u32 ctrl; 168*6baf2925SJonathan Lemon u32 status; 169*6baf2925SJonathan Lemon u32 __pad0; 170*6baf2925SJonathan Lemon u32 version; 171*6baf2925SJonathan Lemon u32 adj_sec; 172*6baf2925SJonathan Lemon }; 173*6baf2925SJonathan Lemon 174*6baf2925SJonathan Lemon #define DCF_S_CTRL_ENABLE BIT(0) 175*6baf2925SJonathan Lemon 176773bda96SJonathan Lemon struct ptp_ocp_flash_info { 177773bda96SJonathan Lemon const char *name; 178773bda96SJonathan Lemon int pci_offset; 179773bda96SJonathan Lemon int data_size; 180773bda96SJonathan Lemon void *data; 181773bda96SJonathan Lemon }; 182773bda96SJonathan Lemon 1831618df6aSJonathan Lemon struct ptp_ocp_i2c_info { 1841618df6aSJonathan Lemon const char *name; 1851618df6aSJonathan Lemon unsigned long fixed_rate; 1861618df6aSJonathan Lemon size_t data_size; 1871618df6aSJonathan Lemon void *data; 1881618df6aSJonathan Lemon }; 1891618df6aSJonathan Lemon 190773bda96SJonathan Lemon struct ptp_ocp_ext_info { 191773bda96SJonathan Lemon int index; 192773bda96SJonathan Lemon irqreturn_t (*irq_fcn)(int irq, void *priv); 193773bda96SJonathan Lemon int (*enable)(void *priv, bool enable); 194773bda96SJonathan Lemon }; 195773bda96SJonathan Lemon 196773bda96SJonathan Lemon struct ptp_ocp_ext_src { 197773bda96SJonathan Lemon void __iomem *mem; 198773bda96SJonathan Lemon struct ptp_ocp *bp; 199773bda96SJonathan Lemon struct ptp_ocp_ext_info *info; 200773bda96SJonathan Lemon int irq_vec; 201773bda96SJonathan Lemon }; 202773bda96SJonathan Lemon 203a7e1abadSJonathan Lemon struct ptp_ocp { 204a7e1abadSJonathan Lemon struct pci_dev *pdev; 205773bda96SJonathan Lemon struct device dev; 206a7e1abadSJonathan Lemon spinlock_t lock; 207a7e1abadSJonathan Lemon struct ocp_reg __iomem *reg; 208a7e1abadSJonathan Lemon struct tod_reg __iomem *tod; 2090d43d4f2SJonathan Lemon struct pps_reg __iomem *pps_to_ext; 2100d43d4f2SJonathan Lemon struct pps_reg __iomem *pps_to_clk; 211e1daf0ecSJonathan Lemon struct gpio_reg __iomem *sma; 212*6baf2925SJonathan Lemon struct irig_master_reg __iomem *irig_out; 213*6baf2925SJonathan Lemon struct irig_slave_reg __iomem *irig_in; 214*6baf2925SJonathan Lemon struct dcf_master_reg __iomem *dcf_out; 215*6baf2925SJonathan Lemon struct dcf_slave_reg __iomem *dcf_in; 216773bda96SJonathan Lemon struct ptp_ocp_ext_src *pps; 217773bda96SJonathan Lemon struct ptp_ocp_ext_src *ts0; 218773bda96SJonathan Lemon struct ptp_ocp_ext_src *ts1; 219dcf61469SJonathan Lemon struct ptp_ocp_ext_src *ts2; 220773bda96SJonathan Lemon struct img_reg __iomem *image; 221a7e1abadSJonathan Lemon struct ptp_clock *ptp; 222a7e1abadSJonathan Lemon struct ptp_clock_info ptp_info; 223773bda96SJonathan Lemon struct platform_device *i2c_ctrl; 224773bda96SJonathan Lemon struct platform_device *spi_flash; 225773bda96SJonathan Lemon struct clk_hw *i2c_clk; 226773bda96SJonathan Lemon struct timer_list watchdog; 227ef0cfb34SJonathan Lemon time64_t gnss_lost; 228773bda96SJonathan Lemon int id; 229773bda96SJonathan Lemon int n_irqs; 230ef0cfb34SJonathan Lemon int gnss_port; 231773bda96SJonathan Lemon int mac_port; /* miniature atomic clock */ 232773bda96SJonathan Lemon u8 serial[6]; 233773bda96SJonathan Lemon int flash_start; 234773bda96SJonathan Lemon bool has_serial; 235a7e1abadSJonathan Lemon }; 236a7e1abadSJonathan Lemon 237773bda96SJonathan Lemon struct ocp_resource { 238773bda96SJonathan Lemon unsigned long offset; 239773bda96SJonathan Lemon int size; 240773bda96SJonathan Lemon int irq_vec; 241773bda96SJonathan Lemon int (*setup)(struct ptp_ocp *bp, struct ocp_resource *r); 242773bda96SJonathan Lemon void *extra; 243773bda96SJonathan Lemon unsigned long bp_offset; 24456ec4403SJonathan Lemon const char * const name; 245773bda96SJonathan Lemon }; 246773bda96SJonathan Lemon 247773bda96SJonathan Lemon static int ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r); 248773bda96SJonathan Lemon static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r); 249773bda96SJonathan Lemon static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r); 250773bda96SJonathan Lemon static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r); 251773bda96SJonathan Lemon static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r); 252773bda96SJonathan Lemon static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r); 253773bda96SJonathan Lemon static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv); 254773bda96SJonathan Lemon static int ptp_ocp_ts_enable(void *priv, bool enable); 255773bda96SJonathan Lemon 256773bda96SJonathan Lemon #define bp_assign_entry(bp, res, val) ({ \ 257773bda96SJonathan Lemon uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset; \ 258773bda96SJonathan Lemon *(typeof(val) *)addr = val; \ 259773bda96SJonathan Lemon }) 260773bda96SJonathan Lemon 261773bda96SJonathan Lemon #define OCP_RES_LOCATION(member) \ 26256ec4403SJonathan Lemon .name = #member, .bp_offset = offsetof(struct ptp_ocp, member) 263773bda96SJonathan Lemon 264773bda96SJonathan Lemon #define OCP_MEM_RESOURCE(member) \ 265773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_mem 266773bda96SJonathan Lemon 267773bda96SJonathan Lemon #define OCP_SERIAL_RESOURCE(member) \ 268773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_serial 269773bda96SJonathan Lemon 270773bda96SJonathan Lemon #define OCP_I2C_RESOURCE(member) \ 271773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_i2c 272773bda96SJonathan Lemon 273773bda96SJonathan Lemon #define OCP_SPI_RESOURCE(member) \ 274773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_spi 275773bda96SJonathan Lemon 276773bda96SJonathan Lemon #define OCP_EXT_RESOURCE(member) \ 277773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_ext 278773bda96SJonathan Lemon 279773bda96SJonathan Lemon /* This is the MSI vector mapping used. 280773bda96SJonathan Lemon * 0: N/C 281773bda96SJonathan Lemon * 1: TS0 282773bda96SJonathan Lemon * 2: TS1 283773bda96SJonathan Lemon * 3: GPS 284773bda96SJonathan Lemon * 4: GPS2 (n/c) 285773bda96SJonathan Lemon * 5: MAC 286dcf61469SJonathan Lemon * 6: TS2 2871447149dSJonathan Lemon * 7: I2C controller 288773bda96SJonathan Lemon * 8: HWICAP 289773bda96SJonathan Lemon * 9: SPI Flash 290773bda96SJonathan Lemon */ 291773bda96SJonathan Lemon 292773bda96SJonathan Lemon static struct ocp_resource ocp_fb_resource[] = { 293773bda96SJonathan Lemon { 294773bda96SJonathan Lemon OCP_MEM_RESOURCE(reg), 295773bda96SJonathan Lemon .offset = 0x01000000, .size = 0x10000, 296773bda96SJonathan Lemon }, 297773bda96SJonathan Lemon { 298773bda96SJonathan Lemon OCP_EXT_RESOURCE(ts0), 299773bda96SJonathan Lemon .offset = 0x01010000, .size = 0x10000, .irq_vec = 1, 300773bda96SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 30156ec4403SJonathan Lemon .index = 0, 302773bda96SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 303773bda96SJonathan Lemon .enable = ptp_ocp_ts_enable, 304773bda96SJonathan Lemon }, 305773bda96SJonathan Lemon }, 306773bda96SJonathan Lemon { 307773bda96SJonathan Lemon OCP_EXT_RESOURCE(ts1), 308773bda96SJonathan Lemon .offset = 0x01020000, .size = 0x10000, .irq_vec = 2, 309773bda96SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 31056ec4403SJonathan Lemon .index = 1, 311773bda96SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 312773bda96SJonathan Lemon .enable = ptp_ocp_ts_enable, 313773bda96SJonathan Lemon }, 314773bda96SJonathan Lemon }, 315773bda96SJonathan Lemon { 316dcf61469SJonathan Lemon OCP_EXT_RESOURCE(ts2), 317dcf61469SJonathan Lemon .offset = 0x01060000, .size = 0x10000, .irq_vec = 6, 318dcf61469SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 319dcf61469SJonathan Lemon .index = 2, 320dcf61469SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 321dcf61469SJonathan Lemon .enable = ptp_ocp_ts_enable, 322dcf61469SJonathan Lemon }, 323dcf61469SJonathan Lemon }, 324dcf61469SJonathan Lemon { 3250d43d4f2SJonathan Lemon OCP_MEM_RESOURCE(pps_to_ext), 3260d43d4f2SJonathan Lemon .offset = 0x01030000, .size = 0x10000, 3270d43d4f2SJonathan Lemon }, 3280d43d4f2SJonathan Lemon { 3290d43d4f2SJonathan Lemon OCP_MEM_RESOURCE(pps_to_clk), 330773bda96SJonathan Lemon .offset = 0x01040000, .size = 0x10000, 331773bda96SJonathan Lemon }, 332773bda96SJonathan Lemon { 333773bda96SJonathan Lemon OCP_MEM_RESOURCE(tod), 334773bda96SJonathan Lemon .offset = 0x01050000, .size = 0x10000, 335773bda96SJonathan Lemon }, 336773bda96SJonathan Lemon { 337*6baf2925SJonathan Lemon OCP_MEM_RESOURCE(irig_in), 338*6baf2925SJonathan Lemon .offset = 0x01070000, .size = 0x10000, 339*6baf2925SJonathan Lemon }, 340*6baf2925SJonathan Lemon { 341*6baf2925SJonathan Lemon OCP_MEM_RESOURCE(irig_out), 342*6baf2925SJonathan Lemon .offset = 0x01080000, .size = 0x10000, 343*6baf2925SJonathan Lemon }, 344*6baf2925SJonathan Lemon { 345*6baf2925SJonathan Lemon OCP_MEM_RESOURCE(dcf_in), 346*6baf2925SJonathan Lemon .offset = 0x01090000, .size = 0x10000, 347*6baf2925SJonathan Lemon }, 348*6baf2925SJonathan Lemon { 349*6baf2925SJonathan Lemon OCP_MEM_RESOURCE(dcf_out), 350*6baf2925SJonathan Lemon .offset = 0x010A0000, .size = 0x10000, 351*6baf2925SJonathan Lemon }, 352*6baf2925SJonathan Lemon { 353773bda96SJonathan Lemon OCP_MEM_RESOURCE(image), 354773bda96SJonathan Lemon .offset = 0x00020000, .size = 0x1000, 355773bda96SJonathan Lemon }, 356773bda96SJonathan Lemon { 357e1daf0ecSJonathan Lemon OCP_MEM_RESOURCE(sma), 358e1daf0ecSJonathan Lemon .offset = 0x00140000, .size = 0x1000, 359e1daf0ecSJonathan Lemon }, 360e1daf0ecSJonathan Lemon { 361773bda96SJonathan Lemon OCP_I2C_RESOURCE(i2c_ctrl), 362773bda96SJonathan Lemon .offset = 0x00150000, .size = 0x10000, .irq_vec = 7, 3631618df6aSJonathan Lemon .extra = &(struct ptp_ocp_i2c_info) { 3641618df6aSJonathan Lemon .name = "xiic-i2c", 3651618df6aSJonathan Lemon .fixed_rate = 50000000, 3661618df6aSJonathan Lemon }, 367773bda96SJonathan Lemon }, 368773bda96SJonathan Lemon { 369ef0cfb34SJonathan Lemon OCP_SERIAL_RESOURCE(gnss_port), 370773bda96SJonathan Lemon .offset = 0x00160000 + 0x1000, .irq_vec = 3, 371773bda96SJonathan Lemon }, 372773bda96SJonathan Lemon { 373773bda96SJonathan Lemon OCP_SERIAL_RESOURCE(mac_port), 374773bda96SJonathan Lemon .offset = 0x00180000 + 0x1000, .irq_vec = 5, 375773bda96SJonathan Lemon }, 376773bda96SJonathan Lemon { 377773bda96SJonathan Lemon OCP_SPI_RESOURCE(spi_flash), 378773bda96SJonathan Lemon .offset = 0x00310000, .size = 0x10000, .irq_vec = 9, 379773bda96SJonathan Lemon .extra = &(struct ptp_ocp_flash_info) { 380773bda96SJonathan Lemon .name = "xilinx_spi", .pci_offset = 0, 381773bda96SJonathan Lemon .data_size = sizeof(struct xspi_platform_data), 382773bda96SJonathan Lemon .data = &(struct xspi_platform_data) { 383773bda96SJonathan Lemon .num_chipselect = 1, 384773bda96SJonathan Lemon .bits_per_word = 8, 385773bda96SJonathan Lemon .num_devices = 1, 386773bda96SJonathan Lemon .devices = &(struct spi_board_info) { 387773bda96SJonathan Lemon .modalias = "spi-nor", 388773bda96SJonathan Lemon }, 389773bda96SJonathan Lemon }, 390773bda96SJonathan Lemon }, 391773bda96SJonathan Lemon }, 392773bda96SJonathan Lemon { 393773bda96SJonathan Lemon .setup = ptp_ocp_fb_board_init, 394773bda96SJonathan Lemon }, 395773bda96SJonathan Lemon { } 396773bda96SJonathan Lemon }; 397773bda96SJonathan Lemon 398773bda96SJonathan Lemon static const struct pci_device_id ptp_ocp_pcidev_id[] = { 399773bda96SJonathan Lemon { PCI_DEVICE_DATA(FACEBOOK, TIMECARD, &ocp_fb_resource) }, 400773bda96SJonathan Lemon { 0 } 401773bda96SJonathan Lemon }; 402773bda96SJonathan Lemon MODULE_DEVICE_TABLE(pci, ptp_ocp_pcidev_id); 403773bda96SJonathan Lemon 404773bda96SJonathan Lemon static DEFINE_MUTEX(ptp_ocp_lock); 405773bda96SJonathan Lemon static DEFINE_IDR(ptp_ocp_idr); 406773bda96SJonathan Lemon 407e1daf0ecSJonathan Lemon struct ocp_selector { 408773bda96SJonathan Lemon const char *name; 409773bda96SJonathan Lemon int value; 410e1daf0ecSJonathan Lemon }; 411e1daf0ecSJonathan Lemon 412e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_clock[] = { 413773bda96SJonathan Lemon { .name = "NONE", .value = 0 }, 414773bda96SJonathan Lemon { .name = "TOD", .value = 1 }, 415773bda96SJonathan Lemon { .name = "IRIG", .value = 2 }, 416773bda96SJonathan Lemon { .name = "PPS", .value = 3 }, 417773bda96SJonathan Lemon { .name = "PTP", .value = 4 }, 418773bda96SJonathan Lemon { .name = "RTC", .value = 5 }, 419773bda96SJonathan Lemon { .name = "DCF", .value = 6 }, 420773bda96SJonathan Lemon { .name = "REGS", .value = 0xfe }, 421773bda96SJonathan Lemon { .name = "EXT", .value = 0xff }, 422e1daf0ecSJonathan Lemon { } 423e1daf0ecSJonathan Lemon }; 424e1daf0ecSJonathan Lemon 425e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_sma_in[] = { 426e1daf0ecSJonathan Lemon { .name = "10Mhz", .value = 0x00 }, 427e1daf0ecSJonathan Lemon { .name = "PPS1", .value = 0x01 }, 428e1daf0ecSJonathan Lemon { .name = "PPS2", .value = 0x02 }, 429e1daf0ecSJonathan Lemon { .name = "TS1", .value = 0x04 }, 430e1daf0ecSJonathan Lemon { .name = "TS2", .value = 0x08 }, 431*6baf2925SJonathan Lemon { .name = "IRIG", .value = 0x10 }, 432*6baf2925SJonathan Lemon { .name = "DCF", .value = 0x20 }, 433e1daf0ecSJonathan Lemon { } 434e1daf0ecSJonathan Lemon }; 435e1daf0ecSJonathan Lemon 436e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_sma_out[] = { 437e1daf0ecSJonathan Lemon { .name = "10Mhz", .value = 0x00 }, 438e1daf0ecSJonathan Lemon { .name = "PHC", .value = 0x01 }, 439e1daf0ecSJonathan Lemon { .name = "MAC", .value = 0x02 }, 440e1daf0ecSJonathan Lemon { .name = "GNSS", .value = 0x04 }, 441e1daf0ecSJonathan Lemon { .name = "GNSS2", .value = 0x08 }, 442*6baf2925SJonathan Lemon { .name = "IRIG", .value = 0x10 }, 443*6baf2925SJonathan Lemon { .name = "DCF", .value = 0x20 }, 444e1daf0ecSJonathan Lemon { } 445773bda96SJonathan Lemon }; 446773bda96SJonathan Lemon 447773bda96SJonathan Lemon static const char * 448e1daf0ecSJonathan Lemon ptp_ocp_select_name_from_val(struct ocp_selector *tbl, int val) 449773bda96SJonathan Lemon { 450773bda96SJonathan Lemon int i; 451773bda96SJonathan Lemon 452e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) 453e1daf0ecSJonathan Lemon if (tbl[i].value == val) 454e1daf0ecSJonathan Lemon return tbl[i].name; 455773bda96SJonathan Lemon return NULL; 456773bda96SJonathan Lemon } 457773bda96SJonathan Lemon 458773bda96SJonathan Lemon static int 459e1daf0ecSJonathan Lemon ptp_ocp_select_val_from_name(struct ocp_selector *tbl, const char *name) 460773bda96SJonathan Lemon { 461e1daf0ecSJonathan Lemon const char *select; 462773bda96SJonathan Lemon int i; 463773bda96SJonathan Lemon 464e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) { 465e1daf0ecSJonathan Lemon select = tbl[i].name; 466e1daf0ecSJonathan Lemon if (!strncasecmp(name, select, strlen(select))) 467e1daf0ecSJonathan Lemon return tbl[i].value; 468773bda96SJonathan Lemon } 469773bda96SJonathan Lemon return -EINVAL; 470773bda96SJonathan Lemon } 471773bda96SJonathan Lemon 472e1daf0ecSJonathan Lemon static ssize_t 473e1daf0ecSJonathan Lemon ptp_ocp_select_table_show(struct ocp_selector *tbl, char *buf) 474e1daf0ecSJonathan Lemon { 475e1daf0ecSJonathan Lemon ssize_t count; 476e1daf0ecSJonathan Lemon int i; 477e1daf0ecSJonathan Lemon 478e1daf0ecSJonathan Lemon count = 0; 479e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) 480e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", tbl[i].name); 481e1daf0ecSJonathan Lemon if (count) 482e1daf0ecSJonathan Lemon count--; 483e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "\n"); 484e1daf0ecSJonathan Lemon return count; 485e1daf0ecSJonathan Lemon } 486e1daf0ecSJonathan Lemon 487a7e1abadSJonathan Lemon static int 488a7e1abadSJonathan Lemon __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts, 489a7e1abadSJonathan Lemon struct ptp_system_timestamp *sts) 490a7e1abadSJonathan Lemon { 491a7e1abadSJonathan Lemon u32 ctrl, time_sec, time_ns; 492a7e1abadSJonathan Lemon int i; 493a7e1abadSJonathan Lemon 494a7e1abadSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 495a7e1abadSJonathan Lemon ctrl |= OCP_CTRL_READ_TIME_REQ; 496a7e1abadSJonathan Lemon 497a7e1abadSJonathan Lemon ptp_read_system_prets(sts); 498a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 499a7e1abadSJonathan Lemon 500a7e1abadSJonathan Lemon for (i = 0; i < 100; i++) { 501a7e1abadSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 502a7e1abadSJonathan Lemon if (ctrl & OCP_CTRL_READ_TIME_DONE) 503a7e1abadSJonathan Lemon break; 504a7e1abadSJonathan Lemon } 505a7e1abadSJonathan Lemon ptp_read_system_postts(sts); 506a7e1abadSJonathan Lemon 507a7e1abadSJonathan Lemon time_ns = ioread32(&bp->reg->time_ns); 508a7e1abadSJonathan Lemon time_sec = ioread32(&bp->reg->time_sec); 509a7e1abadSJonathan Lemon 510a7e1abadSJonathan Lemon ts->tv_sec = time_sec; 511a7e1abadSJonathan Lemon ts->tv_nsec = time_ns; 512a7e1abadSJonathan Lemon 513a7e1abadSJonathan Lemon return ctrl & OCP_CTRL_READ_TIME_DONE ? 0 : -ETIMEDOUT; 514a7e1abadSJonathan Lemon } 515a7e1abadSJonathan Lemon 516a7e1abadSJonathan Lemon static int 517a7e1abadSJonathan Lemon ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts, 518a7e1abadSJonathan Lemon struct ptp_system_timestamp *sts) 519a7e1abadSJonathan Lemon { 520a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 521a7e1abadSJonathan Lemon unsigned long flags; 522a7e1abadSJonathan Lemon int err; 523a7e1abadSJonathan Lemon 524a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 525a7e1abadSJonathan Lemon err = __ptp_ocp_gettime_locked(bp, ts, sts); 526a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 527a7e1abadSJonathan Lemon 528a7e1abadSJonathan Lemon return err; 529a7e1abadSJonathan Lemon } 530a7e1abadSJonathan Lemon 531a7e1abadSJonathan Lemon static void 532a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts) 533a7e1abadSJonathan Lemon { 534a7e1abadSJonathan Lemon u32 ctrl, time_sec, time_ns; 535a7e1abadSJonathan Lemon u32 select; 536a7e1abadSJonathan Lemon 537a7e1abadSJonathan Lemon time_ns = ts->tv_nsec; 538a7e1abadSJonathan Lemon time_sec = ts->tv_sec; 539a7e1abadSJonathan Lemon 540a7e1abadSJonathan Lemon select = ioread32(&bp->reg->select); 541a7e1abadSJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 542a7e1abadSJonathan Lemon 543a7e1abadSJonathan Lemon iowrite32(time_ns, &bp->reg->adjust_ns); 544a7e1abadSJonathan Lemon iowrite32(time_sec, &bp->reg->adjust_sec); 545a7e1abadSJonathan Lemon 546a7e1abadSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 547a7e1abadSJonathan Lemon ctrl |= OCP_CTRL_ADJUST_TIME; 548a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 549a7e1abadSJonathan Lemon 550a7e1abadSJonathan Lemon /* restore clock selection */ 551a7e1abadSJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 552a7e1abadSJonathan Lemon } 553a7e1abadSJonathan Lemon 554a7e1abadSJonathan Lemon static int 555a7e1abadSJonathan Lemon ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts) 556a7e1abadSJonathan Lemon { 557a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 558a7e1abadSJonathan Lemon unsigned long flags; 559a7e1abadSJonathan Lemon 560a7e1abadSJonathan Lemon if (ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC) 561a7e1abadSJonathan Lemon return 0; 562a7e1abadSJonathan Lemon 563a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 564a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(bp, ts); 565a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 566a7e1abadSJonathan Lemon 567a7e1abadSJonathan Lemon return 0; 568a7e1abadSJonathan Lemon } 569a7e1abadSJonathan Lemon 570a7e1abadSJonathan Lemon static int 571a7e1abadSJonathan Lemon ptp_ocp_adjtime(struct ptp_clock_info *ptp_info, s64 delta_ns) 572a7e1abadSJonathan Lemon { 573a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 574a7e1abadSJonathan Lemon struct timespec64 ts; 575a7e1abadSJonathan Lemon unsigned long flags; 576a7e1abadSJonathan Lemon int err; 577a7e1abadSJonathan Lemon 578a7e1abadSJonathan Lemon if (ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC) 579a7e1abadSJonathan Lemon return 0; 580a7e1abadSJonathan Lemon 581a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 582a7e1abadSJonathan Lemon err = __ptp_ocp_gettime_locked(bp, &ts, NULL); 583a7e1abadSJonathan Lemon if (likely(!err)) { 584a7e1abadSJonathan Lemon timespec64_add_ns(&ts, delta_ns); 585a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(bp, &ts); 586a7e1abadSJonathan Lemon } 587a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 588a7e1abadSJonathan Lemon 589a7e1abadSJonathan Lemon return err; 590a7e1abadSJonathan Lemon } 591a7e1abadSJonathan Lemon 592a7e1abadSJonathan Lemon static int 593a7e1abadSJonathan Lemon ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm) 594a7e1abadSJonathan Lemon { 595a7e1abadSJonathan Lemon if (scaled_ppm == 0) 596a7e1abadSJonathan Lemon return 0; 597a7e1abadSJonathan Lemon 598a7e1abadSJonathan Lemon return -EOPNOTSUPP; 599a7e1abadSJonathan Lemon } 600a7e1abadSJonathan Lemon 601773bda96SJonathan Lemon static int 602773bda96SJonathan Lemon ptp_ocp_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns) 603773bda96SJonathan Lemon { 604773bda96SJonathan Lemon return -EOPNOTSUPP; 605773bda96SJonathan Lemon } 606773bda96SJonathan Lemon 607773bda96SJonathan Lemon static int 608773bda96SJonathan Lemon ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq, 609773bda96SJonathan Lemon int on) 610773bda96SJonathan Lemon { 611773bda96SJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 612773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = NULL; 613773bda96SJonathan Lemon int err; 614773bda96SJonathan Lemon 615773bda96SJonathan Lemon switch (rq->type) { 616773bda96SJonathan Lemon case PTP_CLK_REQ_EXTTS: 617773bda96SJonathan Lemon switch (rq->extts.index) { 618773bda96SJonathan Lemon case 0: 619773bda96SJonathan Lemon ext = bp->ts0; 620773bda96SJonathan Lemon break; 621773bda96SJonathan Lemon case 1: 622773bda96SJonathan Lemon ext = bp->ts1; 623773bda96SJonathan Lemon break; 624dcf61469SJonathan Lemon case 2: 625dcf61469SJonathan Lemon ext = bp->ts2; 626dcf61469SJonathan Lemon break; 627773bda96SJonathan Lemon } 628773bda96SJonathan Lemon break; 629773bda96SJonathan Lemon case PTP_CLK_REQ_PPS: 630773bda96SJonathan Lemon ext = bp->pps; 631773bda96SJonathan Lemon break; 632773bda96SJonathan Lemon default: 633773bda96SJonathan Lemon return -EOPNOTSUPP; 634773bda96SJonathan Lemon } 635773bda96SJonathan Lemon 636773bda96SJonathan Lemon err = -ENXIO; 637773bda96SJonathan Lemon if (ext) 638773bda96SJonathan Lemon err = ext->info->enable(ext, on); 639773bda96SJonathan Lemon 640773bda96SJonathan Lemon return err; 641773bda96SJonathan Lemon } 642773bda96SJonathan Lemon 643a7e1abadSJonathan Lemon static const struct ptp_clock_info ptp_ocp_clock_info = { 644a7e1abadSJonathan Lemon .owner = THIS_MODULE, 645a7e1abadSJonathan Lemon .name = KBUILD_MODNAME, 646a7e1abadSJonathan Lemon .max_adj = 100000000, 647a7e1abadSJonathan Lemon .gettimex64 = ptp_ocp_gettimex, 648a7e1abadSJonathan Lemon .settime64 = ptp_ocp_settime, 649a7e1abadSJonathan Lemon .adjtime = ptp_ocp_adjtime, 650a7e1abadSJonathan Lemon .adjfine = ptp_ocp_null_adjfine, 651773bda96SJonathan Lemon .adjphase = ptp_ocp_adjphase, 652773bda96SJonathan Lemon .enable = ptp_ocp_enable, 653773bda96SJonathan Lemon .pps = true, 654dcf61469SJonathan Lemon .n_ext_ts = 3, 655a7e1abadSJonathan Lemon }; 656a7e1abadSJonathan Lemon 657773bda96SJonathan Lemon static void 658773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp) 659773bda96SJonathan Lemon { 660773bda96SJonathan Lemon u32 ctrl, select; 661773bda96SJonathan Lemon 662773bda96SJonathan Lemon select = ioread32(&bp->reg->select); 663773bda96SJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 664773bda96SJonathan Lemon 665773bda96SJonathan Lemon iowrite32(0, &bp->reg->drift_ns); 666773bda96SJonathan Lemon 667773bda96SJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 668773bda96SJonathan Lemon ctrl |= OCP_CTRL_ADJUST_DRIFT; 669773bda96SJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 670773bda96SJonathan Lemon 671773bda96SJonathan Lemon /* restore clock selection */ 672773bda96SJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 673773bda96SJonathan Lemon } 674773bda96SJonathan Lemon 675773bda96SJonathan Lemon static void 676773bda96SJonathan Lemon ptp_ocp_watchdog(struct timer_list *t) 677773bda96SJonathan Lemon { 678773bda96SJonathan Lemon struct ptp_ocp *bp = from_timer(bp, t, watchdog); 679773bda96SJonathan Lemon unsigned long flags; 680773bda96SJonathan Lemon u32 status; 681773bda96SJonathan Lemon 6820d43d4f2SJonathan Lemon status = ioread32(&bp->pps_to_clk->status); 683773bda96SJonathan Lemon 684773bda96SJonathan Lemon if (status & PPS_STATUS_SUPERV_ERR) { 6850d43d4f2SJonathan Lemon iowrite32(status, &bp->pps_to_clk->status); 686ef0cfb34SJonathan Lemon if (!bp->gnss_lost) { 687773bda96SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 688773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(bp); 689773bda96SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 690ef0cfb34SJonathan Lemon bp->gnss_lost = ktime_get_real_seconds(); 691773bda96SJonathan Lemon } 692773bda96SJonathan Lemon 693ef0cfb34SJonathan Lemon } else if (bp->gnss_lost) { 694ef0cfb34SJonathan Lemon bp->gnss_lost = 0; 695773bda96SJonathan Lemon } 696773bda96SJonathan Lemon 697773bda96SJonathan Lemon mod_timer(&bp->watchdog, jiffies + HZ); 698773bda96SJonathan Lemon } 699773bda96SJonathan Lemon 700a7e1abadSJonathan Lemon static int 701773bda96SJonathan Lemon ptp_ocp_init_clock(struct ptp_ocp *bp) 702a7e1abadSJonathan Lemon { 703a7e1abadSJonathan Lemon struct timespec64 ts; 704a7e1abadSJonathan Lemon bool sync; 705a7e1abadSJonathan Lemon u32 ctrl; 706a7e1abadSJonathan Lemon 707a7e1abadSJonathan Lemon /* make sure clock is enabled */ 708a7e1abadSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 709a7e1abadSJonathan Lemon ctrl |= OCP_CTRL_ENABLE; 710a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 711a7e1abadSJonathan Lemon 712773bda96SJonathan Lemon /* NO DRIFT Correction */ 713773bda96SJonathan Lemon /* offset_p:i 1/8, offset_i: 1/16, drift_p: 0, drift_i: 0 */ 714773bda96SJonathan Lemon iowrite32(0x2000, &bp->reg->servo_offset_p); 715773bda96SJonathan Lemon iowrite32(0x1000, &bp->reg->servo_offset_i); 716773bda96SJonathan Lemon iowrite32(0, &bp->reg->servo_drift_p); 717773bda96SJonathan Lemon iowrite32(0, &bp->reg->servo_drift_i); 718773bda96SJonathan Lemon 719773bda96SJonathan Lemon /* latch servo values */ 720773bda96SJonathan Lemon ctrl |= OCP_CTRL_ADJUST_SERVO; 721773bda96SJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 722773bda96SJonathan Lemon 723a7e1abadSJonathan Lemon if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) { 724a7e1abadSJonathan Lemon dev_err(&bp->pdev->dev, "clock not enabled\n"); 725a7e1abadSJonathan Lemon return -ENODEV; 726a7e1abadSJonathan Lemon } 727a7e1abadSJonathan Lemon 728a7e1abadSJonathan Lemon sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC; 729a7e1abadSJonathan Lemon if (!sync) { 730a7e1abadSJonathan Lemon ktime_get_real_ts64(&ts); 731a7e1abadSJonathan Lemon ptp_ocp_settime(&bp->ptp_info, &ts); 732a7e1abadSJonathan Lemon } 733a7e1abadSJonathan Lemon if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL)) 734a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "Time: %lld.%ld, %s\n", 735a7e1abadSJonathan Lemon ts.tv_sec, ts.tv_nsec, 736a7e1abadSJonathan Lemon sync ? "in-sync" : "UNSYNCED"); 737a7e1abadSJonathan Lemon 738773bda96SJonathan Lemon timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0); 739773bda96SJonathan Lemon mod_timer(&bp->watchdog, jiffies + HZ); 740773bda96SJonathan Lemon 741a7e1abadSJonathan Lemon return 0; 742a7e1abadSJonathan Lemon } 743a7e1abadSJonathan Lemon 744a7e1abadSJonathan Lemon static void 745a7e1abadSJonathan Lemon ptp_ocp_tod_info(struct ptp_ocp *bp) 746a7e1abadSJonathan Lemon { 747a7e1abadSJonathan Lemon static const char * const proto_name[] = { 748a7e1abadSJonathan Lemon "NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none", 749a7e1abadSJonathan Lemon "UBX", "UBX_UTC", "UBX_LS", "UBX_none" 750a7e1abadSJonathan Lemon }; 751a7e1abadSJonathan Lemon static const char * const gnss_name[] = { 752a7e1abadSJonathan Lemon "ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU", 753a7e1abadSJonathan Lemon }; 754a7e1abadSJonathan Lemon u32 version, ctrl, reg; 755a7e1abadSJonathan Lemon int idx; 756a7e1abadSJonathan Lemon 757a7e1abadSJonathan Lemon version = ioread32(&bp->tod->version); 758a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "TOD Version %d.%d.%d\n", 759a7e1abadSJonathan Lemon version >> 24, (version >> 16) & 0xff, version & 0xffff); 760a7e1abadSJonathan Lemon 761a7e1abadSJonathan Lemon ctrl = ioread32(&bp->tod->ctrl); 762a7e1abadSJonathan Lemon ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE; 763a7e1abadSJonathan Lemon ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B); 764a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->tod->ctrl); 765a7e1abadSJonathan Lemon 766a7e1abadSJonathan Lemon ctrl = ioread32(&bp->tod->ctrl); 767a7e1abadSJonathan Lemon idx = ctrl & TOD_CTRL_PROTOCOL ? 4 : 0; 768a7e1abadSJonathan Lemon idx += (ctrl >> 16) & 3; 769a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "control: %x\n", ctrl); 770a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "TOD Protocol %s %s\n", proto_name[idx], 771a7e1abadSJonathan Lemon ctrl & TOD_CTRL_ENABLE ? "enabled" : ""); 772a7e1abadSJonathan Lemon 773a7e1abadSJonathan Lemon idx = (ctrl >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK; 774a7e1abadSJonathan Lemon if (idx < ARRAY_SIZE(gnss_name)) 775a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "GNSS %s\n", gnss_name[idx]); 776a7e1abadSJonathan Lemon 777a7e1abadSJonathan Lemon reg = ioread32(&bp->tod->status); 778a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "status: %x\n", reg); 779a7e1abadSJonathan Lemon 780a7e1abadSJonathan Lemon reg = ioread32(&bp->tod->correction_sec); 781a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "correction: %d\n", reg); 782a7e1abadSJonathan Lemon 783a7e1abadSJonathan Lemon reg = ioread32(&bp->tod->utc_status); 784a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "utc_status: %x\n", reg); 785a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "utc_offset: %d valid:%d leap_valid:%d\n", 786a7e1abadSJonathan Lemon reg & TOD_STATUS_UTC_MASK, reg & TOD_STATUS_UTC_VALID ? 1 : 0, 787a7e1abadSJonathan Lemon reg & TOD_STATUS_LEAP_VALID ? 1 : 0); 788a7e1abadSJonathan Lemon } 789a7e1abadSJonathan Lemon 790773bda96SJonathan Lemon static int 791773bda96SJonathan Lemon ptp_ocp_firstchild(struct device *dev, void *data) 792773bda96SJonathan Lemon { 793773bda96SJonathan Lemon return 1; 794773bda96SJonathan Lemon } 795773bda96SJonathan Lemon 796773bda96SJonathan Lemon static int 797773bda96SJonathan Lemon ptp_ocp_read_i2c(struct i2c_adapter *adap, u8 addr, u8 reg, u8 sz, u8 *data) 798773bda96SJonathan Lemon { 799773bda96SJonathan Lemon struct i2c_msg msgs[2] = { 800773bda96SJonathan Lemon { 801773bda96SJonathan Lemon .addr = addr, 802773bda96SJonathan Lemon .len = 1, 803773bda96SJonathan Lemon .buf = ®, 804773bda96SJonathan Lemon }, 805773bda96SJonathan Lemon { 806773bda96SJonathan Lemon .addr = addr, 807773bda96SJonathan Lemon .flags = I2C_M_RD, 808773bda96SJonathan Lemon .len = 2, 809773bda96SJonathan Lemon .buf = data, 810773bda96SJonathan Lemon }, 811773bda96SJonathan Lemon }; 812773bda96SJonathan Lemon int err; 813773bda96SJonathan Lemon u8 len; 814773bda96SJonathan Lemon 815773bda96SJonathan Lemon /* xiic-i2c for some stupid reason only does 2 byte reads. */ 816773bda96SJonathan Lemon while (sz) { 817773bda96SJonathan Lemon len = min_t(u8, sz, 2); 818773bda96SJonathan Lemon msgs[1].len = len; 819773bda96SJonathan Lemon err = i2c_transfer(adap, msgs, 2); 820773bda96SJonathan Lemon if (err != msgs[1].len) 821773bda96SJonathan Lemon return err; 822773bda96SJonathan Lemon msgs[1].buf += len; 823773bda96SJonathan Lemon reg += len; 824773bda96SJonathan Lemon sz -= len; 825773bda96SJonathan Lemon } 826773bda96SJonathan Lemon return 0; 827773bda96SJonathan Lemon } 828773bda96SJonathan Lemon 829773bda96SJonathan Lemon static void 830773bda96SJonathan Lemon ptp_ocp_get_serial_number(struct ptp_ocp *bp) 831773bda96SJonathan Lemon { 832773bda96SJonathan Lemon struct i2c_adapter *adap; 833773bda96SJonathan Lemon struct device *dev; 834773bda96SJonathan Lemon int err; 835773bda96SJonathan Lemon 8361447149dSJonathan Lemon if (!bp->i2c_ctrl) 8371447149dSJonathan Lemon return; 8381447149dSJonathan Lemon 839773bda96SJonathan Lemon dev = device_find_child(&bp->i2c_ctrl->dev, NULL, ptp_ocp_firstchild); 840773bda96SJonathan Lemon if (!dev) { 841773bda96SJonathan Lemon dev_err(&bp->pdev->dev, "Can't find I2C adapter\n"); 842773bda96SJonathan Lemon return; 843773bda96SJonathan Lemon } 844773bda96SJonathan Lemon 845773bda96SJonathan Lemon adap = i2c_verify_adapter(dev); 846773bda96SJonathan Lemon if (!adap) { 847773bda96SJonathan Lemon dev_err(&bp->pdev->dev, "device '%s' isn't an I2C adapter\n", 848773bda96SJonathan Lemon dev_name(dev)); 849773bda96SJonathan Lemon goto out; 850773bda96SJonathan Lemon } 851773bda96SJonathan Lemon 852773bda96SJonathan Lemon err = ptp_ocp_read_i2c(adap, 0x58, 0x9A, 6, bp->serial); 853773bda96SJonathan Lemon if (err) { 854773bda96SJonathan Lemon dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", err); 855773bda96SJonathan Lemon goto out; 856773bda96SJonathan Lemon } 857773bda96SJonathan Lemon 858773bda96SJonathan Lemon bp->has_serial = true; 859773bda96SJonathan Lemon 860773bda96SJonathan Lemon out: 861773bda96SJonathan Lemon put_device(dev); 862773bda96SJonathan Lemon } 863773bda96SJonathan Lemon 864a7e1abadSJonathan Lemon static void 865a7e1abadSJonathan Lemon ptp_ocp_info(struct ptp_ocp *bp) 866a7e1abadSJonathan Lemon { 867a7e1abadSJonathan Lemon u32 version, select; 868a7e1abadSJonathan Lemon 869a7e1abadSJonathan Lemon version = ioread32(&bp->reg->version); 870a7e1abadSJonathan Lemon select = ioread32(&bp->reg->select); 871a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n", 872a7e1abadSJonathan Lemon version >> 24, (version >> 16) & 0xff, version & 0xffff, 873e1daf0ecSJonathan Lemon ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16), 874a7e1abadSJonathan Lemon ptp_clock_index(bp->ptp)); 875a7e1abadSJonathan Lemon 876498ad3f4SJonathan Lemon if (bp->tod) 877a7e1abadSJonathan Lemon ptp_ocp_tod_info(bp); 878a7e1abadSJonathan Lemon } 879a7e1abadSJonathan Lemon 880773bda96SJonathan Lemon static struct device * 881773bda96SJonathan Lemon ptp_ocp_find_flash(struct ptp_ocp *bp) 882773bda96SJonathan Lemon { 883773bda96SJonathan Lemon struct device *dev, *last; 884773bda96SJonathan Lemon 885773bda96SJonathan Lemon last = NULL; 886773bda96SJonathan Lemon dev = &bp->spi_flash->dev; 887773bda96SJonathan Lemon 888773bda96SJonathan Lemon while ((dev = device_find_child(dev, NULL, ptp_ocp_firstchild))) { 889773bda96SJonathan Lemon if (!strcmp("mtd", dev_bus_name(dev))) 890773bda96SJonathan Lemon break; 891773bda96SJonathan Lemon put_device(last); 892773bda96SJonathan Lemon last = dev; 893773bda96SJonathan Lemon } 894773bda96SJonathan Lemon put_device(last); 895773bda96SJonathan Lemon 896773bda96SJonathan Lemon return dev; 897773bda96SJonathan Lemon } 898773bda96SJonathan Lemon 899773bda96SJonathan Lemon static int 900773bda96SJonathan Lemon ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev, 901773bda96SJonathan Lemon const struct firmware *fw) 902773bda96SJonathan Lemon { 903773bda96SJonathan Lemon struct mtd_info *mtd = dev_get_drvdata(dev); 904773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 905773bda96SJonathan Lemon size_t off, len, resid, wrote; 906773bda96SJonathan Lemon struct erase_info erase; 907773bda96SJonathan Lemon size_t base, blksz; 9087c807572SJonathan Lemon int err = 0; 909773bda96SJonathan Lemon 910773bda96SJonathan Lemon off = 0; 911773bda96SJonathan Lemon base = bp->flash_start; 912773bda96SJonathan Lemon blksz = 4096; 913773bda96SJonathan Lemon resid = fw->size; 914773bda96SJonathan Lemon 915773bda96SJonathan Lemon while (resid) { 916773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, "Flashing", 917773bda96SJonathan Lemon NULL, off, fw->size); 918773bda96SJonathan Lemon 919773bda96SJonathan Lemon len = min_t(size_t, resid, blksz); 920773bda96SJonathan Lemon erase.addr = base + off; 921773bda96SJonathan Lemon erase.len = blksz; 922773bda96SJonathan Lemon 923773bda96SJonathan Lemon err = mtd_erase(mtd, &erase); 924773bda96SJonathan Lemon if (err) 925773bda96SJonathan Lemon goto out; 926773bda96SJonathan Lemon 927773bda96SJonathan Lemon err = mtd_write(mtd, base + off, len, &wrote, &fw->data[off]); 928773bda96SJonathan Lemon if (err) 929773bda96SJonathan Lemon goto out; 930773bda96SJonathan Lemon 931773bda96SJonathan Lemon off += blksz; 932773bda96SJonathan Lemon resid -= len; 933773bda96SJonathan Lemon } 934773bda96SJonathan Lemon out: 935773bda96SJonathan Lemon return err; 936773bda96SJonathan Lemon } 937773bda96SJonathan Lemon 938773bda96SJonathan Lemon static int 939773bda96SJonathan Lemon ptp_ocp_devlink_flash_update(struct devlink *devlink, 940773bda96SJonathan Lemon struct devlink_flash_update_params *params, 941773bda96SJonathan Lemon struct netlink_ext_ack *extack) 942773bda96SJonathan Lemon { 943773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 944773bda96SJonathan Lemon struct device *dev; 945773bda96SJonathan Lemon const char *msg; 946773bda96SJonathan Lemon int err; 947773bda96SJonathan Lemon 948773bda96SJonathan Lemon dev = ptp_ocp_find_flash(bp); 949773bda96SJonathan Lemon if (!dev) { 950773bda96SJonathan Lemon dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n"); 951773bda96SJonathan Lemon return -ENODEV; 952773bda96SJonathan Lemon } 953773bda96SJonathan Lemon 954773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, "Preparing to flash", 955773bda96SJonathan Lemon NULL, 0, 0); 956773bda96SJonathan Lemon 957773bda96SJonathan Lemon err = ptp_ocp_devlink_flash(devlink, dev, params->fw); 958773bda96SJonathan Lemon 959773bda96SJonathan Lemon msg = err ? "Flash error" : "Flash complete"; 960773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0); 961773bda96SJonathan Lemon 962773bda96SJonathan Lemon put_device(dev); 963773bda96SJonathan Lemon return err; 964773bda96SJonathan Lemon } 965773bda96SJonathan Lemon 966773bda96SJonathan Lemon static int 967773bda96SJonathan Lemon ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req, 968773bda96SJonathan Lemon struct netlink_ext_ack *extack) 969773bda96SJonathan Lemon { 970773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 971773bda96SJonathan Lemon char buf[32]; 972773bda96SJonathan Lemon int err; 973773bda96SJonathan Lemon 974773bda96SJonathan Lemon err = devlink_info_driver_name_put(req, KBUILD_MODNAME); 975773bda96SJonathan Lemon if (err) 976773bda96SJonathan Lemon return err; 977773bda96SJonathan Lemon 978773bda96SJonathan Lemon if (bp->image) { 979773bda96SJonathan Lemon u32 ver = ioread32(&bp->image->version); 980773bda96SJonathan Lemon 981773bda96SJonathan Lemon if (ver & 0xffff) { 982773bda96SJonathan Lemon sprintf(buf, "%d", ver); 983773bda96SJonathan Lemon err = devlink_info_version_running_put(req, 9841a052da9SJonathan Lemon "fw", 985773bda96SJonathan Lemon buf); 986773bda96SJonathan Lemon } else { 987773bda96SJonathan Lemon sprintf(buf, "%d", ver >> 16); 988773bda96SJonathan Lemon err = devlink_info_version_running_put(req, 9891a052da9SJonathan Lemon "loader", 990773bda96SJonathan Lemon buf); 991773bda96SJonathan Lemon } 992773bda96SJonathan Lemon if (err) 993773bda96SJonathan Lemon return err; 994773bda96SJonathan Lemon } 995773bda96SJonathan Lemon 996773bda96SJonathan Lemon if (!bp->has_serial) 997773bda96SJonathan Lemon ptp_ocp_get_serial_number(bp); 998773bda96SJonathan Lemon 999773bda96SJonathan Lemon if (bp->has_serial) { 1000773bda96SJonathan Lemon sprintf(buf, "%pM", bp->serial); 1001773bda96SJonathan Lemon err = devlink_info_serial_number_put(req, buf); 1002773bda96SJonathan Lemon if (err) 1003773bda96SJonathan Lemon return err; 1004773bda96SJonathan Lemon } 1005773bda96SJonathan Lemon 1006773bda96SJonathan Lemon return 0; 1007773bda96SJonathan Lemon } 1008773bda96SJonathan Lemon 1009773bda96SJonathan Lemon static const struct devlink_ops ptp_ocp_devlink_ops = { 1010773bda96SJonathan Lemon .flash_update = ptp_ocp_devlink_flash_update, 1011773bda96SJonathan Lemon .info_get = ptp_ocp_devlink_info_get, 1012773bda96SJonathan Lemon }; 1013773bda96SJonathan Lemon 1014773bda96SJonathan Lemon static void __iomem * 1015773bda96SJonathan Lemon __ptp_ocp_get_mem(struct ptp_ocp *bp, unsigned long start, int size) 1016773bda96SJonathan Lemon { 1017773bda96SJonathan Lemon struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp"); 1018773bda96SJonathan Lemon 1019773bda96SJonathan Lemon return devm_ioremap_resource(&bp->pdev->dev, &res); 1020773bda96SJonathan Lemon } 1021773bda96SJonathan Lemon 1022773bda96SJonathan Lemon static void __iomem * 1023773bda96SJonathan Lemon ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r) 1024773bda96SJonathan Lemon { 1025773bda96SJonathan Lemon unsigned long start; 1026773bda96SJonathan Lemon 1027773bda96SJonathan Lemon start = pci_resource_start(bp->pdev, 0) + r->offset; 1028773bda96SJonathan Lemon return __ptp_ocp_get_mem(bp, start, r->size); 1029773bda96SJonathan Lemon } 1030773bda96SJonathan Lemon 1031773bda96SJonathan Lemon static void 1032773bda96SJonathan Lemon ptp_ocp_set_irq_resource(struct resource *res, int irq) 1033773bda96SJonathan Lemon { 1034773bda96SJonathan Lemon struct resource r = DEFINE_RES_IRQ(irq); 1035773bda96SJonathan Lemon *res = r; 1036773bda96SJonathan Lemon } 1037773bda96SJonathan Lemon 1038773bda96SJonathan Lemon static void 1039773bda96SJonathan Lemon ptp_ocp_set_mem_resource(struct resource *res, unsigned long start, int size) 1040773bda96SJonathan Lemon { 1041773bda96SJonathan Lemon struct resource r = DEFINE_RES_MEM(start, size); 1042773bda96SJonathan Lemon *res = r; 1043773bda96SJonathan Lemon } 1044773bda96SJonathan Lemon 1045773bda96SJonathan Lemon static int 1046773bda96SJonathan Lemon ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r) 1047773bda96SJonathan Lemon { 1048773bda96SJonathan Lemon struct ptp_ocp_flash_info *info; 1049773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1050773bda96SJonathan Lemon struct platform_device *p; 1051773bda96SJonathan Lemon struct resource res[2]; 1052773bda96SJonathan Lemon unsigned long start; 1053773bda96SJonathan Lemon int id; 1054773bda96SJonathan Lemon 1055773bda96SJonathan Lemon start = pci_resource_start(pdev, 0) + r->offset; 1056773bda96SJonathan Lemon ptp_ocp_set_mem_resource(&res[0], start, r->size); 1057773bda96SJonathan Lemon ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1058773bda96SJonathan Lemon 1059773bda96SJonathan Lemon info = r->extra; 1060773bda96SJonathan Lemon id = pci_dev_id(pdev) << 1; 1061773bda96SJonathan Lemon id += info->pci_offset; 1062773bda96SJonathan Lemon 1063773bda96SJonathan Lemon p = platform_device_register_resndata(&pdev->dev, info->name, id, 1064773bda96SJonathan Lemon res, 2, info->data, 1065773bda96SJonathan Lemon info->data_size); 1066773bda96SJonathan Lemon if (IS_ERR(p)) 1067773bda96SJonathan Lemon return PTR_ERR(p); 1068773bda96SJonathan Lemon 1069773bda96SJonathan Lemon bp_assign_entry(bp, r, p); 1070773bda96SJonathan Lemon 1071773bda96SJonathan Lemon return 0; 1072773bda96SJonathan Lemon } 1073773bda96SJonathan Lemon 1074773bda96SJonathan Lemon static struct platform_device * 1075773bda96SJonathan Lemon ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id) 1076773bda96SJonathan Lemon { 10771618df6aSJonathan Lemon struct ptp_ocp_i2c_info *info; 1078773bda96SJonathan Lemon struct resource res[2]; 1079773bda96SJonathan Lemon unsigned long start; 1080773bda96SJonathan Lemon 10811618df6aSJonathan Lemon info = r->extra; 1082773bda96SJonathan Lemon start = pci_resource_start(pdev, 0) + r->offset; 1083773bda96SJonathan Lemon ptp_ocp_set_mem_resource(&res[0], start, r->size); 1084773bda96SJonathan Lemon ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1085773bda96SJonathan Lemon 10861618df6aSJonathan Lemon return platform_device_register_resndata(&pdev->dev, info->name, 10871618df6aSJonathan Lemon id, res, 2, 10881618df6aSJonathan Lemon info->data, info->data_size); 1089773bda96SJonathan Lemon } 1090773bda96SJonathan Lemon 1091773bda96SJonathan Lemon static int 1092773bda96SJonathan Lemon ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r) 1093773bda96SJonathan Lemon { 1094773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 10951618df6aSJonathan Lemon struct ptp_ocp_i2c_info *info; 1096773bda96SJonathan Lemon struct platform_device *p; 1097773bda96SJonathan Lemon struct clk_hw *clk; 1098773bda96SJonathan Lemon char buf[32]; 1099773bda96SJonathan Lemon int id; 1100773bda96SJonathan Lemon 11011618df6aSJonathan Lemon info = r->extra; 1102773bda96SJonathan Lemon id = pci_dev_id(bp->pdev); 1103773bda96SJonathan Lemon 1104773bda96SJonathan Lemon sprintf(buf, "AXI.%d", id); 11051618df6aSJonathan Lemon clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0, 11061618df6aSJonathan Lemon info->fixed_rate); 1107773bda96SJonathan Lemon if (IS_ERR(clk)) 1108773bda96SJonathan Lemon return PTR_ERR(clk); 1109773bda96SJonathan Lemon bp->i2c_clk = clk; 1110773bda96SJonathan Lemon 11111618df6aSJonathan Lemon sprintf(buf, "%s.%d", info->name, id); 1112773bda96SJonathan Lemon devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf); 1113773bda96SJonathan Lemon p = ptp_ocp_i2c_bus(bp->pdev, r, id); 1114773bda96SJonathan Lemon if (IS_ERR(p)) 1115773bda96SJonathan Lemon return PTR_ERR(p); 1116773bda96SJonathan Lemon 1117773bda96SJonathan Lemon bp_assign_entry(bp, r, p); 1118773bda96SJonathan Lemon 1119773bda96SJonathan Lemon return 0; 1120773bda96SJonathan Lemon } 1121773bda96SJonathan Lemon 1122773bda96SJonathan Lemon static irqreturn_t 1123773bda96SJonathan Lemon ptp_ocp_ts_irq(int irq, void *priv) 1124773bda96SJonathan Lemon { 1125773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1126773bda96SJonathan Lemon struct ts_reg __iomem *reg = ext->mem; 1127773bda96SJonathan Lemon struct ptp_clock_event ev; 1128773bda96SJonathan Lemon u32 sec, nsec; 1129773bda96SJonathan Lemon 1130773bda96SJonathan Lemon /* XXX should fix API - this converts s/ns -> ts -> s/ns */ 1131773bda96SJonathan Lemon sec = ioread32(®->time_sec); 1132773bda96SJonathan Lemon nsec = ioread32(®->time_ns); 1133773bda96SJonathan Lemon 1134773bda96SJonathan Lemon ev.type = PTP_CLOCK_EXTTS; 1135773bda96SJonathan Lemon ev.index = ext->info->index; 1136773bda96SJonathan Lemon ev.timestamp = sec * 1000000000ULL + nsec; 1137773bda96SJonathan Lemon 1138773bda96SJonathan Lemon ptp_clock_event(ext->bp->ptp, &ev); 1139773bda96SJonathan Lemon 1140773bda96SJonathan Lemon iowrite32(1, ®->intr); /* write 1 to ack */ 1141773bda96SJonathan Lemon 1142773bda96SJonathan Lemon return IRQ_HANDLED; 1143773bda96SJonathan Lemon } 1144773bda96SJonathan Lemon 1145773bda96SJonathan Lemon static int 1146773bda96SJonathan Lemon ptp_ocp_ts_enable(void *priv, bool enable) 1147773bda96SJonathan Lemon { 1148773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1149773bda96SJonathan Lemon struct ts_reg __iomem *reg = ext->mem; 1150773bda96SJonathan Lemon 1151773bda96SJonathan Lemon if (enable) { 1152773bda96SJonathan Lemon iowrite32(1, ®->enable); 1153773bda96SJonathan Lemon iowrite32(1, ®->intr_mask); 1154773bda96SJonathan Lemon iowrite32(1, ®->intr); 1155773bda96SJonathan Lemon } else { 1156773bda96SJonathan Lemon iowrite32(0, ®->intr_mask); 1157773bda96SJonathan Lemon iowrite32(0, ®->enable); 1158773bda96SJonathan Lemon } 1159773bda96SJonathan Lemon 1160773bda96SJonathan Lemon return 0; 1161773bda96SJonathan Lemon } 1162773bda96SJonathan Lemon 1163773bda96SJonathan Lemon static void 1164773bda96SJonathan Lemon ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext) 1165773bda96SJonathan Lemon { 1166773bda96SJonathan Lemon ext->info->enable(ext, false); 1167773bda96SJonathan Lemon pci_free_irq(ext->bp->pdev, ext->irq_vec, ext); 1168773bda96SJonathan Lemon kfree(ext); 1169773bda96SJonathan Lemon } 1170773bda96SJonathan Lemon 1171773bda96SJonathan Lemon static int 1172773bda96SJonathan Lemon ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r) 1173773bda96SJonathan Lemon { 1174773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1175773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext; 1176773bda96SJonathan Lemon int err; 1177773bda96SJonathan Lemon 1178773bda96SJonathan Lemon ext = kzalloc(sizeof(*ext), GFP_KERNEL); 1179773bda96SJonathan Lemon if (!ext) 1180773bda96SJonathan Lemon return -ENOMEM; 1181773bda96SJonathan Lemon 1182773bda96SJonathan Lemon err = -EINVAL; 1183773bda96SJonathan Lemon ext->mem = ptp_ocp_get_mem(bp, r); 1184773bda96SJonathan Lemon if (!ext->mem) 1185773bda96SJonathan Lemon goto out; 1186773bda96SJonathan Lemon 1187773bda96SJonathan Lemon ext->bp = bp; 1188773bda96SJonathan Lemon ext->info = r->extra; 1189773bda96SJonathan Lemon ext->irq_vec = r->irq_vec; 1190773bda96SJonathan Lemon 1191773bda96SJonathan Lemon err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL, 119256ec4403SJonathan Lemon ext, "ocp%d.%s", bp->id, r->name); 1193773bda96SJonathan Lemon if (err) { 1194773bda96SJonathan Lemon dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec); 1195773bda96SJonathan Lemon goto out; 1196773bda96SJonathan Lemon } 1197773bda96SJonathan Lemon 1198773bda96SJonathan Lemon bp_assign_entry(bp, r, ext); 1199773bda96SJonathan Lemon 1200773bda96SJonathan Lemon return 0; 1201773bda96SJonathan Lemon 1202773bda96SJonathan Lemon out: 1203773bda96SJonathan Lemon kfree(ext); 1204773bda96SJonathan Lemon return err; 1205773bda96SJonathan Lemon } 1206773bda96SJonathan Lemon 1207773bda96SJonathan Lemon static int 1208773bda96SJonathan Lemon ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r) 1209773bda96SJonathan Lemon { 1210773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1211773bda96SJonathan Lemon struct uart_8250_port uart; 1212773bda96SJonathan Lemon 1213773bda96SJonathan Lemon /* Setting UPF_IOREMAP and leaving port.membase unspecified lets 1214773bda96SJonathan Lemon * the serial port device claim and release the pci resource. 1215773bda96SJonathan Lemon */ 1216773bda96SJonathan Lemon memset(&uart, 0, sizeof(uart)); 1217773bda96SJonathan Lemon uart.port.dev = &pdev->dev; 1218773bda96SJonathan Lemon uart.port.iotype = UPIO_MEM; 1219773bda96SJonathan Lemon uart.port.regshift = 2; 1220773bda96SJonathan Lemon uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset; 1221773bda96SJonathan Lemon uart.port.irq = pci_irq_vector(pdev, r->irq_vec); 1222773bda96SJonathan Lemon uart.port.uartclk = 50000000; 1223773bda96SJonathan Lemon uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP; 1224773bda96SJonathan Lemon uart.port.type = PORT_16550A; 1225773bda96SJonathan Lemon 1226773bda96SJonathan Lemon return serial8250_register_8250_port(&uart); 1227773bda96SJonathan Lemon } 1228773bda96SJonathan Lemon 1229773bda96SJonathan Lemon static int 1230773bda96SJonathan Lemon ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r) 1231773bda96SJonathan Lemon { 1232773bda96SJonathan Lemon int port; 1233773bda96SJonathan Lemon 1234773bda96SJonathan Lemon port = ptp_ocp_serial_line(bp, r); 1235773bda96SJonathan Lemon if (port < 0) 1236773bda96SJonathan Lemon return port; 1237773bda96SJonathan Lemon 1238773bda96SJonathan Lemon bp_assign_entry(bp, r, port); 1239773bda96SJonathan Lemon 1240773bda96SJonathan Lemon return 0; 1241773bda96SJonathan Lemon } 1242773bda96SJonathan Lemon 1243773bda96SJonathan Lemon static int 1244773bda96SJonathan Lemon ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r) 1245773bda96SJonathan Lemon { 1246773bda96SJonathan Lemon void __iomem *mem; 1247773bda96SJonathan Lemon 1248773bda96SJonathan Lemon mem = ptp_ocp_get_mem(bp, r); 1249773bda96SJonathan Lemon if (!mem) 1250773bda96SJonathan Lemon return -EINVAL; 1251773bda96SJonathan Lemon 1252773bda96SJonathan Lemon bp_assign_entry(bp, r, mem); 1253773bda96SJonathan Lemon 1254773bda96SJonathan Lemon return 0; 1255773bda96SJonathan Lemon } 1256773bda96SJonathan Lemon 1257773bda96SJonathan Lemon /* FB specific board initializers; last "resource" registered. */ 1258773bda96SJonathan Lemon static int 1259773bda96SJonathan Lemon ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r) 1260773bda96SJonathan Lemon { 1261773bda96SJonathan Lemon bp->flash_start = 1024 * 4096; 1262773bda96SJonathan Lemon 1263773bda96SJonathan Lemon return ptp_ocp_init_clock(bp); 1264773bda96SJonathan Lemon } 1265773bda96SJonathan Lemon 126656ec4403SJonathan Lemon static bool 126756ec4403SJonathan Lemon ptp_ocp_allow_irq(struct ptp_ocp *bp, struct ocp_resource *r) 126856ec4403SJonathan Lemon { 126956ec4403SJonathan Lemon bool allow = !r->irq_vec || r->irq_vec < bp->n_irqs; 127056ec4403SJonathan Lemon 127156ec4403SJonathan Lemon if (!allow) 127256ec4403SJonathan Lemon dev_err(&bp->pdev->dev, "irq %d out of range, skipping %s\n", 127356ec4403SJonathan Lemon r->irq_vec, r->name); 127456ec4403SJonathan Lemon return allow; 127556ec4403SJonathan Lemon } 127656ec4403SJonathan Lemon 1277773bda96SJonathan Lemon static int 1278773bda96SJonathan Lemon ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data) 1279773bda96SJonathan Lemon { 1280773bda96SJonathan Lemon struct ocp_resource *r, *table; 1281773bda96SJonathan Lemon int err = 0; 1282773bda96SJonathan Lemon 1283773bda96SJonathan Lemon table = (struct ocp_resource *)driver_data; 1284773bda96SJonathan Lemon for (r = table; r->setup; r++) { 128556ec4403SJonathan Lemon if (!ptp_ocp_allow_irq(bp, r)) 128656ec4403SJonathan Lemon continue; 1287773bda96SJonathan Lemon err = r->setup(bp, r); 1288bceff290SJonathan Lemon if (err) { 1289bceff290SJonathan Lemon dev_err(&bp->pdev->dev, 1290bceff290SJonathan Lemon "Could not register %s: err %d\n", 1291bceff290SJonathan Lemon r->name, err); 1292773bda96SJonathan Lemon break; 1293773bda96SJonathan Lemon } 1294bceff290SJonathan Lemon } 1295773bda96SJonathan Lemon return err; 1296773bda96SJonathan Lemon } 1297773bda96SJonathan Lemon 1298*6baf2925SJonathan Lemon static void 1299*6baf2925SJonathan Lemon ptp_ocp_enable_fpga(u32 __iomem *reg, u32 bit, bool enable) 1300*6baf2925SJonathan Lemon { 1301*6baf2925SJonathan Lemon u32 ctrl; 1302*6baf2925SJonathan Lemon bool on; 1303*6baf2925SJonathan Lemon 1304*6baf2925SJonathan Lemon ctrl = ioread32(reg); 1305*6baf2925SJonathan Lemon on = ctrl & bit; 1306*6baf2925SJonathan Lemon if (on ^ enable) { 1307*6baf2925SJonathan Lemon ctrl &= ~bit; 1308*6baf2925SJonathan Lemon ctrl |= enable ? bit : 0; 1309*6baf2925SJonathan Lemon iowrite32(ctrl, reg); 1310*6baf2925SJonathan Lemon } 1311*6baf2925SJonathan Lemon } 1312*6baf2925SJonathan Lemon 1313*6baf2925SJonathan Lemon static void 1314*6baf2925SJonathan Lemon ptp_ocp_irig_out(struct ptp_ocp *bp, bool enable) 1315*6baf2925SJonathan Lemon { 1316*6baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->irig_out->ctrl, 1317*6baf2925SJonathan Lemon IRIG_M_CTRL_ENABLE, enable); 1318*6baf2925SJonathan Lemon } 1319*6baf2925SJonathan Lemon 1320*6baf2925SJonathan Lemon static void 1321*6baf2925SJonathan Lemon ptp_ocp_irig_in(struct ptp_ocp *bp, bool enable) 1322*6baf2925SJonathan Lemon { 1323*6baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->irig_in->ctrl, 1324*6baf2925SJonathan Lemon IRIG_S_CTRL_ENABLE, enable); 1325*6baf2925SJonathan Lemon } 1326*6baf2925SJonathan Lemon 1327*6baf2925SJonathan Lemon static void 1328*6baf2925SJonathan Lemon ptp_ocp_dcf_out(struct ptp_ocp *bp, bool enable) 1329*6baf2925SJonathan Lemon { 1330*6baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->dcf_out->ctrl, 1331*6baf2925SJonathan Lemon DCF_M_CTRL_ENABLE, enable); 1332*6baf2925SJonathan Lemon } 1333*6baf2925SJonathan Lemon 1334*6baf2925SJonathan Lemon static void 1335*6baf2925SJonathan Lemon ptp_ocp_dcf_in(struct ptp_ocp *bp, bool enable) 1336*6baf2925SJonathan Lemon { 1337*6baf2925SJonathan Lemon return ptp_ocp_enable_fpga(&bp->dcf_in->ctrl, 1338*6baf2925SJonathan Lemon DCF_S_CTRL_ENABLE, enable); 1339*6baf2925SJonathan Lemon } 1340*6baf2925SJonathan Lemon 1341*6baf2925SJonathan Lemon static void 1342*6baf2925SJonathan Lemon __handle_signal_outputs(struct ptp_ocp *bp, u32 val) 1343*6baf2925SJonathan Lemon { 1344*6baf2925SJonathan Lemon ptp_ocp_irig_out(bp, val & 0x00100010); 1345*6baf2925SJonathan Lemon ptp_ocp_dcf_out(bp, val & 0x00200020); 1346*6baf2925SJonathan Lemon } 1347*6baf2925SJonathan Lemon 1348*6baf2925SJonathan Lemon static void 1349*6baf2925SJonathan Lemon __handle_signal_inputs(struct ptp_ocp *bp, u32 val) 1350*6baf2925SJonathan Lemon { 1351*6baf2925SJonathan Lemon ptp_ocp_irig_in(bp, val & 0x00100010); 1352*6baf2925SJonathan Lemon ptp_ocp_dcf_in(bp, val & 0x00200020); 1353*6baf2925SJonathan Lemon } 1354*6baf2925SJonathan Lemon 1355e1daf0ecSJonathan Lemon /* 1356e1daf0ecSJonathan Lemon * ANT0 == gps (in) 1357e1daf0ecSJonathan Lemon * ANT1 == sma1 (in) 1358e1daf0ecSJonathan Lemon * ANT2 == sma2 (in) 1359e1daf0ecSJonathan Lemon * ANT3 == sma3 (out) 1360e1daf0ecSJonathan Lemon * ANT4 == sma4 (out) 1361e1daf0ecSJonathan Lemon */ 1362e1daf0ecSJonathan Lemon 1363e1daf0ecSJonathan Lemon enum ptp_ocp_sma_mode { 1364e1daf0ecSJonathan Lemon SMA_MODE_IN, 1365e1daf0ecSJonathan Lemon SMA_MODE_OUT, 1366e1daf0ecSJonathan Lemon }; 1367e1daf0ecSJonathan Lemon 1368e1daf0ecSJonathan Lemon static struct ptp_ocp_sma_connector { 1369e1daf0ecSJonathan Lemon enum ptp_ocp_sma_mode mode; 1370e1daf0ecSJonathan Lemon bool fixed_mode; 1371e1daf0ecSJonathan Lemon u16 default_out_idx; 1372e1daf0ecSJonathan Lemon } ptp_ocp_sma_map[4] = { 1373e1daf0ecSJonathan Lemon { 1374e1daf0ecSJonathan Lemon .mode = SMA_MODE_IN, 1375e1daf0ecSJonathan Lemon .fixed_mode = true, 1376e1daf0ecSJonathan Lemon }, 1377e1daf0ecSJonathan Lemon { 1378e1daf0ecSJonathan Lemon .mode = SMA_MODE_IN, 1379e1daf0ecSJonathan Lemon .fixed_mode = true, 1380e1daf0ecSJonathan Lemon }, 1381e1daf0ecSJonathan Lemon { 1382e1daf0ecSJonathan Lemon .mode = SMA_MODE_OUT, 1383e1daf0ecSJonathan Lemon .fixed_mode = true, 1384e1daf0ecSJonathan Lemon .default_out_idx = 0, /* 10Mhz */ 1385e1daf0ecSJonathan Lemon }, 1386e1daf0ecSJonathan Lemon { 1387e1daf0ecSJonathan Lemon .mode = SMA_MODE_OUT, 1388e1daf0ecSJonathan Lemon .fixed_mode = true, 1389e1daf0ecSJonathan Lemon .default_out_idx = 1, /* PHC */ 1390e1daf0ecSJonathan Lemon }, 1391e1daf0ecSJonathan Lemon }; 1392e1daf0ecSJonathan Lemon 1393e1daf0ecSJonathan Lemon static ssize_t 1394e1daf0ecSJonathan Lemon ptp_ocp_show_output(u32 val, char *buf, int default_idx) 1395e1daf0ecSJonathan Lemon { 1396e1daf0ecSJonathan Lemon const char *name; 1397e1daf0ecSJonathan Lemon ssize_t count; 1398e1daf0ecSJonathan Lemon 1399e1daf0ecSJonathan Lemon count = sysfs_emit(buf, "OUT: "); 1400e1daf0ecSJonathan Lemon name = ptp_ocp_select_name_from_val(ptp_ocp_sma_out, val); 1401e1daf0ecSJonathan Lemon if (!name) 1402e1daf0ecSJonathan Lemon name = ptp_ocp_sma_out[default_idx].name; 1403e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s\n", name); 1404e1daf0ecSJonathan Lemon return count; 1405e1daf0ecSJonathan Lemon } 1406e1daf0ecSJonathan Lemon 1407e1daf0ecSJonathan Lemon static ssize_t 1408e1daf0ecSJonathan Lemon ptp_ocp_show_inputs(u32 val, char *buf, const char *zero_in) 1409e1daf0ecSJonathan Lemon { 1410e1daf0ecSJonathan Lemon const char *name; 1411e1daf0ecSJonathan Lemon ssize_t count; 1412e1daf0ecSJonathan Lemon int i; 1413e1daf0ecSJonathan Lemon 1414e1daf0ecSJonathan Lemon count = sysfs_emit(buf, "IN: "); 1415e1daf0ecSJonathan Lemon for (i = 0; i < ARRAY_SIZE(ptp_ocp_sma_in); i++) { 1416e1daf0ecSJonathan Lemon if (val & ptp_ocp_sma_in[i].value) { 1417e1daf0ecSJonathan Lemon name = ptp_ocp_sma_in[i].name; 1418e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", name); 1419e1daf0ecSJonathan Lemon } 1420e1daf0ecSJonathan Lemon } 1421e1daf0ecSJonathan Lemon if (!val && zero_in) 1422e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", zero_in); 1423e1daf0ecSJonathan Lemon if (count) 1424e1daf0ecSJonathan Lemon count--; 1425e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "\n"); 1426e1daf0ecSJonathan Lemon return count; 1427e1daf0ecSJonathan Lemon } 1428e1daf0ecSJonathan Lemon 1429e1daf0ecSJonathan Lemon static int 1430e1daf0ecSJonathan Lemon sma_parse_inputs(const char *buf, enum ptp_ocp_sma_mode *mode) 1431e1daf0ecSJonathan Lemon { 1432e1daf0ecSJonathan Lemon struct ocp_selector *tbl[] = { ptp_ocp_sma_in, ptp_ocp_sma_out }; 1433e1daf0ecSJonathan Lemon int idx, count, dir; 1434e1daf0ecSJonathan Lemon char **argv; 1435e1daf0ecSJonathan Lemon int ret; 1436e1daf0ecSJonathan Lemon 1437e1daf0ecSJonathan Lemon argv = argv_split(GFP_KERNEL, buf, &count); 1438e1daf0ecSJonathan Lemon if (!argv) 1439e1daf0ecSJonathan Lemon return -ENOMEM; 1440e1daf0ecSJonathan Lemon 1441e1daf0ecSJonathan Lemon ret = -EINVAL; 1442e1daf0ecSJonathan Lemon if (!count) 1443e1daf0ecSJonathan Lemon goto out; 1444e1daf0ecSJonathan Lemon 1445e1daf0ecSJonathan Lemon idx = 0; 1446e1daf0ecSJonathan Lemon dir = *mode == SMA_MODE_IN ? 0 : 1; 1447e1daf0ecSJonathan Lemon if (!strcasecmp("IN:", argv[idx])) { 1448e1daf0ecSJonathan Lemon dir = 0; 1449e1daf0ecSJonathan Lemon idx++; 1450e1daf0ecSJonathan Lemon } 1451e1daf0ecSJonathan Lemon if (!strcasecmp("OUT:", argv[0])) { 1452e1daf0ecSJonathan Lemon dir = 1; 1453e1daf0ecSJonathan Lemon idx++; 1454e1daf0ecSJonathan Lemon } 1455e1daf0ecSJonathan Lemon *mode = dir == 0 ? SMA_MODE_IN : SMA_MODE_OUT; 1456e1daf0ecSJonathan Lemon 1457e1daf0ecSJonathan Lemon ret = 0; 1458e1daf0ecSJonathan Lemon for (; idx < count; idx++) 1459e1daf0ecSJonathan Lemon ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]); 1460e1daf0ecSJonathan Lemon if (ret < 0) 1461e1daf0ecSJonathan Lemon ret = -EINVAL; 1462e1daf0ecSJonathan Lemon 1463e1daf0ecSJonathan Lemon out: 1464e1daf0ecSJonathan Lemon argv_free(argv); 1465e1daf0ecSJonathan Lemon return ret; 1466e1daf0ecSJonathan Lemon } 1467e1daf0ecSJonathan Lemon 1468e1daf0ecSJonathan Lemon static ssize_t 1469e1daf0ecSJonathan Lemon ptp_ocp_sma_show(struct ptp_ocp *bp, int sma_nr, u32 val, char *buf, 1470e1daf0ecSJonathan Lemon const char *zero_in) 1471e1daf0ecSJonathan Lemon { 1472e1daf0ecSJonathan Lemon struct ptp_ocp_sma_connector *sma = &ptp_ocp_sma_map[sma_nr - 1]; 1473e1daf0ecSJonathan Lemon 1474e1daf0ecSJonathan Lemon if (sma->mode == SMA_MODE_IN) 1475e1daf0ecSJonathan Lemon return ptp_ocp_show_inputs(val, buf, zero_in); 1476e1daf0ecSJonathan Lemon 1477e1daf0ecSJonathan Lemon return ptp_ocp_show_output(val, buf, sma->default_out_idx); 1478e1daf0ecSJonathan Lemon } 1479e1daf0ecSJonathan Lemon 1480e1daf0ecSJonathan Lemon static ssize_t 1481e1daf0ecSJonathan Lemon sma1_show(struct device *dev, struct device_attribute *attr, char *buf) 1482e1daf0ecSJonathan Lemon { 1483e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1484e1daf0ecSJonathan Lemon u32 val; 1485e1daf0ecSJonathan Lemon 1486e1daf0ecSJonathan Lemon val = ioread32(&bp->sma->gpio1) & 0x3f; 1487e1daf0ecSJonathan Lemon return ptp_ocp_sma_show(bp, 1, val, buf, ptp_ocp_sma_in[0].name); 1488e1daf0ecSJonathan Lemon } 1489e1daf0ecSJonathan Lemon 1490e1daf0ecSJonathan Lemon static ssize_t 1491e1daf0ecSJonathan Lemon sma2_show(struct device *dev, struct device_attribute *attr, char *buf) 1492e1daf0ecSJonathan Lemon { 1493e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1494e1daf0ecSJonathan Lemon u32 val; 1495e1daf0ecSJonathan Lemon 1496e1daf0ecSJonathan Lemon val = (ioread32(&bp->sma->gpio1) >> 16) & 0x3f; 1497e1daf0ecSJonathan Lemon return ptp_ocp_sma_show(bp, 2, val, buf, NULL); 1498e1daf0ecSJonathan Lemon } 1499e1daf0ecSJonathan Lemon 1500e1daf0ecSJonathan Lemon static ssize_t 1501e1daf0ecSJonathan Lemon sma3_show(struct device *dev, struct device_attribute *attr, char *buf) 1502e1daf0ecSJonathan Lemon { 1503e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1504e1daf0ecSJonathan Lemon u32 val; 1505e1daf0ecSJonathan Lemon 1506e1daf0ecSJonathan Lemon val = ioread32(&bp->sma->gpio2) & 0x3f; 1507e1daf0ecSJonathan Lemon return ptp_ocp_sma_show(bp, 3, val, buf, NULL); 1508e1daf0ecSJonathan Lemon } 1509e1daf0ecSJonathan Lemon 1510e1daf0ecSJonathan Lemon static ssize_t 1511e1daf0ecSJonathan Lemon sma4_show(struct device *dev, struct device_attribute *attr, char *buf) 1512e1daf0ecSJonathan Lemon { 1513e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1514e1daf0ecSJonathan Lemon u32 val; 1515e1daf0ecSJonathan Lemon 1516e1daf0ecSJonathan Lemon val = (ioread32(&bp->sma->gpio2) >> 16) & 0x3f; 1517e1daf0ecSJonathan Lemon return ptp_ocp_sma_show(bp, 4, val, buf, NULL); 1518e1daf0ecSJonathan Lemon } 1519e1daf0ecSJonathan Lemon 1520e1daf0ecSJonathan Lemon static void 1521e1daf0ecSJonathan Lemon ptp_ocp_sma_store_output(struct ptp_ocp *bp, u32 val, u32 shift) 1522e1daf0ecSJonathan Lemon { 1523e1daf0ecSJonathan Lemon unsigned long flags; 1524e1daf0ecSJonathan Lemon u32 gpio, mask; 1525e1daf0ecSJonathan Lemon 1526e1daf0ecSJonathan Lemon mask = 0xffff << (16 - shift); 1527e1daf0ecSJonathan Lemon 1528e1daf0ecSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1529e1daf0ecSJonathan Lemon 1530e1daf0ecSJonathan Lemon gpio = ioread32(&bp->sma->gpio2); 1531e1daf0ecSJonathan Lemon gpio = (gpio & mask) | (val << shift); 1532*6baf2925SJonathan Lemon 1533*6baf2925SJonathan Lemon __handle_signal_outputs(bp, gpio); 1534*6baf2925SJonathan Lemon 1535e1daf0ecSJonathan Lemon iowrite32(gpio, &bp->sma->gpio2); 1536e1daf0ecSJonathan Lemon 1537e1daf0ecSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1538e1daf0ecSJonathan Lemon } 1539e1daf0ecSJonathan Lemon 1540e1daf0ecSJonathan Lemon static void 1541e1daf0ecSJonathan Lemon ptp_ocp_sma_store_inputs(struct ptp_ocp *bp, u32 val, u32 shift) 1542e1daf0ecSJonathan Lemon { 1543e1daf0ecSJonathan Lemon unsigned long flags; 1544e1daf0ecSJonathan Lemon u32 gpio, mask; 1545e1daf0ecSJonathan Lemon 1546e1daf0ecSJonathan Lemon mask = 0xffff << (16 - shift); 1547e1daf0ecSJonathan Lemon 1548e1daf0ecSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1549e1daf0ecSJonathan Lemon 1550e1daf0ecSJonathan Lemon gpio = ioread32(&bp->sma->gpio1); 1551e1daf0ecSJonathan Lemon gpio = (gpio & mask) | (val << shift); 1552*6baf2925SJonathan Lemon 1553*6baf2925SJonathan Lemon __handle_signal_inputs(bp, gpio); 1554*6baf2925SJonathan Lemon 1555e1daf0ecSJonathan Lemon iowrite32(gpio, &bp->sma->gpio1); 1556e1daf0ecSJonathan Lemon 1557e1daf0ecSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1558e1daf0ecSJonathan Lemon } 1559e1daf0ecSJonathan Lemon 1560e1daf0ecSJonathan Lemon static ssize_t 1561e1daf0ecSJonathan Lemon ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr, u32 shift) 1562e1daf0ecSJonathan Lemon { 1563e1daf0ecSJonathan Lemon struct ptp_ocp_sma_connector *sma = &ptp_ocp_sma_map[sma_nr - 1]; 1564e1daf0ecSJonathan Lemon enum ptp_ocp_sma_mode mode; 1565e1daf0ecSJonathan Lemon int val; 1566e1daf0ecSJonathan Lemon 1567e1daf0ecSJonathan Lemon mode = sma->mode; 1568e1daf0ecSJonathan Lemon val = sma_parse_inputs(buf, &mode); 1569e1daf0ecSJonathan Lemon if (val < 0) 1570e1daf0ecSJonathan Lemon return val; 1571e1daf0ecSJonathan Lemon 1572e1daf0ecSJonathan Lemon if (mode != sma->mode && sma->fixed_mode) 1573e1daf0ecSJonathan Lemon return -EOPNOTSUPP; 1574e1daf0ecSJonathan Lemon 1575e1daf0ecSJonathan Lemon if (mode != sma->mode) { 1576e1daf0ecSJonathan Lemon pr_err("Mode changes not supported yet.\n"); 1577e1daf0ecSJonathan Lemon return -EOPNOTSUPP; 1578e1daf0ecSJonathan Lemon } 1579e1daf0ecSJonathan Lemon 1580e1daf0ecSJonathan Lemon if (sma->mode == SMA_MODE_IN) 1581e1daf0ecSJonathan Lemon ptp_ocp_sma_store_inputs(bp, val, shift); 1582e1daf0ecSJonathan Lemon else 1583e1daf0ecSJonathan Lemon ptp_ocp_sma_store_output(bp, val, shift); 1584e1daf0ecSJonathan Lemon 1585e1daf0ecSJonathan Lemon return 0; 1586e1daf0ecSJonathan Lemon } 1587e1daf0ecSJonathan Lemon 1588e1daf0ecSJonathan Lemon static ssize_t 1589e1daf0ecSJonathan Lemon sma1_store(struct device *dev, struct device_attribute *attr, 1590e1daf0ecSJonathan Lemon const char *buf, size_t count) 1591e1daf0ecSJonathan Lemon { 1592e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1593e1daf0ecSJonathan Lemon int err; 1594e1daf0ecSJonathan Lemon 1595e1daf0ecSJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 1, 0); 1596e1daf0ecSJonathan Lemon return err ? err : count; 1597e1daf0ecSJonathan Lemon } 1598e1daf0ecSJonathan Lemon 1599e1daf0ecSJonathan Lemon static ssize_t 1600e1daf0ecSJonathan Lemon sma2_store(struct device *dev, struct device_attribute *attr, 1601e1daf0ecSJonathan Lemon const char *buf, size_t count) 1602e1daf0ecSJonathan Lemon { 1603e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1604e1daf0ecSJonathan Lemon int err; 1605e1daf0ecSJonathan Lemon 1606e1daf0ecSJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 2, 16); 1607e1daf0ecSJonathan Lemon return err ? err : count; 1608e1daf0ecSJonathan Lemon } 1609e1daf0ecSJonathan Lemon 1610e1daf0ecSJonathan Lemon static ssize_t 1611e1daf0ecSJonathan Lemon sma3_store(struct device *dev, struct device_attribute *attr, 1612e1daf0ecSJonathan Lemon const char *buf, size_t count) 1613e1daf0ecSJonathan Lemon { 1614e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1615e1daf0ecSJonathan Lemon int err; 1616e1daf0ecSJonathan Lemon 1617e1daf0ecSJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 3, 0); 1618e1daf0ecSJonathan Lemon return err ? err : count; 1619e1daf0ecSJonathan Lemon } 1620e1daf0ecSJonathan Lemon 1621e1daf0ecSJonathan Lemon static ssize_t 1622e1daf0ecSJonathan Lemon sma4_store(struct device *dev, struct device_attribute *attr, 1623e1daf0ecSJonathan Lemon const char *buf, size_t count) 1624e1daf0ecSJonathan Lemon { 1625e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1626e1daf0ecSJonathan Lemon int err; 1627e1daf0ecSJonathan Lemon 1628e1daf0ecSJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 4, 16); 1629e1daf0ecSJonathan Lemon return err ? err : count; 1630e1daf0ecSJonathan Lemon } 1631e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma1); 1632e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma2); 1633e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma3); 1634e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma4); 1635e1daf0ecSJonathan Lemon 1636e1daf0ecSJonathan Lemon static ssize_t 1637e1daf0ecSJonathan Lemon available_sma_inputs_show(struct device *dev, 1638e1daf0ecSJonathan Lemon struct device_attribute *attr, char *buf) 1639e1daf0ecSJonathan Lemon { 1640e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_sma_in, buf); 1641e1daf0ecSJonathan Lemon } 1642e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_inputs); 1643e1daf0ecSJonathan Lemon 1644e1daf0ecSJonathan Lemon static ssize_t 1645e1daf0ecSJonathan Lemon available_sma_outputs_show(struct device *dev, 1646e1daf0ecSJonathan Lemon struct device_attribute *attr, char *buf) 1647e1daf0ecSJonathan Lemon { 1648e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_sma_out, buf); 1649e1daf0ecSJonathan Lemon } 1650e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_outputs); 1651e1daf0ecSJonathan Lemon 1652773bda96SJonathan Lemon static ssize_t 1653773bda96SJonathan Lemon serialnum_show(struct device *dev, struct device_attribute *attr, char *buf) 1654773bda96SJonathan Lemon { 1655773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1656773bda96SJonathan Lemon 1657773bda96SJonathan Lemon if (!bp->has_serial) 1658773bda96SJonathan Lemon ptp_ocp_get_serial_number(bp); 1659773bda96SJonathan Lemon 1660773bda96SJonathan Lemon return sysfs_emit(buf, "%pM\n", bp->serial); 1661773bda96SJonathan Lemon } 1662773bda96SJonathan Lemon static DEVICE_ATTR_RO(serialnum); 1663773bda96SJonathan Lemon 1664773bda96SJonathan Lemon static ssize_t 1665ef0cfb34SJonathan Lemon gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf) 1666773bda96SJonathan Lemon { 1667773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1668773bda96SJonathan Lemon ssize_t ret; 1669773bda96SJonathan Lemon 1670ef0cfb34SJonathan Lemon if (bp->gnss_lost) 1671ef0cfb34SJonathan Lemon ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost); 1672773bda96SJonathan Lemon else 1673773bda96SJonathan Lemon ret = sysfs_emit(buf, "SYNC\n"); 1674773bda96SJonathan Lemon 1675773bda96SJonathan Lemon return ret; 1676773bda96SJonathan Lemon } 1677ef0cfb34SJonathan Lemon static DEVICE_ATTR_RO(gnss_sync); 1678773bda96SJonathan Lemon 1679773bda96SJonathan Lemon static ssize_t 1680773bda96SJonathan Lemon clock_source_show(struct device *dev, struct device_attribute *attr, char *buf) 1681773bda96SJonathan Lemon { 1682773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1683773bda96SJonathan Lemon const char *p; 1684773bda96SJonathan Lemon u32 select; 1685773bda96SJonathan Lemon 1686773bda96SJonathan Lemon select = ioread32(&bp->reg->select); 1687e1daf0ecSJonathan Lemon p = ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16); 1688773bda96SJonathan Lemon 1689773bda96SJonathan Lemon return sysfs_emit(buf, "%s\n", p); 1690773bda96SJonathan Lemon } 1691773bda96SJonathan Lemon 1692773bda96SJonathan Lemon static ssize_t 1693773bda96SJonathan Lemon clock_source_store(struct device *dev, struct device_attribute *attr, 1694773bda96SJonathan Lemon const char *buf, size_t count) 1695773bda96SJonathan Lemon { 1696773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1697773bda96SJonathan Lemon unsigned long flags; 1698773bda96SJonathan Lemon int val; 1699773bda96SJonathan Lemon 1700e1daf0ecSJonathan Lemon val = ptp_ocp_select_val_from_name(ptp_ocp_clock, buf); 1701773bda96SJonathan Lemon if (val < 0) 1702773bda96SJonathan Lemon return val; 1703773bda96SJonathan Lemon 1704773bda96SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1705773bda96SJonathan Lemon iowrite32(val, &bp->reg->select); 1706773bda96SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1707773bda96SJonathan Lemon 1708773bda96SJonathan Lemon return count; 1709773bda96SJonathan Lemon } 1710773bda96SJonathan Lemon static DEVICE_ATTR_RW(clock_source); 1711773bda96SJonathan Lemon 1712773bda96SJonathan Lemon static ssize_t 1713773bda96SJonathan Lemon available_clock_sources_show(struct device *dev, 1714773bda96SJonathan Lemon struct device_attribute *attr, char *buf) 1715773bda96SJonathan Lemon { 1716e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_clock, buf); 1717773bda96SJonathan Lemon } 1718773bda96SJonathan Lemon static DEVICE_ATTR_RO(available_clock_sources); 1719773bda96SJonathan Lemon 1720773bda96SJonathan Lemon static struct attribute *timecard_attrs[] = { 1721773bda96SJonathan Lemon &dev_attr_serialnum.attr, 1722ef0cfb34SJonathan Lemon &dev_attr_gnss_sync.attr, 1723773bda96SJonathan Lemon &dev_attr_clock_source.attr, 1724773bda96SJonathan Lemon &dev_attr_available_clock_sources.attr, 1725e1daf0ecSJonathan Lemon &dev_attr_sma1.attr, 1726e1daf0ecSJonathan Lemon &dev_attr_sma2.attr, 1727e1daf0ecSJonathan Lemon &dev_attr_sma3.attr, 1728e1daf0ecSJonathan Lemon &dev_attr_sma4.attr, 1729e1daf0ecSJonathan Lemon &dev_attr_available_sma_inputs.attr, 1730e1daf0ecSJonathan Lemon &dev_attr_available_sma_outputs.attr, 1731773bda96SJonathan Lemon NULL, 1732773bda96SJonathan Lemon }; 1733773bda96SJonathan Lemon ATTRIBUTE_GROUPS(timecard); 1734773bda96SJonathan Lemon 1735773bda96SJonathan Lemon static void 1736773bda96SJonathan Lemon ptp_ocp_dev_release(struct device *dev) 1737773bda96SJonathan Lemon { 1738773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1739773bda96SJonathan Lemon 1740773bda96SJonathan Lemon mutex_lock(&ptp_ocp_lock); 1741773bda96SJonathan Lemon idr_remove(&ptp_ocp_idr, bp->id); 1742773bda96SJonathan Lemon mutex_unlock(&ptp_ocp_lock); 1743773bda96SJonathan Lemon } 1744773bda96SJonathan Lemon 1745773bda96SJonathan Lemon static int 1746773bda96SJonathan Lemon ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev) 1747773bda96SJonathan Lemon { 1748773bda96SJonathan Lemon int err; 1749773bda96SJonathan Lemon 1750773bda96SJonathan Lemon mutex_lock(&ptp_ocp_lock); 1751773bda96SJonathan Lemon err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL); 1752773bda96SJonathan Lemon mutex_unlock(&ptp_ocp_lock); 1753773bda96SJonathan Lemon if (err < 0) { 1754773bda96SJonathan Lemon dev_err(&pdev->dev, "idr_alloc failed: %d\n", err); 1755773bda96SJonathan Lemon return err; 1756773bda96SJonathan Lemon } 1757773bda96SJonathan Lemon bp->id = err; 1758773bda96SJonathan Lemon 1759773bda96SJonathan Lemon bp->ptp_info = ptp_ocp_clock_info; 1760773bda96SJonathan Lemon spin_lock_init(&bp->lock); 1761ef0cfb34SJonathan Lemon bp->gnss_port = -1; 1762773bda96SJonathan Lemon bp->mac_port = -1; 1763773bda96SJonathan Lemon bp->pdev = pdev; 1764773bda96SJonathan Lemon 1765773bda96SJonathan Lemon device_initialize(&bp->dev); 1766773bda96SJonathan Lemon dev_set_name(&bp->dev, "ocp%d", bp->id); 1767773bda96SJonathan Lemon bp->dev.class = &timecard_class; 1768773bda96SJonathan Lemon bp->dev.parent = &pdev->dev; 1769773bda96SJonathan Lemon bp->dev.release = ptp_ocp_dev_release; 1770773bda96SJonathan Lemon dev_set_drvdata(&bp->dev, bp); 1771773bda96SJonathan Lemon 1772773bda96SJonathan Lemon err = device_add(&bp->dev); 1773773bda96SJonathan Lemon if (err) { 1774773bda96SJonathan Lemon dev_err(&bp->dev, "device add failed: %d\n", err); 1775773bda96SJonathan Lemon goto out; 1776773bda96SJonathan Lemon } 1777773bda96SJonathan Lemon 1778773bda96SJonathan Lemon pci_set_drvdata(pdev, bp); 1779773bda96SJonathan Lemon 1780773bda96SJonathan Lemon return 0; 1781773bda96SJonathan Lemon 1782773bda96SJonathan Lemon out: 1783773bda96SJonathan Lemon ptp_ocp_dev_release(&bp->dev); 1784d12f23faSJonathan Lemon put_device(&bp->dev); 1785773bda96SJonathan Lemon return err; 1786773bda96SJonathan Lemon } 1787773bda96SJonathan Lemon 1788773bda96SJonathan Lemon static void 1789773bda96SJonathan Lemon ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link) 1790773bda96SJonathan Lemon { 1791773bda96SJonathan Lemon struct device *dev = &bp->dev; 1792773bda96SJonathan Lemon 1793773bda96SJonathan Lemon if (sysfs_create_link(&dev->kobj, &child->kobj, link)) 1794773bda96SJonathan Lemon dev_err(dev, "%s symlink failed\n", link); 1795773bda96SJonathan Lemon } 1796773bda96SJonathan Lemon 1797773bda96SJonathan Lemon static void 1798773bda96SJonathan Lemon ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link) 1799773bda96SJonathan Lemon { 1800773bda96SJonathan Lemon struct device *dev, *child; 1801773bda96SJonathan Lemon 1802773bda96SJonathan Lemon dev = &bp->pdev->dev; 1803773bda96SJonathan Lemon 1804773bda96SJonathan Lemon child = device_find_child_by_name(dev, name); 1805773bda96SJonathan Lemon if (!child) { 1806773bda96SJonathan Lemon dev_err(dev, "Could not find device %s\n", name); 1807773bda96SJonathan Lemon return; 1808773bda96SJonathan Lemon } 1809773bda96SJonathan Lemon 1810773bda96SJonathan Lemon ptp_ocp_symlink(bp, child, link); 1811773bda96SJonathan Lemon put_device(child); 1812773bda96SJonathan Lemon } 1813773bda96SJonathan Lemon 1814773bda96SJonathan Lemon static int 1815773bda96SJonathan Lemon ptp_ocp_complete(struct ptp_ocp *bp) 1816773bda96SJonathan Lemon { 1817773bda96SJonathan Lemon struct pps_device *pps; 1818773bda96SJonathan Lemon char buf[32]; 1819773bda96SJonathan Lemon 1820ef0cfb34SJonathan Lemon if (bp->gnss_port != -1) { 1821ef0cfb34SJonathan Lemon sprintf(buf, "ttyS%d", bp->gnss_port); 1822ef0cfb34SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyGNSS"); 1823773bda96SJonathan Lemon } 1824773bda96SJonathan Lemon if (bp->mac_port != -1) { 1825773bda96SJonathan Lemon sprintf(buf, "ttyS%d", bp->mac_port); 1826773bda96SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyMAC"); 1827773bda96SJonathan Lemon } 1828773bda96SJonathan Lemon sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp)); 1829773bda96SJonathan Lemon ptp_ocp_link_child(bp, buf, "ptp"); 1830773bda96SJonathan Lemon 1831773bda96SJonathan Lemon pps = pps_lookup_dev(bp->ptp); 1832773bda96SJonathan Lemon if (pps) 1833773bda96SJonathan Lemon ptp_ocp_symlink(bp, pps->dev, "pps"); 1834773bda96SJonathan Lemon 1835773bda96SJonathan Lemon if (device_add_groups(&bp->dev, timecard_groups)) 1836773bda96SJonathan Lemon pr_err("device add groups failed\n"); 1837773bda96SJonathan Lemon 1838773bda96SJonathan Lemon return 0; 1839773bda96SJonathan Lemon } 1840773bda96SJonathan Lemon 1841773bda96SJonathan Lemon static void 1842773bda96SJonathan Lemon ptp_ocp_resource_summary(struct ptp_ocp *bp) 1843773bda96SJonathan Lemon { 1844773bda96SJonathan Lemon struct device *dev = &bp->pdev->dev; 1845773bda96SJonathan Lemon 1846773bda96SJonathan Lemon if (bp->image) { 1847773bda96SJonathan Lemon u32 ver = ioread32(&bp->image->version); 1848773bda96SJonathan Lemon 1849773bda96SJonathan Lemon dev_info(dev, "version %x\n", ver); 1850773bda96SJonathan Lemon if (ver & 0xffff) 1851773bda96SJonathan Lemon dev_info(dev, "regular image, version %d\n", 1852773bda96SJonathan Lemon ver & 0xffff); 1853773bda96SJonathan Lemon else 1854773bda96SJonathan Lemon dev_info(dev, "golden image, version %d\n", 1855773bda96SJonathan Lemon ver >> 16); 1856773bda96SJonathan Lemon } 1857ef0cfb34SJonathan Lemon if (bp->gnss_port != -1) 1858ef0cfb34SJonathan Lemon dev_info(dev, "GNSS @ /dev/ttyS%d 115200\n", bp->gnss_port); 1859773bda96SJonathan Lemon if (bp->mac_port != -1) 1860773bda96SJonathan Lemon dev_info(dev, "MAC @ /dev/ttyS%d 57600\n", bp->mac_port); 1861773bda96SJonathan Lemon } 1862773bda96SJonathan Lemon 1863773bda96SJonathan Lemon static void 1864773bda96SJonathan Lemon ptp_ocp_detach_sysfs(struct ptp_ocp *bp) 1865773bda96SJonathan Lemon { 1866773bda96SJonathan Lemon struct device *dev = &bp->dev; 1867773bda96SJonathan Lemon 1868ef0cfb34SJonathan Lemon sysfs_remove_link(&dev->kobj, "ttyGNSS"); 1869773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "ttyMAC"); 1870773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "ptp"); 1871773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "pps"); 1872773bda96SJonathan Lemon device_remove_groups(dev, timecard_groups); 1873773bda96SJonathan Lemon } 1874773bda96SJonathan Lemon 1875773bda96SJonathan Lemon static void 1876773bda96SJonathan Lemon ptp_ocp_detach(struct ptp_ocp *bp) 1877773bda96SJonathan Lemon { 1878773bda96SJonathan Lemon ptp_ocp_detach_sysfs(bp); 1879773bda96SJonathan Lemon if (timer_pending(&bp->watchdog)) 1880773bda96SJonathan Lemon del_timer_sync(&bp->watchdog); 1881773bda96SJonathan Lemon if (bp->ts0) 1882773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->ts0); 1883773bda96SJonathan Lemon if (bp->ts1) 1884773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->ts1); 1885dcf61469SJonathan Lemon if (bp->ts2) 1886dcf61469SJonathan Lemon ptp_ocp_unregister_ext(bp->ts2); 1887773bda96SJonathan Lemon if (bp->pps) 1888773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->pps); 1889ef0cfb34SJonathan Lemon if (bp->gnss_port != -1) 1890ef0cfb34SJonathan Lemon serial8250_unregister_port(bp->gnss_port); 1891773bda96SJonathan Lemon if (bp->mac_port != -1) 1892773bda96SJonathan Lemon serial8250_unregister_port(bp->mac_port); 1893773bda96SJonathan Lemon if (bp->spi_flash) 1894773bda96SJonathan Lemon platform_device_unregister(bp->spi_flash); 1895773bda96SJonathan Lemon if (bp->i2c_ctrl) 1896773bda96SJonathan Lemon platform_device_unregister(bp->i2c_ctrl); 1897773bda96SJonathan Lemon if (bp->i2c_clk) 1898773bda96SJonathan Lemon clk_hw_unregister_fixed_rate(bp->i2c_clk); 1899773bda96SJonathan Lemon if (bp->n_irqs) 1900773bda96SJonathan Lemon pci_free_irq_vectors(bp->pdev); 1901773bda96SJonathan Lemon if (bp->ptp) 1902773bda96SJonathan Lemon ptp_clock_unregister(bp->ptp); 1903773bda96SJonathan Lemon device_unregister(&bp->dev); 1904773bda96SJonathan Lemon } 1905773bda96SJonathan Lemon 1906a7e1abadSJonathan Lemon static int 1907a7e1abadSJonathan Lemon ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id) 1908a7e1abadSJonathan Lemon { 1909773bda96SJonathan Lemon struct devlink *devlink; 1910a7e1abadSJonathan Lemon struct ptp_ocp *bp; 1911a7e1abadSJonathan Lemon int err; 1912a7e1abadSJonathan Lemon 1913919d13a7SLeon Romanovsky devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp), &pdev->dev); 1914773bda96SJonathan Lemon if (!devlink) { 1915773bda96SJonathan Lemon dev_err(&pdev->dev, "devlink_alloc failed\n"); 1916a7e1abadSJonathan Lemon return -ENOMEM; 1917773bda96SJonathan Lemon } 1918773bda96SJonathan Lemon 1919919d13a7SLeon Romanovsky err = devlink_register(devlink); 1920773bda96SJonathan Lemon if (err) 1921773bda96SJonathan Lemon goto out_free; 1922a7e1abadSJonathan Lemon 1923a7e1abadSJonathan Lemon err = pci_enable_device(pdev); 1924a7e1abadSJonathan Lemon if (err) { 1925a7e1abadSJonathan Lemon dev_err(&pdev->dev, "pci_enable_device\n"); 1926773bda96SJonathan Lemon goto out_unregister; 1927a7e1abadSJonathan Lemon } 1928a7e1abadSJonathan Lemon 1929773bda96SJonathan Lemon bp = devlink_priv(devlink); 1930773bda96SJonathan Lemon err = ptp_ocp_device_init(bp, pdev); 1931773bda96SJonathan Lemon if (err) 1932d9fdbf13SJonathan Lemon goto out_disable; 1933a7e1abadSJonathan Lemon 1934773bda96SJonathan Lemon /* compat mode. 1935773bda96SJonathan Lemon * Older FPGA firmware only returns 2 irq's. 1936773bda96SJonathan Lemon * allow this - if not all of the IRQ's are returned, skip the 1937773bda96SJonathan Lemon * extra devices and just register the clock. 1938773bda96SJonathan Lemon */ 1939773bda96SJonathan Lemon err = pci_alloc_irq_vectors(pdev, 1, 10, PCI_IRQ_MSI | PCI_IRQ_MSIX); 1940773bda96SJonathan Lemon if (err < 0) { 1941773bda96SJonathan Lemon dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err); 1942773bda96SJonathan Lemon goto out; 1943a7e1abadSJonathan Lemon } 1944773bda96SJonathan Lemon bp->n_irqs = err; 1945773bda96SJonathan Lemon pci_set_master(pdev); 1946a7e1abadSJonathan Lemon 1947773bda96SJonathan Lemon err = ptp_ocp_register_resources(bp, id->driver_data); 1948a7e1abadSJonathan Lemon if (err) 1949a7e1abadSJonathan Lemon goto out; 1950a7e1abadSJonathan Lemon 1951a7e1abadSJonathan Lemon bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev); 1952a7e1abadSJonathan Lemon if (IS_ERR(bp->ptp)) { 1953a7e1abadSJonathan Lemon err = PTR_ERR(bp->ptp); 1954773bda96SJonathan Lemon dev_err(&pdev->dev, "ptp_clock_register: %d\n", err); 1955773bda96SJonathan Lemon bp->ptp = NULL; 1956a7e1abadSJonathan Lemon goto out; 1957a7e1abadSJonathan Lemon } 1958a7e1abadSJonathan Lemon 1959773bda96SJonathan Lemon err = ptp_ocp_complete(bp); 1960773bda96SJonathan Lemon if (err) 1961773bda96SJonathan Lemon goto out; 1962773bda96SJonathan Lemon 1963a7e1abadSJonathan Lemon ptp_ocp_info(bp); 1964773bda96SJonathan Lemon ptp_ocp_resource_summary(bp); 1965a7e1abadSJonathan Lemon 1966a7e1abadSJonathan Lemon return 0; 1967a7e1abadSJonathan Lemon 1968a7e1abadSJonathan Lemon out: 1969773bda96SJonathan Lemon ptp_ocp_detach(bp); 1970773bda96SJonathan Lemon pci_set_drvdata(pdev, NULL); 1971d9fdbf13SJonathan Lemon out_disable: 1972d9fdbf13SJonathan Lemon pci_disable_device(pdev); 1973773bda96SJonathan Lemon out_unregister: 1974919d13a7SLeon Romanovsky devlink_unregister(devlink); 1975a7e1abadSJonathan Lemon out_free: 1976773bda96SJonathan Lemon devlink_free(devlink); 1977a7e1abadSJonathan Lemon 1978a7e1abadSJonathan Lemon return err; 1979a7e1abadSJonathan Lemon } 1980a7e1abadSJonathan Lemon 1981a7e1abadSJonathan Lemon static void 1982a7e1abadSJonathan Lemon ptp_ocp_remove(struct pci_dev *pdev) 1983a7e1abadSJonathan Lemon { 1984a7e1abadSJonathan Lemon struct ptp_ocp *bp = pci_get_drvdata(pdev); 1985773bda96SJonathan Lemon struct devlink *devlink = priv_to_devlink(bp); 1986a7e1abadSJonathan Lemon 1987773bda96SJonathan Lemon ptp_ocp_detach(bp); 1988a7e1abadSJonathan Lemon pci_set_drvdata(pdev, NULL); 1989d9fdbf13SJonathan Lemon pci_disable_device(pdev); 1990773bda96SJonathan Lemon 1991919d13a7SLeon Romanovsky devlink_unregister(devlink); 1992773bda96SJonathan Lemon devlink_free(devlink); 1993a7e1abadSJonathan Lemon } 1994a7e1abadSJonathan Lemon 1995a7e1abadSJonathan Lemon static struct pci_driver ptp_ocp_driver = { 1996a7e1abadSJonathan Lemon .name = KBUILD_MODNAME, 1997a7e1abadSJonathan Lemon .id_table = ptp_ocp_pcidev_id, 1998a7e1abadSJonathan Lemon .probe = ptp_ocp_probe, 1999a7e1abadSJonathan Lemon .remove = ptp_ocp_remove, 2000a7e1abadSJonathan Lemon }; 2001a7e1abadSJonathan Lemon 2002773bda96SJonathan Lemon static int 2003773bda96SJonathan Lemon ptp_ocp_i2c_notifier_call(struct notifier_block *nb, 2004773bda96SJonathan Lemon unsigned long action, void *data) 2005773bda96SJonathan Lemon { 2006773bda96SJonathan Lemon struct device *dev, *child = data; 2007773bda96SJonathan Lemon struct ptp_ocp *bp; 2008773bda96SJonathan Lemon bool add; 2009773bda96SJonathan Lemon 2010773bda96SJonathan Lemon switch (action) { 2011773bda96SJonathan Lemon case BUS_NOTIFY_ADD_DEVICE: 2012773bda96SJonathan Lemon case BUS_NOTIFY_DEL_DEVICE: 2013773bda96SJonathan Lemon add = action == BUS_NOTIFY_ADD_DEVICE; 2014773bda96SJonathan Lemon break; 2015773bda96SJonathan Lemon default: 2016773bda96SJonathan Lemon return 0; 2017773bda96SJonathan Lemon } 2018773bda96SJonathan Lemon 2019773bda96SJonathan Lemon if (!i2c_verify_adapter(child)) 2020773bda96SJonathan Lemon return 0; 2021773bda96SJonathan Lemon 2022773bda96SJonathan Lemon dev = child; 2023773bda96SJonathan Lemon while ((dev = dev->parent)) 2024773bda96SJonathan Lemon if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME)) 2025773bda96SJonathan Lemon goto found; 2026773bda96SJonathan Lemon return 0; 2027773bda96SJonathan Lemon 2028773bda96SJonathan Lemon found: 2029773bda96SJonathan Lemon bp = dev_get_drvdata(dev); 2030773bda96SJonathan Lemon if (add) 2031773bda96SJonathan Lemon ptp_ocp_symlink(bp, child, "i2c"); 2032773bda96SJonathan Lemon else 2033773bda96SJonathan Lemon sysfs_remove_link(&bp->dev.kobj, "i2c"); 2034773bda96SJonathan Lemon 2035773bda96SJonathan Lemon return 0; 2036773bda96SJonathan Lemon } 2037773bda96SJonathan Lemon 2038773bda96SJonathan Lemon static struct notifier_block ptp_ocp_i2c_notifier = { 2039773bda96SJonathan Lemon .notifier_call = ptp_ocp_i2c_notifier_call, 2040773bda96SJonathan Lemon }; 2041773bda96SJonathan Lemon 2042a7e1abadSJonathan Lemon static int __init 2043a7e1abadSJonathan Lemon ptp_ocp_init(void) 2044a7e1abadSJonathan Lemon { 2045773bda96SJonathan Lemon const char *what; 2046a7e1abadSJonathan Lemon int err; 2047a7e1abadSJonathan Lemon 2048773bda96SJonathan Lemon what = "timecard class"; 2049773bda96SJonathan Lemon err = class_register(&timecard_class); 2050773bda96SJonathan Lemon if (err) 2051773bda96SJonathan Lemon goto out; 2052773bda96SJonathan Lemon 2053773bda96SJonathan Lemon what = "i2c notifier"; 2054773bda96SJonathan Lemon err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2055773bda96SJonathan Lemon if (err) 2056773bda96SJonathan Lemon goto out_notifier; 2057773bda96SJonathan Lemon 2058773bda96SJonathan Lemon what = "ptp_ocp driver"; 2059a7e1abadSJonathan Lemon err = pci_register_driver(&ptp_ocp_driver); 2060773bda96SJonathan Lemon if (err) 2061773bda96SJonathan Lemon goto out_register; 2062773bda96SJonathan Lemon 2063773bda96SJonathan Lemon return 0; 2064773bda96SJonathan Lemon 2065773bda96SJonathan Lemon out_register: 2066773bda96SJonathan Lemon bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2067773bda96SJonathan Lemon out_notifier: 2068773bda96SJonathan Lemon class_unregister(&timecard_class); 2069773bda96SJonathan Lemon out: 2070773bda96SJonathan Lemon pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err); 2071a7e1abadSJonathan Lemon return err; 2072a7e1abadSJonathan Lemon } 2073a7e1abadSJonathan Lemon 2074a7e1abadSJonathan Lemon static void __exit 2075a7e1abadSJonathan Lemon ptp_ocp_fini(void) 2076a7e1abadSJonathan Lemon { 2077773bda96SJonathan Lemon bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2078a7e1abadSJonathan Lemon pci_unregister_driver(&ptp_ocp_driver); 2079773bda96SJonathan Lemon class_unregister(&timecard_class); 2080a7e1abadSJonathan Lemon } 2081a7e1abadSJonathan Lemon 2082a7e1abadSJonathan Lemon module_init(ptp_ocp_init); 2083a7e1abadSJonathan Lemon module_exit(ptp_ocp_fini); 2084a7e1abadSJonathan Lemon 2085a7e1abadSJonathan Lemon MODULE_DESCRIPTION("OpenCompute TimeCard driver"); 2086a7e1abadSJonathan Lemon MODULE_LICENSE("GPL v2"); 2087