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 127*e1daf0ecSJonathan Lemon struct gpio_reg { 128*e1daf0ecSJonathan Lemon u32 gpio1; 129*e1daf0ecSJonathan Lemon u32 __pad0; 130*e1daf0ecSJonathan Lemon u32 gpio2; 131*e1daf0ecSJonathan Lemon u32 __pad1; 132*e1daf0ecSJonathan Lemon }; 133*e1daf0ecSJonathan Lemon 134773bda96SJonathan Lemon struct ptp_ocp_flash_info { 135773bda96SJonathan Lemon const char *name; 136773bda96SJonathan Lemon int pci_offset; 137773bda96SJonathan Lemon int data_size; 138773bda96SJonathan Lemon void *data; 139773bda96SJonathan Lemon }; 140773bda96SJonathan Lemon 1411618df6aSJonathan Lemon struct ptp_ocp_i2c_info { 1421618df6aSJonathan Lemon const char *name; 1431618df6aSJonathan Lemon unsigned long fixed_rate; 1441618df6aSJonathan Lemon size_t data_size; 1451618df6aSJonathan Lemon void *data; 1461618df6aSJonathan Lemon }; 1471618df6aSJonathan Lemon 148773bda96SJonathan Lemon struct ptp_ocp_ext_info { 149773bda96SJonathan Lemon int index; 150773bda96SJonathan Lemon irqreturn_t (*irq_fcn)(int irq, void *priv); 151773bda96SJonathan Lemon int (*enable)(void *priv, bool enable); 152773bda96SJonathan Lemon }; 153773bda96SJonathan Lemon 154773bda96SJonathan Lemon struct ptp_ocp_ext_src { 155773bda96SJonathan Lemon void __iomem *mem; 156773bda96SJonathan Lemon struct ptp_ocp *bp; 157773bda96SJonathan Lemon struct ptp_ocp_ext_info *info; 158773bda96SJonathan Lemon int irq_vec; 159773bda96SJonathan Lemon }; 160773bda96SJonathan Lemon 161a7e1abadSJonathan Lemon struct ptp_ocp { 162a7e1abadSJonathan Lemon struct pci_dev *pdev; 163773bda96SJonathan Lemon struct device dev; 164a7e1abadSJonathan Lemon spinlock_t lock; 165a7e1abadSJonathan Lemon struct ocp_reg __iomem *reg; 166a7e1abadSJonathan Lemon struct tod_reg __iomem *tod; 1670d43d4f2SJonathan Lemon struct pps_reg __iomem *pps_to_ext; 1680d43d4f2SJonathan Lemon struct pps_reg __iomem *pps_to_clk; 169*e1daf0ecSJonathan Lemon struct gpio_reg __iomem *sma; 170773bda96SJonathan Lemon struct ptp_ocp_ext_src *pps; 171773bda96SJonathan Lemon struct ptp_ocp_ext_src *ts0; 172773bda96SJonathan Lemon struct ptp_ocp_ext_src *ts1; 173dcf61469SJonathan Lemon struct ptp_ocp_ext_src *ts2; 174773bda96SJonathan Lemon struct img_reg __iomem *image; 175a7e1abadSJonathan Lemon struct ptp_clock *ptp; 176a7e1abadSJonathan Lemon struct ptp_clock_info ptp_info; 177773bda96SJonathan Lemon struct platform_device *i2c_ctrl; 178773bda96SJonathan Lemon struct platform_device *spi_flash; 179773bda96SJonathan Lemon struct clk_hw *i2c_clk; 180773bda96SJonathan Lemon struct timer_list watchdog; 181ef0cfb34SJonathan Lemon time64_t gnss_lost; 182773bda96SJonathan Lemon int id; 183773bda96SJonathan Lemon int n_irqs; 184ef0cfb34SJonathan Lemon int gnss_port; 185773bda96SJonathan Lemon int mac_port; /* miniature atomic clock */ 186773bda96SJonathan Lemon u8 serial[6]; 187773bda96SJonathan Lemon int flash_start; 188773bda96SJonathan Lemon bool has_serial; 189a7e1abadSJonathan Lemon }; 190a7e1abadSJonathan Lemon 191773bda96SJonathan Lemon struct ocp_resource { 192773bda96SJonathan Lemon unsigned long offset; 193773bda96SJonathan Lemon int size; 194773bda96SJonathan Lemon int irq_vec; 195773bda96SJonathan Lemon int (*setup)(struct ptp_ocp *bp, struct ocp_resource *r); 196773bda96SJonathan Lemon void *extra; 197773bda96SJonathan Lemon unsigned long bp_offset; 19856ec4403SJonathan Lemon const char * const name; 199773bda96SJonathan Lemon }; 200773bda96SJonathan Lemon 201773bda96SJonathan Lemon static int ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r); 202773bda96SJonathan Lemon static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r); 203773bda96SJonathan Lemon static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r); 204773bda96SJonathan Lemon static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r); 205773bda96SJonathan Lemon static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r); 206773bda96SJonathan Lemon static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r); 207773bda96SJonathan Lemon static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv); 208773bda96SJonathan Lemon static int ptp_ocp_ts_enable(void *priv, bool enable); 209773bda96SJonathan Lemon 210773bda96SJonathan Lemon #define bp_assign_entry(bp, res, val) ({ \ 211773bda96SJonathan Lemon uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset; \ 212773bda96SJonathan Lemon *(typeof(val) *)addr = val; \ 213773bda96SJonathan Lemon }) 214773bda96SJonathan Lemon 215773bda96SJonathan Lemon #define OCP_RES_LOCATION(member) \ 21656ec4403SJonathan Lemon .name = #member, .bp_offset = offsetof(struct ptp_ocp, member) 217773bda96SJonathan Lemon 218773bda96SJonathan Lemon #define OCP_MEM_RESOURCE(member) \ 219773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_mem 220773bda96SJonathan Lemon 221773bda96SJonathan Lemon #define OCP_SERIAL_RESOURCE(member) \ 222773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_serial 223773bda96SJonathan Lemon 224773bda96SJonathan Lemon #define OCP_I2C_RESOURCE(member) \ 225773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_i2c 226773bda96SJonathan Lemon 227773bda96SJonathan Lemon #define OCP_SPI_RESOURCE(member) \ 228773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_spi 229773bda96SJonathan Lemon 230773bda96SJonathan Lemon #define OCP_EXT_RESOURCE(member) \ 231773bda96SJonathan Lemon OCP_RES_LOCATION(member), .setup = ptp_ocp_register_ext 232773bda96SJonathan Lemon 233773bda96SJonathan Lemon /* This is the MSI vector mapping used. 234773bda96SJonathan Lemon * 0: N/C 235773bda96SJonathan Lemon * 1: TS0 236773bda96SJonathan Lemon * 2: TS1 237773bda96SJonathan Lemon * 3: GPS 238773bda96SJonathan Lemon * 4: GPS2 (n/c) 239773bda96SJonathan Lemon * 5: MAC 240dcf61469SJonathan Lemon * 6: TS2 2411447149dSJonathan Lemon * 7: I2C controller 242773bda96SJonathan Lemon * 8: HWICAP 243773bda96SJonathan Lemon * 9: SPI Flash 244773bda96SJonathan Lemon */ 245773bda96SJonathan Lemon 246773bda96SJonathan Lemon static struct ocp_resource ocp_fb_resource[] = { 247773bda96SJonathan Lemon { 248773bda96SJonathan Lemon OCP_MEM_RESOURCE(reg), 249773bda96SJonathan Lemon .offset = 0x01000000, .size = 0x10000, 250773bda96SJonathan Lemon }, 251773bda96SJonathan Lemon { 252773bda96SJonathan Lemon OCP_EXT_RESOURCE(ts0), 253773bda96SJonathan Lemon .offset = 0x01010000, .size = 0x10000, .irq_vec = 1, 254773bda96SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 25556ec4403SJonathan Lemon .index = 0, 256773bda96SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 257773bda96SJonathan Lemon .enable = ptp_ocp_ts_enable, 258773bda96SJonathan Lemon }, 259773bda96SJonathan Lemon }, 260773bda96SJonathan Lemon { 261773bda96SJonathan Lemon OCP_EXT_RESOURCE(ts1), 262773bda96SJonathan Lemon .offset = 0x01020000, .size = 0x10000, .irq_vec = 2, 263773bda96SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 26456ec4403SJonathan Lemon .index = 1, 265773bda96SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 266773bda96SJonathan Lemon .enable = ptp_ocp_ts_enable, 267773bda96SJonathan Lemon }, 268773bda96SJonathan Lemon }, 269773bda96SJonathan Lemon { 270dcf61469SJonathan Lemon OCP_EXT_RESOURCE(ts2), 271dcf61469SJonathan Lemon .offset = 0x01060000, .size = 0x10000, .irq_vec = 6, 272dcf61469SJonathan Lemon .extra = &(struct ptp_ocp_ext_info) { 273dcf61469SJonathan Lemon .index = 2, 274dcf61469SJonathan Lemon .irq_fcn = ptp_ocp_ts_irq, 275dcf61469SJonathan Lemon .enable = ptp_ocp_ts_enable, 276dcf61469SJonathan Lemon }, 277dcf61469SJonathan Lemon }, 278dcf61469SJonathan Lemon { 2790d43d4f2SJonathan Lemon OCP_MEM_RESOURCE(pps_to_ext), 2800d43d4f2SJonathan Lemon .offset = 0x01030000, .size = 0x10000, 2810d43d4f2SJonathan Lemon }, 2820d43d4f2SJonathan Lemon { 2830d43d4f2SJonathan Lemon OCP_MEM_RESOURCE(pps_to_clk), 284773bda96SJonathan Lemon .offset = 0x01040000, .size = 0x10000, 285773bda96SJonathan Lemon }, 286773bda96SJonathan Lemon { 287773bda96SJonathan Lemon OCP_MEM_RESOURCE(tod), 288773bda96SJonathan Lemon .offset = 0x01050000, .size = 0x10000, 289773bda96SJonathan Lemon }, 290773bda96SJonathan Lemon { 291773bda96SJonathan Lemon OCP_MEM_RESOURCE(image), 292773bda96SJonathan Lemon .offset = 0x00020000, .size = 0x1000, 293773bda96SJonathan Lemon }, 294773bda96SJonathan Lemon { 295*e1daf0ecSJonathan Lemon OCP_MEM_RESOURCE(sma), 296*e1daf0ecSJonathan Lemon .offset = 0x00140000, .size = 0x1000, 297*e1daf0ecSJonathan Lemon }, 298*e1daf0ecSJonathan Lemon { 299773bda96SJonathan Lemon OCP_I2C_RESOURCE(i2c_ctrl), 300773bda96SJonathan Lemon .offset = 0x00150000, .size = 0x10000, .irq_vec = 7, 3011618df6aSJonathan Lemon .extra = &(struct ptp_ocp_i2c_info) { 3021618df6aSJonathan Lemon .name = "xiic-i2c", 3031618df6aSJonathan Lemon .fixed_rate = 50000000, 3041618df6aSJonathan Lemon }, 305773bda96SJonathan Lemon }, 306773bda96SJonathan Lemon { 307ef0cfb34SJonathan Lemon OCP_SERIAL_RESOURCE(gnss_port), 308773bda96SJonathan Lemon .offset = 0x00160000 + 0x1000, .irq_vec = 3, 309773bda96SJonathan Lemon }, 310773bda96SJonathan Lemon { 311773bda96SJonathan Lemon OCP_SERIAL_RESOURCE(mac_port), 312773bda96SJonathan Lemon .offset = 0x00180000 + 0x1000, .irq_vec = 5, 313773bda96SJonathan Lemon }, 314773bda96SJonathan Lemon { 315773bda96SJonathan Lemon OCP_SPI_RESOURCE(spi_flash), 316773bda96SJonathan Lemon .offset = 0x00310000, .size = 0x10000, .irq_vec = 9, 317773bda96SJonathan Lemon .extra = &(struct ptp_ocp_flash_info) { 318773bda96SJonathan Lemon .name = "xilinx_spi", .pci_offset = 0, 319773bda96SJonathan Lemon .data_size = sizeof(struct xspi_platform_data), 320773bda96SJonathan Lemon .data = &(struct xspi_platform_data) { 321773bda96SJonathan Lemon .num_chipselect = 1, 322773bda96SJonathan Lemon .bits_per_word = 8, 323773bda96SJonathan Lemon .num_devices = 1, 324773bda96SJonathan Lemon .devices = &(struct spi_board_info) { 325773bda96SJonathan Lemon .modalias = "spi-nor", 326773bda96SJonathan Lemon }, 327773bda96SJonathan Lemon }, 328773bda96SJonathan Lemon }, 329773bda96SJonathan Lemon }, 330773bda96SJonathan Lemon { 331773bda96SJonathan Lemon .setup = ptp_ocp_fb_board_init, 332773bda96SJonathan Lemon }, 333773bda96SJonathan Lemon { } 334773bda96SJonathan Lemon }; 335773bda96SJonathan Lemon 336773bda96SJonathan Lemon static const struct pci_device_id ptp_ocp_pcidev_id[] = { 337773bda96SJonathan Lemon { PCI_DEVICE_DATA(FACEBOOK, TIMECARD, &ocp_fb_resource) }, 338773bda96SJonathan Lemon { 0 } 339773bda96SJonathan Lemon }; 340773bda96SJonathan Lemon MODULE_DEVICE_TABLE(pci, ptp_ocp_pcidev_id); 341773bda96SJonathan Lemon 342773bda96SJonathan Lemon static DEFINE_MUTEX(ptp_ocp_lock); 343773bda96SJonathan Lemon static DEFINE_IDR(ptp_ocp_idr); 344773bda96SJonathan Lemon 345*e1daf0ecSJonathan Lemon struct ocp_selector { 346773bda96SJonathan Lemon const char *name; 347773bda96SJonathan Lemon int value; 348*e1daf0ecSJonathan Lemon }; 349*e1daf0ecSJonathan Lemon 350*e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_clock[] = { 351773bda96SJonathan Lemon { .name = "NONE", .value = 0 }, 352773bda96SJonathan Lemon { .name = "TOD", .value = 1 }, 353773bda96SJonathan Lemon { .name = "IRIG", .value = 2 }, 354773bda96SJonathan Lemon { .name = "PPS", .value = 3 }, 355773bda96SJonathan Lemon { .name = "PTP", .value = 4 }, 356773bda96SJonathan Lemon { .name = "RTC", .value = 5 }, 357773bda96SJonathan Lemon { .name = "DCF", .value = 6 }, 358773bda96SJonathan Lemon { .name = "REGS", .value = 0xfe }, 359773bda96SJonathan Lemon { .name = "EXT", .value = 0xff }, 360*e1daf0ecSJonathan Lemon { } 361*e1daf0ecSJonathan Lemon }; 362*e1daf0ecSJonathan Lemon 363*e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_sma_in[] = { 364*e1daf0ecSJonathan Lemon { .name = "10Mhz", .value = 0x00 }, 365*e1daf0ecSJonathan Lemon { .name = "PPS1", .value = 0x01 }, 366*e1daf0ecSJonathan Lemon { .name = "PPS2", .value = 0x02 }, 367*e1daf0ecSJonathan Lemon { .name = "TS1", .value = 0x04 }, 368*e1daf0ecSJonathan Lemon { .name = "TS2", .value = 0x08 }, 369*e1daf0ecSJonathan Lemon { } 370*e1daf0ecSJonathan Lemon }; 371*e1daf0ecSJonathan Lemon 372*e1daf0ecSJonathan Lemon static struct ocp_selector ptp_ocp_sma_out[] = { 373*e1daf0ecSJonathan Lemon { .name = "10Mhz", .value = 0x00 }, 374*e1daf0ecSJonathan Lemon { .name = "PHC", .value = 0x01 }, 375*e1daf0ecSJonathan Lemon { .name = "MAC", .value = 0x02 }, 376*e1daf0ecSJonathan Lemon { .name = "GNSS", .value = 0x04 }, 377*e1daf0ecSJonathan Lemon { .name = "GNSS2", .value = 0x08 }, 378*e1daf0ecSJonathan Lemon { } 379773bda96SJonathan Lemon }; 380773bda96SJonathan Lemon 381773bda96SJonathan Lemon static const char * 382*e1daf0ecSJonathan Lemon ptp_ocp_select_name_from_val(struct ocp_selector *tbl, int val) 383773bda96SJonathan Lemon { 384773bda96SJonathan Lemon int i; 385773bda96SJonathan Lemon 386*e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) 387*e1daf0ecSJonathan Lemon if (tbl[i].value == val) 388*e1daf0ecSJonathan Lemon return tbl[i].name; 389773bda96SJonathan Lemon return NULL; 390773bda96SJonathan Lemon } 391773bda96SJonathan Lemon 392773bda96SJonathan Lemon static int 393*e1daf0ecSJonathan Lemon ptp_ocp_select_val_from_name(struct ocp_selector *tbl, const char *name) 394773bda96SJonathan Lemon { 395*e1daf0ecSJonathan Lemon const char *select; 396773bda96SJonathan Lemon int i; 397773bda96SJonathan Lemon 398*e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) { 399*e1daf0ecSJonathan Lemon select = tbl[i].name; 400*e1daf0ecSJonathan Lemon if (!strncasecmp(name, select, strlen(select))) 401*e1daf0ecSJonathan Lemon return tbl[i].value; 402773bda96SJonathan Lemon } 403773bda96SJonathan Lemon return -EINVAL; 404773bda96SJonathan Lemon } 405773bda96SJonathan Lemon 406*e1daf0ecSJonathan Lemon static ssize_t 407*e1daf0ecSJonathan Lemon ptp_ocp_select_table_show(struct ocp_selector *tbl, char *buf) 408*e1daf0ecSJonathan Lemon { 409*e1daf0ecSJonathan Lemon ssize_t count; 410*e1daf0ecSJonathan Lemon int i; 411*e1daf0ecSJonathan Lemon 412*e1daf0ecSJonathan Lemon count = 0; 413*e1daf0ecSJonathan Lemon for (i = 0; tbl[i].name; i++) 414*e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", tbl[i].name); 415*e1daf0ecSJonathan Lemon if (count) 416*e1daf0ecSJonathan Lemon count--; 417*e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "\n"); 418*e1daf0ecSJonathan Lemon return count; 419*e1daf0ecSJonathan Lemon } 420*e1daf0ecSJonathan Lemon 421a7e1abadSJonathan Lemon static int 422a7e1abadSJonathan Lemon __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts, 423a7e1abadSJonathan Lemon struct ptp_system_timestamp *sts) 424a7e1abadSJonathan Lemon { 425a7e1abadSJonathan Lemon u32 ctrl, time_sec, time_ns; 426a7e1abadSJonathan Lemon int i; 427a7e1abadSJonathan Lemon 428a7e1abadSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 429a7e1abadSJonathan Lemon ctrl |= OCP_CTRL_READ_TIME_REQ; 430a7e1abadSJonathan Lemon 431a7e1abadSJonathan Lemon ptp_read_system_prets(sts); 432a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 433a7e1abadSJonathan Lemon 434a7e1abadSJonathan Lemon for (i = 0; i < 100; i++) { 435a7e1abadSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 436a7e1abadSJonathan Lemon if (ctrl & OCP_CTRL_READ_TIME_DONE) 437a7e1abadSJonathan Lemon break; 438a7e1abadSJonathan Lemon } 439a7e1abadSJonathan Lemon ptp_read_system_postts(sts); 440a7e1abadSJonathan Lemon 441a7e1abadSJonathan Lemon time_ns = ioread32(&bp->reg->time_ns); 442a7e1abadSJonathan Lemon time_sec = ioread32(&bp->reg->time_sec); 443a7e1abadSJonathan Lemon 444a7e1abadSJonathan Lemon ts->tv_sec = time_sec; 445a7e1abadSJonathan Lemon ts->tv_nsec = time_ns; 446a7e1abadSJonathan Lemon 447a7e1abadSJonathan Lemon return ctrl & OCP_CTRL_READ_TIME_DONE ? 0 : -ETIMEDOUT; 448a7e1abadSJonathan Lemon } 449a7e1abadSJonathan Lemon 450a7e1abadSJonathan Lemon static int 451a7e1abadSJonathan Lemon ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts, 452a7e1abadSJonathan Lemon struct ptp_system_timestamp *sts) 453a7e1abadSJonathan Lemon { 454a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 455a7e1abadSJonathan Lemon unsigned long flags; 456a7e1abadSJonathan Lemon int err; 457a7e1abadSJonathan Lemon 458a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 459a7e1abadSJonathan Lemon err = __ptp_ocp_gettime_locked(bp, ts, sts); 460a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 461a7e1abadSJonathan Lemon 462a7e1abadSJonathan Lemon return err; 463a7e1abadSJonathan Lemon } 464a7e1abadSJonathan Lemon 465a7e1abadSJonathan Lemon static void 466a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts) 467a7e1abadSJonathan Lemon { 468a7e1abadSJonathan Lemon u32 ctrl, time_sec, time_ns; 469a7e1abadSJonathan Lemon u32 select; 470a7e1abadSJonathan Lemon 471a7e1abadSJonathan Lemon time_ns = ts->tv_nsec; 472a7e1abadSJonathan Lemon time_sec = ts->tv_sec; 473a7e1abadSJonathan Lemon 474a7e1abadSJonathan Lemon select = ioread32(&bp->reg->select); 475a7e1abadSJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 476a7e1abadSJonathan Lemon 477a7e1abadSJonathan Lemon iowrite32(time_ns, &bp->reg->adjust_ns); 478a7e1abadSJonathan Lemon iowrite32(time_sec, &bp->reg->adjust_sec); 479a7e1abadSJonathan Lemon 480a7e1abadSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 481a7e1abadSJonathan Lemon ctrl |= OCP_CTRL_ADJUST_TIME; 482a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 483a7e1abadSJonathan Lemon 484a7e1abadSJonathan Lemon /* restore clock selection */ 485a7e1abadSJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 486a7e1abadSJonathan Lemon } 487a7e1abadSJonathan Lemon 488a7e1abadSJonathan Lemon static int 489a7e1abadSJonathan Lemon ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts) 490a7e1abadSJonathan Lemon { 491a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 492a7e1abadSJonathan Lemon unsigned long flags; 493a7e1abadSJonathan Lemon 494a7e1abadSJonathan Lemon if (ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC) 495a7e1abadSJonathan Lemon return 0; 496a7e1abadSJonathan Lemon 497a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 498a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(bp, ts); 499a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 500a7e1abadSJonathan Lemon 501a7e1abadSJonathan Lemon return 0; 502a7e1abadSJonathan Lemon } 503a7e1abadSJonathan Lemon 504a7e1abadSJonathan Lemon static int 505a7e1abadSJonathan Lemon ptp_ocp_adjtime(struct ptp_clock_info *ptp_info, s64 delta_ns) 506a7e1abadSJonathan Lemon { 507a7e1abadSJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 508a7e1abadSJonathan Lemon struct timespec64 ts; 509a7e1abadSJonathan Lemon unsigned long flags; 510a7e1abadSJonathan Lemon int err; 511a7e1abadSJonathan Lemon 512a7e1abadSJonathan Lemon if (ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC) 513a7e1abadSJonathan Lemon return 0; 514a7e1abadSJonathan Lemon 515a7e1abadSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 516a7e1abadSJonathan Lemon err = __ptp_ocp_gettime_locked(bp, &ts, NULL); 517a7e1abadSJonathan Lemon if (likely(!err)) { 518a7e1abadSJonathan Lemon timespec64_add_ns(&ts, delta_ns); 519a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(bp, &ts); 520a7e1abadSJonathan Lemon } 521a7e1abadSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 522a7e1abadSJonathan Lemon 523a7e1abadSJonathan Lemon return err; 524a7e1abadSJonathan Lemon } 525a7e1abadSJonathan Lemon 526a7e1abadSJonathan Lemon static int 527a7e1abadSJonathan Lemon ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm) 528a7e1abadSJonathan Lemon { 529a7e1abadSJonathan Lemon if (scaled_ppm == 0) 530a7e1abadSJonathan Lemon return 0; 531a7e1abadSJonathan Lemon 532a7e1abadSJonathan Lemon return -EOPNOTSUPP; 533a7e1abadSJonathan Lemon } 534a7e1abadSJonathan Lemon 535773bda96SJonathan Lemon static int 536773bda96SJonathan Lemon ptp_ocp_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns) 537773bda96SJonathan Lemon { 538773bda96SJonathan Lemon return -EOPNOTSUPP; 539773bda96SJonathan Lemon } 540773bda96SJonathan Lemon 541773bda96SJonathan Lemon static int 542773bda96SJonathan Lemon ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq, 543773bda96SJonathan Lemon int on) 544773bda96SJonathan Lemon { 545773bda96SJonathan Lemon struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 546773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = NULL; 547773bda96SJonathan Lemon int err; 548773bda96SJonathan Lemon 549773bda96SJonathan Lemon switch (rq->type) { 550773bda96SJonathan Lemon case PTP_CLK_REQ_EXTTS: 551773bda96SJonathan Lemon switch (rq->extts.index) { 552773bda96SJonathan Lemon case 0: 553773bda96SJonathan Lemon ext = bp->ts0; 554773bda96SJonathan Lemon break; 555773bda96SJonathan Lemon case 1: 556773bda96SJonathan Lemon ext = bp->ts1; 557773bda96SJonathan Lemon break; 558dcf61469SJonathan Lemon case 2: 559dcf61469SJonathan Lemon ext = bp->ts2; 560dcf61469SJonathan Lemon break; 561773bda96SJonathan Lemon } 562773bda96SJonathan Lemon break; 563773bda96SJonathan Lemon case PTP_CLK_REQ_PPS: 564773bda96SJonathan Lemon ext = bp->pps; 565773bda96SJonathan Lemon break; 566773bda96SJonathan Lemon default: 567773bda96SJonathan Lemon return -EOPNOTSUPP; 568773bda96SJonathan Lemon } 569773bda96SJonathan Lemon 570773bda96SJonathan Lemon err = -ENXIO; 571773bda96SJonathan Lemon if (ext) 572773bda96SJonathan Lemon err = ext->info->enable(ext, on); 573773bda96SJonathan Lemon 574773bda96SJonathan Lemon return err; 575773bda96SJonathan Lemon } 576773bda96SJonathan Lemon 577a7e1abadSJonathan Lemon static const struct ptp_clock_info ptp_ocp_clock_info = { 578a7e1abadSJonathan Lemon .owner = THIS_MODULE, 579a7e1abadSJonathan Lemon .name = KBUILD_MODNAME, 580a7e1abadSJonathan Lemon .max_adj = 100000000, 581a7e1abadSJonathan Lemon .gettimex64 = ptp_ocp_gettimex, 582a7e1abadSJonathan Lemon .settime64 = ptp_ocp_settime, 583a7e1abadSJonathan Lemon .adjtime = ptp_ocp_adjtime, 584a7e1abadSJonathan Lemon .adjfine = ptp_ocp_null_adjfine, 585773bda96SJonathan Lemon .adjphase = ptp_ocp_adjphase, 586773bda96SJonathan Lemon .enable = ptp_ocp_enable, 587773bda96SJonathan Lemon .pps = true, 588dcf61469SJonathan Lemon .n_ext_ts = 3, 589a7e1abadSJonathan Lemon }; 590a7e1abadSJonathan Lemon 591773bda96SJonathan Lemon static void 592773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp) 593773bda96SJonathan Lemon { 594773bda96SJonathan Lemon u32 ctrl, select; 595773bda96SJonathan Lemon 596773bda96SJonathan Lemon select = ioread32(&bp->reg->select); 597773bda96SJonathan Lemon iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 598773bda96SJonathan Lemon 599773bda96SJonathan Lemon iowrite32(0, &bp->reg->drift_ns); 600773bda96SJonathan Lemon 601773bda96SJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 602773bda96SJonathan Lemon ctrl |= OCP_CTRL_ADJUST_DRIFT; 603773bda96SJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 604773bda96SJonathan Lemon 605773bda96SJonathan Lemon /* restore clock selection */ 606773bda96SJonathan Lemon iowrite32(select >> 16, &bp->reg->select); 607773bda96SJonathan Lemon } 608773bda96SJonathan Lemon 609773bda96SJonathan Lemon static void 610773bda96SJonathan Lemon ptp_ocp_watchdog(struct timer_list *t) 611773bda96SJonathan Lemon { 612773bda96SJonathan Lemon struct ptp_ocp *bp = from_timer(bp, t, watchdog); 613773bda96SJonathan Lemon unsigned long flags; 614773bda96SJonathan Lemon u32 status; 615773bda96SJonathan Lemon 6160d43d4f2SJonathan Lemon status = ioread32(&bp->pps_to_clk->status); 617773bda96SJonathan Lemon 618773bda96SJonathan Lemon if (status & PPS_STATUS_SUPERV_ERR) { 6190d43d4f2SJonathan Lemon iowrite32(status, &bp->pps_to_clk->status); 620ef0cfb34SJonathan Lemon if (!bp->gnss_lost) { 621773bda96SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 622773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(bp); 623773bda96SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 624ef0cfb34SJonathan Lemon bp->gnss_lost = ktime_get_real_seconds(); 625773bda96SJonathan Lemon } 626773bda96SJonathan Lemon 627ef0cfb34SJonathan Lemon } else if (bp->gnss_lost) { 628ef0cfb34SJonathan Lemon bp->gnss_lost = 0; 629773bda96SJonathan Lemon } 630773bda96SJonathan Lemon 631773bda96SJonathan Lemon mod_timer(&bp->watchdog, jiffies + HZ); 632773bda96SJonathan Lemon } 633773bda96SJonathan Lemon 634a7e1abadSJonathan Lemon static int 635773bda96SJonathan Lemon ptp_ocp_init_clock(struct ptp_ocp *bp) 636a7e1abadSJonathan Lemon { 637a7e1abadSJonathan Lemon struct timespec64 ts; 638a7e1abadSJonathan Lemon bool sync; 639a7e1abadSJonathan Lemon u32 ctrl; 640a7e1abadSJonathan Lemon 641a7e1abadSJonathan Lemon /* make sure clock is enabled */ 642a7e1abadSJonathan Lemon ctrl = ioread32(&bp->reg->ctrl); 643a7e1abadSJonathan Lemon ctrl |= OCP_CTRL_ENABLE; 644a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 645a7e1abadSJonathan Lemon 646773bda96SJonathan Lemon /* NO DRIFT Correction */ 647773bda96SJonathan Lemon /* offset_p:i 1/8, offset_i: 1/16, drift_p: 0, drift_i: 0 */ 648773bda96SJonathan Lemon iowrite32(0x2000, &bp->reg->servo_offset_p); 649773bda96SJonathan Lemon iowrite32(0x1000, &bp->reg->servo_offset_i); 650773bda96SJonathan Lemon iowrite32(0, &bp->reg->servo_drift_p); 651773bda96SJonathan Lemon iowrite32(0, &bp->reg->servo_drift_i); 652773bda96SJonathan Lemon 653773bda96SJonathan Lemon /* latch servo values */ 654773bda96SJonathan Lemon ctrl |= OCP_CTRL_ADJUST_SERVO; 655773bda96SJonathan Lemon iowrite32(ctrl, &bp->reg->ctrl); 656773bda96SJonathan Lemon 657a7e1abadSJonathan Lemon if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) { 658a7e1abadSJonathan Lemon dev_err(&bp->pdev->dev, "clock not enabled\n"); 659a7e1abadSJonathan Lemon return -ENODEV; 660a7e1abadSJonathan Lemon } 661a7e1abadSJonathan Lemon 662a7e1abadSJonathan Lemon sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC; 663a7e1abadSJonathan Lemon if (!sync) { 664a7e1abadSJonathan Lemon ktime_get_real_ts64(&ts); 665a7e1abadSJonathan Lemon ptp_ocp_settime(&bp->ptp_info, &ts); 666a7e1abadSJonathan Lemon } 667a7e1abadSJonathan Lemon if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL)) 668a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "Time: %lld.%ld, %s\n", 669a7e1abadSJonathan Lemon ts.tv_sec, ts.tv_nsec, 670a7e1abadSJonathan Lemon sync ? "in-sync" : "UNSYNCED"); 671a7e1abadSJonathan Lemon 672773bda96SJonathan Lemon timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0); 673773bda96SJonathan Lemon mod_timer(&bp->watchdog, jiffies + HZ); 674773bda96SJonathan Lemon 675a7e1abadSJonathan Lemon return 0; 676a7e1abadSJonathan Lemon } 677a7e1abadSJonathan Lemon 678a7e1abadSJonathan Lemon static void 679a7e1abadSJonathan Lemon ptp_ocp_tod_info(struct ptp_ocp *bp) 680a7e1abadSJonathan Lemon { 681a7e1abadSJonathan Lemon static const char * const proto_name[] = { 682a7e1abadSJonathan Lemon "NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none", 683a7e1abadSJonathan Lemon "UBX", "UBX_UTC", "UBX_LS", "UBX_none" 684a7e1abadSJonathan Lemon }; 685a7e1abadSJonathan Lemon static const char * const gnss_name[] = { 686a7e1abadSJonathan Lemon "ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU", 687a7e1abadSJonathan Lemon }; 688a7e1abadSJonathan Lemon u32 version, ctrl, reg; 689a7e1abadSJonathan Lemon int idx; 690a7e1abadSJonathan Lemon 691a7e1abadSJonathan Lemon version = ioread32(&bp->tod->version); 692a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "TOD Version %d.%d.%d\n", 693a7e1abadSJonathan Lemon version >> 24, (version >> 16) & 0xff, version & 0xffff); 694a7e1abadSJonathan Lemon 695a7e1abadSJonathan Lemon ctrl = ioread32(&bp->tod->ctrl); 696a7e1abadSJonathan Lemon ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE; 697a7e1abadSJonathan Lemon ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B); 698a7e1abadSJonathan Lemon iowrite32(ctrl, &bp->tod->ctrl); 699a7e1abadSJonathan Lemon 700a7e1abadSJonathan Lemon ctrl = ioread32(&bp->tod->ctrl); 701a7e1abadSJonathan Lemon idx = ctrl & TOD_CTRL_PROTOCOL ? 4 : 0; 702a7e1abadSJonathan Lemon idx += (ctrl >> 16) & 3; 703a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "control: %x\n", ctrl); 704a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "TOD Protocol %s %s\n", proto_name[idx], 705a7e1abadSJonathan Lemon ctrl & TOD_CTRL_ENABLE ? "enabled" : ""); 706a7e1abadSJonathan Lemon 707a7e1abadSJonathan Lemon idx = (ctrl >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK; 708a7e1abadSJonathan Lemon if (idx < ARRAY_SIZE(gnss_name)) 709a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "GNSS %s\n", gnss_name[idx]); 710a7e1abadSJonathan Lemon 711a7e1abadSJonathan Lemon reg = ioread32(&bp->tod->status); 712a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "status: %x\n", reg); 713a7e1abadSJonathan Lemon 714a7e1abadSJonathan Lemon reg = ioread32(&bp->tod->correction_sec); 715a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "correction: %d\n", reg); 716a7e1abadSJonathan Lemon 717a7e1abadSJonathan Lemon reg = ioread32(&bp->tod->utc_status); 718a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "utc_status: %x\n", reg); 719a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "utc_offset: %d valid:%d leap_valid:%d\n", 720a7e1abadSJonathan Lemon reg & TOD_STATUS_UTC_MASK, reg & TOD_STATUS_UTC_VALID ? 1 : 0, 721a7e1abadSJonathan Lemon reg & TOD_STATUS_LEAP_VALID ? 1 : 0); 722a7e1abadSJonathan Lemon } 723a7e1abadSJonathan Lemon 724773bda96SJonathan Lemon static int 725773bda96SJonathan Lemon ptp_ocp_firstchild(struct device *dev, void *data) 726773bda96SJonathan Lemon { 727773bda96SJonathan Lemon return 1; 728773bda96SJonathan Lemon } 729773bda96SJonathan Lemon 730773bda96SJonathan Lemon static int 731773bda96SJonathan Lemon ptp_ocp_read_i2c(struct i2c_adapter *adap, u8 addr, u8 reg, u8 sz, u8 *data) 732773bda96SJonathan Lemon { 733773bda96SJonathan Lemon struct i2c_msg msgs[2] = { 734773bda96SJonathan Lemon { 735773bda96SJonathan Lemon .addr = addr, 736773bda96SJonathan Lemon .len = 1, 737773bda96SJonathan Lemon .buf = ®, 738773bda96SJonathan Lemon }, 739773bda96SJonathan Lemon { 740773bda96SJonathan Lemon .addr = addr, 741773bda96SJonathan Lemon .flags = I2C_M_RD, 742773bda96SJonathan Lemon .len = 2, 743773bda96SJonathan Lemon .buf = data, 744773bda96SJonathan Lemon }, 745773bda96SJonathan Lemon }; 746773bda96SJonathan Lemon int err; 747773bda96SJonathan Lemon u8 len; 748773bda96SJonathan Lemon 749773bda96SJonathan Lemon /* xiic-i2c for some stupid reason only does 2 byte reads. */ 750773bda96SJonathan Lemon while (sz) { 751773bda96SJonathan Lemon len = min_t(u8, sz, 2); 752773bda96SJonathan Lemon msgs[1].len = len; 753773bda96SJonathan Lemon err = i2c_transfer(adap, msgs, 2); 754773bda96SJonathan Lemon if (err != msgs[1].len) 755773bda96SJonathan Lemon return err; 756773bda96SJonathan Lemon msgs[1].buf += len; 757773bda96SJonathan Lemon reg += len; 758773bda96SJonathan Lemon sz -= len; 759773bda96SJonathan Lemon } 760773bda96SJonathan Lemon return 0; 761773bda96SJonathan Lemon } 762773bda96SJonathan Lemon 763773bda96SJonathan Lemon static void 764773bda96SJonathan Lemon ptp_ocp_get_serial_number(struct ptp_ocp *bp) 765773bda96SJonathan Lemon { 766773bda96SJonathan Lemon struct i2c_adapter *adap; 767773bda96SJonathan Lemon struct device *dev; 768773bda96SJonathan Lemon int err; 769773bda96SJonathan Lemon 7701447149dSJonathan Lemon if (!bp->i2c_ctrl) 7711447149dSJonathan Lemon return; 7721447149dSJonathan Lemon 773773bda96SJonathan Lemon dev = device_find_child(&bp->i2c_ctrl->dev, NULL, ptp_ocp_firstchild); 774773bda96SJonathan Lemon if (!dev) { 775773bda96SJonathan Lemon dev_err(&bp->pdev->dev, "Can't find I2C adapter\n"); 776773bda96SJonathan Lemon return; 777773bda96SJonathan Lemon } 778773bda96SJonathan Lemon 779773bda96SJonathan Lemon adap = i2c_verify_adapter(dev); 780773bda96SJonathan Lemon if (!adap) { 781773bda96SJonathan Lemon dev_err(&bp->pdev->dev, "device '%s' isn't an I2C adapter\n", 782773bda96SJonathan Lemon dev_name(dev)); 783773bda96SJonathan Lemon goto out; 784773bda96SJonathan Lemon } 785773bda96SJonathan Lemon 786773bda96SJonathan Lemon err = ptp_ocp_read_i2c(adap, 0x58, 0x9A, 6, bp->serial); 787773bda96SJonathan Lemon if (err) { 788773bda96SJonathan Lemon dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", err); 789773bda96SJonathan Lemon goto out; 790773bda96SJonathan Lemon } 791773bda96SJonathan Lemon 792773bda96SJonathan Lemon bp->has_serial = true; 793773bda96SJonathan Lemon 794773bda96SJonathan Lemon out: 795773bda96SJonathan Lemon put_device(dev); 796773bda96SJonathan Lemon } 797773bda96SJonathan Lemon 798a7e1abadSJonathan Lemon static void 799a7e1abadSJonathan Lemon ptp_ocp_info(struct ptp_ocp *bp) 800a7e1abadSJonathan Lemon { 801a7e1abadSJonathan Lemon u32 version, select; 802a7e1abadSJonathan Lemon 803a7e1abadSJonathan Lemon version = ioread32(&bp->reg->version); 804a7e1abadSJonathan Lemon select = ioread32(&bp->reg->select); 805a7e1abadSJonathan Lemon dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n", 806a7e1abadSJonathan Lemon version >> 24, (version >> 16) & 0xff, version & 0xffff, 807*e1daf0ecSJonathan Lemon ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16), 808a7e1abadSJonathan Lemon ptp_clock_index(bp->ptp)); 809a7e1abadSJonathan Lemon 810498ad3f4SJonathan Lemon if (bp->tod) 811a7e1abadSJonathan Lemon ptp_ocp_tod_info(bp); 812a7e1abadSJonathan Lemon } 813a7e1abadSJonathan Lemon 814773bda96SJonathan Lemon static struct device * 815773bda96SJonathan Lemon ptp_ocp_find_flash(struct ptp_ocp *bp) 816773bda96SJonathan Lemon { 817773bda96SJonathan Lemon struct device *dev, *last; 818773bda96SJonathan Lemon 819773bda96SJonathan Lemon last = NULL; 820773bda96SJonathan Lemon dev = &bp->spi_flash->dev; 821773bda96SJonathan Lemon 822773bda96SJonathan Lemon while ((dev = device_find_child(dev, NULL, ptp_ocp_firstchild))) { 823773bda96SJonathan Lemon if (!strcmp("mtd", dev_bus_name(dev))) 824773bda96SJonathan Lemon break; 825773bda96SJonathan Lemon put_device(last); 826773bda96SJonathan Lemon last = dev; 827773bda96SJonathan Lemon } 828773bda96SJonathan Lemon put_device(last); 829773bda96SJonathan Lemon 830773bda96SJonathan Lemon return dev; 831773bda96SJonathan Lemon } 832773bda96SJonathan Lemon 833773bda96SJonathan Lemon static int 834773bda96SJonathan Lemon ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev, 835773bda96SJonathan Lemon const struct firmware *fw) 836773bda96SJonathan Lemon { 837773bda96SJonathan Lemon struct mtd_info *mtd = dev_get_drvdata(dev); 838773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 839773bda96SJonathan Lemon size_t off, len, resid, wrote; 840773bda96SJonathan Lemon struct erase_info erase; 841773bda96SJonathan Lemon size_t base, blksz; 8427c807572SJonathan Lemon int err = 0; 843773bda96SJonathan Lemon 844773bda96SJonathan Lemon off = 0; 845773bda96SJonathan Lemon base = bp->flash_start; 846773bda96SJonathan Lemon blksz = 4096; 847773bda96SJonathan Lemon resid = fw->size; 848773bda96SJonathan Lemon 849773bda96SJonathan Lemon while (resid) { 850773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, "Flashing", 851773bda96SJonathan Lemon NULL, off, fw->size); 852773bda96SJonathan Lemon 853773bda96SJonathan Lemon len = min_t(size_t, resid, blksz); 854773bda96SJonathan Lemon erase.addr = base + off; 855773bda96SJonathan Lemon erase.len = blksz; 856773bda96SJonathan Lemon 857773bda96SJonathan Lemon err = mtd_erase(mtd, &erase); 858773bda96SJonathan Lemon if (err) 859773bda96SJonathan Lemon goto out; 860773bda96SJonathan Lemon 861773bda96SJonathan Lemon err = mtd_write(mtd, base + off, len, &wrote, &fw->data[off]); 862773bda96SJonathan Lemon if (err) 863773bda96SJonathan Lemon goto out; 864773bda96SJonathan Lemon 865773bda96SJonathan Lemon off += blksz; 866773bda96SJonathan Lemon resid -= len; 867773bda96SJonathan Lemon } 868773bda96SJonathan Lemon out: 869773bda96SJonathan Lemon return err; 870773bda96SJonathan Lemon } 871773bda96SJonathan Lemon 872773bda96SJonathan Lemon static int 873773bda96SJonathan Lemon ptp_ocp_devlink_flash_update(struct devlink *devlink, 874773bda96SJonathan Lemon struct devlink_flash_update_params *params, 875773bda96SJonathan Lemon struct netlink_ext_ack *extack) 876773bda96SJonathan Lemon { 877773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 878773bda96SJonathan Lemon struct device *dev; 879773bda96SJonathan Lemon const char *msg; 880773bda96SJonathan Lemon int err; 881773bda96SJonathan Lemon 882773bda96SJonathan Lemon dev = ptp_ocp_find_flash(bp); 883773bda96SJonathan Lemon if (!dev) { 884773bda96SJonathan Lemon dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n"); 885773bda96SJonathan Lemon return -ENODEV; 886773bda96SJonathan Lemon } 887773bda96SJonathan Lemon 888773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, "Preparing to flash", 889773bda96SJonathan Lemon NULL, 0, 0); 890773bda96SJonathan Lemon 891773bda96SJonathan Lemon err = ptp_ocp_devlink_flash(devlink, dev, params->fw); 892773bda96SJonathan Lemon 893773bda96SJonathan Lemon msg = err ? "Flash error" : "Flash complete"; 894773bda96SJonathan Lemon devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0); 895773bda96SJonathan Lemon 896773bda96SJonathan Lemon put_device(dev); 897773bda96SJonathan Lemon return err; 898773bda96SJonathan Lemon } 899773bda96SJonathan Lemon 900773bda96SJonathan Lemon static int 901773bda96SJonathan Lemon ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req, 902773bda96SJonathan Lemon struct netlink_ext_ack *extack) 903773bda96SJonathan Lemon { 904773bda96SJonathan Lemon struct ptp_ocp *bp = devlink_priv(devlink); 905773bda96SJonathan Lemon char buf[32]; 906773bda96SJonathan Lemon int err; 907773bda96SJonathan Lemon 908773bda96SJonathan Lemon err = devlink_info_driver_name_put(req, KBUILD_MODNAME); 909773bda96SJonathan Lemon if (err) 910773bda96SJonathan Lemon return err; 911773bda96SJonathan Lemon 912773bda96SJonathan Lemon if (bp->image) { 913773bda96SJonathan Lemon u32 ver = ioread32(&bp->image->version); 914773bda96SJonathan Lemon 915773bda96SJonathan Lemon if (ver & 0xffff) { 916773bda96SJonathan Lemon sprintf(buf, "%d", ver); 917773bda96SJonathan Lemon err = devlink_info_version_running_put(req, 9181a052da9SJonathan Lemon "fw", 919773bda96SJonathan Lemon buf); 920773bda96SJonathan Lemon } else { 921773bda96SJonathan Lemon sprintf(buf, "%d", ver >> 16); 922773bda96SJonathan Lemon err = devlink_info_version_running_put(req, 9231a052da9SJonathan Lemon "loader", 924773bda96SJonathan Lemon buf); 925773bda96SJonathan Lemon } 926773bda96SJonathan Lemon if (err) 927773bda96SJonathan Lemon return err; 928773bda96SJonathan Lemon } 929773bda96SJonathan Lemon 930773bda96SJonathan Lemon if (!bp->has_serial) 931773bda96SJonathan Lemon ptp_ocp_get_serial_number(bp); 932773bda96SJonathan Lemon 933773bda96SJonathan Lemon if (bp->has_serial) { 934773bda96SJonathan Lemon sprintf(buf, "%pM", bp->serial); 935773bda96SJonathan Lemon err = devlink_info_serial_number_put(req, buf); 936773bda96SJonathan Lemon if (err) 937773bda96SJonathan Lemon return err; 938773bda96SJonathan Lemon } 939773bda96SJonathan Lemon 940773bda96SJonathan Lemon return 0; 941773bda96SJonathan Lemon } 942773bda96SJonathan Lemon 943773bda96SJonathan Lemon static const struct devlink_ops ptp_ocp_devlink_ops = { 944773bda96SJonathan Lemon .flash_update = ptp_ocp_devlink_flash_update, 945773bda96SJonathan Lemon .info_get = ptp_ocp_devlink_info_get, 946773bda96SJonathan Lemon }; 947773bda96SJonathan Lemon 948773bda96SJonathan Lemon static void __iomem * 949773bda96SJonathan Lemon __ptp_ocp_get_mem(struct ptp_ocp *bp, unsigned long start, int size) 950773bda96SJonathan Lemon { 951773bda96SJonathan Lemon struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp"); 952773bda96SJonathan Lemon 953773bda96SJonathan Lemon return devm_ioremap_resource(&bp->pdev->dev, &res); 954773bda96SJonathan Lemon } 955773bda96SJonathan Lemon 956773bda96SJonathan Lemon static void __iomem * 957773bda96SJonathan Lemon ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r) 958773bda96SJonathan Lemon { 959773bda96SJonathan Lemon unsigned long start; 960773bda96SJonathan Lemon 961773bda96SJonathan Lemon start = pci_resource_start(bp->pdev, 0) + r->offset; 962773bda96SJonathan Lemon return __ptp_ocp_get_mem(bp, start, r->size); 963773bda96SJonathan Lemon } 964773bda96SJonathan Lemon 965773bda96SJonathan Lemon static void 966773bda96SJonathan Lemon ptp_ocp_set_irq_resource(struct resource *res, int irq) 967773bda96SJonathan Lemon { 968773bda96SJonathan Lemon struct resource r = DEFINE_RES_IRQ(irq); 969773bda96SJonathan Lemon *res = r; 970773bda96SJonathan Lemon } 971773bda96SJonathan Lemon 972773bda96SJonathan Lemon static void 973773bda96SJonathan Lemon ptp_ocp_set_mem_resource(struct resource *res, unsigned long start, int size) 974773bda96SJonathan Lemon { 975773bda96SJonathan Lemon struct resource r = DEFINE_RES_MEM(start, size); 976773bda96SJonathan Lemon *res = r; 977773bda96SJonathan Lemon } 978773bda96SJonathan Lemon 979773bda96SJonathan Lemon static int 980773bda96SJonathan Lemon ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r) 981773bda96SJonathan Lemon { 982773bda96SJonathan Lemon struct ptp_ocp_flash_info *info; 983773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 984773bda96SJonathan Lemon struct platform_device *p; 985773bda96SJonathan Lemon struct resource res[2]; 986773bda96SJonathan Lemon unsigned long start; 987773bda96SJonathan Lemon int id; 988773bda96SJonathan Lemon 989773bda96SJonathan Lemon start = pci_resource_start(pdev, 0) + r->offset; 990773bda96SJonathan Lemon ptp_ocp_set_mem_resource(&res[0], start, r->size); 991773bda96SJonathan Lemon ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 992773bda96SJonathan Lemon 993773bda96SJonathan Lemon info = r->extra; 994773bda96SJonathan Lemon id = pci_dev_id(pdev) << 1; 995773bda96SJonathan Lemon id += info->pci_offset; 996773bda96SJonathan Lemon 997773bda96SJonathan Lemon p = platform_device_register_resndata(&pdev->dev, info->name, id, 998773bda96SJonathan Lemon res, 2, info->data, 999773bda96SJonathan Lemon info->data_size); 1000773bda96SJonathan Lemon if (IS_ERR(p)) 1001773bda96SJonathan Lemon return PTR_ERR(p); 1002773bda96SJonathan Lemon 1003773bda96SJonathan Lemon bp_assign_entry(bp, r, p); 1004773bda96SJonathan Lemon 1005773bda96SJonathan Lemon return 0; 1006773bda96SJonathan Lemon } 1007773bda96SJonathan Lemon 1008773bda96SJonathan Lemon static struct platform_device * 1009773bda96SJonathan Lemon ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id) 1010773bda96SJonathan Lemon { 10111618df6aSJonathan Lemon struct ptp_ocp_i2c_info *info; 1012773bda96SJonathan Lemon struct resource res[2]; 1013773bda96SJonathan Lemon unsigned long start; 1014773bda96SJonathan Lemon 10151618df6aSJonathan Lemon info = r->extra; 1016773bda96SJonathan Lemon start = pci_resource_start(pdev, 0) + r->offset; 1017773bda96SJonathan Lemon ptp_ocp_set_mem_resource(&res[0], start, r->size); 1018773bda96SJonathan Lemon ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1019773bda96SJonathan Lemon 10201618df6aSJonathan Lemon return platform_device_register_resndata(&pdev->dev, info->name, 10211618df6aSJonathan Lemon id, res, 2, 10221618df6aSJonathan Lemon info->data, info->data_size); 1023773bda96SJonathan Lemon } 1024773bda96SJonathan Lemon 1025773bda96SJonathan Lemon static int 1026773bda96SJonathan Lemon ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r) 1027773bda96SJonathan Lemon { 1028773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 10291618df6aSJonathan Lemon struct ptp_ocp_i2c_info *info; 1030773bda96SJonathan Lemon struct platform_device *p; 1031773bda96SJonathan Lemon struct clk_hw *clk; 1032773bda96SJonathan Lemon char buf[32]; 1033773bda96SJonathan Lemon int id; 1034773bda96SJonathan Lemon 10351618df6aSJonathan Lemon info = r->extra; 1036773bda96SJonathan Lemon id = pci_dev_id(bp->pdev); 1037773bda96SJonathan Lemon 1038773bda96SJonathan Lemon sprintf(buf, "AXI.%d", id); 10391618df6aSJonathan Lemon clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0, 10401618df6aSJonathan Lemon info->fixed_rate); 1041773bda96SJonathan Lemon if (IS_ERR(clk)) 1042773bda96SJonathan Lemon return PTR_ERR(clk); 1043773bda96SJonathan Lemon bp->i2c_clk = clk; 1044773bda96SJonathan Lemon 10451618df6aSJonathan Lemon sprintf(buf, "%s.%d", info->name, id); 1046773bda96SJonathan Lemon devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf); 1047773bda96SJonathan Lemon p = ptp_ocp_i2c_bus(bp->pdev, r, id); 1048773bda96SJonathan Lemon if (IS_ERR(p)) 1049773bda96SJonathan Lemon return PTR_ERR(p); 1050773bda96SJonathan Lemon 1051773bda96SJonathan Lemon bp_assign_entry(bp, r, p); 1052773bda96SJonathan Lemon 1053773bda96SJonathan Lemon return 0; 1054773bda96SJonathan Lemon } 1055773bda96SJonathan Lemon 1056773bda96SJonathan Lemon static irqreturn_t 1057773bda96SJonathan Lemon ptp_ocp_ts_irq(int irq, void *priv) 1058773bda96SJonathan Lemon { 1059773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1060773bda96SJonathan Lemon struct ts_reg __iomem *reg = ext->mem; 1061773bda96SJonathan Lemon struct ptp_clock_event ev; 1062773bda96SJonathan Lemon u32 sec, nsec; 1063773bda96SJonathan Lemon 1064773bda96SJonathan Lemon /* XXX should fix API - this converts s/ns -> ts -> s/ns */ 1065773bda96SJonathan Lemon sec = ioread32(®->time_sec); 1066773bda96SJonathan Lemon nsec = ioread32(®->time_ns); 1067773bda96SJonathan Lemon 1068773bda96SJonathan Lemon ev.type = PTP_CLOCK_EXTTS; 1069773bda96SJonathan Lemon ev.index = ext->info->index; 1070773bda96SJonathan Lemon ev.timestamp = sec * 1000000000ULL + nsec; 1071773bda96SJonathan Lemon 1072773bda96SJonathan Lemon ptp_clock_event(ext->bp->ptp, &ev); 1073773bda96SJonathan Lemon 1074773bda96SJonathan Lemon iowrite32(1, ®->intr); /* write 1 to ack */ 1075773bda96SJonathan Lemon 1076773bda96SJonathan Lemon return IRQ_HANDLED; 1077773bda96SJonathan Lemon } 1078773bda96SJonathan Lemon 1079773bda96SJonathan Lemon static int 1080773bda96SJonathan Lemon ptp_ocp_ts_enable(void *priv, bool enable) 1081773bda96SJonathan Lemon { 1082773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext = priv; 1083773bda96SJonathan Lemon struct ts_reg __iomem *reg = ext->mem; 1084773bda96SJonathan Lemon 1085773bda96SJonathan Lemon if (enable) { 1086773bda96SJonathan Lemon iowrite32(1, ®->enable); 1087773bda96SJonathan Lemon iowrite32(1, ®->intr_mask); 1088773bda96SJonathan Lemon iowrite32(1, ®->intr); 1089773bda96SJonathan Lemon } else { 1090773bda96SJonathan Lemon iowrite32(0, ®->intr_mask); 1091773bda96SJonathan Lemon iowrite32(0, ®->enable); 1092773bda96SJonathan Lemon } 1093773bda96SJonathan Lemon 1094773bda96SJonathan Lemon return 0; 1095773bda96SJonathan Lemon } 1096773bda96SJonathan Lemon 1097773bda96SJonathan Lemon static void 1098773bda96SJonathan Lemon ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext) 1099773bda96SJonathan Lemon { 1100773bda96SJonathan Lemon ext->info->enable(ext, false); 1101773bda96SJonathan Lemon pci_free_irq(ext->bp->pdev, ext->irq_vec, ext); 1102773bda96SJonathan Lemon kfree(ext); 1103773bda96SJonathan Lemon } 1104773bda96SJonathan Lemon 1105773bda96SJonathan Lemon static int 1106773bda96SJonathan Lemon ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r) 1107773bda96SJonathan Lemon { 1108773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1109773bda96SJonathan Lemon struct ptp_ocp_ext_src *ext; 1110773bda96SJonathan Lemon int err; 1111773bda96SJonathan Lemon 1112773bda96SJonathan Lemon ext = kzalloc(sizeof(*ext), GFP_KERNEL); 1113773bda96SJonathan Lemon if (!ext) 1114773bda96SJonathan Lemon return -ENOMEM; 1115773bda96SJonathan Lemon 1116773bda96SJonathan Lemon err = -EINVAL; 1117773bda96SJonathan Lemon ext->mem = ptp_ocp_get_mem(bp, r); 1118773bda96SJonathan Lemon if (!ext->mem) 1119773bda96SJonathan Lemon goto out; 1120773bda96SJonathan Lemon 1121773bda96SJonathan Lemon ext->bp = bp; 1122773bda96SJonathan Lemon ext->info = r->extra; 1123773bda96SJonathan Lemon ext->irq_vec = r->irq_vec; 1124773bda96SJonathan Lemon 1125773bda96SJonathan Lemon err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL, 112656ec4403SJonathan Lemon ext, "ocp%d.%s", bp->id, r->name); 1127773bda96SJonathan Lemon if (err) { 1128773bda96SJonathan Lemon dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec); 1129773bda96SJonathan Lemon goto out; 1130773bda96SJonathan Lemon } 1131773bda96SJonathan Lemon 1132773bda96SJonathan Lemon bp_assign_entry(bp, r, ext); 1133773bda96SJonathan Lemon 1134773bda96SJonathan Lemon return 0; 1135773bda96SJonathan Lemon 1136773bda96SJonathan Lemon out: 1137773bda96SJonathan Lemon kfree(ext); 1138773bda96SJonathan Lemon return err; 1139773bda96SJonathan Lemon } 1140773bda96SJonathan Lemon 1141773bda96SJonathan Lemon static int 1142773bda96SJonathan Lemon ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r) 1143773bda96SJonathan Lemon { 1144773bda96SJonathan Lemon struct pci_dev *pdev = bp->pdev; 1145773bda96SJonathan Lemon struct uart_8250_port uart; 1146773bda96SJonathan Lemon 1147773bda96SJonathan Lemon /* Setting UPF_IOREMAP and leaving port.membase unspecified lets 1148773bda96SJonathan Lemon * the serial port device claim and release the pci resource. 1149773bda96SJonathan Lemon */ 1150773bda96SJonathan Lemon memset(&uart, 0, sizeof(uart)); 1151773bda96SJonathan Lemon uart.port.dev = &pdev->dev; 1152773bda96SJonathan Lemon uart.port.iotype = UPIO_MEM; 1153773bda96SJonathan Lemon uart.port.regshift = 2; 1154773bda96SJonathan Lemon uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset; 1155773bda96SJonathan Lemon uart.port.irq = pci_irq_vector(pdev, r->irq_vec); 1156773bda96SJonathan Lemon uart.port.uartclk = 50000000; 1157773bda96SJonathan Lemon uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP; 1158773bda96SJonathan Lemon uart.port.type = PORT_16550A; 1159773bda96SJonathan Lemon 1160773bda96SJonathan Lemon return serial8250_register_8250_port(&uart); 1161773bda96SJonathan Lemon } 1162773bda96SJonathan Lemon 1163773bda96SJonathan Lemon static int 1164773bda96SJonathan Lemon ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r) 1165773bda96SJonathan Lemon { 1166773bda96SJonathan Lemon int port; 1167773bda96SJonathan Lemon 1168773bda96SJonathan Lemon port = ptp_ocp_serial_line(bp, r); 1169773bda96SJonathan Lemon if (port < 0) 1170773bda96SJonathan Lemon return port; 1171773bda96SJonathan Lemon 1172773bda96SJonathan Lemon bp_assign_entry(bp, r, port); 1173773bda96SJonathan Lemon 1174773bda96SJonathan Lemon return 0; 1175773bda96SJonathan Lemon } 1176773bda96SJonathan Lemon 1177773bda96SJonathan Lemon static int 1178773bda96SJonathan Lemon ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r) 1179773bda96SJonathan Lemon { 1180773bda96SJonathan Lemon void __iomem *mem; 1181773bda96SJonathan Lemon 1182773bda96SJonathan Lemon mem = ptp_ocp_get_mem(bp, r); 1183773bda96SJonathan Lemon if (!mem) 1184773bda96SJonathan Lemon return -EINVAL; 1185773bda96SJonathan Lemon 1186773bda96SJonathan Lemon bp_assign_entry(bp, r, mem); 1187773bda96SJonathan Lemon 1188773bda96SJonathan Lemon return 0; 1189773bda96SJonathan Lemon } 1190773bda96SJonathan Lemon 1191773bda96SJonathan Lemon /* FB specific board initializers; last "resource" registered. */ 1192773bda96SJonathan Lemon static int 1193773bda96SJonathan Lemon ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r) 1194773bda96SJonathan Lemon { 1195773bda96SJonathan Lemon bp->flash_start = 1024 * 4096; 1196773bda96SJonathan Lemon 1197773bda96SJonathan Lemon return ptp_ocp_init_clock(bp); 1198773bda96SJonathan Lemon } 1199773bda96SJonathan Lemon 120056ec4403SJonathan Lemon static bool 120156ec4403SJonathan Lemon ptp_ocp_allow_irq(struct ptp_ocp *bp, struct ocp_resource *r) 120256ec4403SJonathan Lemon { 120356ec4403SJonathan Lemon bool allow = !r->irq_vec || r->irq_vec < bp->n_irqs; 120456ec4403SJonathan Lemon 120556ec4403SJonathan Lemon if (!allow) 120656ec4403SJonathan Lemon dev_err(&bp->pdev->dev, "irq %d out of range, skipping %s\n", 120756ec4403SJonathan Lemon r->irq_vec, r->name); 120856ec4403SJonathan Lemon return allow; 120956ec4403SJonathan Lemon } 121056ec4403SJonathan Lemon 1211773bda96SJonathan Lemon static int 1212773bda96SJonathan Lemon ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data) 1213773bda96SJonathan Lemon { 1214773bda96SJonathan Lemon struct ocp_resource *r, *table; 1215773bda96SJonathan Lemon int err = 0; 1216773bda96SJonathan Lemon 1217773bda96SJonathan Lemon table = (struct ocp_resource *)driver_data; 1218773bda96SJonathan Lemon for (r = table; r->setup; r++) { 121956ec4403SJonathan Lemon if (!ptp_ocp_allow_irq(bp, r)) 122056ec4403SJonathan Lemon continue; 1221773bda96SJonathan Lemon err = r->setup(bp, r); 1222bceff290SJonathan Lemon if (err) { 1223bceff290SJonathan Lemon dev_err(&bp->pdev->dev, 1224bceff290SJonathan Lemon "Could not register %s: err %d\n", 1225bceff290SJonathan Lemon r->name, err); 1226773bda96SJonathan Lemon break; 1227773bda96SJonathan Lemon } 1228bceff290SJonathan Lemon } 1229773bda96SJonathan Lemon return err; 1230773bda96SJonathan Lemon } 1231773bda96SJonathan Lemon 1232*e1daf0ecSJonathan Lemon /* 1233*e1daf0ecSJonathan Lemon * ANT0 == gps (in) 1234*e1daf0ecSJonathan Lemon * ANT1 == sma1 (in) 1235*e1daf0ecSJonathan Lemon * ANT2 == sma2 (in) 1236*e1daf0ecSJonathan Lemon * ANT3 == sma3 (out) 1237*e1daf0ecSJonathan Lemon * ANT4 == sma4 (out) 1238*e1daf0ecSJonathan Lemon */ 1239*e1daf0ecSJonathan Lemon 1240*e1daf0ecSJonathan Lemon enum ptp_ocp_sma_mode { 1241*e1daf0ecSJonathan Lemon SMA_MODE_IN, 1242*e1daf0ecSJonathan Lemon SMA_MODE_OUT, 1243*e1daf0ecSJonathan Lemon }; 1244*e1daf0ecSJonathan Lemon 1245*e1daf0ecSJonathan Lemon static struct ptp_ocp_sma_connector { 1246*e1daf0ecSJonathan Lemon enum ptp_ocp_sma_mode mode; 1247*e1daf0ecSJonathan Lemon bool fixed_mode; 1248*e1daf0ecSJonathan Lemon u16 default_out_idx; 1249*e1daf0ecSJonathan Lemon } ptp_ocp_sma_map[4] = { 1250*e1daf0ecSJonathan Lemon { 1251*e1daf0ecSJonathan Lemon .mode = SMA_MODE_IN, 1252*e1daf0ecSJonathan Lemon .fixed_mode = true, 1253*e1daf0ecSJonathan Lemon }, 1254*e1daf0ecSJonathan Lemon { 1255*e1daf0ecSJonathan Lemon .mode = SMA_MODE_IN, 1256*e1daf0ecSJonathan Lemon .fixed_mode = true, 1257*e1daf0ecSJonathan Lemon }, 1258*e1daf0ecSJonathan Lemon { 1259*e1daf0ecSJonathan Lemon .mode = SMA_MODE_OUT, 1260*e1daf0ecSJonathan Lemon .fixed_mode = true, 1261*e1daf0ecSJonathan Lemon .default_out_idx = 0, /* 10Mhz */ 1262*e1daf0ecSJonathan Lemon }, 1263*e1daf0ecSJonathan Lemon { 1264*e1daf0ecSJonathan Lemon .mode = SMA_MODE_OUT, 1265*e1daf0ecSJonathan Lemon .fixed_mode = true, 1266*e1daf0ecSJonathan Lemon .default_out_idx = 1, /* PHC */ 1267*e1daf0ecSJonathan Lemon }, 1268*e1daf0ecSJonathan Lemon }; 1269*e1daf0ecSJonathan Lemon 1270*e1daf0ecSJonathan Lemon static ssize_t 1271*e1daf0ecSJonathan Lemon ptp_ocp_show_output(u32 val, char *buf, int default_idx) 1272*e1daf0ecSJonathan Lemon { 1273*e1daf0ecSJonathan Lemon const char *name; 1274*e1daf0ecSJonathan Lemon ssize_t count; 1275*e1daf0ecSJonathan Lemon 1276*e1daf0ecSJonathan Lemon count = sysfs_emit(buf, "OUT: "); 1277*e1daf0ecSJonathan Lemon name = ptp_ocp_select_name_from_val(ptp_ocp_sma_out, val); 1278*e1daf0ecSJonathan Lemon if (!name) 1279*e1daf0ecSJonathan Lemon name = ptp_ocp_sma_out[default_idx].name; 1280*e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s\n", name); 1281*e1daf0ecSJonathan Lemon return count; 1282*e1daf0ecSJonathan Lemon } 1283*e1daf0ecSJonathan Lemon 1284*e1daf0ecSJonathan Lemon static ssize_t 1285*e1daf0ecSJonathan Lemon ptp_ocp_show_inputs(u32 val, char *buf, const char *zero_in) 1286*e1daf0ecSJonathan Lemon { 1287*e1daf0ecSJonathan Lemon const char *name; 1288*e1daf0ecSJonathan Lemon ssize_t count; 1289*e1daf0ecSJonathan Lemon int i; 1290*e1daf0ecSJonathan Lemon 1291*e1daf0ecSJonathan Lemon count = sysfs_emit(buf, "IN: "); 1292*e1daf0ecSJonathan Lemon for (i = 0; i < ARRAY_SIZE(ptp_ocp_sma_in); i++) { 1293*e1daf0ecSJonathan Lemon if (val & ptp_ocp_sma_in[i].value) { 1294*e1daf0ecSJonathan Lemon name = ptp_ocp_sma_in[i].name; 1295*e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", name); 1296*e1daf0ecSJonathan Lemon } 1297*e1daf0ecSJonathan Lemon } 1298*e1daf0ecSJonathan Lemon if (!val && zero_in) 1299*e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "%s ", zero_in); 1300*e1daf0ecSJonathan Lemon if (count) 1301*e1daf0ecSJonathan Lemon count--; 1302*e1daf0ecSJonathan Lemon count += sysfs_emit_at(buf, count, "\n"); 1303*e1daf0ecSJonathan Lemon return count; 1304*e1daf0ecSJonathan Lemon } 1305*e1daf0ecSJonathan Lemon 1306*e1daf0ecSJonathan Lemon static int 1307*e1daf0ecSJonathan Lemon sma_parse_inputs(const char *buf, enum ptp_ocp_sma_mode *mode) 1308*e1daf0ecSJonathan Lemon { 1309*e1daf0ecSJonathan Lemon struct ocp_selector *tbl[] = { ptp_ocp_sma_in, ptp_ocp_sma_out }; 1310*e1daf0ecSJonathan Lemon int idx, count, dir; 1311*e1daf0ecSJonathan Lemon char **argv; 1312*e1daf0ecSJonathan Lemon int ret; 1313*e1daf0ecSJonathan Lemon 1314*e1daf0ecSJonathan Lemon argv = argv_split(GFP_KERNEL, buf, &count); 1315*e1daf0ecSJonathan Lemon if (!argv) 1316*e1daf0ecSJonathan Lemon return -ENOMEM; 1317*e1daf0ecSJonathan Lemon 1318*e1daf0ecSJonathan Lemon ret = -EINVAL; 1319*e1daf0ecSJonathan Lemon if (!count) 1320*e1daf0ecSJonathan Lemon goto out; 1321*e1daf0ecSJonathan Lemon 1322*e1daf0ecSJonathan Lemon idx = 0; 1323*e1daf0ecSJonathan Lemon dir = *mode == SMA_MODE_IN ? 0 : 1; 1324*e1daf0ecSJonathan Lemon if (!strcasecmp("IN:", argv[idx])) { 1325*e1daf0ecSJonathan Lemon dir = 0; 1326*e1daf0ecSJonathan Lemon idx++; 1327*e1daf0ecSJonathan Lemon } 1328*e1daf0ecSJonathan Lemon if (!strcasecmp("OUT:", argv[0])) { 1329*e1daf0ecSJonathan Lemon dir = 1; 1330*e1daf0ecSJonathan Lemon idx++; 1331*e1daf0ecSJonathan Lemon } 1332*e1daf0ecSJonathan Lemon *mode = dir == 0 ? SMA_MODE_IN : SMA_MODE_OUT; 1333*e1daf0ecSJonathan Lemon 1334*e1daf0ecSJonathan Lemon ret = 0; 1335*e1daf0ecSJonathan Lemon for (; idx < count; idx++) 1336*e1daf0ecSJonathan Lemon ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]); 1337*e1daf0ecSJonathan Lemon if (ret < 0) 1338*e1daf0ecSJonathan Lemon ret = -EINVAL; 1339*e1daf0ecSJonathan Lemon 1340*e1daf0ecSJonathan Lemon out: 1341*e1daf0ecSJonathan Lemon argv_free(argv); 1342*e1daf0ecSJonathan Lemon return ret; 1343*e1daf0ecSJonathan Lemon } 1344*e1daf0ecSJonathan Lemon 1345*e1daf0ecSJonathan Lemon static ssize_t 1346*e1daf0ecSJonathan Lemon ptp_ocp_sma_show(struct ptp_ocp *bp, int sma_nr, u32 val, char *buf, 1347*e1daf0ecSJonathan Lemon const char *zero_in) 1348*e1daf0ecSJonathan Lemon { 1349*e1daf0ecSJonathan Lemon struct ptp_ocp_sma_connector *sma = &ptp_ocp_sma_map[sma_nr - 1]; 1350*e1daf0ecSJonathan Lemon 1351*e1daf0ecSJonathan Lemon if (sma->mode == SMA_MODE_IN) 1352*e1daf0ecSJonathan Lemon return ptp_ocp_show_inputs(val, buf, zero_in); 1353*e1daf0ecSJonathan Lemon 1354*e1daf0ecSJonathan Lemon return ptp_ocp_show_output(val, buf, sma->default_out_idx); 1355*e1daf0ecSJonathan Lemon } 1356*e1daf0ecSJonathan Lemon 1357*e1daf0ecSJonathan Lemon static ssize_t 1358*e1daf0ecSJonathan Lemon sma1_show(struct device *dev, struct device_attribute *attr, char *buf) 1359*e1daf0ecSJonathan Lemon { 1360*e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1361*e1daf0ecSJonathan Lemon u32 val; 1362*e1daf0ecSJonathan Lemon 1363*e1daf0ecSJonathan Lemon val = ioread32(&bp->sma->gpio1) & 0x3f; 1364*e1daf0ecSJonathan Lemon return ptp_ocp_sma_show(bp, 1, val, buf, ptp_ocp_sma_in[0].name); 1365*e1daf0ecSJonathan Lemon } 1366*e1daf0ecSJonathan Lemon 1367*e1daf0ecSJonathan Lemon static ssize_t 1368*e1daf0ecSJonathan Lemon sma2_show(struct device *dev, struct device_attribute *attr, char *buf) 1369*e1daf0ecSJonathan Lemon { 1370*e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1371*e1daf0ecSJonathan Lemon u32 val; 1372*e1daf0ecSJonathan Lemon 1373*e1daf0ecSJonathan Lemon val = (ioread32(&bp->sma->gpio1) >> 16) & 0x3f; 1374*e1daf0ecSJonathan Lemon return ptp_ocp_sma_show(bp, 2, val, buf, NULL); 1375*e1daf0ecSJonathan Lemon } 1376*e1daf0ecSJonathan Lemon 1377*e1daf0ecSJonathan Lemon static ssize_t 1378*e1daf0ecSJonathan Lemon sma3_show(struct device *dev, struct device_attribute *attr, char *buf) 1379*e1daf0ecSJonathan Lemon { 1380*e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1381*e1daf0ecSJonathan Lemon u32 val; 1382*e1daf0ecSJonathan Lemon 1383*e1daf0ecSJonathan Lemon val = ioread32(&bp->sma->gpio2) & 0x3f; 1384*e1daf0ecSJonathan Lemon return ptp_ocp_sma_show(bp, 3, val, buf, NULL); 1385*e1daf0ecSJonathan Lemon } 1386*e1daf0ecSJonathan Lemon 1387*e1daf0ecSJonathan Lemon static ssize_t 1388*e1daf0ecSJonathan Lemon sma4_show(struct device *dev, struct device_attribute *attr, char *buf) 1389*e1daf0ecSJonathan Lemon { 1390*e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1391*e1daf0ecSJonathan Lemon u32 val; 1392*e1daf0ecSJonathan Lemon 1393*e1daf0ecSJonathan Lemon val = (ioread32(&bp->sma->gpio2) >> 16) & 0x3f; 1394*e1daf0ecSJonathan Lemon return ptp_ocp_sma_show(bp, 4, val, buf, NULL); 1395*e1daf0ecSJonathan Lemon } 1396*e1daf0ecSJonathan Lemon 1397*e1daf0ecSJonathan Lemon static void 1398*e1daf0ecSJonathan Lemon ptp_ocp_sma_store_output(struct ptp_ocp *bp, u32 val, u32 shift) 1399*e1daf0ecSJonathan Lemon { 1400*e1daf0ecSJonathan Lemon unsigned long flags; 1401*e1daf0ecSJonathan Lemon u32 gpio, mask; 1402*e1daf0ecSJonathan Lemon 1403*e1daf0ecSJonathan Lemon mask = 0xffff << (16 - shift); 1404*e1daf0ecSJonathan Lemon 1405*e1daf0ecSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1406*e1daf0ecSJonathan Lemon 1407*e1daf0ecSJonathan Lemon gpio = ioread32(&bp->sma->gpio2); 1408*e1daf0ecSJonathan Lemon gpio = (gpio & mask) | (val << shift); 1409*e1daf0ecSJonathan Lemon iowrite32(gpio, &bp->sma->gpio2); 1410*e1daf0ecSJonathan Lemon 1411*e1daf0ecSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1412*e1daf0ecSJonathan Lemon } 1413*e1daf0ecSJonathan Lemon 1414*e1daf0ecSJonathan Lemon static void 1415*e1daf0ecSJonathan Lemon ptp_ocp_sma_store_inputs(struct ptp_ocp *bp, u32 val, u32 shift) 1416*e1daf0ecSJonathan Lemon { 1417*e1daf0ecSJonathan Lemon unsigned long flags; 1418*e1daf0ecSJonathan Lemon u32 gpio, mask; 1419*e1daf0ecSJonathan Lemon 1420*e1daf0ecSJonathan Lemon mask = 0xffff << (16 - shift); 1421*e1daf0ecSJonathan Lemon 1422*e1daf0ecSJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1423*e1daf0ecSJonathan Lemon 1424*e1daf0ecSJonathan Lemon gpio = ioread32(&bp->sma->gpio1); 1425*e1daf0ecSJonathan Lemon gpio = (gpio & mask) | (val << shift); 1426*e1daf0ecSJonathan Lemon iowrite32(gpio, &bp->sma->gpio1); 1427*e1daf0ecSJonathan Lemon 1428*e1daf0ecSJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1429*e1daf0ecSJonathan Lemon } 1430*e1daf0ecSJonathan Lemon 1431*e1daf0ecSJonathan Lemon static ssize_t 1432*e1daf0ecSJonathan Lemon ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr, u32 shift) 1433*e1daf0ecSJonathan Lemon { 1434*e1daf0ecSJonathan Lemon struct ptp_ocp_sma_connector *sma = &ptp_ocp_sma_map[sma_nr - 1]; 1435*e1daf0ecSJonathan Lemon enum ptp_ocp_sma_mode mode; 1436*e1daf0ecSJonathan Lemon int val; 1437*e1daf0ecSJonathan Lemon 1438*e1daf0ecSJonathan Lemon mode = sma->mode; 1439*e1daf0ecSJonathan Lemon val = sma_parse_inputs(buf, &mode); 1440*e1daf0ecSJonathan Lemon if (val < 0) 1441*e1daf0ecSJonathan Lemon return val; 1442*e1daf0ecSJonathan Lemon 1443*e1daf0ecSJonathan Lemon if (mode != sma->mode && sma->fixed_mode) 1444*e1daf0ecSJonathan Lemon return -EOPNOTSUPP; 1445*e1daf0ecSJonathan Lemon 1446*e1daf0ecSJonathan Lemon if (mode != sma->mode) { 1447*e1daf0ecSJonathan Lemon pr_err("Mode changes not supported yet.\n"); 1448*e1daf0ecSJonathan Lemon return -EOPNOTSUPP; 1449*e1daf0ecSJonathan Lemon } 1450*e1daf0ecSJonathan Lemon 1451*e1daf0ecSJonathan Lemon if (sma->mode == SMA_MODE_IN) 1452*e1daf0ecSJonathan Lemon ptp_ocp_sma_store_inputs(bp, val, shift); 1453*e1daf0ecSJonathan Lemon else 1454*e1daf0ecSJonathan Lemon ptp_ocp_sma_store_output(bp, val, shift); 1455*e1daf0ecSJonathan Lemon 1456*e1daf0ecSJonathan Lemon return 0; 1457*e1daf0ecSJonathan Lemon } 1458*e1daf0ecSJonathan Lemon 1459*e1daf0ecSJonathan Lemon static ssize_t 1460*e1daf0ecSJonathan Lemon sma1_store(struct device *dev, struct device_attribute *attr, 1461*e1daf0ecSJonathan Lemon const char *buf, size_t count) 1462*e1daf0ecSJonathan Lemon { 1463*e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1464*e1daf0ecSJonathan Lemon int err; 1465*e1daf0ecSJonathan Lemon 1466*e1daf0ecSJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 1, 0); 1467*e1daf0ecSJonathan Lemon return err ? err : count; 1468*e1daf0ecSJonathan Lemon } 1469*e1daf0ecSJonathan Lemon 1470*e1daf0ecSJonathan Lemon static ssize_t 1471*e1daf0ecSJonathan Lemon sma2_store(struct device *dev, struct device_attribute *attr, 1472*e1daf0ecSJonathan Lemon const char *buf, size_t count) 1473*e1daf0ecSJonathan Lemon { 1474*e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1475*e1daf0ecSJonathan Lemon int err; 1476*e1daf0ecSJonathan Lemon 1477*e1daf0ecSJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 2, 16); 1478*e1daf0ecSJonathan Lemon return err ? err : count; 1479*e1daf0ecSJonathan Lemon } 1480*e1daf0ecSJonathan Lemon 1481*e1daf0ecSJonathan Lemon static ssize_t 1482*e1daf0ecSJonathan Lemon sma3_store(struct device *dev, struct device_attribute *attr, 1483*e1daf0ecSJonathan Lemon const char *buf, size_t count) 1484*e1daf0ecSJonathan Lemon { 1485*e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1486*e1daf0ecSJonathan Lemon int err; 1487*e1daf0ecSJonathan Lemon 1488*e1daf0ecSJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 3, 0); 1489*e1daf0ecSJonathan Lemon return err ? err : count; 1490*e1daf0ecSJonathan Lemon } 1491*e1daf0ecSJonathan Lemon 1492*e1daf0ecSJonathan Lemon static ssize_t 1493*e1daf0ecSJonathan Lemon sma4_store(struct device *dev, struct device_attribute *attr, 1494*e1daf0ecSJonathan Lemon const char *buf, size_t count) 1495*e1daf0ecSJonathan Lemon { 1496*e1daf0ecSJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1497*e1daf0ecSJonathan Lemon int err; 1498*e1daf0ecSJonathan Lemon 1499*e1daf0ecSJonathan Lemon err = ptp_ocp_sma_store(bp, buf, 4, 16); 1500*e1daf0ecSJonathan Lemon return err ? err : count; 1501*e1daf0ecSJonathan Lemon } 1502*e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma1); 1503*e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma2); 1504*e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma3); 1505*e1daf0ecSJonathan Lemon static DEVICE_ATTR_RW(sma4); 1506*e1daf0ecSJonathan Lemon 1507*e1daf0ecSJonathan Lemon static ssize_t 1508*e1daf0ecSJonathan Lemon available_sma_inputs_show(struct device *dev, 1509*e1daf0ecSJonathan Lemon struct device_attribute *attr, char *buf) 1510*e1daf0ecSJonathan Lemon { 1511*e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_sma_in, buf); 1512*e1daf0ecSJonathan Lemon } 1513*e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_inputs); 1514*e1daf0ecSJonathan Lemon 1515*e1daf0ecSJonathan Lemon static ssize_t 1516*e1daf0ecSJonathan Lemon available_sma_outputs_show(struct device *dev, 1517*e1daf0ecSJonathan Lemon struct device_attribute *attr, char *buf) 1518*e1daf0ecSJonathan Lemon { 1519*e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_sma_out, buf); 1520*e1daf0ecSJonathan Lemon } 1521*e1daf0ecSJonathan Lemon static DEVICE_ATTR_RO(available_sma_outputs); 1522*e1daf0ecSJonathan Lemon 1523773bda96SJonathan Lemon static ssize_t 1524773bda96SJonathan Lemon serialnum_show(struct device *dev, struct device_attribute *attr, char *buf) 1525773bda96SJonathan Lemon { 1526773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1527773bda96SJonathan Lemon 1528773bda96SJonathan Lemon if (!bp->has_serial) 1529773bda96SJonathan Lemon ptp_ocp_get_serial_number(bp); 1530773bda96SJonathan Lemon 1531773bda96SJonathan Lemon return sysfs_emit(buf, "%pM\n", bp->serial); 1532773bda96SJonathan Lemon } 1533773bda96SJonathan Lemon static DEVICE_ATTR_RO(serialnum); 1534773bda96SJonathan Lemon 1535773bda96SJonathan Lemon static ssize_t 1536ef0cfb34SJonathan Lemon gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf) 1537773bda96SJonathan Lemon { 1538773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1539773bda96SJonathan Lemon ssize_t ret; 1540773bda96SJonathan Lemon 1541ef0cfb34SJonathan Lemon if (bp->gnss_lost) 1542ef0cfb34SJonathan Lemon ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost); 1543773bda96SJonathan Lemon else 1544773bda96SJonathan Lemon ret = sysfs_emit(buf, "SYNC\n"); 1545773bda96SJonathan Lemon 1546773bda96SJonathan Lemon return ret; 1547773bda96SJonathan Lemon } 1548ef0cfb34SJonathan Lemon static DEVICE_ATTR_RO(gnss_sync); 1549773bda96SJonathan Lemon 1550773bda96SJonathan Lemon static ssize_t 1551773bda96SJonathan Lemon clock_source_show(struct device *dev, struct device_attribute *attr, char *buf) 1552773bda96SJonathan Lemon { 1553773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1554773bda96SJonathan Lemon const char *p; 1555773bda96SJonathan Lemon u32 select; 1556773bda96SJonathan Lemon 1557773bda96SJonathan Lemon select = ioread32(&bp->reg->select); 1558*e1daf0ecSJonathan Lemon p = ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16); 1559773bda96SJonathan Lemon 1560773bda96SJonathan Lemon return sysfs_emit(buf, "%s\n", p); 1561773bda96SJonathan Lemon } 1562773bda96SJonathan Lemon 1563773bda96SJonathan Lemon static ssize_t 1564773bda96SJonathan Lemon clock_source_store(struct device *dev, struct device_attribute *attr, 1565773bda96SJonathan Lemon const char *buf, size_t count) 1566773bda96SJonathan Lemon { 1567773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1568773bda96SJonathan Lemon unsigned long flags; 1569773bda96SJonathan Lemon int val; 1570773bda96SJonathan Lemon 1571*e1daf0ecSJonathan Lemon val = ptp_ocp_select_val_from_name(ptp_ocp_clock, buf); 1572773bda96SJonathan Lemon if (val < 0) 1573773bda96SJonathan Lemon return val; 1574773bda96SJonathan Lemon 1575773bda96SJonathan Lemon spin_lock_irqsave(&bp->lock, flags); 1576773bda96SJonathan Lemon iowrite32(val, &bp->reg->select); 1577773bda96SJonathan Lemon spin_unlock_irqrestore(&bp->lock, flags); 1578773bda96SJonathan Lemon 1579773bda96SJonathan Lemon return count; 1580773bda96SJonathan Lemon } 1581773bda96SJonathan Lemon static DEVICE_ATTR_RW(clock_source); 1582773bda96SJonathan Lemon 1583773bda96SJonathan Lemon static ssize_t 1584773bda96SJonathan Lemon available_clock_sources_show(struct device *dev, 1585773bda96SJonathan Lemon struct device_attribute *attr, char *buf) 1586773bda96SJonathan Lemon { 1587*e1daf0ecSJonathan Lemon return ptp_ocp_select_table_show(ptp_ocp_clock, buf); 1588773bda96SJonathan Lemon } 1589773bda96SJonathan Lemon static DEVICE_ATTR_RO(available_clock_sources); 1590773bda96SJonathan Lemon 1591773bda96SJonathan Lemon static struct attribute *timecard_attrs[] = { 1592773bda96SJonathan Lemon &dev_attr_serialnum.attr, 1593ef0cfb34SJonathan Lemon &dev_attr_gnss_sync.attr, 1594773bda96SJonathan Lemon &dev_attr_clock_source.attr, 1595773bda96SJonathan Lemon &dev_attr_available_clock_sources.attr, 1596*e1daf0ecSJonathan Lemon &dev_attr_sma1.attr, 1597*e1daf0ecSJonathan Lemon &dev_attr_sma2.attr, 1598*e1daf0ecSJonathan Lemon &dev_attr_sma3.attr, 1599*e1daf0ecSJonathan Lemon &dev_attr_sma4.attr, 1600*e1daf0ecSJonathan Lemon &dev_attr_available_sma_inputs.attr, 1601*e1daf0ecSJonathan Lemon &dev_attr_available_sma_outputs.attr, 1602773bda96SJonathan Lemon NULL, 1603773bda96SJonathan Lemon }; 1604773bda96SJonathan Lemon ATTRIBUTE_GROUPS(timecard); 1605773bda96SJonathan Lemon 1606773bda96SJonathan Lemon static void 1607773bda96SJonathan Lemon ptp_ocp_dev_release(struct device *dev) 1608773bda96SJonathan Lemon { 1609773bda96SJonathan Lemon struct ptp_ocp *bp = dev_get_drvdata(dev); 1610773bda96SJonathan Lemon 1611773bda96SJonathan Lemon mutex_lock(&ptp_ocp_lock); 1612773bda96SJonathan Lemon idr_remove(&ptp_ocp_idr, bp->id); 1613773bda96SJonathan Lemon mutex_unlock(&ptp_ocp_lock); 1614773bda96SJonathan Lemon } 1615773bda96SJonathan Lemon 1616773bda96SJonathan Lemon static int 1617773bda96SJonathan Lemon ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev) 1618773bda96SJonathan Lemon { 1619773bda96SJonathan Lemon int err; 1620773bda96SJonathan Lemon 1621773bda96SJonathan Lemon mutex_lock(&ptp_ocp_lock); 1622773bda96SJonathan Lemon err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL); 1623773bda96SJonathan Lemon mutex_unlock(&ptp_ocp_lock); 1624773bda96SJonathan Lemon if (err < 0) { 1625773bda96SJonathan Lemon dev_err(&pdev->dev, "idr_alloc failed: %d\n", err); 1626773bda96SJonathan Lemon return err; 1627773bda96SJonathan Lemon } 1628773bda96SJonathan Lemon bp->id = err; 1629773bda96SJonathan Lemon 1630773bda96SJonathan Lemon bp->ptp_info = ptp_ocp_clock_info; 1631773bda96SJonathan Lemon spin_lock_init(&bp->lock); 1632ef0cfb34SJonathan Lemon bp->gnss_port = -1; 1633773bda96SJonathan Lemon bp->mac_port = -1; 1634773bda96SJonathan Lemon bp->pdev = pdev; 1635773bda96SJonathan Lemon 1636773bda96SJonathan Lemon device_initialize(&bp->dev); 1637773bda96SJonathan Lemon dev_set_name(&bp->dev, "ocp%d", bp->id); 1638773bda96SJonathan Lemon bp->dev.class = &timecard_class; 1639773bda96SJonathan Lemon bp->dev.parent = &pdev->dev; 1640773bda96SJonathan Lemon bp->dev.release = ptp_ocp_dev_release; 1641773bda96SJonathan Lemon dev_set_drvdata(&bp->dev, bp); 1642773bda96SJonathan Lemon 1643773bda96SJonathan Lemon err = device_add(&bp->dev); 1644773bda96SJonathan Lemon if (err) { 1645773bda96SJonathan Lemon dev_err(&bp->dev, "device add failed: %d\n", err); 1646773bda96SJonathan Lemon goto out; 1647773bda96SJonathan Lemon } 1648773bda96SJonathan Lemon 1649773bda96SJonathan Lemon pci_set_drvdata(pdev, bp); 1650773bda96SJonathan Lemon 1651773bda96SJonathan Lemon return 0; 1652773bda96SJonathan Lemon 1653773bda96SJonathan Lemon out: 1654773bda96SJonathan Lemon ptp_ocp_dev_release(&bp->dev); 1655d12f23faSJonathan Lemon put_device(&bp->dev); 1656773bda96SJonathan Lemon return err; 1657773bda96SJonathan Lemon } 1658773bda96SJonathan Lemon 1659773bda96SJonathan Lemon static void 1660773bda96SJonathan Lemon ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link) 1661773bda96SJonathan Lemon { 1662773bda96SJonathan Lemon struct device *dev = &bp->dev; 1663773bda96SJonathan Lemon 1664773bda96SJonathan Lemon if (sysfs_create_link(&dev->kobj, &child->kobj, link)) 1665773bda96SJonathan Lemon dev_err(dev, "%s symlink failed\n", link); 1666773bda96SJonathan Lemon } 1667773bda96SJonathan Lemon 1668773bda96SJonathan Lemon static void 1669773bda96SJonathan Lemon ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link) 1670773bda96SJonathan Lemon { 1671773bda96SJonathan Lemon struct device *dev, *child; 1672773bda96SJonathan Lemon 1673773bda96SJonathan Lemon dev = &bp->pdev->dev; 1674773bda96SJonathan Lemon 1675773bda96SJonathan Lemon child = device_find_child_by_name(dev, name); 1676773bda96SJonathan Lemon if (!child) { 1677773bda96SJonathan Lemon dev_err(dev, "Could not find device %s\n", name); 1678773bda96SJonathan Lemon return; 1679773bda96SJonathan Lemon } 1680773bda96SJonathan Lemon 1681773bda96SJonathan Lemon ptp_ocp_symlink(bp, child, link); 1682773bda96SJonathan Lemon put_device(child); 1683773bda96SJonathan Lemon } 1684773bda96SJonathan Lemon 1685773bda96SJonathan Lemon static int 1686773bda96SJonathan Lemon ptp_ocp_complete(struct ptp_ocp *bp) 1687773bda96SJonathan Lemon { 1688773bda96SJonathan Lemon struct pps_device *pps; 1689773bda96SJonathan Lemon char buf[32]; 1690773bda96SJonathan Lemon 1691ef0cfb34SJonathan Lemon if (bp->gnss_port != -1) { 1692ef0cfb34SJonathan Lemon sprintf(buf, "ttyS%d", bp->gnss_port); 1693ef0cfb34SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyGNSS"); 1694773bda96SJonathan Lemon } 1695773bda96SJonathan Lemon if (bp->mac_port != -1) { 1696773bda96SJonathan Lemon sprintf(buf, "ttyS%d", bp->mac_port); 1697773bda96SJonathan Lemon ptp_ocp_link_child(bp, buf, "ttyMAC"); 1698773bda96SJonathan Lemon } 1699773bda96SJonathan Lemon sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp)); 1700773bda96SJonathan Lemon ptp_ocp_link_child(bp, buf, "ptp"); 1701773bda96SJonathan Lemon 1702773bda96SJonathan Lemon pps = pps_lookup_dev(bp->ptp); 1703773bda96SJonathan Lemon if (pps) 1704773bda96SJonathan Lemon ptp_ocp_symlink(bp, pps->dev, "pps"); 1705773bda96SJonathan Lemon 1706773bda96SJonathan Lemon if (device_add_groups(&bp->dev, timecard_groups)) 1707773bda96SJonathan Lemon pr_err("device add groups failed\n"); 1708773bda96SJonathan Lemon 1709773bda96SJonathan Lemon return 0; 1710773bda96SJonathan Lemon } 1711773bda96SJonathan Lemon 1712773bda96SJonathan Lemon static void 1713773bda96SJonathan Lemon ptp_ocp_resource_summary(struct ptp_ocp *bp) 1714773bda96SJonathan Lemon { 1715773bda96SJonathan Lemon struct device *dev = &bp->pdev->dev; 1716773bda96SJonathan Lemon 1717773bda96SJonathan Lemon if (bp->image) { 1718773bda96SJonathan Lemon u32 ver = ioread32(&bp->image->version); 1719773bda96SJonathan Lemon 1720773bda96SJonathan Lemon dev_info(dev, "version %x\n", ver); 1721773bda96SJonathan Lemon if (ver & 0xffff) 1722773bda96SJonathan Lemon dev_info(dev, "regular image, version %d\n", 1723773bda96SJonathan Lemon ver & 0xffff); 1724773bda96SJonathan Lemon else 1725773bda96SJonathan Lemon dev_info(dev, "golden image, version %d\n", 1726773bda96SJonathan Lemon ver >> 16); 1727773bda96SJonathan Lemon } 1728ef0cfb34SJonathan Lemon if (bp->gnss_port != -1) 1729ef0cfb34SJonathan Lemon dev_info(dev, "GNSS @ /dev/ttyS%d 115200\n", bp->gnss_port); 1730773bda96SJonathan Lemon if (bp->mac_port != -1) 1731773bda96SJonathan Lemon dev_info(dev, "MAC @ /dev/ttyS%d 57600\n", bp->mac_port); 1732773bda96SJonathan Lemon } 1733773bda96SJonathan Lemon 1734773bda96SJonathan Lemon static void 1735773bda96SJonathan Lemon ptp_ocp_detach_sysfs(struct ptp_ocp *bp) 1736773bda96SJonathan Lemon { 1737773bda96SJonathan Lemon struct device *dev = &bp->dev; 1738773bda96SJonathan Lemon 1739ef0cfb34SJonathan Lemon sysfs_remove_link(&dev->kobj, "ttyGNSS"); 1740773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "ttyMAC"); 1741773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "ptp"); 1742773bda96SJonathan Lemon sysfs_remove_link(&dev->kobj, "pps"); 1743773bda96SJonathan Lemon device_remove_groups(dev, timecard_groups); 1744773bda96SJonathan Lemon } 1745773bda96SJonathan Lemon 1746773bda96SJonathan Lemon static void 1747773bda96SJonathan Lemon ptp_ocp_detach(struct ptp_ocp *bp) 1748773bda96SJonathan Lemon { 1749773bda96SJonathan Lemon ptp_ocp_detach_sysfs(bp); 1750773bda96SJonathan Lemon if (timer_pending(&bp->watchdog)) 1751773bda96SJonathan Lemon del_timer_sync(&bp->watchdog); 1752773bda96SJonathan Lemon if (bp->ts0) 1753773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->ts0); 1754773bda96SJonathan Lemon if (bp->ts1) 1755773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->ts1); 1756dcf61469SJonathan Lemon if (bp->ts2) 1757dcf61469SJonathan Lemon ptp_ocp_unregister_ext(bp->ts2); 1758773bda96SJonathan Lemon if (bp->pps) 1759773bda96SJonathan Lemon ptp_ocp_unregister_ext(bp->pps); 1760ef0cfb34SJonathan Lemon if (bp->gnss_port != -1) 1761ef0cfb34SJonathan Lemon serial8250_unregister_port(bp->gnss_port); 1762773bda96SJonathan Lemon if (bp->mac_port != -1) 1763773bda96SJonathan Lemon serial8250_unregister_port(bp->mac_port); 1764773bda96SJonathan Lemon if (bp->spi_flash) 1765773bda96SJonathan Lemon platform_device_unregister(bp->spi_flash); 1766773bda96SJonathan Lemon if (bp->i2c_ctrl) 1767773bda96SJonathan Lemon platform_device_unregister(bp->i2c_ctrl); 1768773bda96SJonathan Lemon if (bp->i2c_clk) 1769773bda96SJonathan Lemon clk_hw_unregister_fixed_rate(bp->i2c_clk); 1770773bda96SJonathan Lemon if (bp->n_irqs) 1771773bda96SJonathan Lemon pci_free_irq_vectors(bp->pdev); 1772773bda96SJonathan Lemon if (bp->ptp) 1773773bda96SJonathan Lemon ptp_clock_unregister(bp->ptp); 1774773bda96SJonathan Lemon device_unregister(&bp->dev); 1775773bda96SJonathan Lemon } 1776773bda96SJonathan Lemon 1777a7e1abadSJonathan Lemon static int 1778a7e1abadSJonathan Lemon ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id) 1779a7e1abadSJonathan Lemon { 1780773bda96SJonathan Lemon struct devlink *devlink; 1781a7e1abadSJonathan Lemon struct ptp_ocp *bp; 1782a7e1abadSJonathan Lemon int err; 1783a7e1abadSJonathan Lemon 1784919d13a7SLeon Romanovsky devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp), &pdev->dev); 1785773bda96SJonathan Lemon if (!devlink) { 1786773bda96SJonathan Lemon dev_err(&pdev->dev, "devlink_alloc failed\n"); 1787a7e1abadSJonathan Lemon return -ENOMEM; 1788773bda96SJonathan Lemon } 1789773bda96SJonathan Lemon 1790919d13a7SLeon Romanovsky err = devlink_register(devlink); 1791773bda96SJonathan Lemon if (err) 1792773bda96SJonathan Lemon goto out_free; 1793a7e1abadSJonathan Lemon 1794a7e1abadSJonathan Lemon err = pci_enable_device(pdev); 1795a7e1abadSJonathan Lemon if (err) { 1796a7e1abadSJonathan Lemon dev_err(&pdev->dev, "pci_enable_device\n"); 1797773bda96SJonathan Lemon goto out_unregister; 1798a7e1abadSJonathan Lemon } 1799a7e1abadSJonathan Lemon 1800773bda96SJonathan Lemon bp = devlink_priv(devlink); 1801773bda96SJonathan Lemon err = ptp_ocp_device_init(bp, pdev); 1802773bda96SJonathan Lemon if (err) 1803d9fdbf13SJonathan Lemon goto out_disable; 1804a7e1abadSJonathan Lemon 1805773bda96SJonathan Lemon /* compat mode. 1806773bda96SJonathan Lemon * Older FPGA firmware only returns 2 irq's. 1807773bda96SJonathan Lemon * allow this - if not all of the IRQ's are returned, skip the 1808773bda96SJonathan Lemon * extra devices and just register the clock. 1809773bda96SJonathan Lemon */ 1810773bda96SJonathan Lemon err = pci_alloc_irq_vectors(pdev, 1, 10, PCI_IRQ_MSI | PCI_IRQ_MSIX); 1811773bda96SJonathan Lemon if (err < 0) { 1812773bda96SJonathan Lemon dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err); 1813773bda96SJonathan Lemon goto out; 1814a7e1abadSJonathan Lemon } 1815773bda96SJonathan Lemon bp->n_irqs = err; 1816773bda96SJonathan Lemon pci_set_master(pdev); 1817a7e1abadSJonathan Lemon 1818773bda96SJonathan Lemon err = ptp_ocp_register_resources(bp, id->driver_data); 1819a7e1abadSJonathan Lemon if (err) 1820a7e1abadSJonathan Lemon goto out; 1821a7e1abadSJonathan Lemon 1822a7e1abadSJonathan Lemon bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev); 1823a7e1abadSJonathan Lemon if (IS_ERR(bp->ptp)) { 1824a7e1abadSJonathan Lemon err = PTR_ERR(bp->ptp); 1825773bda96SJonathan Lemon dev_err(&pdev->dev, "ptp_clock_register: %d\n", err); 1826773bda96SJonathan Lemon bp->ptp = NULL; 1827a7e1abadSJonathan Lemon goto out; 1828a7e1abadSJonathan Lemon } 1829a7e1abadSJonathan Lemon 1830773bda96SJonathan Lemon err = ptp_ocp_complete(bp); 1831773bda96SJonathan Lemon if (err) 1832773bda96SJonathan Lemon goto out; 1833773bda96SJonathan Lemon 1834a7e1abadSJonathan Lemon ptp_ocp_info(bp); 1835773bda96SJonathan Lemon ptp_ocp_resource_summary(bp); 1836a7e1abadSJonathan Lemon 1837a7e1abadSJonathan Lemon return 0; 1838a7e1abadSJonathan Lemon 1839a7e1abadSJonathan Lemon out: 1840773bda96SJonathan Lemon ptp_ocp_detach(bp); 1841773bda96SJonathan Lemon pci_set_drvdata(pdev, NULL); 1842d9fdbf13SJonathan Lemon out_disable: 1843d9fdbf13SJonathan Lemon pci_disable_device(pdev); 1844773bda96SJonathan Lemon out_unregister: 1845919d13a7SLeon Romanovsky devlink_unregister(devlink); 1846a7e1abadSJonathan Lemon out_free: 1847773bda96SJonathan Lemon devlink_free(devlink); 1848a7e1abadSJonathan Lemon 1849a7e1abadSJonathan Lemon return err; 1850a7e1abadSJonathan Lemon } 1851a7e1abadSJonathan Lemon 1852a7e1abadSJonathan Lemon static void 1853a7e1abadSJonathan Lemon ptp_ocp_remove(struct pci_dev *pdev) 1854a7e1abadSJonathan Lemon { 1855a7e1abadSJonathan Lemon struct ptp_ocp *bp = pci_get_drvdata(pdev); 1856773bda96SJonathan Lemon struct devlink *devlink = priv_to_devlink(bp); 1857a7e1abadSJonathan Lemon 1858773bda96SJonathan Lemon ptp_ocp_detach(bp); 1859a7e1abadSJonathan Lemon pci_set_drvdata(pdev, NULL); 1860d9fdbf13SJonathan Lemon pci_disable_device(pdev); 1861773bda96SJonathan Lemon 1862919d13a7SLeon Romanovsky devlink_unregister(devlink); 1863773bda96SJonathan Lemon devlink_free(devlink); 1864a7e1abadSJonathan Lemon } 1865a7e1abadSJonathan Lemon 1866a7e1abadSJonathan Lemon static struct pci_driver ptp_ocp_driver = { 1867a7e1abadSJonathan Lemon .name = KBUILD_MODNAME, 1868a7e1abadSJonathan Lemon .id_table = ptp_ocp_pcidev_id, 1869a7e1abadSJonathan Lemon .probe = ptp_ocp_probe, 1870a7e1abadSJonathan Lemon .remove = ptp_ocp_remove, 1871a7e1abadSJonathan Lemon }; 1872a7e1abadSJonathan Lemon 1873773bda96SJonathan Lemon static int 1874773bda96SJonathan Lemon ptp_ocp_i2c_notifier_call(struct notifier_block *nb, 1875773bda96SJonathan Lemon unsigned long action, void *data) 1876773bda96SJonathan Lemon { 1877773bda96SJonathan Lemon struct device *dev, *child = data; 1878773bda96SJonathan Lemon struct ptp_ocp *bp; 1879773bda96SJonathan Lemon bool add; 1880773bda96SJonathan Lemon 1881773bda96SJonathan Lemon switch (action) { 1882773bda96SJonathan Lemon case BUS_NOTIFY_ADD_DEVICE: 1883773bda96SJonathan Lemon case BUS_NOTIFY_DEL_DEVICE: 1884773bda96SJonathan Lemon add = action == BUS_NOTIFY_ADD_DEVICE; 1885773bda96SJonathan Lemon break; 1886773bda96SJonathan Lemon default: 1887773bda96SJonathan Lemon return 0; 1888773bda96SJonathan Lemon } 1889773bda96SJonathan Lemon 1890773bda96SJonathan Lemon if (!i2c_verify_adapter(child)) 1891773bda96SJonathan Lemon return 0; 1892773bda96SJonathan Lemon 1893773bda96SJonathan Lemon dev = child; 1894773bda96SJonathan Lemon while ((dev = dev->parent)) 1895773bda96SJonathan Lemon if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME)) 1896773bda96SJonathan Lemon goto found; 1897773bda96SJonathan Lemon return 0; 1898773bda96SJonathan Lemon 1899773bda96SJonathan Lemon found: 1900773bda96SJonathan Lemon bp = dev_get_drvdata(dev); 1901773bda96SJonathan Lemon if (add) 1902773bda96SJonathan Lemon ptp_ocp_symlink(bp, child, "i2c"); 1903773bda96SJonathan Lemon else 1904773bda96SJonathan Lemon sysfs_remove_link(&bp->dev.kobj, "i2c"); 1905773bda96SJonathan Lemon 1906773bda96SJonathan Lemon return 0; 1907773bda96SJonathan Lemon } 1908773bda96SJonathan Lemon 1909773bda96SJonathan Lemon static struct notifier_block ptp_ocp_i2c_notifier = { 1910773bda96SJonathan Lemon .notifier_call = ptp_ocp_i2c_notifier_call, 1911773bda96SJonathan Lemon }; 1912773bda96SJonathan Lemon 1913a7e1abadSJonathan Lemon static int __init 1914a7e1abadSJonathan Lemon ptp_ocp_init(void) 1915a7e1abadSJonathan Lemon { 1916773bda96SJonathan Lemon const char *what; 1917a7e1abadSJonathan Lemon int err; 1918a7e1abadSJonathan Lemon 1919773bda96SJonathan Lemon what = "timecard class"; 1920773bda96SJonathan Lemon err = class_register(&timecard_class); 1921773bda96SJonathan Lemon if (err) 1922773bda96SJonathan Lemon goto out; 1923773bda96SJonathan Lemon 1924773bda96SJonathan Lemon what = "i2c notifier"; 1925773bda96SJonathan Lemon err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 1926773bda96SJonathan Lemon if (err) 1927773bda96SJonathan Lemon goto out_notifier; 1928773bda96SJonathan Lemon 1929773bda96SJonathan Lemon what = "ptp_ocp driver"; 1930a7e1abadSJonathan Lemon err = pci_register_driver(&ptp_ocp_driver); 1931773bda96SJonathan Lemon if (err) 1932773bda96SJonathan Lemon goto out_register; 1933773bda96SJonathan Lemon 1934773bda96SJonathan Lemon return 0; 1935773bda96SJonathan Lemon 1936773bda96SJonathan Lemon out_register: 1937773bda96SJonathan Lemon bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 1938773bda96SJonathan Lemon out_notifier: 1939773bda96SJonathan Lemon class_unregister(&timecard_class); 1940773bda96SJonathan Lemon out: 1941773bda96SJonathan Lemon pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err); 1942a7e1abadSJonathan Lemon return err; 1943a7e1abadSJonathan Lemon } 1944a7e1abadSJonathan Lemon 1945a7e1abadSJonathan Lemon static void __exit 1946a7e1abadSJonathan Lemon ptp_ocp_fini(void) 1947a7e1abadSJonathan Lemon { 1948773bda96SJonathan Lemon bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 1949a7e1abadSJonathan Lemon pci_unregister_driver(&ptp_ocp_driver); 1950773bda96SJonathan Lemon class_unregister(&timecard_class); 1951a7e1abadSJonathan Lemon } 1952a7e1abadSJonathan Lemon 1953a7e1abadSJonathan Lemon module_init(ptp_ocp_init); 1954a7e1abadSJonathan Lemon module_exit(ptp_ocp_fini); 1955a7e1abadSJonathan Lemon 1956a7e1abadSJonathan Lemon MODULE_DESCRIPTION("OpenCompute TimeCard driver"); 1957a7e1abadSJonathan Lemon MODULE_LICENSE("GPL v2"); 1958