1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (c) 2020 Facebook */ 3 4 #include <linux/bits.h> 5 #include <linux/err.h> 6 #include <linux/kernel.h> 7 #include <linux/module.h> 8 #include <linux/debugfs.h> 9 #include <linux/init.h> 10 #include <linux/pci.h> 11 #include <linux/serial_8250.h> 12 #include <linux/clkdev.h> 13 #include <linux/clk-provider.h> 14 #include <linux/platform_device.h> 15 #include <linux/platform_data/i2c-xiic.h> 16 #include <linux/platform_data/i2c-ocores.h> 17 #include <linux/ptp_clock_kernel.h> 18 #include <linux/spi/spi.h> 19 #include <linux/spi/xilinx_spi.h> 20 #include <linux/spi/altera.h> 21 #include <net/devlink.h> 22 #include <linux/i2c.h> 23 #include <linux/mtd/mtd.h> 24 #include <linux/nvmem-consumer.h> 25 #include <linux/crc16.h> 26 #include <linux/dpll.h> 27 28 #define PCI_VENDOR_ID_FACEBOOK 0x1d9b 29 #define PCI_DEVICE_ID_FACEBOOK_TIMECARD 0x0400 30 31 #define PCI_VENDOR_ID_CELESTICA 0x18d4 32 #define PCI_DEVICE_ID_CELESTICA_TIMECARD 0x1008 33 34 #define PCI_VENDOR_ID_OROLIA 0x1ad7 35 #define PCI_DEVICE_ID_OROLIA_ARTCARD 0xa000 36 37 static struct class timecard_class = { 38 .name = "timecard", 39 }; 40 41 struct ocp_reg { 42 u32 ctrl; 43 u32 status; 44 u32 select; 45 u32 version; 46 u32 time_ns; 47 u32 time_sec; 48 u32 __pad0[2]; 49 u32 adjust_ns; 50 u32 adjust_sec; 51 u32 __pad1[2]; 52 u32 offset_ns; 53 u32 offset_window_ns; 54 u32 __pad2[2]; 55 u32 drift_ns; 56 u32 drift_window_ns; 57 u32 __pad3[6]; 58 u32 servo_offset_p; 59 u32 servo_offset_i; 60 u32 servo_drift_p; 61 u32 servo_drift_i; 62 u32 status_offset; 63 u32 status_drift; 64 }; 65 66 #define OCP_CTRL_ENABLE BIT(0) 67 #define OCP_CTRL_ADJUST_TIME BIT(1) 68 #define OCP_CTRL_ADJUST_OFFSET BIT(2) 69 #define OCP_CTRL_ADJUST_DRIFT BIT(3) 70 #define OCP_CTRL_ADJUST_SERVO BIT(8) 71 #define OCP_CTRL_READ_TIME_REQ BIT(30) 72 #define OCP_CTRL_READ_TIME_DONE BIT(31) 73 74 #define OCP_STATUS_IN_SYNC BIT(0) 75 #define OCP_STATUS_IN_HOLDOVER BIT(1) 76 77 #define OCP_SELECT_CLK_NONE 0 78 #define OCP_SELECT_CLK_REG 0xfe 79 80 struct tod_reg { 81 u32 ctrl; 82 u32 status; 83 u32 uart_polarity; 84 u32 version; 85 u32 adj_sec; 86 u32 __pad0[3]; 87 u32 uart_baud; 88 u32 __pad1[3]; 89 u32 utc_status; 90 u32 leap; 91 }; 92 93 #define TOD_CTRL_PROTOCOL BIT(28) 94 #define TOD_CTRL_DISABLE_FMT_A BIT(17) 95 #define TOD_CTRL_DISABLE_FMT_B BIT(16) 96 #define TOD_CTRL_ENABLE BIT(0) 97 #define TOD_CTRL_GNSS_MASK GENMASK(3, 0) 98 #define TOD_CTRL_GNSS_SHIFT 24 99 100 #define TOD_STATUS_UTC_MASK GENMASK(7, 0) 101 #define TOD_STATUS_UTC_VALID BIT(8) 102 #define TOD_STATUS_LEAP_ANNOUNCE BIT(12) 103 #define TOD_STATUS_LEAP_VALID BIT(16) 104 105 struct ts_reg { 106 u32 enable; 107 u32 error; 108 u32 polarity; 109 u32 version; 110 u32 __pad0[4]; 111 u32 cable_delay; 112 u32 __pad1[3]; 113 u32 intr; 114 u32 intr_mask; 115 u32 event_count; 116 u32 __pad2[1]; 117 u32 ts_count; 118 u32 time_ns; 119 u32 time_sec; 120 u32 data_width; 121 u32 data; 122 }; 123 124 struct pps_reg { 125 u32 ctrl; 126 u32 status; 127 u32 __pad0[6]; 128 u32 cable_delay; 129 }; 130 131 #define PPS_STATUS_FILTER_ERR BIT(0) 132 #define PPS_STATUS_SUPERV_ERR BIT(1) 133 134 struct img_reg { 135 u32 version; 136 }; 137 138 struct gpio_reg { 139 u32 gpio1; 140 u32 __pad0; 141 u32 gpio2; 142 u32 __pad1; 143 }; 144 145 struct irig_master_reg { 146 u32 ctrl; 147 u32 status; 148 u32 __pad0; 149 u32 version; 150 u32 adj_sec; 151 u32 mode_ctrl; 152 }; 153 154 #define IRIG_M_CTRL_ENABLE BIT(0) 155 156 struct irig_slave_reg { 157 u32 ctrl; 158 u32 status; 159 u32 __pad0; 160 u32 version; 161 u32 adj_sec; 162 u32 mode_ctrl; 163 }; 164 165 #define IRIG_S_CTRL_ENABLE BIT(0) 166 167 struct dcf_master_reg { 168 u32 ctrl; 169 u32 status; 170 u32 __pad0; 171 u32 version; 172 u32 adj_sec; 173 }; 174 175 #define DCF_M_CTRL_ENABLE BIT(0) 176 177 struct dcf_slave_reg { 178 u32 ctrl; 179 u32 status; 180 u32 __pad0; 181 u32 version; 182 u32 adj_sec; 183 }; 184 185 #define DCF_S_CTRL_ENABLE BIT(0) 186 187 struct signal_reg { 188 u32 enable; 189 u32 status; 190 u32 polarity; 191 u32 version; 192 u32 __pad0[4]; 193 u32 cable_delay; 194 u32 __pad1[3]; 195 u32 intr; 196 u32 intr_mask; 197 u32 __pad2[2]; 198 u32 start_ns; 199 u32 start_sec; 200 u32 pulse_ns; 201 u32 pulse_sec; 202 u32 period_ns; 203 u32 period_sec; 204 u32 repeat_count; 205 }; 206 207 struct frequency_reg { 208 u32 ctrl; 209 u32 status; 210 }; 211 212 struct board_config_reg { 213 u32 mro50_serial_activate; 214 }; 215 216 #define FREQ_STATUS_VALID BIT(31) 217 #define FREQ_STATUS_ERROR BIT(30) 218 #define FREQ_STATUS_OVERRUN BIT(29) 219 #define FREQ_STATUS_MASK GENMASK(23, 0) 220 221 struct ptp_ocp_flash_info { 222 const char *name; 223 int pci_offset; 224 int data_size; 225 void *data; 226 }; 227 228 struct ptp_ocp_firmware_header { 229 char magic[4]; 230 __be16 pci_vendor_id; 231 __be16 pci_device_id; 232 __be32 image_size; 233 __be16 hw_revision; 234 __be16 crc; 235 }; 236 237 #define OCP_FIRMWARE_MAGIC_HEADER "OCPC" 238 239 struct ptp_ocp_i2c_info { 240 const char *name; 241 unsigned long fixed_rate; 242 size_t data_size; 243 void *data; 244 }; 245 246 struct ptp_ocp_ext_info { 247 int index; 248 irqreturn_t (*irq_fcn)(int irq, void *priv); 249 int (*enable)(void *priv, u32 req, bool enable); 250 }; 251 252 struct ptp_ocp_ext_src { 253 void __iomem *mem; 254 struct ptp_ocp *bp; 255 struct ptp_ocp_ext_info *info; 256 int irq_vec; 257 }; 258 259 enum ptp_ocp_sma_mode { 260 SMA_MODE_IN, 261 SMA_MODE_OUT, 262 }; 263 264 static struct dpll_pin_frequency ptp_ocp_sma_freq[] = { 265 DPLL_PIN_FREQUENCY_1PPS, 266 DPLL_PIN_FREQUENCY_10MHZ, 267 DPLL_PIN_FREQUENCY_IRIG_B, 268 DPLL_PIN_FREQUENCY_DCF77, 269 }; 270 271 struct ptp_ocp_sma_connector { 272 enum ptp_ocp_sma_mode mode; 273 bool fixed_fcn; 274 bool fixed_dir; 275 bool disabled; 276 u8 default_fcn; 277 struct dpll_pin *dpll_pin; 278 struct dpll_pin_properties dpll_prop; 279 }; 280 281 struct ocp_attr_group { 282 u64 cap; 283 const struct attribute_group *group; 284 }; 285 286 #define OCP_CAP_BASIC BIT(0) 287 #define OCP_CAP_SIGNAL BIT(1) 288 #define OCP_CAP_FREQ BIT(2) 289 290 struct ptp_ocp_signal { 291 ktime_t period; 292 ktime_t pulse; 293 ktime_t phase; 294 ktime_t start; 295 int duty; 296 bool polarity; 297 bool running; 298 }; 299 300 struct ptp_ocp_serial_port { 301 int line; 302 int baud; 303 }; 304 305 #define OCP_BOARD_ID_LEN 13 306 #define OCP_SERIAL_LEN 6 307 #define OCP_SMA_NUM 4 308 309 struct ptp_ocp { 310 struct pci_dev *pdev; 311 struct device dev; 312 spinlock_t lock; 313 struct ocp_reg __iomem *reg; 314 struct tod_reg __iomem *tod; 315 struct pps_reg __iomem *pps_to_ext; 316 struct pps_reg __iomem *pps_to_clk; 317 struct board_config_reg __iomem *board_config; 318 struct gpio_reg __iomem *pps_select; 319 struct gpio_reg __iomem *sma_map1; 320 struct gpio_reg __iomem *sma_map2; 321 struct irig_master_reg __iomem *irig_out; 322 struct irig_slave_reg __iomem *irig_in; 323 struct dcf_master_reg __iomem *dcf_out; 324 struct dcf_slave_reg __iomem *dcf_in; 325 struct tod_reg __iomem *nmea_out; 326 struct frequency_reg __iomem *freq_in[4]; 327 struct ptp_ocp_ext_src *signal_out[4]; 328 struct ptp_ocp_ext_src *pps; 329 struct ptp_ocp_ext_src *ts0; 330 struct ptp_ocp_ext_src *ts1; 331 struct ptp_ocp_ext_src *ts2; 332 struct ptp_ocp_ext_src *ts3; 333 struct ptp_ocp_ext_src *ts4; 334 struct ocp_art_gpio_reg __iomem *art_sma; 335 struct img_reg __iomem *image; 336 struct ptp_clock *ptp; 337 struct ptp_clock_info ptp_info; 338 struct platform_device *i2c_ctrl; 339 struct platform_device *spi_flash; 340 struct clk_hw *i2c_clk; 341 struct timer_list watchdog; 342 const struct attribute_group **attr_group; 343 const struct ptp_ocp_eeprom_map *eeprom_map; 344 struct dentry *debug_root; 345 bool sync; 346 time64_t gnss_lost; 347 struct delayed_work sync_work; 348 int id; 349 int n_irqs; 350 struct ptp_ocp_serial_port gnss_port; 351 struct ptp_ocp_serial_port gnss2_port; 352 struct ptp_ocp_serial_port mac_port; /* miniature atomic clock */ 353 struct ptp_ocp_serial_port nmea_port; 354 bool fw_loader; 355 u8 fw_tag; 356 u16 fw_version; 357 u8 board_id[OCP_BOARD_ID_LEN]; 358 u8 serial[OCP_SERIAL_LEN]; 359 bool has_eeprom_data; 360 u32 pps_req_map; 361 int flash_start; 362 u32 utc_tai_offset; 363 u32 ts_window_adjust; 364 u64 fw_cap; 365 struct ptp_ocp_signal signal[4]; 366 struct ptp_ocp_sma_connector sma[OCP_SMA_NUM]; 367 const struct ocp_sma_op *sma_op; 368 struct dpll_device *dpll; 369 }; 370 371 #define OCP_REQ_TIMESTAMP BIT(0) 372 #define OCP_REQ_PPS BIT(1) 373 374 struct ocp_resource { 375 unsigned long offset; 376 int size; 377 int irq_vec; 378 int (*setup)(struct ptp_ocp *bp, struct ocp_resource *r); 379 void *extra; 380 unsigned long bp_offset; 381 const char * const name; 382 }; 383 384 static int ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r); 385 static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r); 386 static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r); 387 static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r); 388 static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r); 389 static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r); 390 static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv); 391 static irqreturn_t ptp_ocp_signal_irq(int irq, void *priv); 392 static int ptp_ocp_ts_enable(void *priv, u32 req, bool enable); 393 static int ptp_ocp_signal_from_perout(struct ptp_ocp *bp, int gen, 394 struct ptp_perout_request *req); 395 static int ptp_ocp_signal_enable(void *priv, u32 req, bool enable); 396 static int ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr); 397 398 static int ptp_ocp_art_board_init(struct ptp_ocp *bp, struct ocp_resource *r); 399 400 static const struct ocp_attr_group fb_timecard_groups[]; 401 402 static const struct ocp_attr_group art_timecard_groups[]; 403 404 struct ptp_ocp_eeprom_map { 405 u16 off; 406 u16 len; 407 u32 bp_offset; 408 const void * const tag; 409 }; 410 411 #define EEPROM_ENTRY(addr, member) \ 412 .off = addr, \ 413 .len = sizeof_field(struct ptp_ocp, member), \ 414 .bp_offset = offsetof(struct ptp_ocp, member) 415 416 #define BP_MAP_ENTRY_ADDR(bp, map) ({ \ 417 (void *)((uintptr_t)(bp) + (map)->bp_offset); \ 418 }) 419 420 static struct ptp_ocp_eeprom_map fb_eeprom_map[] = { 421 { EEPROM_ENTRY(0x43, board_id) }, 422 { EEPROM_ENTRY(0x00, serial), .tag = "mac" }, 423 { } 424 }; 425 426 static struct ptp_ocp_eeprom_map art_eeprom_map[] = { 427 { EEPROM_ENTRY(0x200 + 0x43, board_id) }, 428 { EEPROM_ENTRY(0x200 + 0x63, serial) }, 429 { } 430 }; 431 432 #define bp_assign_entry(bp, res, val) ({ \ 433 uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset; \ 434 *(typeof(val) *)addr = val; \ 435 }) 436 437 #define OCP_RES_LOCATION(member) \ 438 .name = #member, .bp_offset = offsetof(struct ptp_ocp, member) 439 440 #define OCP_MEM_RESOURCE(member) \ 441 OCP_RES_LOCATION(member), .setup = ptp_ocp_register_mem 442 443 #define OCP_SERIAL_RESOURCE(member) \ 444 OCP_RES_LOCATION(member), .setup = ptp_ocp_register_serial 445 446 #define OCP_I2C_RESOURCE(member) \ 447 OCP_RES_LOCATION(member), .setup = ptp_ocp_register_i2c 448 449 #define OCP_SPI_RESOURCE(member) \ 450 OCP_RES_LOCATION(member), .setup = ptp_ocp_register_spi 451 452 #define OCP_EXT_RESOURCE(member) \ 453 OCP_RES_LOCATION(member), .setup = ptp_ocp_register_ext 454 455 /* This is the MSI vector mapping used. 456 * 0: PPS (TS5) 457 * 1: TS0 458 * 2: TS1 459 * 3: GNSS1 460 * 4: GNSS2 461 * 5: MAC 462 * 6: TS2 463 * 7: I2C controller 464 * 8: HWICAP (notused) 465 * 9: SPI Flash 466 * 10: NMEA 467 * 11: Signal Generator 1 468 * 12: Signal Generator 2 469 * 13: Signal Generator 3 470 * 14: Signal Generator 4 471 * 15: TS3 472 * 16: TS4 473 -- 474 * 8: Orolia TS1 475 * 10: Orolia TS2 476 * 11: Orolia TS0 (GNSS) 477 * 12: Orolia PPS 478 * 14: Orolia TS3 479 * 15: Orolia TS4 480 */ 481 482 static struct ocp_resource ocp_fb_resource[] = { 483 { 484 OCP_MEM_RESOURCE(reg), 485 .offset = 0x01000000, .size = 0x10000, 486 }, 487 { 488 OCP_EXT_RESOURCE(ts0), 489 .offset = 0x01010000, .size = 0x10000, .irq_vec = 1, 490 .extra = &(struct ptp_ocp_ext_info) { 491 .index = 0, 492 .irq_fcn = ptp_ocp_ts_irq, 493 .enable = ptp_ocp_ts_enable, 494 }, 495 }, 496 { 497 OCP_EXT_RESOURCE(ts1), 498 .offset = 0x01020000, .size = 0x10000, .irq_vec = 2, 499 .extra = &(struct ptp_ocp_ext_info) { 500 .index = 1, 501 .irq_fcn = ptp_ocp_ts_irq, 502 .enable = ptp_ocp_ts_enable, 503 }, 504 }, 505 { 506 OCP_EXT_RESOURCE(ts2), 507 .offset = 0x01060000, .size = 0x10000, .irq_vec = 6, 508 .extra = &(struct ptp_ocp_ext_info) { 509 .index = 2, 510 .irq_fcn = ptp_ocp_ts_irq, 511 .enable = ptp_ocp_ts_enable, 512 }, 513 }, 514 { 515 OCP_EXT_RESOURCE(ts3), 516 .offset = 0x01110000, .size = 0x10000, .irq_vec = 15, 517 .extra = &(struct ptp_ocp_ext_info) { 518 .index = 3, 519 .irq_fcn = ptp_ocp_ts_irq, 520 .enable = ptp_ocp_ts_enable, 521 }, 522 }, 523 { 524 OCP_EXT_RESOURCE(ts4), 525 .offset = 0x01120000, .size = 0x10000, .irq_vec = 16, 526 .extra = &(struct ptp_ocp_ext_info) { 527 .index = 4, 528 .irq_fcn = ptp_ocp_ts_irq, 529 .enable = ptp_ocp_ts_enable, 530 }, 531 }, 532 /* Timestamp for PHC and/or PPS generator */ 533 { 534 OCP_EXT_RESOURCE(pps), 535 .offset = 0x010C0000, .size = 0x10000, .irq_vec = 0, 536 .extra = &(struct ptp_ocp_ext_info) { 537 .index = 5, 538 .irq_fcn = ptp_ocp_ts_irq, 539 .enable = ptp_ocp_ts_enable, 540 }, 541 }, 542 { 543 OCP_EXT_RESOURCE(signal_out[0]), 544 .offset = 0x010D0000, .size = 0x10000, .irq_vec = 11, 545 .extra = &(struct ptp_ocp_ext_info) { 546 .index = 1, 547 .irq_fcn = ptp_ocp_signal_irq, 548 .enable = ptp_ocp_signal_enable, 549 }, 550 }, 551 { 552 OCP_EXT_RESOURCE(signal_out[1]), 553 .offset = 0x010E0000, .size = 0x10000, .irq_vec = 12, 554 .extra = &(struct ptp_ocp_ext_info) { 555 .index = 2, 556 .irq_fcn = ptp_ocp_signal_irq, 557 .enable = ptp_ocp_signal_enable, 558 }, 559 }, 560 { 561 OCP_EXT_RESOURCE(signal_out[2]), 562 .offset = 0x010F0000, .size = 0x10000, .irq_vec = 13, 563 .extra = &(struct ptp_ocp_ext_info) { 564 .index = 3, 565 .irq_fcn = ptp_ocp_signal_irq, 566 .enable = ptp_ocp_signal_enable, 567 }, 568 }, 569 { 570 OCP_EXT_RESOURCE(signal_out[3]), 571 .offset = 0x01100000, .size = 0x10000, .irq_vec = 14, 572 .extra = &(struct ptp_ocp_ext_info) { 573 .index = 4, 574 .irq_fcn = ptp_ocp_signal_irq, 575 .enable = ptp_ocp_signal_enable, 576 }, 577 }, 578 { 579 OCP_MEM_RESOURCE(pps_to_ext), 580 .offset = 0x01030000, .size = 0x10000, 581 }, 582 { 583 OCP_MEM_RESOURCE(pps_to_clk), 584 .offset = 0x01040000, .size = 0x10000, 585 }, 586 { 587 OCP_MEM_RESOURCE(tod), 588 .offset = 0x01050000, .size = 0x10000, 589 }, 590 { 591 OCP_MEM_RESOURCE(irig_in), 592 .offset = 0x01070000, .size = 0x10000, 593 }, 594 { 595 OCP_MEM_RESOURCE(irig_out), 596 .offset = 0x01080000, .size = 0x10000, 597 }, 598 { 599 OCP_MEM_RESOURCE(dcf_in), 600 .offset = 0x01090000, .size = 0x10000, 601 }, 602 { 603 OCP_MEM_RESOURCE(dcf_out), 604 .offset = 0x010A0000, .size = 0x10000, 605 }, 606 { 607 OCP_MEM_RESOURCE(nmea_out), 608 .offset = 0x010B0000, .size = 0x10000, 609 }, 610 { 611 OCP_MEM_RESOURCE(image), 612 .offset = 0x00020000, .size = 0x1000, 613 }, 614 { 615 OCP_MEM_RESOURCE(pps_select), 616 .offset = 0x00130000, .size = 0x1000, 617 }, 618 { 619 OCP_MEM_RESOURCE(sma_map1), 620 .offset = 0x00140000, .size = 0x1000, 621 }, 622 { 623 OCP_MEM_RESOURCE(sma_map2), 624 .offset = 0x00220000, .size = 0x1000, 625 }, 626 { 627 OCP_I2C_RESOURCE(i2c_ctrl), 628 .offset = 0x00150000, .size = 0x10000, .irq_vec = 7, 629 .extra = &(struct ptp_ocp_i2c_info) { 630 .name = "xiic-i2c", 631 .fixed_rate = 50000000, 632 .data_size = sizeof(struct xiic_i2c_platform_data), 633 .data = &(struct xiic_i2c_platform_data) { 634 .num_devices = 2, 635 .devices = (struct i2c_board_info[]) { 636 { I2C_BOARD_INFO("24c02", 0x50) }, 637 { I2C_BOARD_INFO("24mac402", 0x58), 638 .platform_data = "mac" }, 639 }, 640 }, 641 }, 642 }, 643 { 644 OCP_SERIAL_RESOURCE(gnss_port), 645 .offset = 0x00160000 + 0x1000, .irq_vec = 3, 646 .extra = &(struct ptp_ocp_serial_port) { 647 .baud = 115200, 648 }, 649 }, 650 { 651 OCP_SERIAL_RESOURCE(gnss2_port), 652 .offset = 0x00170000 + 0x1000, .irq_vec = 4, 653 .extra = &(struct ptp_ocp_serial_port) { 654 .baud = 115200, 655 }, 656 }, 657 { 658 OCP_SERIAL_RESOURCE(mac_port), 659 .offset = 0x00180000 + 0x1000, .irq_vec = 5, 660 .extra = &(struct ptp_ocp_serial_port) { 661 .baud = 57600, 662 }, 663 }, 664 { 665 OCP_SERIAL_RESOURCE(nmea_port), 666 .offset = 0x00190000 + 0x1000, .irq_vec = 10, 667 }, 668 { 669 OCP_SPI_RESOURCE(spi_flash), 670 .offset = 0x00310000, .size = 0x10000, .irq_vec = 9, 671 .extra = &(struct ptp_ocp_flash_info) { 672 .name = "xilinx_spi", .pci_offset = 0, 673 .data_size = sizeof(struct xspi_platform_data), 674 .data = &(struct xspi_platform_data) { 675 .num_chipselect = 1, 676 .bits_per_word = 8, 677 .num_devices = 1, 678 .force_irq = true, 679 .devices = &(struct spi_board_info) { 680 .modalias = "spi-nor", 681 }, 682 }, 683 }, 684 }, 685 { 686 OCP_MEM_RESOURCE(freq_in[0]), 687 .offset = 0x01200000, .size = 0x10000, 688 }, 689 { 690 OCP_MEM_RESOURCE(freq_in[1]), 691 .offset = 0x01210000, .size = 0x10000, 692 }, 693 { 694 OCP_MEM_RESOURCE(freq_in[2]), 695 .offset = 0x01220000, .size = 0x10000, 696 }, 697 { 698 OCP_MEM_RESOURCE(freq_in[3]), 699 .offset = 0x01230000, .size = 0x10000, 700 }, 701 { 702 .setup = ptp_ocp_fb_board_init, 703 }, 704 { } 705 }; 706 707 #define OCP_ART_CONFIG_SIZE 144 708 #define OCP_ART_TEMP_TABLE_SIZE 368 709 710 struct ocp_art_gpio_reg { 711 struct { 712 u32 gpio; 713 u32 __pad[3]; 714 } map[4]; 715 }; 716 717 static struct ocp_resource ocp_art_resource[] = { 718 { 719 OCP_MEM_RESOURCE(reg), 720 .offset = 0x01000000, .size = 0x10000, 721 }, 722 { 723 OCP_SERIAL_RESOURCE(gnss_port), 724 .offset = 0x00160000 + 0x1000, .irq_vec = 3, 725 .extra = &(struct ptp_ocp_serial_port) { 726 .baud = 115200, 727 }, 728 }, 729 { 730 OCP_MEM_RESOURCE(art_sma), 731 .offset = 0x003C0000, .size = 0x1000, 732 }, 733 /* Timestamp associated with GNSS1 receiver PPS */ 734 { 735 OCP_EXT_RESOURCE(ts0), 736 .offset = 0x360000, .size = 0x20, .irq_vec = 12, 737 .extra = &(struct ptp_ocp_ext_info) { 738 .index = 0, 739 .irq_fcn = ptp_ocp_ts_irq, 740 .enable = ptp_ocp_ts_enable, 741 }, 742 }, 743 { 744 OCP_EXT_RESOURCE(ts1), 745 .offset = 0x380000, .size = 0x20, .irq_vec = 8, 746 .extra = &(struct ptp_ocp_ext_info) { 747 .index = 1, 748 .irq_fcn = ptp_ocp_ts_irq, 749 .enable = ptp_ocp_ts_enable, 750 }, 751 }, 752 { 753 OCP_EXT_RESOURCE(ts2), 754 .offset = 0x390000, .size = 0x20, .irq_vec = 10, 755 .extra = &(struct ptp_ocp_ext_info) { 756 .index = 2, 757 .irq_fcn = ptp_ocp_ts_irq, 758 .enable = ptp_ocp_ts_enable, 759 }, 760 }, 761 { 762 OCP_EXT_RESOURCE(ts3), 763 .offset = 0x3A0000, .size = 0x20, .irq_vec = 14, 764 .extra = &(struct ptp_ocp_ext_info) { 765 .index = 3, 766 .irq_fcn = ptp_ocp_ts_irq, 767 .enable = ptp_ocp_ts_enable, 768 }, 769 }, 770 { 771 OCP_EXT_RESOURCE(ts4), 772 .offset = 0x3B0000, .size = 0x20, .irq_vec = 15, 773 .extra = &(struct ptp_ocp_ext_info) { 774 .index = 4, 775 .irq_fcn = ptp_ocp_ts_irq, 776 .enable = ptp_ocp_ts_enable, 777 }, 778 }, 779 /* Timestamp associated with Internal PPS of the card */ 780 { 781 OCP_EXT_RESOURCE(pps), 782 .offset = 0x00330000, .size = 0x20, .irq_vec = 11, 783 .extra = &(struct ptp_ocp_ext_info) { 784 .index = 5, 785 .irq_fcn = ptp_ocp_ts_irq, 786 .enable = ptp_ocp_ts_enable, 787 }, 788 }, 789 { 790 OCP_SPI_RESOURCE(spi_flash), 791 .offset = 0x00310000, .size = 0x10000, .irq_vec = 9, 792 .extra = &(struct ptp_ocp_flash_info) { 793 .name = "spi_altera", .pci_offset = 0, 794 .data_size = sizeof(struct altera_spi_platform_data), 795 .data = &(struct altera_spi_platform_data) { 796 .num_chipselect = 1, 797 .num_devices = 1, 798 .devices = &(struct spi_board_info) { 799 .modalias = "spi-nor", 800 }, 801 }, 802 }, 803 }, 804 { 805 OCP_I2C_RESOURCE(i2c_ctrl), 806 .offset = 0x350000, .size = 0x100, .irq_vec = 4, 807 .extra = &(struct ptp_ocp_i2c_info) { 808 .name = "ocores-i2c", 809 .fixed_rate = 400000, 810 .data_size = sizeof(struct ocores_i2c_platform_data), 811 .data = &(struct ocores_i2c_platform_data) { 812 .clock_khz = 125000, 813 .bus_khz = 400, 814 .num_devices = 1, 815 .devices = &(struct i2c_board_info) { 816 I2C_BOARD_INFO("24c08", 0x50), 817 }, 818 }, 819 }, 820 }, 821 { 822 OCP_SERIAL_RESOURCE(mac_port), 823 .offset = 0x00190000, .irq_vec = 7, 824 .extra = &(struct ptp_ocp_serial_port) { 825 .baud = 9600, 826 }, 827 }, 828 { 829 OCP_MEM_RESOURCE(board_config), 830 .offset = 0x210000, .size = 0x1000, 831 }, 832 { 833 .setup = ptp_ocp_art_board_init, 834 }, 835 { } 836 }; 837 838 static const struct pci_device_id ptp_ocp_pcidev_id[] = { 839 { PCI_DEVICE_DATA(FACEBOOK, TIMECARD, &ocp_fb_resource) }, 840 { PCI_DEVICE_DATA(CELESTICA, TIMECARD, &ocp_fb_resource) }, 841 { PCI_DEVICE_DATA(OROLIA, ARTCARD, &ocp_art_resource) }, 842 { } 843 }; 844 MODULE_DEVICE_TABLE(pci, ptp_ocp_pcidev_id); 845 846 static DEFINE_MUTEX(ptp_ocp_lock); 847 static DEFINE_IDR(ptp_ocp_idr); 848 849 struct ocp_selector { 850 const char *name; 851 int value; 852 u64 frequency; 853 }; 854 855 static const struct ocp_selector ptp_ocp_clock[] = { 856 { .name = "NONE", .value = 0 }, 857 { .name = "TOD", .value = 1 }, 858 { .name = "IRIG", .value = 2 }, 859 { .name = "PPS", .value = 3 }, 860 { .name = "PTP", .value = 4 }, 861 { .name = "RTC", .value = 5 }, 862 { .name = "DCF", .value = 6 }, 863 { .name = "REGS", .value = 0xfe }, 864 { .name = "EXT", .value = 0xff }, 865 { } 866 }; 867 868 #define SMA_DISABLE BIT(16) 869 #define SMA_ENABLE BIT(15) 870 #define SMA_SELECT_MASK GENMASK(14, 0) 871 872 static const struct ocp_selector ptp_ocp_sma_in[] = { 873 { .name = "10Mhz", .value = 0x0000, .frequency = 10000000 }, 874 { .name = "PPS1", .value = 0x0001, .frequency = 1 }, 875 { .name = "PPS2", .value = 0x0002, .frequency = 1 }, 876 { .name = "TS1", .value = 0x0004, .frequency = 0 }, 877 { .name = "TS2", .value = 0x0008, .frequency = 0 }, 878 { .name = "IRIG", .value = 0x0010, .frequency = 10000 }, 879 { .name = "DCF", .value = 0x0020, .frequency = 77500 }, 880 { .name = "TS3", .value = 0x0040, .frequency = 0 }, 881 { .name = "TS4", .value = 0x0080, .frequency = 0 }, 882 { .name = "FREQ1", .value = 0x0100, .frequency = 0 }, 883 { .name = "FREQ2", .value = 0x0200, .frequency = 0 }, 884 { .name = "FREQ3", .value = 0x0400, .frequency = 0 }, 885 { .name = "FREQ4", .value = 0x0800, .frequency = 0 }, 886 { .name = "None", .value = SMA_DISABLE, .frequency = 0 }, 887 { } 888 }; 889 890 static const struct ocp_selector ptp_ocp_sma_out[] = { 891 { .name = "10Mhz", .value = 0x0000, .frequency = 10000000 }, 892 { .name = "PHC", .value = 0x0001, .frequency = 1 }, 893 { .name = "MAC", .value = 0x0002, .frequency = 1 }, 894 { .name = "GNSS1", .value = 0x0004, .frequency = 1 }, 895 { .name = "GNSS2", .value = 0x0008, .frequency = 1 }, 896 { .name = "IRIG", .value = 0x0010, .frequency = 10000 }, 897 { .name = "DCF", .value = 0x0020, .frequency = 77000 }, 898 { .name = "GEN1", .value = 0x0040 }, 899 { .name = "GEN2", .value = 0x0080 }, 900 { .name = "GEN3", .value = 0x0100 }, 901 { .name = "GEN4", .value = 0x0200 }, 902 { .name = "GND", .value = 0x2000 }, 903 { .name = "VCC", .value = 0x4000 }, 904 { } 905 }; 906 907 static const struct ocp_selector ptp_ocp_art_sma_in[] = { 908 { .name = "PPS1", .value = 0x0001, .frequency = 1 }, 909 { .name = "10Mhz", .value = 0x0008, .frequency = 1000000 }, 910 { } 911 }; 912 913 static const struct ocp_selector ptp_ocp_art_sma_out[] = { 914 { .name = "PHC", .value = 0x0002, .frequency = 1 }, 915 { .name = "GNSS", .value = 0x0004, .frequency = 1 }, 916 { .name = "10Mhz", .value = 0x0010, .frequency = 10000000 }, 917 { } 918 }; 919 920 struct ocp_sma_op { 921 const struct ocp_selector *tbl[2]; 922 void (*init)(struct ptp_ocp *bp); 923 u32 (*get)(struct ptp_ocp *bp, int sma_nr); 924 int (*set_inputs)(struct ptp_ocp *bp, int sma_nr, u32 val); 925 int (*set_output)(struct ptp_ocp *bp, int sma_nr, u32 val); 926 }; 927 928 static void 929 ptp_ocp_sma_init(struct ptp_ocp *bp) 930 { 931 return bp->sma_op->init(bp); 932 } 933 934 static u32 935 ptp_ocp_sma_get(struct ptp_ocp *bp, int sma_nr) 936 { 937 return bp->sma_op->get(bp, sma_nr); 938 } 939 940 static int 941 ptp_ocp_sma_set_inputs(struct ptp_ocp *bp, int sma_nr, u32 val) 942 { 943 return bp->sma_op->set_inputs(bp, sma_nr, val); 944 } 945 946 static int 947 ptp_ocp_sma_set_output(struct ptp_ocp *bp, int sma_nr, u32 val) 948 { 949 return bp->sma_op->set_output(bp, sma_nr, val); 950 } 951 952 static const char * 953 ptp_ocp_select_name_from_val(const struct ocp_selector *tbl, int val) 954 { 955 int i; 956 957 for (i = 0; tbl[i].name; i++) 958 if (tbl[i].value == val) 959 return tbl[i].name; 960 return NULL; 961 } 962 963 static int 964 ptp_ocp_select_val_from_name(const struct ocp_selector *tbl, const char *name) 965 { 966 const char *select; 967 int i; 968 969 for (i = 0; tbl[i].name; i++) { 970 select = tbl[i].name; 971 if (!strncasecmp(name, select, strlen(select))) 972 return tbl[i].value; 973 } 974 return -EINVAL; 975 } 976 977 static ssize_t 978 ptp_ocp_select_table_show(const struct ocp_selector *tbl, char *buf) 979 { 980 ssize_t count; 981 int i; 982 983 count = 0; 984 for (i = 0; tbl[i].name; i++) 985 count += sysfs_emit_at(buf, count, "%s ", tbl[i].name); 986 if (count) 987 count--; 988 count += sysfs_emit_at(buf, count, "\n"); 989 return count; 990 } 991 992 static int 993 __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts, 994 struct ptp_system_timestamp *sts) 995 { 996 u32 ctrl, time_sec, time_ns; 997 int i; 998 999 ptp_read_system_prets(sts); 1000 1001 ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE; 1002 iowrite32(ctrl, &bp->reg->ctrl); 1003 1004 for (i = 0; i < 100; i++) { 1005 ctrl = ioread32(&bp->reg->ctrl); 1006 if (ctrl & OCP_CTRL_READ_TIME_DONE) 1007 break; 1008 } 1009 ptp_read_system_postts(sts); 1010 1011 if (sts && bp->ts_window_adjust) { 1012 s64 ns = timespec64_to_ns(&sts->post_ts); 1013 1014 sts->post_ts = ns_to_timespec64(ns - bp->ts_window_adjust); 1015 } 1016 1017 time_ns = ioread32(&bp->reg->time_ns); 1018 time_sec = ioread32(&bp->reg->time_sec); 1019 1020 ts->tv_sec = time_sec; 1021 ts->tv_nsec = time_ns; 1022 1023 return ctrl & OCP_CTRL_READ_TIME_DONE ? 0 : -ETIMEDOUT; 1024 } 1025 1026 static int 1027 ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts, 1028 struct ptp_system_timestamp *sts) 1029 { 1030 struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 1031 unsigned long flags; 1032 int err; 1033 1034 spin_lock_irqsave(&bp->lock, flags); 1035 err = __ptp_ocp_gettime_locked(bp, ts, sts); 1036 spin_unlock_irqrestore(&bp->lock, flags); 1037 1038 return err; 1039 } 1040 1041 static void 1042 __ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts) 1043 { 1044 u32 ctrl, time_sec, time_ns; 1045 u32 select; 1046 1047 time_ns = ts->tv_nsec; 1048 time_sec = ts->tv_sec; 1049 1050 select = ioread32(&bp->reg->select); 1051 iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 1052 1053 iowrite32(time_ns, &bp->reg->adjust_ns); 1054 iowrite32(time_sec, &bp->reg->adjust_sec); 1055 1056 ctrl = OCP_CTRL_ADJUST_TIME | OCP_CTRL_ENABLE; 1057 iowrite32(ctrl, &bp->reg->ctrl); 1058 1059 /* restore clock selection */ 1060 iowrite32(select >> 16, &bp->reg->select); 1061 } 1062 1063 static int 1064 ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts) 1065 { 1066 struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 1067 unsigned long flags; 1068 1069 spin_lock_irqsave(&bp->lock, flags); 1070 __ptp_ocp_settime_locked(bp, ts); 1071 spin_unlock_irqrestore(&bp->lock, flags); 1072 1073 return 0; 1074 } 1075 1076 static void 1077 __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val) 1078 { 1079 u32 select, ctrl; 1080 1081 select = ioread32(&bp->reg->select); 1082 iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 1083 1084 iowrite32(adj_val, &bp->reg->offset_ns); 1085 iowrite32(NSEC_PER_SEC, &bp->reg->offset_window_ns); 1086 1087 ctrl = OCP_CTRL_ADJUST_OFFSET | OCP_CTRL_ENABLE; 1088 iowrite32(ctrl, &bp->reg->ctrl); 1089 1090 /* restore clock selection */ 1091 iowrite32(select >> 16, &bp->reg->select); 1092 } 1093 1094 static void 1095 ptp_ocp_adjtime_coarse(struct ptp_ocp *bp, s64 delta_ns) 1096 { 1097 struct timespec64 ts; 1098 unsigned long flags; 1099 int err; 1100 1101 spin_lock_irqsave(&bp->lock, flags); 1102 err = __ptp_ocp_gettime_locked(bp, &ts, NULL); 1103 if (likely(!err)) { 1104 set_normalized_timespec64(&ts, ts.tv_sec, 1105 ts.tv_nsec + delta_ns); 1106 __ptp_ocp_settime_locked(bp, &ts); 1107 } 1108 spin_unlock_irqrestore(&bp->lock, flags); 1109 } 1110 1111 static int 1112 ptp_ocp_adjtime(struct ptp_clock_info *ptp_info, s64 delta_ns) 1113 { 1114 struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 1115 unsigned long flags; 1116 u32 adj_ns, sign; 1117 1118 if (delta_ns > NSEC_PER_SEC || -delta_ns > NSEC_PER_SEC) { 1119 ptp_ocp_adjtime_coarse(bp, delta_ns); 1120 return 0; 1121 } 1122 1123 sign = delta_ns < 0 ? BIT(31) : 0; 1124 adj_ns = sign ? -delta_ns : delta_ns; 1125 1126 spin_lock_irqsave(&bp->lock, flags); 1127 __ptp_ocp_adjtime_locked(bp, sign | adj_ns); 1128 spin_unlock_irqrestore(&bp->lock, flags); 1129 1130 return 0; 1131 } 1132 1133 static int 1134 ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm) 1135 { 1136 if (scaled_ppm == 0) 1137 return 0; 1138 1139 return -EOPNOTSUPP; 1140 } 1141 1142 static s32 1143 ptp_ocp_null_getmaxphase(struct ptp_clock_info *ptp_info) 1144 { 1145 return 0; 1146 } 1147 1148 static int 1149 ptp_ocp_null_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns) 1150 { 1151 return -EOPNOTSUPP; 1152 } 1153 1154 static int 1155 ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq, 1156 int on) 1157 { 1158 struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 1159 struct ptp_ocp_ext_src *ext = NULL; 1160 u32 req; 1161 int err; 1162 1163 switch (rq->type) { 1164 case PTP_CLK_REQ_EXTTS: 1165 req = OCP_REQ_TIMESTAMP; 1166 switch (rq->extts.index) { 1167 case 0: 1168 ext = bp->ts0; 1169 break; 1170 case 1: 1171 ext = bp->ts1; 1172 break; 1173 case 2: 1174 ext = bp->ts2; 1175 break; 1176 case 3: 1177 ext = bp->ts3; 1178 break; 1179 case 4: 1180 ext = bp->ts4; 1181 break; 1182 case 5: 1183 ext = bp->pps; 1184 break; 1185 } 1186 break; 1187 case PTP_CLK_REQ_PPS: 1188 req = OCP_REQ_PPS; 1189 ext = bp->pps; 1190 break; 1191 case PTP_CLK_REQ_PEROUT: 1192 switch (rq->perout.index) { 1193 case 0: 1194 /* This is a request for 1PPS on an output SMA. 1195 * Allow, but assume manual configuration. 1196 */ 1197 if (on && (rq->perout.period.sec != 1 || 1198 rq->perout.period.nsec != 0)) 1199 return -EINVAL; 1200 return 0; 1201 case 1: 1202 case 2: 1203 case 3: 1204 case 4: 1205 req = rq->perout.index - 1; 1206 ext = bp->signal_out[req]; 1207 err = ptp_ocp_signal_from_perout(bp, req, &rq->perout); 1208 if (err) 1209 return err; 1210 break; 1211 } 1212 break; 1213 default: 1214 return -EOPNOTSUPP; 1215 } 1216 1217 err = -ENXIO; 1218 if (ext) 1219 err = ext->info->enable(ext, req, on); 1220 1221 return err; 1222 } 1223 1224 static int 1225 ptp_ocp_verify(struct ptp_clock_info *ptp_info, unsigned pin, 1226 enum ptp_pin_function func, unsigned chan) 1227 { 1228 struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 1229 char buf[16]; 1230 1231 switch (func) { 1232 case PTP_PF_NONE: 1233 snprintf(buf, sizeof(buf), "IN: None"); 1234 break; 1235 case PTP_PF_EXTTS: 1236 /* Allow timestamps, but require sysfs configuration. */ 1237 return 0; 1238 case PTP_PF_PEROUT: 1239 /* channel 0 is 1PPS from PHC. 1240 * channels 1..4 are the frequency generators. 1241 */ 1242 if (chan) 1243 snprintf(buf, sizeof(buf), "OUT: GEN%d", chan); 1244 else 1245 snprintf(buf, sizeof(buf), "OUT: PHC"); 1246 break; 1247 default: 1248 return -EOPNOTSUPP; 1249 } 1250 1251 return ptp_ocp_sma_store(bp, buf, pin + 1); 1252 } 1253 1254 static const struct ptp_clock_info ptp_ocp_clock_info = { 1255 .owner = THIS_MODULE, 1256 .name = KBUILD_MODNAME, 1257 .max_adj = 100000000, 1258 .gettimex64 = ptp_ocp_gettimex, 1259 .settime64 = ptp_ocp_settime, 1260 .adjtime = ptp_ocp_adjtime, 1261 .adjfine = ptp_ocp_null_adjfine, 1262 .adjphase = ptp_ocp_null_adjphase, 1263 .getmaxphase = ptp_ocp_null_getmaxphase, 1264 .enable = ptp_ocp_enable, 1265 .verify = ptp_ocp_verify, 1266 .pps = true, 1267 .n_ext_ts = 6, 1268 .n_per_out = 5, 1269 }; 1270 1271 static void 1272 __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp) 1273 { 1274 u32 ctrl, select; 1275 1276 select = ioread32(&bp->reg->select); 1277 iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 1278 1279 iowrite32(0, &bp->reg->drift_ns); 1280 1281 ctrl = OCP_CTRL_ADJUST_DRIFT | OCP_CTRL_ENABLE; 1282 iowrite32(ctrl, &bp->reg->ctrl); 1283 1284 /* restore clock selection */ 1285 iowrite32(select >> 16, &bp->reg->select); 1286 } 1287 1288 static void 1289 ptp_ocp_utc_distribute(struct ptp_ocp *bp, u32 val) 1290 { 1291 unsigned long flags; 1292 1293 spin_lock_irqsave(&bp->lock, flags); 1294 1295 bp->utc_tai_offset = val; 1296 1297 if (bp->irig_out) 1298 iowrite32(val, &bp->irig_out->adj_sec); 1299 if (bp->dcf_out) 1300 iowrite32(val, &bp->dcf_out->adj_sec); 1301 if (bp->nmea_out) 1302 iowrite32(val, &bp->nmea_out->adj_sec); 1303 1304 spin_unlock_irqrestore(&bp->lock, flags); 1305 } 1306 1307 static void 1308 ptp_ocp_watchdog(struct timer_list *t) 1309 { 1310 struct ptp_ocp *bp = from_timer(bp, t, watchdog); 1311 unsigned long flags; 1312 u32 status, utc_offset; 1313 1314 status = ioread32(&bp->pps_to_clk->status); 1315 1316 if (status & PPS_STATUS_SUPERV_ERR) { 1317 iowrite32(status, &bp->pps_to_clk->status); 1318 if (!bp->gnss_lost) { 1319 spin_lock_irqsave(&bp->lock, flags); 1320 __ptp_ocp_clear_drift_locked(bp); 1321 spin_unlock_irqrestore(&bp->lock, flags); 1322 bp->gnss_lost = ktime_get_real_seconds(); 1323 } 1324 1325 } else if (bp->gnss_lost) { 1326 bp->gnss_lost = 0; 1327 } 1328 1329 /* if GNSS provides correct data we can rely on 1330 * it to get leap second information 1331 */ 1332 if (bp->tod) { 1333 status = ioread32(&bp->tod->utc_status); 1334 utc_offset = status & TOD_STATUS_UTC_MASK; 1335 if (status & TOD_STATUS_UTC_VALID && 1336 utc_offset != bp->utc_tai_offset) 1337 ptp_ocp_utc_distribute(bp, utc_offset); 1338 } 1339 1340 mod_timer(&bp->watchdog, jiffies + HZ); 1341 } 1342 1343 static void 1344 ptp_ocp_estimate_pci_timing(struct ptp_ocp *bp) 1345 { 1346 ktime_t start, end; 1347 ktime_t delay; 1348 u32 ctrl; 1349 1350 ctrl = ioread32(&bp->reg->ctrl); 1351 ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE; 1352 1353 iowrite32(ctrl, &bp->reg->ctrl); 1354 1355 start = ktime_get_ns(); 1356 1357 ctrl = ioread32(&bp->reg->ctrl); 1358 1359 end = ktime_get_ns(); 1360 1361 delay = end - start; 1362 bp->ts_window_adjust = (delay >> 5) * 3; 1363 } 1364 1365 static int 1366 ptp_ocp_init_clock(struct ptp_ocp *bp) 1367 { 1368 struct timespec64 ts; 1369 u32 ctrl; 1370 1371 ctrl = OCP_CTRL_ENABLE; 1372 iowrite32(ctrl, &bp->reg->ctrl); 1373 1374 /* NO DRIFT Correction */ 1375 /* offset_p:i 1/8, offset_i: 1/16, drift_p: 0, drift_i: 0 */ 1376 iowrite32(0x2000, &bp->reg->servo_offset_p); 1377 iowrite32(0x1000, &bp->reg->servo_offset_i); 1378 iowrite32(0, &bp->reg->servo_drift_p); 1379 iowrite32(0, &bp->reg->servo_drift_i); 1380 1381 /* latch servo values */ 1382 ctrl |= OCP_CTRL_ADJUST_SERVO; 1383 iowrite32(ctrl, &bp->reg->ctrl); 1384 1385 if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) { 1386 dev_err(&bp->pdev->dev, "clock not enabled\n"); 1387 return -ENODEV; 1388 } 1389 1390 ptp_ocp_estimate_pci_timing(bp); 1391 1392 bp->sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC; 1393 if (!bp->sync) { 1394 ktime_get_clocktai_ts64(&ts); 1395 ptp_ocp_settime(&bp->ptp_info, &ts); 1396 } 1397 1398 /* If there is a clock supervisor, then enable the watchdog */ 1399 if (bp->pps_to_clk) { 1400 timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0); 1401 mod_timer(&bp->watchdog, jiffies + HZ); 1402 } 1403 1404 return 0; 1405 } 1406 1407 static void 1408 ptp_ocp_tod_init(struct ptp_ocp *bp) 1409 { 1410 u32 ctrl, reg; 1411 1412 ctrl = ioread32(&bp->tod->ctrl); 1413 ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE; 1414 ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B); 1415 iowrite32(ctrl, &bp->tod->ctrl); 1416 1417 reg = ioread32(&bp->tod->utc_status); 1418 if (reg & TOD_STATUS_UTC_VALID) 1419 ptp_ocp_utc_distribute(bp, reg & TOD_STATUS_UTC_MASK); 1420 } 1421 1422 static const char * 1423 ptp_ocp_tod_proto_name(const int idx) 1424 { 1425 static const char * const proto_name[] = { 1426 "NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none", 1427 "UBX", "UBX_UTC", "UBX_LS", "UBX_none" 1428 }; 1429 return proto_name[idx]; 1430 } 1431 1432 static const char * 1433 ptp_ocp_tod_gnss_name(int idx) 1434 { 1435 static const char * const gnss_name[] = { 1436 "ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU", 1437 "Unknown" 1438 }; 1439 if (idx >= ARRAY_SIZE(gnss_name)) 1440 idx = ARRAY_SIZE(gnss_name) - 1; 1441 return gnss_name[idx]; 1442 } 1443 1444 struct ptp_ocp_nvmem_match_info { 1445 struct ptp_ocp *bp; 1446 const void * const tag; 1447 }; 1448 1449 static int 1450 ptp_ocp_nvmem_match(struct device *dev, const void *data) 1451 { 1452 const struct ptp_ocp_nvmem_match_info *info = data; 1453 1454 dev = dev->parent; 1455 if (!i2c_verify_client(dev) || info->tag != dev->platform_data) 1456 return 0; 1457 1458 while ((dev = dev->parent)) 1459 if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME)) 1460 return info->bp == dev_get_drvdata(dev); 1461 return 0; 1462 } 1463 1464 static inline struct nvmem_device * 1465 ptp_ocp_nvmem_device_get(struct ptp_ocp *bp, const void * const tag) 1466 { 1467 struct ptp_ocp_nvmem_match_info info = { .bp = bp, .tag = tag }; 1468 1469 return nvmem_device_find(&info, ptp_ocp_nvmem_match); 1470 } 1471 1472 static inline void 1473 ptp_ocp_nvmem_device_put(struct nvmem_device **nvmemp) 1474 { 1475 if (!IS_ERR_OR_NULL(*nvmemp)) 1476 nvmem_device_put(*nvmemp); 1477 *nvmemp = NULL; 1478 } 1479 1480 static void 1481 ptp_ocp_read_eeprom(struct ptp_ocp *bp) 1482 { 1483 const struct ptp_ocp_eeprom_map *map; 1484 struct nvmem_device *nvmem; 1485 const void *tag; 1486 int ret; 1487 1488 if (!bp->i2c_ctrl) 1489 return; 1490 1491 tag = NULL; 1492 nvmem = NULL; 1493 1494 for (map = bp->eeprom_map; map->len; map++) { 1495 if (map->tag != tag) { 1496 tag = map->tag; 1497 ptp_ocp_nvmem_device_put(&nvmem); 1498 } 1499 if (!nvmem) { 1500 nvmem = ptp_ocp_nvmem_device_get(bp, tag); 1501 if (IS_ERR(nvmem)) { 1502 ret = PTR_ERR(nvmem); 1503 goto fail; 1504 } 1505 } 1506 ret = nvmem_device_read(nvmem, map->off, map->len, 1507 BP_MAP_ENTRY_ADDR(bp, map)); 1508 if (ret != map->len) 1509 goto fail; 1510 } 1511 1512 bp->has_eeprom_data = true; 1513 1514 out: 1515 ptp_ocp_nvmem_device_put(&nvmem); 1516 return; 1517 1518 fail: 1519 dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", ret); 1520 goto out; 1521 } 1522 1523 static struct device * 1524 ptp_ocp_find_flash(struct ptp_ocp *bp) 1525 { 1526 struct device *dev, *last; 1527 1528 last = NULL; 1529 dev = &bp->spi_flash->dev; 1530 1531 while ((dev = device_find_any_child(dev))) { 1532 if (!strcmp("mtd", dev_bus_name(dev))) 1533 break; 1534 put_device(last); 1535 last = dev; 1536 } 1537 put_device(last); 1538 1539 return dev; 1540 } 1541 1542 static int 1543 ptp_ocp_devlink_fw_image(struct devlink *devlink, const struct firmware *fw, 1544 const u8 **data, size_t *size) 1545 { 1546 struct ptp_ocp *bp = devlink_priv(devlink); 1547 const struct ptp_ocp_firmware_header *hdr; 1548 size_t offset, length; 1549 u16 crc; 1550 1551 hdr = (const struct ptp_ocp_firmware_header *)fw->data; 1552 if (memcmp(hdr->magic, OCP_FIRMWARE_MAGIC_HEADER, 4)) { 1553 devlink_flash_update_status_notify(devlink, 1554 "No firmware header found, cancel firmware upgrade", 1555 NULL, 0, 0); 1556 return -EINVAL; 1557 } 1558 1559 if (be16_to_cpu(hdr->pci_vendor_id) != bp->pdev->vendor || 1560 be16_to_cpu(hdr->pci_device_id) != bp->pdev->device) { 1561 devlink_flash_update_status_notify(devlink, 1562 "Firmware image compatibility check failed", 1563 NULL, 0, 0); 1564 return -EINVAL; 1565 } 1566 1567 offset = sizeof(*hdr); 1568 length = be32_to_cpu(hdr->image_size); 1569 if (length != (fw->size - offset)) { 1570 devlink_flash_update_status_notify(devlink, 1571 "Firmware image size check failed", 1572 NULL, 0, 0); 1573 return -EINVAL; 1574 } 1575 1576 crc = crc16(0xffff, &fw->data[offset], length); 1577 if (be16_to_cpu(hdr->crc) != crc) { 1578 devlink_flash_update_status_notify(devlink, 1579 "Firmware image CRC check failed", 1580 NULL, 0, 0); 1581 return -EINVAL; 1582 } 1583 1584 *data = &fw->data[offset]; 1585 *size = length; 1586 1587 return 0; 1588 } 1589 1590 static int 1591 ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev, 1592 const struct firmware *fw) 1593 { 1594 struct mtd_info *mtd = dev_get_drvdata(dev); 1595 struct ptp_ocp *bp = devlink_priv(devlink); 1596 size_t off, len, size, resid, wrote; 1597 struct erase_info erase; 1598 size_t base, blksz; 1599 const u8 *data; 1600 int err; 1601 1602 err = ptp_ocp_devlink_fw_image(devlink, fw, &data, &size); 1603 if (err) 1604 goto out; 1605 1606 off = 0; 1607 base = bp->flash_start; 1608 blksz = 4096; 1609 resid = size; 1610 1611 while (resid) { 1612 devlink_flash_update_status_notify(devlink, "Flashing", 1613 NULL, off, size); 1614 1615 len = min_t(size_t, resid, blksz); 1616 erase.addr = base + off; 1617 erase.len = blksz; 1618 1619 err = mtd_erase(mtd, &erase); 1620 if (err) 1621 goto out; 1622 1623 err = mtd_write(mtd, base + off, len, &wrote, data + off); 1624 if (err) 1625 goto out; 1626 1627 off += blksz; 1628 resid -= len; 1629 } 1630 out: 1631 return err; 1632 } 1633 1634 static int 1635 ptp_ocp_devlink_flash_update(struct devlink *devlink, 1636 struct devlink_flash_update_params *params, 1637 struct netlink_ext_ack *extack) 1638 { 1639 struct ptp_ocp *bp = devlink_priv(devlink); 1640 struct device *dev; 1641 const char *msg; 1642 int err; 1643 1644 dev = ptp_ocp_find_flash(bp); 1645 if (!dev) { 1646 dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n"); 1647 return -ENODEV; 1648 } 1649 1650 devlink_flash_update_status_notify(devlink, "Preparing to flash", 1651 NULL, 0, 0); 1652 1653 err = ptp_ocp_devlink_flash(devlink, dev, params->fw); 1654 1655 msg = err ? "Flash error" : "Flash complete"; 1656 devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0); 1657 1658 put_device(dev); 1659 return err; 1660 } 1661 1662 static int 1663 ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req, 1664 struct netlink_ext_ack *extack) 1665 { 1666 struct ptp_ocp *bp = devlink_priv(devlink); 1667 const char *fw_image; 1668 char buf[32]; 1669 int err; 1670 1671 fw_image = bp->fw_loader ? "loader" : "fw"; 1672 sprintf(buf, "%d.%d", bp->fw_tag, bp->fw_version); 1673 err = devlink_info_version_running_put(req, fw_image, buf); 1674 if (err) 1675 return err; 1676 1677 if (!bp->has_eeprom_data) { 1678 ptp_ocp_read_eeprom(bp); 1679 if (!bp->has_eeprom_data) 1680 return 0; 1681 } 1682 1683 sprintf(buf, "%pM", bp->serial); 1684 err = devlink_info_serial_number_put(req, buf); 1685 if (err) 1686 return err; 1687 1688 err = devlink_info_version_fixed_put(req, 1689 DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, 1690 bp->board_id); 1691 if (err) 1692 return err; 1693 1694 return 0; 1695 } 1696 1697 static const struct devlink_ops ptp_ocp_devlink_ops = { 1698 .flash_update = ptp_ocp_devlink_flash_update, 1699 .info_get = ptp_ocp_devlink_info_get, 1700 }; 1701 1702 static void __iomem * 1703 __ptp_ocp_get_mem(struct ptp_ocp *bp, resource_size_t start, int size) 1704 { 1705 struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp"); 1706 1707 return devm_ioremap_resource(&bp->pdev->dev, &res); 1708 } 1709 1710 static void __iomem * 1711 ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r) 1712 { 1713 resource_size_t start; 1714 1715 start = pci_resource_start(bp->pdev, 0) + r->offset; 1716 return __ptp_ocp_get_mem(bp, start, r->size); 1717 } 1718 1719 static void 1720 ptp_ocp_set_irq_resource(struct resource *res, int irq) 1721 { 1722 struct resource r = DEFINE_RES_IRQ(irq); 1723 *res = r; 1724 } 1725 1726 static void 1727 ptp_ocp_set_mem_resource(struct resource *res, resource_size_t start, int size) 1728 { 1729 struct resource r = DEFINE_RES_MEM(start, size); 1730 *res = r; 1731 } 1732 1733 static int 1734 ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r) 1735 { 1736 struct ptp_ocp_flash_info *info; 1737 struct pci_dev *pdev = bp->pdev; 1738 struct platform_device *p; 1739 struct resource res[2]; 1740 resource_size_t start; 1741 int id; 1742 1743 start = pci_resource_start(pdev, 0) + r->offset; 1744 ptp_ocp_set_mem_resource(&res[0], start, r->size); 1745 ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1746 1747 info = r->extra; 1748 id = pci_dev_id(pdev) << 1; 1749 id += info->pci_offset; 1750 1751 p = platform_device_register_resndata(&pdev->dev, info->name, id, 1752 res, 2, info->data, 1753 info->data_size); 1754 if (IS_ERR(p)) 1755 return PTR_ERR(p); 1756 1757 bp_assign_entry(bp, r, p); 1758 1759 return 0; 1760 } 1761 1762 static struct platform_device * 1763 ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id) 1764 { 1765 struct ptp_ocp_i2c_info *info; 1766 struct resource res[2]; 1767 resource_size_t start; 1768 1769 info = r->extra; 1770 start = pci_resource_start(pdev, 0) + r->offset; 1771 ptp_ocp_set_mem_resource(&res[0], start, r->size); 1772 ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1773 1774 return platform_device_register_resndata(&pdev->dev, info->name, 1775 id, res, 2, 1776 info->data, info->data_size); 1777 } 1778 1779 static int 1780 ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r) 1781 { 1782 struct pci_dev *pdev = bp->pdev; 1783 struct ptp_ocp_i2c_info *info; 1784 struct platform_device *p; 1785 struct clk_hw *clk; 1786 char buf[32]; 1787 int id; 1788 1789 info = r->extra; 1790 id = pci_dev_id(bp->pdev); 1791 1792 sprintf(buf, "AXI.%d", id); 1793 clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0, 1794 info->fixed_rate); 1795 if (IS_ERR(clk)) 1796 return PTR_ERR(clk); 1797 bp->i2c_clk = clk; 1798 1799 sprintf(buf, "%s.%d", info->name, id); 1800 devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf); 1801 p = ptp_ocp_i2c_bus(bp->pdev, r, id); 1802 if (IS_ERR(p)) 1803 return PTR_ERR(p); 1804 1805 bp_assign_entry(bp, r, p); 1806 1807 return 0; 1808 } 1809 1810 /* The expectation is that this is triggered only on error. */ 1811 static irqreturn_t 1812 ptp_ocp_signal_irq(int irq, void *priv) 1813 { 1814 struct ptp_ocp_ext_src *ext = priv; 1815 struct signal_reg __iomem *reg = ext->mem; 1816 struct ptp_ocp *bp = ext->bp; 1817 u32 enable, status; 1818 int gen; 1819 1820 gen = ext->info->index - 1; 1821 1822 enable = ioread32(®->enable); 1823 status = ioread32(®->status); 1824 1825 /* disable generator on error */ 1826 if (status || !enable) { 1827 iowrite32(0, ®->intr_mask); 1828 iowrite32(0, ®->enable); 1829 bp->signal[gen].running = false; 1830 } 1831 1832 iowrite32(0, ®->intr); /* ack interrupt */ 1833 1834 return IRQ_HANDLED; 1835 } 1836 1837 static int 1838 ptp_ocp_signal_set(struct ptp_ocp *bp, int gen, struct ptp_ocp_signal *s) 1839 { 1840 struct ptp_system_timestamp sts; 1841 struct timespec64 ts; 1842 ktime_t start_ns; 1843 int err; 1844 1845 if (!s->period) 1846 return 0; 1847 1848 if (!s->pulse) 1849 s->pulse = ktime_divns(s->period * s->duty, 100); 1850 1851 err = ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts); 1852 if (err) 1853 return err; 1854 1855 start_ns = ktime_set(ts.tv_sec, ts.tv_nsec) + NSEC_PER_MSEC; 1856 if (!s->start) { 1857 /* roundup() does not work on 32-bit systems */ 1858 s->start = DIV64_U64_ROUND_UP(start_ns, s->period); 1859 s->start = ktime_add(s->start, s->phase); 1860 } 1861 1862 if (s->duty < 1 || s->duty > 99) 1863 return -EINVAL; 1864 1865 if (s->pulse < 1 || s->pulse > s->period) 1866 return -EINVAL; 1867 1868 if (s->start < start_ns) 1869 return -EINVAL; 1870 1871 bp->signal[gen] = *s; 1872 1873 return 0; 1874 } 1875 1876 static int 1877 ptp_ocp_signal_from_perout(struct ptp_ocp *bp, int gen, 1878 struct ptp_perout_request *req) 1879 { 1880 struct ptp_ocp_signal s = { }; 1881 1882 s.polarity = bp->signal[gen].polarity; 1883 s.period = ktime_set(req->period.sec, req->period.nsec); 1884 if (!s.period) 1885 return 0; 1886 1887 if (req->flags & PTP_PEROUT_DUTY_CYCLE) { 1888 s.pulse = ktime_set(req->on.sec, req->on.nsec); 1889 s.duty = ktime_divns(s.pulse * 100, s.period); 1890 } 1891 1892 if (req->flags & PTP_PEROUT_PHASE) 1893 s.phase = ktime_set(req->phase.sec, req->phase.nsec); 1894 else 1895 s.start = ktime_set(req->start.sec, req->start.nsec); 1896 1897 return ptp_ocp_signal_set(bp, gen, &s); 1898 } 1899 1900 static int 1901 ptp_ocp_signal_enable(void *priv, u32 req, bool enable) 1902 { 1903 struct ptp_ocp_ext_src *ext = priv; 1904 struct signal_reg __iomem *reg = ext->mem; 1905 struct ptp_ocp *bp = ext->bp; 1906 struct timespec64 ts; 1907 int gen; 1908 1909 gen = ext->info->index - 1; 1910 1911 iowrite32(0, ®->intr_mask); 1912 iowrite32(0, ®->enable); 1913 bp->signal[gen].running = false; 1914 if (!enable) 1915 return 0; 1916 1917 ts = ktime_to_timespec64(bp->signal[gen].start); 1918 iowrite32(ts.tv_sec, ®->start_sec); 1919 iowrite32(ts.tv_nsec, ®->start_ns); 1920 1921 ts = ktime_to_timespec64(bp->signal[gen].period); 1922 iowrite32(ts.tv_sec, ®->period_sec); 1923 iowrite32(ts.tv_nsec, ®->period_ns); 1924 1925 ts = ktime_to_timespec64(bp->signal[gen].pulse); 1926 iowrite32(ts.tv_sec, ®->pulse_sec); 1927 iowrite32(ts.tv_nsec, ®->pulse_ns); 1928 1929 iowrite32(bp->signal[gen].polarity, ®->polarity); 1930 iowrite32(0, ®->repeat_count); 1931 1932 iowrite32(0, ®->intr); /* clear interrupt state */ 1933 iowrite32(1, ®->intr_mask); /* enable interrupt */ 1934 iowrite32(3, ®->enable); /* valid & enable */ 1935 1936 bp->signal[gen].running = true; 1937 1938 return 0; 1939 } 1940 1941 static irqreturn_t 1942 ptp_ocp_ts_irq(int irq, void *priv) 1943 { 1944 struct ptp_ocp_ext_src *ext = priv; 1945 struct ts_reg __iomem *reg = ext->mem; 1946 struct ptp_clock_event ev; 1947 u32 sec, nsec; 1948 1949 if (ext == ext->bp->pps) { 1950 if (ext->bp->pps_req_map & OCP_REQ_PPS) { 1951 ev.type = PTP_CLOCK_PPS; 1952 ptp_clock_event(ext->bp->ptp, &ev); 1953 } 1954 1955 if ((ext->bp->pps_req_map & ~OCP_REQ_PPS) == 0) 1956 goto out; 1957 } 1958 1959 /* XXX should fix API - this converts s/ns -> ts -> s/ns */ 1960 sec = ioread32(®->time_sec); 1961 nsec = ioread32(®->time_ns); 1962 1963 ev.type = PTP_CLOCK_EXTTS; 1964 ev.index = ext->info->index; 1965 ev.timestamp = sec * NSEC_PER_SEC + nsec; 1966 1967 ptp_clock_event(ext->bp->ptp, &ev); 1968 1969 out: 1970 iowrite32(1, ®->intr); /* write 1 to ack */ 1971 1972 return IRQ_HANDLED; 1973 } 1974 1975 static int 1976 ptp_ocp_ts_enable(void *priv, u32 req, bool enable) 1977 { 1978 struct ptp_ocp_ext_src *ext = priv; 1979 struct ts_reg __iomem *reg = ext->mem; 1980 struct ptp_ocp *bp = ext->bp; 1981 1982 if (ext == bp->pps) { 1983 u32 old_map = bp->pps_req_map; 1984 1985 if (enable) 1986 bp->pps_req_map |= req; 1987 else 1988 bp->pps_req_map &= ~req; 1989 1990 /* if no state change, just return */ 1991 if ((!!old_map ^ !!bp->pps_req_map) == 0) 1992 return 0; 1993 } 1994 1995 if (enable) { 1996 iowrite32(1, ®->enable); 1997 iowrite32(1, ®->intr_mask); 1998 iowrite32(1, ®->intr); 1999 } else { 2000 iowrite32(0, ®->intr_mask); 2001 iowrite32(0, ®->enable); 2002 } 2003 2004 return 0; 2005 } 2006 2007 static void 2008 ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext) 2009 { 2010 ext->info->enable(ext, ~0, false); 2011 pci_free_irq(ext->bp->pdev, ext->irq_vec, ext); 2012 kfree(ext); 2013 } 2014 2015 static int 2016 ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r) 2017 { 2018 struct pci_dev *pdev = bp->pdev; 2019 struct ptp_ocp_ext_src *ext; 2020 int err; 2021 2022 ext = kzalloc(sizeof(*ext), GFP_KERNEL); 2023 if (!ext) 2024 return -ENOMEM; 2025 2026 ext->mem = ptp_ocp_get_mem(bp, r); 2027 if (IS_ERR(ext->mem)) { 2028 err = PTR_ERR(ext->mem); 2029 goto out; 2030 } 2031 2032 ext->bp = bp; 2033 ext->info = r->extra; 2034 ext->irq_vec = r->irq_vec; 2035 2036 err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL, 2037 ext, "ocp%d.%s", bp->id, r->name); 2038 if (err) { 2039 dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec); 2040 goto out; 2041 } 2042 2043 bp_assign_entry(bp, r, ext); 2044 2045 return 0; 2046 2047 out: 2048 kfree(ext); 2049 return err; 2050 } 2051 2052 static int 2053 ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r) 2054 { 2055 struct pci_dev *pdev = bp->pdev; 2056 struct uart_8250_port uart; 2057 2058 /* Setting UPF_IOREMAP and leaving port.membase unspecified lets 2059 * the serial port device claim and release the pci resource. 2060 */ 2061 memset(&uart, 0, sizeof(uart)); 2062 uart.port.dev = &pdev->dev; 2063 uart.port.iotype = UPIO_MEM; 2064 uart.port.regshift = 2; 2065 uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset; 2066 uart.port.irq = pci_irq_vector(pdev, r->irq_vec); 2067 uart.port.uartclk = 50000000; 2068 uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP | UPF_NO_THRE_TEST; 2069 uart.port.type = PORT_16550A; 2070 2071 return serial8250_register_8250_port(&uart); 2072 } 2073 2074 static int 2075 ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r) 2076 { 2077 struct ptp_ocp_serial_port *p = (struct ptp_ocp_serial_port *)r->extra; 2078 struct ptp_ocp_serial_port port = {}; 2079 2080 port.line = ptp_ocp_serial_line(bp, r); 2081 if (port.line < 0) 2082 return port.line; 2083 2084 if (p) 2085 port.baud = p->baud; 2086 2087 bp_assign_entry(bp, r, port); 2088 2089 return 0; 2090 } 2091 2092 static int 2093 ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r) 2094 { 2095 void __iomem *mem; 2096 2097 mem = ptp_ocp_get_mem(bp, r); 2098 if (IS_ERR(mem)) 2099 return PTR_ERR(mem); 2100 2101 bp_assign_entry(bp, r, mem); 2102 2103 return 0; 2104 } 2105 2106 static void 2107 ptp_ocp_nmea_out_init(struct ptp_ocp *bp) 2108 { 2109 if (!bp->nmea_out) 2110 return; 2111 2112 iowrite32(0, &bp->nmea_out->ctrl); /* disable */ 2113 iowrite32(7, &bp->nmea_out->uart_baud); /* 115200 */ 2114 iowrite32(1, &bp->nmea_out->ctrl); /* enable */ 2115 } 2116 2117 static void 2118 _ptp_ocp_signal_init(struct ptp_ocp_signal *s, struct signal_reg __iomem *reg) 2119 { 2120 u32 val; 2121 2122 iowrite32(0, ®->enable); /* disable */ 2123 2124 val = ioread32(®->polarity); 2125 s->polarity = val ? true : false; 2126 s->duty = 50; 2127 } 2128 2129 static void 2130 ptp_ocp_signal_init(struct ptp_ocp *bp) 2131 { 2132 int i; 2133 2134 for (i = 0; i < 4; i++) 2135 if (bp->signal_out[i]) 2136 _ptp_ocp_signal_init(&bp->signal[i], 2137 bp->signal_out[i]->mem); 2138 } 2139 2140 static void 2141 ptp_ocp_attr_group_del(struct ptp_ocp *bp) 2142 { 2143 sysfs_remove_groups(&bp->dev.kobj, bp->attr_group); 2144 kfree(bp->attr_group); 2145 } 2146 2147 static int 2148 ptp_ocp_attr_group_add(struct ptp_ocp *bp, 2149 const struct ocp_attr_group *attr_tbl) 2150 { 2151 int count, i; 2152 int err; 2153 2154 count = 0; 2155 for (i = 0; attr_tbl[i].cap; i++) 2156 if (attr_tbl[i].cap & bp->fw_cap) 2157 count++; 2158 2159 bp->attr_group = kcalloc(count + 1, sizeof(struct attribute_group *), 2160 GFP_KERNEL); 2161 if (!bp->attr_group) 2162 return -ENOMEM; 2163 2164 count = 0; 2165 for (i = 0; attr_tbl[i].cap; i++) 2166 if (attr_tbl[i].cap & bp->fw_cap) 2167 bp->attr_group[count++] = attr_tbl[i].group; 2168 2169 err = sysfs_create_groups(&bp->dev.kobj, bp->attr_group); 2170 if (err) 2171 bp->attr_group[0] = NULL; 2172 2173 return err; 2174 } 2175 2176 static void 2177 ptp_ocp_enable_fpga(u32 __iomem *reg, u32 bit, bool enable) 2178 { 2179 u32 ctrl; 2180 bool on; 2181 2182 ctrl = ioread32(reg); 2183 on = ctrl & bit; 2184 if (on ^ enable) { 2185 ctrl &= ~bit; 2186 ctrl |= enable ? bit : 0; 2187 iowrite32(ctrl, reg); 2188 } 2189 } 2190 2191 static void 2192 ptp_ocp_irig_out(struct ptp_ocp *bp, bool enable) 2193 { 2194 return ptp_ocp_enable_fpga(&bp->irig_out->ctrl, 2195 IRIG_M_CTRL_ENABLE, enable); 2196 } 2197 2198 static void 2199 ptp_ocp_irig_in(struct ptp_ocp *bp, bool enable) 2200 { 2201 return ptp_ocp_enable_fpga(&bp->irig_in->ctrl, 2202 IRIG_S_CTRL_ENABLE, enable); 2203 } 2204 2205 static void 2206 ptp_ocp_dcf_out(struct ptp_ocp *bp, bool enable) 2207 { 2208 return ptp_ocp_enable_fpga(&bp->dcf_out->ctrl, 2209 DCF_M_CTRL_ENABLE, enable); 2210 } 2211 2212 static void 2213 ptp_ocp_dcf_in(struct ptp_ocp *bp, bool enable) 2214 { 2215 return ptp_ocp_enable_fpga(&bp->dcf_in->ctrl, 2216 DCF_S_CTRL_ENABLE, enable); 2217 } 2218 2219 static void 2220 __handle_signal_outputs(struct ptp_ocp *bp, u32 val) 2221 { 2222 ptp_ocp_irig_out(bp, val & 0x00100010); 2223 ptp_ocp_dcf_out(bp, val & 0x00200020); 2224 } 2225 2226 static void 2227 __handle_signal_inputs(struct ptp_ocp *bp, u32 val) 2228 { 2229 ptp_ocp_irig_in(bp, val & 0x00100010); 2230 ptp_ocp_dcf_in(bp, val & 0x00200020); 2231 } 2232 2233 static u32 2234 ptp_ocp_sma_fb_get(struct ptp_ocp *bp, int sma_nr) 2235 { 2236 u32 __iomem *gpio; 2237 u32 shift; 2238 2239 if (bp->sma[sma_nr - 1].fixed_fcn) 2240 return (sma_nr - 1) & 1; 2241 2242 if (bp->sma[sma_nr - 1].mode == SMA_MODE_IN) 2243 gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1; 2244 else 2245 gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2; 2246 shift = sma_nr & 1 ? 0 : 16; 2247 2248 return (ioread32(gpio) >> shift) & 0xffff; 2249 } 2250 2251 static int 2252 ptp_ocp_sma_fb_set_output(struct ptp_ocp *bp, int sma_nr, u32 val) 2253 { 2254 u32 reg, mask, shift; 2255 unsigned long flags; 2256 u32 __iomem *gpio; 2257 2258 gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2; 2259 shift = sma_nr & 1 ? 0 : 16; 2260 2261 mask = 0xffff << (16 - shift); 2262 2263 spin_lock_irqsave(&bp->lock, flags); 2264 2265 reg = ioread32(gpio); 2266 reg = (reg & mask) | (val << shift); 2267 2268 __handle_signal_outputs(bp, reg); 2269 2270 iowrite32(reg, gpio); 2271 2272 spin_unlock_irqrestore(&bp->lock, flags); 2273 2274 return 0; 2275 } 2276 2277 static int 2278 ptp_ocp_sma_fb_set_inputs(struct ptp_ocp *bp, int sma_nr, u32 val) 2279 { 2280 u32 reg, mask, shift; 2281 unsigned long flags; 2282 u32 __iomem *gpio; 2283 2284 gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1; 2285 shift = sma_nr & 1 ? 0 : 16; 2286 2287 mask = 0xffff << (16 - shift); 2288 2289 spin_lock_irqsave(&bp->lock, flags); 2290 2291 reg = ioread32(gpio); 2292 reg = (reg & mask) | (val << shift); 2293 2294 __handle_signal_inputs(bp, reg); 2295 2296 iowrite32(reg, gpio); 2297 2298 spin_unlock_irqrestore(&bp->lock, flags); 2299 2300 return 0; 2301 } 2302 2303 static void 2304 ptp_ocp_sma_fb_init(struct ptp_ocp *bp) 2305 { 2306 struct dpll_pin_properties prop = { 2307 .board_label = NULL, 2308 .type = DPLL_PIN_TYPE_EXT, 2309 .capabilities = DPLL_PIN_CAPABILITIES_DIRECTION_CAN_CHANGE, 2310 .freq_supported_num = ARRAY_SIZE(ptp_ocp_sma_freq), 2311 .freq_supported = ptp_ocp_sma_freq, 2312 2313 }; 2314 u32 reg; 2315 int i; 2316 2317 /* defaults */ 2318 for (i = 0; i < OCP_SMA_NUM; i++) { 2319 bp->sma[i].default_fcn = i & 1; 2320 bp->sma[i].dpll_prop = prop; 2321 bp->sma[i].dpll_prop.board_label = 2322 bp->ptp_info.pin_config[i].name; 2323 } 2324 bp->sma[0].mode = SMA_MODE_IN; 2325 bp->sma[1].mode = SMA_MODE_IN; 2326 bp->sma[2].mode = SMA_MODE_OUT; 2327 bp->sma[3].mode = SMA_MODE_OUT; 2328 /* If no SMA1 map, the pin functions and directions are fixed. */ 2329 if (!bp->sma_map1) { 2330 for (i = 0; i < OCP_SMA_NUM; i++) { 2331 bp->sma[i].fixed_fcn = true; 2332 bp->sma[i].fixed_dir = true; 2333 bp->sma[1].dpll_prop.capabilities &= 2334 ~DPLL_PIN_CAPABILITIES_DIRECTION_CAN_CHANGE; 2335 } 2336 return; 2337 } 2338 2339 /* If SMA2 GPIO output map is all 1, it is not present. 2340 * This indicates the firmware has fixed direction SMA pins. 2341 */ 2342 reg = ioread32(&bp->sma_map2->gpio2); 2343 if (reg == 0xffffffff) { 2344 for (i = 0; i < OCP_SMA_NUM; i++) 2345 bp->sma[i].fixed_dir = true; 2346 } else { 2347 reg = ioread32(&bp->sma_map1->gpio1); 2348 bp->sma[0].mode = reg & BIT(15) ? SMA_MODE_IN : SMA_MODE_OUT; 2349 bp->sma[1].mode = reg & BIT(31) ? SMA_MODE_IN : SMA_MODE_OUT; 2350 2351 reg = ioread32(&bp->sma_map1->gpio2); 2352 bp->sma[2].mode = reg & BIT(15) ? SMA_MODE_OUT : SMA_MODE_IN; 2353 bp->sma[3].mode = reg & BIT(31) ? SMA_MODE_OUT : SMA_MODE_IN; 2354 } 2355 } 2356 2357 static const struct ocp_sma_op ocp_fb_sma_op = { 2358 .tbl = { ptp_ocp_sma_in, ptp_ocp_sma_out }, 2359 .init = ptp_ocp_sma_fb_init, 2360 .get = ptp_ocp_sma_fb_get, 2361 .set_inputs = ptp_ocp_sma_fb_set_inputs, 2362 .set_output = ptp_ocp_sma_fb_set_output, 2363 }; 2364 2365 static int 2366 ptp_ocp_set_pins(struct ptp_ocp *bp) 2367 { 2368 struct ptp_pin_desc *config; 2369 int i; 2370 2371 config = kcalloc(4, sizeof(*config), GFP_KERNEL); 2372 if (!config) 2373 return -ENOMEM; 2374 2375 for (i = 0; i < 4; i++) { 2376 sprintf(config[i].name, "sma%d", i + 1); 2377 config[i].index = i; 2378 } 2379 2380 bp->ptp_info.n_pins = 4; 2381 bp->ptp_info.pin_config = config; 2382 2383 return 0; 2384 } 2385 2386 static void 2387 ptp_ocp_fb_set_version(struct ptp_ocp *bp) 2388 { 2389 u64 cap = OCP_CAP_BASIC; 2390 u32 version; 2391 2392 version = ioread32(&bp->image->version); 2393 2394 /* if lower 16 bits are empty, this is the fw loader. */ 2395 if ((version & 0xffff) == 0) { 2396 version = version >> 16; 2397 bp->fw_loader = true; 2398 } 2399 2400 bp->fw_tag = version >> 15; 2401 bp->fw_version = version & 0x7fff; 2402 2403 if (bp->fw_tag) { 2404 /* FPGA firmware */ 2405 if (version >= 5) 2406 cap |= OCP_CAP_SIGNAL | OCP_CAP_FREQ; 2407 } else { 2408 /* SOM firmware */ 2409 if (version >= 19) 2410 cap |= OCP_CAP_SIGNAL; 2411 if (version >= 20) 2412 cap |= OCP_CAP_FREQ; 2413 } 2414 2415 bp->fw_cap = cap; 2416 } 2417 2418 /* FB specific board initializers; last "resource" registered. */ 2419 static int 2420 ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r) 2421 { 2422 int err; 2423 2424 bp->flash_start = 1024 * 4096; 2425 bp->eeprom_map = fb_eeprom_map; 2426 bp->fw_version = ioread32(&bp->image->version); 2427 bp->sma_op = &ocp_fb_sma_op; 2428 2429 ptp_ocp_fb_set_version(bp); 2430 2431 ptp_ocp_tod_init(bp); 2432 ptp_ocp_nmea_out_init(bp); 2433 ptp_ocp_signal_init(bp); 2434 2435 err = ptp_ocp_attr_group_add(bp, fb_timecard_groups); 2436 if (err) 2437 return err; 2438 2439 err = ptp_ocp_set_pins(bp); 2440 if (err) 2441 return err; 2442 ptp_ocp_sma_init(bp); 2443 2444 return ptp_ocp_init_clock(bp); 2445 } 2446 2447 static bool 2448 ptp_ocp_allow_irq(struct ptp_ocp *bp, struct ocp_resource *r) 2449 { 2450 bool allow = !r->irq_vec || r->irq_vec < bp->n_irqs; 2451 2452 if (!allow) 2453 dev_err(&bp->pdev->dev, "irq %d out of range, skipping %s\n", 2454 r->irq_vec, r->name); 2455 return allow; 2456 } 2457 2458 static int 2459 ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data) 2460 { 2461 struct ocp_resource *r, *table; 2462 int err = 0; 2463 2464 table = (struct ocp_resource *)driver_data; 2465 for (r = table; r->setup; r++) { 2466 if (!ptp_ocp_allow_irq(bp, r)) 2467 continue; 2468 err = r->setup(bp, r); 2469 if (err) { 2470 dev_err(&bp->pdev->dev, 2471 "Could not register %s: err %d\n", 2472 r->name, err); 2473 break; 2474 } 2475 } 2476 return err; 2477 } 2478 2479 static void 2480 ptp_ocp_art_sma_init(struct ptp_ocp *bp) 2481 { 2482 struct dpll_pin_properties prop = { 2483 .board_label = NULL, 2484 .type = DPLL_PIN_TYPE_EXT, 2485 .capabilities = 0, 2486 .freq_supported_num = ARRAY_SIZE(ptp_ocp_sma_freq), 2487 .freq_supported = ptp_ocp_sma_freq, 2488 2489 }; 2490 u32 reg; 2491 int i; 2492 2493 /* defaults */ 2494 bp->sma[0].mode = SMA_MODE_IN; 2495 bp->sma[1].mode = SMA_MODE_IN; 2496 bp->sma[2].mode = SMA_MODE_OUT; 2497 bp->sma[3].mode = SMA_MODE_OUT; 2498 2499 bp->sma[0].default_fcn = 0x08; /* IN: 10Mhz */ 2500 bp->sma[1].default_fcn = 0x01; /* IN: PPS1 */ 2501 bp->sma[2].default_fcn = 0x10; /* OUT: 10Mhz */ 2502 bp->sma[3].default_fcn = 0x02; /* OUT: PHC */ 2503 2504 for (i = 0; i < OCP_SMA_NUM; i++) { 2505 /* If no SMA map, the pin functions and directions are fixed. */ 2506 bp->sma[i].dpll_prop = prop; 2507 bp->sma[i].dpll_prop.board_label = 2508 bp->ptp_info.pin_config[i].name; 2509 if (!bp->art_sma) { 2510 bp->sma[i].fixed_fcn = true; 2511 bp->sma[i].fixed_dir = true; 2512 continue; 2513 } 2514 reg = ioread32(&bp->art_sma->map[i].gpio); 2515 2516 switch (reg & 0xff) { 2517 case 0: 2518 bp->sma[i].fixed_fcn = true; 2519 bp->sma[i].fixed_dir = true; 2520 break; 2521 case 1: 2522 case 8: 2523 bp->sma[i].mode = SMA_MODE_IN; 2524 bp->sma[i].dpll_prop.capabilities = 2525 DPLL_PIN_CAPABILITIES_DIRECTION_CAN_CHANGE; 2526 break; 2527 default: 2528 bp->sma[i].mode = SMA_MODE_OUT; 2529 bp->sma[i].dpll_prop.capabilities = 2530 DPLL_PIN_CAPABILITIES_DIRECTION_CAN_CHANGE; 2531 break; 2532 } 2533 } 2534 } 2535 2536 static u32 2537 ptp_ocp_art_sma_get(struct ptp_ocp *bp, int sma_nr) 2538 { 2539 if (bp->sma[sma_nr - 1].fixed_fcn) 2540 return bp->sma[sma_nr - 1].default_fcn; 2541 2542 return ioread32(&bp->art_sma->map[sma_nr - 1].gpio) & 0xff; 2543 } 2544 2545 /* note: store 0 is considered invalid. */ 2546 static int 2547 ptp_ocp_art_sma_set(struct ptp_ocp *bp, int sma_nr, u32 val) 2548 { 2549 unsigned long flags; 2550 u32 __iomem *gpio; 2551 int err = 0; 2552 u32 reg; 2553 2554 val &= SMA_SELECT_MASK; 2555 if (hweight32(val) > 1) 2556 return -EINVAL; 2557 2558 gpio = &bp->art_sma->map[sma_nr - 1].gpio; 2559 2560 spin_lock_irqsave(&bp->lock, flags); 2561 reg = ioread32(gpio); 2562 if (((reg >> 16) & val) == 0) { 2563 err = -EOPNOTSUPP; 2564 } else { 2565 reg = (reg & 0xff00) | (val & 0xff); 2566 iowrite32(reg, gpio); 2567 } 2568 spin_unlock_irqrestore(&bp->lock, flags); 2569 2570 return err; 2571 } 2572 2573 static const struct ocp_sma_op ocp_art_sma_op = { 2574 .tbl = { ptp_ocp_art_sma_in, ptp_ocp_art_sma_out }, 2575 .init = ptp_ocp_art_sma_init, 2576 .get = ptp_ocp_art_sma_get, 2577 .set_inputs = ptp_ocp_art_sma_set, 2578 .set_output = ptp_ocp_art_sma_set, 2579 }; 2580 2581 /* ART specific board initializers; last "resource" registered. */ 2582 static int 2583 ptp_ocp_art_board_init(struct ptp_ocp *bp, struct ocp_resource *r) 2584 { 2585 int err; 2586 2587 bp->flash_start = 0x1000000; 2588 bp->eeprom_map = art_eeprom_map; 2589 bp->fw_cap = OCP_CAP_BASIC; 2590 bp->fw_version = ioread32(&bp->reg->version); 2591 bp->fw_tag = 2; 2592 bp->sma_op = &ocp_art_sma_op; 2593 2594 /* Enable MAC serial port during initialisation */ 2595 iowrite32(1, &bp->board_config->mro50_serial_activate); 2596 2597 err = ptp_ocp_set_pins(bp); 2598 if (err) 2599 return err; 2600 ptp_ocp_sma_init(bp); 2601 2602 err = ptp_ocp_attr_group_add(bp, art_timecard_groups); 2603 if (err) 2604 return err; 2605 2606 return ptp_ocp_init_clock(bp); 2607 } 2608 2609 static ssize_t 2610 ptp_ocp_show_output(const struct ocp_selector *tbl, u32 val, char *buf, 2611 int def_val) 2612 { 2613 const char *name; 2614 ssize_t count; 2615 2616 count = sysfs_emit(buf, "OUT: "); 2617 name = ptp_ocp_select_name_from_val(tbl, val); 2618 if (!name) 2619 name = ptp_ocp_select_name_from_val(tbl, def_val); 2620 count += sysfs_emit_at(buf, count, "%s\n", name); 2621 return count; 2622 } 2623 2624 static ssize_t 2625 ptp_ocp_show_inputs(const struct ocp_selector *tbl, u32 val, char *buf, 2626 int def_val) 2627 { 2628 const char *name; 2629 ssize_t count; 2630 int i; 2631 2632 count = sysfs_emit(buf, "IN: "); 2633 for (i = 0; tbl[i].name; i++) { 2634 if (val & tbl[i].value) { 2635 name = tbl[i].name; 2636 count += sysfs_emit_at(buf, count, "%s ", name); 2637 } 2638 } 2639 if (!val && def_val >= 0) { 2640 name = ptp_ocp_select_name_from_val(tbl, def_val); 2641 count += sysfs_emit_at(buf, count, "%s ", name); 2642 } 2643 if (count) 2644 count--; 2645 count += sysfs_emit_at(buf, count, "\n"); 2646 return count; 2647 } 2648 2649 static int 2650 sma_parse_inputs(const struct ocp_selector * const tbl[], const char *buf, 2651 enum ptp_ocp_sma_mode *mode) 2652 { 2653 int idx, count, dir; 2654 char **argv; 2655 int ret; 2656 2657 argv = argv_split(GFP_KERNEL, buf, &count); 2658 if (!argv) 2659 return -ENOMEM; 2660 2661 ret = -EINVAL; 2662 if (!count) 2663 goto out; 2664 2665 idx = 0; 2666 dir = *mode == SMA_MODE_IN ? 0 : 1; 2667 if (!strcasecmp("IN:", argv[0])) { 2668 dir = 0; 2669 idx++; 2670 } 2671 if (!strcasecmp("OUT:", argv[0])) { 2672 dir = 1; 2673 idx++; 2674 } 2675 *mode = dir == 0 ? SMA_MODE_IN : SMA_MODE_OUT; 2676 2677 ret = 0; 2678 for (; idx < count; idx++) 2679 ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]); 2680 if (ret < 0) 2681 ret = -EINVAL; 2682 2683 out: 2684 argv_free(argv); 2685 return ret; 2686 } 2687 2688 static ssize_t 2689 ptp_ocp_sma_show(struct ptp_ocp *bp, int sma_nr, char *buf, 2690 int default_in_val, int default_out_val) 2691 { 2692 struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1]; 2693 const struct ocp_selector * const *tbl; 2694 u32 val; 2695 2696 tbl = bp->sma_op->tbl; 2697 val = ptp_ocp_sma_get(bp, sma_nr) & SMA_SELECT_MASK; 2698 2699 if (sma->mode == SMA_MODE_IN) { 2700 if (sma->disabled) 2701 val = SMA_DISABLE; 2702 return ptp_ocp_show_inputs(tbl[0], val, buf, default_in_val); 2703 } 2704 2705 return ptp_ocp_show_output(tbl[1], val, buf, default_out_val); 2706 } 2707 2708 static ssize_t 2709 sma1_show(struct device *dev, struct device_attribute *attr, char *buf) 2710 { 2711 struct ptp_ocp *bp = dev_get_drvdata(dev); 2712 2713 return ptp_ocp_sma_show(bp, 1, buf, 0, 1); 2714 } 2715 2716 static ssize_t 2717 sma2_show(struct device *dev, struct device_attribute *attr, char *buf) 2718 { 2719 struct ptp_ocp *bp = dev_get_drvdata(dev); 2720 2721 return ptp_ocp_sma_show(bp, 2, buf, -1, 1); 2722 } 2723 2724 static ssize_t 2725 sma3_show(struct device *dev, struct device_attribute *attr, char *buf) 2726 { 2727 struct ptp_ocp *bp = dev_get_drvdata(dev); 2728 2729 return ptp_ocp_sma_show(bp, 3, buf, -1, 0); 2730 } 2731 2732 static ssize_t 2733 sma4_show(struct device *dev, struct device_attribute *attr, char *buf) 2734 { 2735 struct ptp_ocp *bp = dev_get_drvdata(dev); 2736 2737 return ptp_ocp_sma_show(bp, 4, buf, -1, 1); 2738 } 2739 2740 static int 2741 ptp_ocp_sma_store_val(struct ptp_ocp *bp, int val, enum ptp_ocp_sma_mode mode, int sma_nr) 2742 { 2743 struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1]; 2744 2745 if (sma->fixed_dir && (mode != sma->mode || val & SMA_DISABLE)) 2746 return -EOPNOTSUPP; 2747 2748 if (sma->fixed_fcn) { 2749 if (val != sma->default_fcn) 2750 return -EOPNOTSUPP; 2751 return 0; 2752 } 2753 2754 sma->disabled = !!(val & SMA_DISABLE); 2755 2756 if (mode != sma->mode) { 2757 if (mode == SMA_MODE_IN) 2758 ptp_ocp_sma_set_output(bp, sma_nr, 0); 2759 else 2760 ptp_ocp_sma_set_inputs(bp, sma_nr, 0); 2761 sma->mode = mode; 2762 } 2763 2764 if (!sma->fixed_dir) 2765 val |= SMA_ENABLE; /* add enable bit */ 2766 2767 if (sma->disabled) 2768 val = 0; 2769 2770 if (mode == SMA_MODE_IN) 2771 val = ptp_ocp_sma_set_inputs(bp, sma_nr, val); 2772 else 2773 val = ptp_ocp_sma_set_output(bp, sma_nr, val); 2774 2775 return val; 2776 } 2777 2778 static int 2779 ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr) 2780 { 2781 struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1]; 2782 enum ptp_ocp_sma_mode mode; 2783 int val; 2784 2785 mode = sma->mode; 2786 val = sma_parse_inputs(bp->sma_op->tbl, buf, &mode); 2787 if (val < 0) 2788 return val; 2789 return ptp_ocp_sma_store_val(bp, val, mode, sma_nr); 2790 } 2791 2792 static ssize_t 2793 sma1_store(struct device *dev, struct device_attribute *attr, 2794 const char *buf, size_t count) 2795 { 2796 struct ptp_ocp *bp = dev_get_drvdata(dev); 2797 int err; 2798 2799 err = ptp_ocp_sma_store(bp, buf, 1); 2800 return err ? err : count; 2801 } 2802 2803 static ssize_t 2804 sma2_store(struct device *dev, struct device_attribute *attr, 2805 const char *buf, size_t count) 2806 { 2807 struct ptp_ocp *bp = dev_get_drvdata(dev); 2808 int err; 2809 2810 err = ptp_ocp_sma_store(bp, buf, 2); 2811 return err ? err : count; 2812 } 2813 2814 static ssize_t 2815 sma3_store(struct device *dev, struct device_attribute *attr, 2816 const char *buf, size_t count) 2817 { 2818 struct ptp_ocp *bp = dev_get_drvdata(dev); 2819 int err; 2820 2821 err = ptp_ocp_sma_store(bp, buf, 3); 2822 return err ? err : count; 2823 } 2824 2825 static ssize_t 2826 sma4_store(struct device *dev, struct device_attribute *attr, 2827 const char *buf, size_t count) 2828 { 2829 struct ptp_ocp *bp = dev_get_drvdata(dev); 2830 int err; 2831 2832 err = ptp_ocp_sma_store(bp, buf, 4); 2833 return err ? err : count; 2834 } 2835 static DEVICE_ATTR_RW(sma1); 2836 static DEVICE_ATTR_RW(sma2); 2837 static DEVICE_ATTR_RW(sma3); 2838 static DEVICE_ATTR_RW(sma4); 2839 2840 static ssize_t 2841 available_sma_inputs_show(struct device *dev, 2842 struct device_attribute *attr, char *buf) 2843 { 2844 struct ptp_ocp *bp = dev_get_drvdata(dev); 2845 2846 return ptp_ocp_select_table_show(bp->sma_op->tbl[0], buf); 2847 } 2848 static DEVICE_ATTR_RO(available_sma_inputs); 2849 2850 static ssize_t 2851 available_sma_outputs_show(struct device *dev, 2852 struct device_attribute *attr, char *buf) 2853 { 2854 struct ptp_ocp *bp = dev_get_drvdata(dev); 2855 2856 return ptp_ocp_select_table_show(bp->sma_op->tbl[1], buf); 2857 } 2858 static DEVICE_ATTR_RO(available_sma_outputs); 2859 2860 #define EXT_ATTR_RO(_group, _name, _val) \ 2861 struct dev_ext_attribute dev_attr_##_group##_val##_##_name = \ 2862 { __ATTR_RO(_name), (void *)_val } 2863 #define EXT_ATTR_RW(_group, _name, _val) \ 2864 struct dev_ext_attribute dev_attr_##_group##_val##_##_name = \ 2865 { __ATTR_RW(_name), (void *)_val } 2866 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr) 2867 2868 /* period [duty [phase [polarity]]] */ 2869 static ssize_t 2870 signal_store(struct device *dev, struct device_attribute *attr, 2871 const char *buf, size_t count) 2872 { 2873 struct dev_ext_attribute *ea = to_ext_attr(attr); 2874 struct ptp_ocp *bp = dev_get_drvdata(dev); 2875 struct ptp_ocp_signal s = { }; 2876 int gen = (uintptr_t)ea->var; 2877 int argc, err; 2878 char **argv; 2879 2880 argv = argv_split(GFP_KERNEL, buf, &argc); 2881 if (!argv) 2882 return -ENOMEM; 2883 2884 err = -EINVAL; 2885 s.duty = bp->signal[gen].duty; 2886 s.phase = bp->signal[gen].phase; 2887 s.period = bp->signal[gen].period; 2888 s.polarity = bp->signal[gen].polarity; 2889 2890 switch (argc) { 2891 case 4: 2892 argc--; 2893 err = kstrtobool(argv[argc], &s.polarity); 2894 if (err) 2895 goto out; 2896 fallthrough; 2897 case 3: 2898 argc--; 2899 err = kstrtou64(argv[argc], 0, &s.phase); 2900 if (err) 2901 goto out; 2902 fallthrough; 2903 case 2: 2904 argc--; 2905 err = kstrtoint(argv[argc], 0, &s.duty); 2906 if (err) 2907 goto out; 2908 fallthrough; 2909 case 1: 2910 argc--; 2911 err = kstrtou64(argv[argc], 0, &s.period); 2912 if (err) 2913 goto out; 2914 break; 2915 default: 2916 goto out; 2917 } 2918 2919 err = ptp_ocp_signal_set(bp, gen, &s); 2920 if (err) 2921 goto out; 2922 2923 err = ptp_ocp_signal_enable(bp->signal_out[gen], gen, s.period != 0); 2924 2925 out: 2926 argv_free(argv); 2927 return err ? err : count; 2928 } 2929 2930 static ssize_t 2931 signal_show(struct device *dev, struct device_attribute *attr, char *buf) 2932 { 2933 struct dev_ext_attribute *ea = to_ext_attr(attr); 2934 struct ptp_ocp *bp = dev_get_drvdata(dev); 2935 struct ptp_ocp_signal *signal; 2936 struct timespec64 ts; 2937 ssize_t count; 2938 int i; 2939 2940 i = (uintptr_t)ea->var; 2941 signal = &bp->signal[i]; 2942 2943 count = sysfs_emit(buf, "%llu %d %llu %d", signal->period, 2944 signal->duty, signal->phase, signal->polarity); 2945 2946 ts = ktime_to_timespec64(signal->start); 2947 count += sysfs_emit_at(buf, count, " %ptT TAI\n", &ts); 2948 2949 return count; 2950 } 2951 static EXT_ATTR_RW(signal, signal, 0); 2952 static EXT_ATTR_RW(signal, signal, 1); 2953 static EXT_ATTR_RW(signal, signal, 2); 2954 static EXT_ATTR_RW(signal, signal, 3); 2955 2956 static ssize_t 2957 duty_show(struct device *dev, struct device_attribute *attr, char *buf) 2958 { 2959 struct dev_ext_attribute *ea = to_ext_attr(attr); 2960 struct ptp_ocp *bp = dev_get_drvdata(dev); 2961 int i = (uintptr_t)ea->var; 2962 2963 return sysfs_emit(buf, "%d\n", bp->signal[i].duty); 2964 } 2965 static EXT_ATTR_RO(signal, duty, 0); 2966 static EXT_ATTR_RO(signal, duty, 1); 2967 static EXT_ATTR_RO(signal, duty, 2); 2968 static EXT_ATTR_RO(signal, duty, 3); 2969 2970 static ssize_t 2971 period_show(struct device *dev, struct device_attribute *attr, char *buf) 2972 { 2973 struct dev_ext_attribute *ea = to_ext_attr(attr); 2974 struct ptp_ocp *bp = dev_get_drvdata(dev); 2975 int i = (uintptr_t)ea->var; 2976 2977 return sysfs_emit(buf, "%llu\n", bp->signal[i].period); 2978 } 2979 static EXT_ATTR_RO(signal, period, 0); 2980 static EXT_ATTR_RO(signal, period, 1); 2981 static EXT_ATTR_RO(signal, period, 2); 2982 static EXT_ATTR_RO(signal, period, 3); 2983 2984 static ssize_t 2985 phase_show(struct device *dev, struct device_attribute *attr, char *buf) 2986 { 2987 struct dev_ext_attribute *ea = to_ext_attr(attr); 2988 struct ptp_ocp *bp = dev_get_drvdata(dev); 2989 int i = (uintptr_t)ea->var; 2990 2991 return sysfs_emit(buf, "%llu\n", bp->signal[i].phase); 2992 } 2993 static EXT_ATTR_RO(signal, phase, 0); 2994 static EXT_ATTR_RO(signal, phase, 1); 2995 static EXT_ATTR_RO(signal, phase, 2); 2996 static EXT_ATTR_RO(signal, phase, 3); 2997 2998 static ssize_t 2999 polarity_show(struct device *dev, struct device_attribute *attr, 3000 char *buf) 3001 { 3002 struct dev_ext_attribute *ea = to_ext_attr(attr); 3003 struct ptp_ocp *bp = dev_get_drvdata(dev); 3004 int i = (uintptr_t)ea->var; 3005 3006 return sysfs_emit(buf, "%d\n", bp->signal[i].polarity); 3007 } 3008 static EXT_ATTR_RO(signal, polarity, 0); 3009 static EXT_ATTR_RO(signal, polarity, 1); 3010 static EXT_ATTR_RO(signal, polarity, 2); 3011 static EXT_ATTR_RO(signal, polarity, 3); 3012 3013 static ssize_t 3014 running_show(struct device *dev, struct device_attribute *attr, char *buf) 3015 { 3016 struct dev_ext_attribute *ea = to_ext_attr(attr); 3017 struct ptp_ocp *bp = dev_get_drvdata(dev); 3018 int i = (uintptr_t)ea->var; 3019 3020 return sysfs_emit(buf, "%d\n", bp->signal[i].running); 3021 } 3022 static EXT_ATTR_RO(signal, running, 0); 3023 static EXT_ATTR_RO(signal, running, 1); 3024 static EXT_ATTR_RO(signal, running, 2); 3025 static EXT_ATTR_RO(signal, running, 3); 3026 3027 static ssize_t 3028 start_show(struct device *dev, struct device_attribute *attr, char *buf) 3029 { 3030 struct dev_ext_attribute *ea = to_ext_attr(attr); 3031 struct ptp_ocp *bp = dev_get_drvdata(dev); 3032 int i = (uintptr_t)ea->var; 3033 struct timespec64 ts; 3034 3035 ts = ktime_to_timespec64(bp->signal[i].start); 3036 return sysfs_emit(buf, "%llu.%lu\n", ts.tv_sec, ts.tv_nsec); 3037 } 3038 static EXT_ATTR_RO(signal, start, 0); 3039 static EXT_ATTR_RO(signal, start, 1); 3040 static EXT_ATTR_RO(signal, start, 2); 3041 static EXT_ATTR_RO(signal, start, 3); 3042 3043 static ssize_t 3044 seconds_store(struct device *dev, struct device_attribute *attr, 3045 const char *buf, size_t count) 3046 { 3047 struct dev_ext_attribute *ea = to_ext_attr(attr); 3048 struct ptp_ocp *bp = dev_get_drvdata(dev); 3049 int idx = (uintptr_t)ea->var; 3050 u32 val; 3051 int err; 3052 3053 err = kstrtou32(buf, 0, &val); 3054 if (err) 3055 return err; 3056 if (val > 0xff) 3057 return -EINVAL; 3058 3059 if (val) 3060 val = (val << 8) | 0x1; 3061 3062 iowrite32(val, &bp->freq_in[idx]->ctrl); 3063 3064 return count; 3065 } 3066 3067 static ssize_t 3068 seconds_show(struct device *dev, struct device_attribute *attr, char *buf) 3069 { 3070 struct dev_ext_attribute *ea = to_ext_attr(attr); 3071 struct ptp_ocp *bp = dev_get_drvdata(dev); 3072 int idx = (uintptr_t)ea->var; 3073 u32 val; 3074 3075 val = ioread32(&bp->freq_in[idx]->ctrl); 3076 if (val & 1) 3077 val = (val >> 8) & 0xff; 3078 else 3079 val = 0; 3080 3081 return sysfs_emit(buf, "%u\n", val); 3082 } 3083 static EXT_ATTR_RW(freq, seconds, 0); 3084 static EXT_ATTR_RW(freq, seconds, 1); 3085 static EXT_ATTR_RW(freq, seconds, 2); 3086 static EXT_ATTR_RW(freq, seconds, 3); 3087 3088 static ssize_t 3089 frequency_show(struct device *dev, struct device_attribute *attr, char *buf) 3090 { 3091 struct dev_ext_attribute *ea = to_ext_attr(attr); 3092 struct ptp_ocp *bp = dev_get_drvdata(dev); 3093 int idx = (uintptr_t)ea->var; 3094 u32 val; 3095 3096 val = ioread32(&bp->freq_in[idx]->status); 3097 if (val & FREQ_STATUS_ERROR) 3098 return sysfs_emit(buf, "error\n"); 3099 if (val & FREQ_STATUS_OVERRUN) 3100 return sysfs_emit(buf, "overrun\n"); 3101 if (val & FREQ_STATUS_VALID) 3102 return sysfs_emit(buf, "%lu\n", val & FREQ_STATUS_MASK); 3103 return 0; 3104 } 3105 static EXT_ATTR_RO(freq, frequency, 0); 3106 static EXT_ATTR_RO(freq, frequency, 1); 3107 static EXT_ATTR_RO(freq, frequency, 2); 3108 static EXT_ATTR_RO(freq, frequency, 3); 3109 3110 static ssize_t 3111 serialnum_show(struct device *dev, struct device_attribute *attr, char *buf) 3112 { 3113 struct ptp_ocp *bp = dev_get_drvdata(dev); 3114 3115 if (!bp->has_eeprom_data) 3116 ptp_ocp_read_eeprom(bp); 3117 3118 return sysfs_emit(buf, "%pM\n", bp->serial); 3119 } 3120 static DEVICE_ATTR_RO(serialnum); 3121 3122 static ssize_t 3123 gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf) 3124 { 3125 struct ptp_ocp *bp = dev_get_drvdata(dev); 3126 ssize_t ret; 3127 3128 if (bp->gnss_lost) 3129 ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost); 3130 else 3131 ret = sysfs_emit(buf, "SYNC\n"); 3132 3133 return ret; 3134 } 3135 static DEVICE_ATTR_RO(gnss_sync); 3136 3137 static ssize_t 3138 utc_tai_offset_show(struct device *dev, 3139 struct device_attribute *attr, char *buf) 3140 { 3141 struct ptp_ocp *bp = dev_get_drvdata(dev); 3142 3143 return sysfs_emit(buf, "%d\n", bp->utc_tai_offset); 3144 } 3145 3146 static ssize_t 3147 utc_tai_offset_store(struct device *dev, 3148 struct device_attribute *attr, 3149 const char *buf, size_t count) 3150 { 3151 struct ptp_ocp *bp = dev_get_drvdata(dev); 3152 int err; 3153 u32 val; 3154 3155 err = kstrtou32(buf, 0, &val); 3156 if (err) 3157 return err; 3158 3159 ptp_ocp_utc_distribute(bp, val); 3160 3161 return count; 3162 } 3163 static DEVICE_ATTR_RW(utc_tai_offset); 3164 3165 static ssize_t 3166 ts_window_adjust_show(struct device *dev, 3167 struct device_attribute *attr, char *buf) 3168 { 3169 struct ptp_ocp *bp = dev_get_drvdata(dev); 3170 3171 return sysfs_emit(buf, "%d\n", bp->ts_window_adjust); 3172 } 3173 3174 static ssize_t 3175 ts_window_adjust_store(struct device *dev, 3176 struct device_attribute *attr, 3177 const char *buf, size_t count) 3178 { 3179 struct ptp_ocp *bp = dev_get_drvdata(dev); 3180 int err; 3181 u32 val; 3182 3183 err = kstrtou32(buf, 0, &val); 3184 if (err) 3185 return err; 3186 3187 bp->ts_window_adjust = val; 3188 3189 return count; 3190 } 3191 static DEVICE_ATTR_RW(ts_window_adjust); 3192 3193 static ssize_t 3194 irig_b_mode_show(struct device *dev, struct device_attribute *attr, char *buf) 3195 { 3196 struct ptp_ocp *bp = dev_get_drvdata(dev); 3197 u32 val; 3198 3199 val = ioread32(&bp->irig_out->ctrl); 3200 val = (val >> 16) & 0x07; 3201 return sysfs_emit(buf, "%d\n", val); 3202 } 3203 3204 static ssize_t 3205 irig_b_mode_store(struct device *dev, 3206 struct device_attribute *attr, 3207 const char *buf, size_t count) 3208 { 3209 struct ptp_ocp *bp = dev_get_drvdata(dev); 3210 unsigned long flags; 3211 int err; 3212 u32 reg; 3213 u8 val; 3214 3215 err = kstrtou8(buf, 0, &val); 3216 if (err) 3217 return err; 3218 if (val > 7) 3219 return -EINVAL; 3220 3221 reg = ((val & 0x7) << 16); 3222 3223 spin_lock_irqsave(&bp->lock, flags); 3224 iowrite32(0, &bp->irig_out->ctrl); /* disable */ 3225 iowrite32(reg, &bp->irig_out->ctrl); /* change mode */ 3226 iowrite32(reg | IRIG_M_CTRL_ENABLE, &bp->irig_out->ctrl); 3227 spin_unlock_irqrestore(&bp->lock, flags); 3228 3229 return count; 3230 } 3231 static DEVICE_ATTR_RW(irig_b_mode); 3232 3233 static ssize_t 3234 clock_source_show(struct device *dev, struct device_attribute *attr, char *buf) 3235 { 3236 struct ptp_ocp *bp = dev_get_drvdata(dev); 3237 const char *p; 3238 u32 select; 3239 3240 select = ioread32(&bp->reg->select); 3241 p = ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16); 3242 3243 return sysfs_emit(buf, "%s\n", p); 3244 } 3245 3246 static ssize_t 3247 clock_source_store(struct device *dev, struct device_attribute *attr, 3248 const char *buf, size_t count) 3249 { 3250 struct ptp_ocp *bp = dev_get_drvdata(dev); 3251 unsigned long flags; 3252 int val; 3253 3254 val = ptp_ocp_select_val_from_name(ptp_ocp_clock, buf); 3255 if (val < 0) 3256 return val; 3257 3258 spin_lock_irqsave(&bp->lock, flags); 3259 iowrite32(val, &bp->reg->select); 3260 spin_unlock_irqrestore(&bp->lock, flags); 3261 3262 return count; 3263 } 3264 static DEVICE_ATTR_RW(clock_source); 3265 3266 static ssize_t 3267 available_clock_sources_show(struct device *dev, 3268 struct device_attribute *attr, char *buf) 3269 { 3270 return ptp_ocp_select_table_show(ptp_ocp_clock, buf); 3271 } 3272 static DEVICE_ATTR_RO(available_clock_sources); 3273 3274 static ssize_t 3275 clock_status_drift_show(struct device *dev, 3276 struct device_attribute *attr, char *buf) 3277 { 3278 struct ptp_ocp *bp = dev_get_drvdata(dev); 3279 u32 val; 3280 int res; 3281 3282 val = ioread32(&bp->reg->status_drift); 3283 res = (val & ~INT_MAX) ? -1 : 1; 3284 res *= (val & INT_MAX); 3285 return sysfs_emit(buf, "%d\n", res); 3286 } 3287 static DEVICE_ATTR_RO(clock_status_drift); 3288 3289 static ssize_t 3290 clock_status_offset_show(struct device *dev, 3291 struct device_attribute *attr, char *buf) 3292 { 3293 struct ptp_ocp *bp = dev_get_drvdata(dev); 3294 u32 val; 3295 int res; 3296 3297 val = ioread32(&bp->reg->status_offset); 3298 res = (val & ~INT_MAX) ? -1 : 1; 3299 res *= (val & INT_MAX); 3300 return sysfs_emit(buf, "%d\n", res); 3301 } 3302 static DEVICE_ATTR_RO(clock_status_offset); 3303 3304 static ssize_t 3305 tod_correction_show(struct device *dev, 3306 struct device_attribute *attr, char *buf) 3307 { 3308 struct ptp_ocp *bp = dev_get_drvdata(dev); 3309 u32 val; 3310 int res; 3311 3312 val = ioread32(&bp->tod->adj_sec); 3313 res = (val & ~INT_MAX) ? -1 : 1; 3314 res *= (val & INT_MAX); 3315 return sysfs_emit(buf, "%d\n", res); 3316 } 3317 3318 static ssize_t 3319 tod_correction_store(struct device *dev, struct device_attribute *attr, 3320 const char *buf, size_t count) 3321 { 3322 struct ptp_ocp *bp = dev_get_drvdata(dev); 3323 unsigned long flags; 3324 int err, res; 3325 u32 val = 0; 3326 3327 err = kstrtos32(buf, 0, &res); 3328 if (err) 3329 return err; 3330 if (res < 0) { 3331 res *= -1; 3332 val |= BIT(31); 3333 } 3334 val |= res; 3335 3336 spin_lock_irqsave(&bp->lock, flags); 3337 iowrite32(val, &bp->tod->adj_sec); 3338 spin_unlock_irqrestore(&bp->lock, flags); 3339 3340 return count; 3341 } 3342 static DEVICE_ATTR_RW(tod_correction); 3343 3344 #define _DEVICE_SIGNAL_GROUP_ATTRS(_nr) \ 3345 static struct attribute *fb_timecard_signal##_nr##_attrs[] = { \ 3346 &dev_attr_signal##_nr##_signal.attr.attr, \ 3347 &dev_attr_signal##_nr##_duty.attr.attr, \ 3348 &dev_attr_signal##_nr##_phase.attr.attr, \ 3349 &dev_attr_signal##_nr##_period.attr.attr, \ 3350 &dev_attr_signal##_nr##_polarity.attr.attr, \ 3351 &dev_attr_signal##_nr##_running.attr.attr, \ 3352 &dev_attr_signal##_nr##_start.attr.attr, \ 3353 NULL, \ 3354 } 3355 3356 #define DEVICE_SIGNAL_GROUP(_name, _nr) \ 3357 _DEVICE_SIGNAL_GROUP_ATTRS(_nr); \ 3358 static const struct attribute_group \ 3359 fb_timecard_signal##_nr##_group = { \ 3360 .name = #_name, \ 3361 .attrs = fb_timecard_signal##_nr##_attrs, \ 3362 } 3363 3364 DEVICE_SIGNAL_GROUP(gen1, 0); 3365 DEVICE_SIGNAL_GROUP(gen2, 1); 3366 DEVICE_SIGNAL_GROUP(gen3, 2); 3367 DEVICE_SIGNAL_GROUP(gen4, 3); 3368 3369 #define _DEVICE_FREQ_GROUP_ATTRS(_nr) \ 3370 static struct attribute *fb_timecard_freq##_nr##_attrs[] = { \ 3371 &dev_attr_freq##_nr##_seconds.attr.attr, \ 3372 &dev_attr_freq##_nr##_frequency.attr.attr, \ 3373 NULL, \ 3374 } 3375 3376 #define DEVICE_FREQ_GROUP(_name, _nr) \ 3377 _DEVICE_FREQ_GROUP_ATTRS(_nr); \ 3378 static const struct attribute_group \ 3379 fb_timecard_freq##_nr##_group = { \ 3380 .name = #_name, \ 3381 .attrs = fb_timecard_freq##_nr##_attrs, \ 3382 } 3383 3384 DEVICE_FREQ_GROUP(freq1, 0); 3385 DEVICE_FREQ_GROUP(freq2, 1); 3386 DEVICE_FREQ_GROUP(freq3, 2); 3387 DEVICE_FREQ_GROUP(freq4, 3); 3388 3389 static ssize_t 3390 disciplining_config_read(struct file *filp, struct kobject *kobj, 3391 struct bin_attribute *bin_attr, char *buf, 3392 loff_t off, size_t count) 3393 { 3394 struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj)); 3395 size_t size = OCP_ART_CONFIG_SIZE; 3396 struct nvmem_device *nvmem; 3397 ssize_t err; 3398 3399 nvmem = ptp_ocp_nvmem_device_get(bp, NULL); 3400 if (IS_ERR(nvmem)) 3401 return PTR_ERR(nvmem); 3402 3403 if (off > size) { 3404 err = 0; 3405 goto out; 3406 } 3407 3408 if (off + count > size) 3409 count = size - off; 3410 3411 // the configuration is in the very beginning of the EEPROM 3412 err = nvmem_device_read(nvmem, off, count, buf); 3413 if (err != count) { 3414 err = -EFAULT; 3415 goto out; 3416 } 3417 3418 out: 3419 ptp_ocp_nvmem_device_put(&nvmem); 3420 3421 return err; 3422 } 3423 3424 static ssize_t 3425 disciplining_config_write(struct file *filp, struct kobject *kobj, 3426 struct bin_attribute *bin_attr, char *buf, 3427 loff_t off, size_t count) 3428 { 3429 struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj)); 3430 struct nvmem_device *nvmem; 3431 ssize_t err; 3432 3433 /* Allow write of the whole area only */ 3434 if (off || count != OCP_ART_CONFIG_SIZE) 3435 return -EFAULT; 3436 3437 nvmem = ptp_ocp_nvmem_device_get(bp, NULL); 3438 if (IS_ERR(nvmem)) 3439 return PTR_ERR(nvmem); 3440 3441 err = nvmem_device_write(nvmem, 0x00, count, buf); 3442 if (err != count) 3443 err = -EFAULT; 3444 3445 ptp_ocp_nvmem_device_put(&nvmem); 3446 3447 return err; 3448 } 3449 static BIN_ATTR_RW(disciplining_config, OCP_ART_CONFIG_SIZE); 3450 3451 static ssize_t 3452 temperature_table_read(struct file *filp, struct kobject *kobj, 3453 struct bin_attribute *bin_attr, char *buf, 3454 loff_t off, size_t count) 3455 { 3456 struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj)); 3457 size_t size = OCP_ART_TEMP_TABLE_SIZE; 3458 struct nvmem_device *nvmem; 3459 ssize_t err; 3460 3461 nvmem = ptp_ocp_nvmem_device_get(bp, NULL); 3462 if (IS_ERR(nvmem)) 3463 return PTR_ERR(nvmem); 3464 3465 if (off > size) { 3466 err = 0; 3467 goto out; 3468 } 3469 3470 if (off + count > size) 3471 count = size - off; 3472 3473 // the configuration is in the very beginning of the EEPROM 3474 err = nvmem_device_read(nvmem, 0x90 + off, count, buf); 3475 if (err != count) { 3476 err = -EFAULT; 3477 goto out; 3478 } 3479 3480 out: 3481 ptp_ocp_nvmem_device_put(&nvmem); 3482 3483 return err; 3484 } 3485 3486 static ssize_t 3487 temperature_table_write(struct file *filp, struct kobject *kobj, 3488 struct bin_attribute *bin_attr, char *buf, 3489 loff_t off, size_t count) 3490 { 3491 struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj)); 3492 struct nvmem_device *nvmem; 3493 ssize_t err; 3494 3495 /* Allow write of the whole area only */ 3496 if (off || count != OCP_ART_TEMP_TABLE_SIZE) 3497 return -EFAULT; 3498 3499 nvmem = ptp_ocp_nvmem_device_get(bp, NULL); 3500 if (IS_ERR(nvmem)) 3501 return PTR_ERR(nvmem); 3502 3503 err = nvmem_device_write(nvmem, 0x90, count, buf); 3504 if (err != count) 3505 err = -EFAULT; 3506 3507 ptp_ocp_nvmem_device_put(&nvmem); 3508 3509 return err; 3510 } 3511 static BIN_ATTR_RW(temperature_table, OCP_ART_TEMP_TABLE_SIZE); 3512 3513 static struct attribute *fb_timecard_attrs[] = { 3514 &dev_attr_serialnum.attr, 3515 &dev_attr_gnss_sync.attr, 3516 &dev_attr_clock_source.attr, 3517 &dev_attr_available_clock_sources.attr, 3518 &dev_attr_sma1.attr, 3519 &dev_attr_sma2.attr, 3520 &dev_attr_sma3.attr, 3521 &dev_attr_sma4.attr, 3522 &dev_attr_available_sma_inputs.attr, 3523 &dev_attr_available_sma_outputs.attr, 3524 &dev_attr_clock_status_drift.attr, 3525 &dev_attr_clock_status_offset.attr, 3526 &dev_attr_irig_b_mode.attr, 3527 &dev_attr_utc_tai_offset.attr, 3528 &dev_attr_ts_window_adjust.attr, 3529 &dev_attr_tod_correction.attr, 3530 NULL, 3531 }; 3532 3533 static const struct attribute_group fb_timecard_group = { 3534 .attrs = fb_timecard_attrs, 3535 }; 3536 3537 static const struct ocp_attr_group fb_timecard_groups[] = { 3538 { .cap = OCP_CAP_BASIC, .group = &fb_timecard_group }, 3539 { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal0_group }, 3540 { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal1_group }, 3541 { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal2_group }, 3542 { .cap = OCP_CAP_SIGNAL, .group = &fb_timecard_signal3_group }, 3543 { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq0_group }, 3544 { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq1_group }, 3545 { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq2_group }, 3546 { .cap = OCP_CAP_FREQ, .group = &fb_timecard_freq3_group }, 3547 { }, 3548 }; 3549 3550 static struct attribute *art_timecard_attrs[] = { 3551 &dev_attr_serialnum.attr, 3552 &dev_attr_clock_source.attr, 3553 &dev_attr_available_clock_sources.attr, 3554 &dev_attr_utc_tai_offset.attr, 3555 &dev_attr_ts_window_adjust.attr, 3556 &dev_attr_sma1.attr, 3557 &dev_attr_sma2.attr, 3558 &dev_attr_sma3.attr, 3559 &dev_attr_sma4.attr, 3560 &dev_attr_available_sma_inputs.attr, 3561 &dev_attr_available_sma_outputs.attr, 3562 NULL, 3563 }; 3564 3565 static struct bin_attribute *bin_art_timecard_attrs[] = { 3566 &bin_attr_disciplining_config, 3567 &bin_attr_temperature_table, 3568 NULL, 3569 }; 3570 3571 static const struct attribute_group art_timecard_group = { 3572 .attrs = art_timecard_attrs, 3573 .bin_attrs = bin_art_timecard_attrs, 3574 }; 3575 3576 static const struct ocp_attr_group art_timecard_groups[] = { 3577 { .cap = OCP_CAP_BASIC, .group = &art_timecard_group }, 3578 { }, 3579 }; 3580 3581 static void 3582 gpio_input_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit, 3583 const char *def) 3584 { 3585 int i; 3586 3587 for (i = 0; i < 4; i++) { 3588 if (bp->sma[i].mode != SMA_MODE_IN) 3589 continue; 3590 if (map[i][0] & (1 << bit)) { 3591 sprintf(buf, "sma%d", i + 1); 3592 return; 3593 } 3594 } 3595 if (!def) 3596 def = "----"; 3597 strcpy(buf, def); 3598 } 3599 3600 static void 3601 gpio_output_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit) 3602 { 3603 char *ans = buf; 3604 int i; 3605 3606 strcpy(ans, "----"); 3607 for (i = 0; i < 4; i++) { 3608 if (bp->sma[i].mode != SMA_MODE_OUT) 3609 continue; 3610 if (map[i][1] & (1 << bit)) 3611 ans += sprintf(ans, "sma%d ", i + 1); 3612 } 3613 } 3614 3615 static void 3616 _signal_summary_show(struct seq_file *s, struct ptp_ocp *bp, int nr) 3617 { 3618 struct signal_reg __iomem *reg = bp->signal_out[nr]->mem; 3619 struct ptp_ocp_signal *signal = &bp->signal[nr]; 3620 char label[8]; 3621 bool on; 3622 u32 val; 3623 3624 if (!signal) 3625 return; 3626 3627 on = signal->running; 3628 sprintf(label, "GEN%d", nr + 1); 3629 seq_printf(s, "%7s: %s, period:%llu duty:%d%% phase:%llu pol:%d", 3630 label, on ? " ON" : "OFF", 3631 signal->period, signal->duty, signal->phase, 3632 signal->polarity); 3633 3634 val = ioread32(®->enable); 3635 seq_printf(s, " [%x", val); 3636 val = ioread32(®->status); 3637 seq_printf(s, " %x]", val); 3638 3639 seq_printf(s, " start:%llu\n", signal->start); 3640 } 3641 3642 static void 3643 _frequency_summary_show(struct seq_file *s, int nr, 3644 struct frequency_reg __iomem *reg) 3645 { 3646 char label[8]; 3647 bool on; 3648 u32 val; 3649 3650 if (!reg) 3651 return; 3652 3653 sprintf(label, "FREQ%d", nr + 1); 3654 val = ioread32(®->ctrl); 3655 on = val & 1; 3656 val = (val >> 8) & 0xff; 3657 seq_printf(s, "%7s: %s, sec:%u", 3658 label, 3659 on ? " ON" : "OFF", 3660 val); 3661 3662 val = ioread32(®->status); 3663 if (val & FREQ_STATUS_ERROR) 3664 seq_printf(s, ", error"); 3665 if (val & FREQ_STATUS_OVERRUN) 3666 seq_printf(s, ", overrun"); 3667 if (val & FREQ_STATUS_VALID) 3668 seq_printf(s, ", freq %lu Hz", val & FREQ_STATUS_MASK); 3669 seq_printf(s, " reg:%x\n", val); 3670 } 3671 3672 static int 3673 ptp_ocp_summary_show(struct seq_file *s, void *data) 3674 { 3675 struct device *dev = s->private; 3676 struct ptp_system_timestamp sts; 3677 struct ts_reg __iomem *ts_reg; 3678 char *buf, *src, *mac_src; 3679 struct timespec64 ts; 3680 struct ptp_ocp *bp; 3681 u16 sma_val[4][2]; 3682 u32 ctrl, val; 3683 bool on, map; 3684 int i; 3685 3686 buf = (char *)__get_free_page(GFP_KERNEL); 3687 if (!buf) 3688 return -ENOMEM; 3689 3690 bp = dev_get_drvdata(dev); 3691 3692 seq_printf(s, "%7s: /dev/ptp%d\n", "PTP", ptp_clock_index(bp->ptp)); 3693 if (bp->gnss_port.line != -1) 3694 seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS1", 3695 bp->gnss_port.line); 3696 if (bp->gnss2_port.line != -1) 3697 seq_printf(s, "%7s: /dev/ttyS%d\n", "GNSS2", 3698 bp->gnss2_port.line); 3699 if (bp->mac_port.line != -1) 3700 seq_printf(s, "%7s: /dev/ttyS%d\n", "MAC", bp->mac_port.line); 3701 if (bp->nmea_port.line != -1) 3702 seq_printf(s, "%7s: /dev/ttyS%d\n", "NMEA", bp->nmea_port.line); 3703 3704 memset(sma_val, 0xff, sizeof(sma_val)); 3705 if (bp->sma_map1) { 3706 u32 reg; 3707 3708 reg = ioread32(&bp->sma_map1->gpio1); 3709 sma_val[0][0] = reg & 0xffff; 3710 sma_val[1][0] = reg >> 16; 3711 3712 reg = ioread32(&bp->sma_map1->gpio2); 3713 sma_val[2][1] = reg & 0xffff; 3714 sma_val[3][1] = reg >> 16; 3715 3716 reg = ioread32(&bp->sma_map2->gpio1); 3717 sma_val[2][0] = reg & 0xffff; 3718 sma_val[3][0] = reg >> 16; 3719 3720 reg = ioread32(&bp->sma_map2->gpio2); 3721 sma_val[0][1] = reg & 0xffff; 3722 sma_val[1][1] = reg >> 16; 3723 } 3724 3725 sma1_show(dev, NULL, buf); 3726 seq_printf(s, " sma1: %04x,%04x %s", 3727 sma_val[0][0], sma_val[0][1], buf); 3728 3729 sma2_show(dev, NULL, buf); 3730 seq_printf(s, " sma2: %04x,%04x %s", 3731 sma_val[1][0], sma_val[1][1], buf); 3732 3733 sma3_show(dev, NULL, buf); 3734 seq_printf(s, " sma3: %04x,%04x %s", 3735 sma_val[2][0], sma_val[2][1], buf); 3736 3737 sma4_show(dev, NULL, buf); 3738 seq_printf(s, " sma4: %04x,%04x %s", 3739 sma_val[3][0], sma_val[3][1], buf); 3740 3741 if (bp->ts0) { 3742 ts_reg = bp->ts0->mem; 3743 on = ioread32(&ts_reg->enable); 3744 src = "GNSS1"; 3745 seq_printf(s, "%7s: %s, src: %s\n", "TS0", 3746 on ? " ON" : "OFF", src); 3747 } 3748 3749 if (bp->ts1) { 3750 ts_reg = bp->ts1->mem; 3751 on = ioread32(&ts_reg->enable); 3752 gpio_input_map(buf, bp, sma_val, 2, NULL); 3753 seq_printf(s, "%7s: %s, src: %s\n", "TS1", 3754 on ? " ON" : "OFF", buf); 3755 } 3756 3757 if (bp->ts2) { 3758 ts_reg = bp->ts2->mem; 3759 on = ioread32(&ts_reg->enable); 3760 gpio_input_map(buf, bp, sma_val, 3, NULL); 3761 seq_printf(s, "%7s: %s, src: %s\n", "TS2", 3762 on ? " ON" : "OFF", buf); 3763 } 3764 3765 if (bp->ts3) { 3766 ts_reg = bp->ts3->mem; 3767 on = ioread32(&ts_reg->enable); 3768 gpio_input_map(buf, bp, sma_val, 6, NULL); 3769 seq_printf(s, "%7s: %s, src: %s\n", "TS3", 3770 on ? " ON" : "OFF", buf); 3771 } 3772 3773 if (bp->ts4) { 3774 ts_reg = bp->ts4->mem; 3775 on = ioread32(&ts_reg->enable); 3776 gpio_input_map(buf, bp, sma_val, 7, NULL); 3777 seq_printf(s, "%7s: %s, src: %s\n", "TS4", 3778 on ? " ON" : "OFF", buf); 3779 } 3780 3781 if (bp->pps) { 3782 ts_reg = bp->pps->mem; 3783 src = "PHC"; 3784 on = ioread32(&ts_reg->enable); 3785 map = !!(bp->pps_req_map & OCP_REQ_TIMESTAMP); 3786 seq_printf(s, "%7s: %s, src: %s\n", "TS5", 3787 on && map ? " ON" : "OFF", src); 3788 3789 map = !!(bp->pps_req_map & OCP_REQ_PPS); 3790 seq_printf(s, "%7s: %s, src: %s\n", "PPS", 3791 on && map ? " ON" : "OFF", src); 3792 } 3793 3794 if (bp->fw_cap & OCP_CAP_SIGNAL) 3795 for (i = 0; i < 4; i++) 3796 _signal_summary_show(s, bp, i); 3797 3798 if (bp->fw_cap & OCP_CAP_FREQ) 3799 for (i = 0; i < 4; i++) 3800 _frequency_summary_show(s, i, bp->freq_in[i]); 3801 3802 if (bp->irig_out) { 3803 ctrl = ioread32(&bp->irig_out->ctrl); 3804 on = ctrl & IRIG_M_CTRL_ENABLE; 3805 val = ioread32(&bp->irig_out->status); 3806 gpio_output_map(buf, bp, sma_val, 4); 3807 seq_printf(s, "%7s: %s, error: %d, mode %d, out: %s\n", "IRIG", 3808 on ? " ON" : "OFF", val, (ctrl >> 16), buf); 3809 } 3810 3811 if (bp->irig_in) { 3812 on = ioread32(&bp->irig_in->ctrl) & IRIG_S_CTRL_ENABLE; 3813 val = ioread32(&bp->irig_in->status); 3814 gpio_input_map(buf, bp, sma_val, 4, NULL); 3815 seq_printf(s, "%7s: %s, error: %d, src: %s\n", "IRIG in", 3816 on ? " ON" : "OFF", val, buf); 3817 } 3818 3819 if (bp->dcf_out) { 3820 on = ioread32(&bp->dcf_out->ctrl) & DCF_M_CTRL_ENABLE; 3821 val = ioread32(&bp->dcf_out->status); 3822 gpio_output_map(buf, bp, sma_val, 5); 3823 seq_printf(s, "%7s: %s, error: %d, out: %s\n", "DCF", 3824 on ? " ON" : "OFF", val, buf); 3825 } 3826 3827 if (bp->dcf_in) { 3828 on = ioread32(&bp->dcf_in->ctrl) & DCF_S_CTRL_ENABLE; 3829 val = ioread32(&bp->dcf_in->status); 3830 gpio_input_map(buf, bp, sma_val, 5, NULL); 3831 seq_printf(s, "%7s: %s, error: %d, src: %s\n", "DCF in", 3832 on ? " ON" : "OFF", val, buf); 3833 } 3834 3835 if (bp->nmea_out) { 3836 on = ioread32(&bp->nmea_out->ctrl) & 1; 3837 val = ioread32(&bp->nmea_out->status); 3838 seq_printf(s, "%7s: %s, error: %d\n", "NMEA", 3839 on ? " ON" : "OFF", val); 3840 } 3841 3842 /* compute src for PPS1, used below. */ 3843 if (bp->pps_select) { 3844 val = ioread32(&bp->pps_select->gpio1); 3845 src = &buf[80]; 3846 mac_src = "GNSS1"; 3847 if (val & 0x01) { 3848 gpio_input_map(src, bp, sma_val, 0, NULL); 3849 mac_src = src; 3850 } else if (val & 0x02) { 3851 src = "MAC"; 3852 } else if (val & 0x04) { 3853 src = "GNSS1"; 3854 } else { 3855 src = "----"; 3856 mac_src = src; 3857 } 3858 } else { 3859 src = "?"; 3860 mac_src = src; 3861 } 3862 seq_printf(s, "MAC PPS1 src: %s\n", mac_src); 3863 3864 gpio_input_map(buf, bp, sma_val, 1, "GNSS2"); 3865 seq_printf(s, "MAC PPS2 src: %s\n", buf); 3866 3867 /* assumes automatic switchover/selection */ 3868 val = ioread32(&bp->reg->select); 3869 switch (val >> 16) { 3870 case 0: 3871 sprintf(buf, "----"); 3872 break; 3873 case 2: 3874 sprintf(buf, "IRIG"); 3875 break; 3876 case 3: 3877 sprintf(buf, "%s via PPS1", src); 3878 break; 3879 case 6: 3880 sprintf(buf, "DCF"); 3881 break; 3882 default: 3883 strcpy(buf, "unknown"); 3884 break; 3885 } 3886 seq_printf(s, "%7s: %s, state: %s\n", "PHC src", buf, 3887 bp->sync ? "sync" : "unsynced"); 3888 3889 if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts)) { 3890 struct timespec64 sys_ts; 3891 s64 pre_ns, post_ns, ns; 3892 3893 pre_ns = timespec64_to_ns(&sts.pre_ts); 3894 post_ns = timespec64_to_ns(&sts.post_ts); 3895 ns = (pre_ns + post_ns) / 2; 3896 ns += (s64)bp->utc_tai_offset * NSEC_PER_SEC; 3897 sys_ts = ns_to_timespec64(ns); 3898 3899 seq_printf(s, "%7s: %lld.%ld == %ptT TAI\n", "PHC", 3900 ts.tv_sec, ts.tv_nsec, &ts); 3901 seq_printf(s, "%7s: %lld.%ld == %ptT UTC offset %d\n", "SYS", 3902 sys_ts.tv_sec, sys_ts.tv_nsec, &sys_ts, 3903 bp->utc_tai_offset); 3904 seq_printf(s, "%7s: PHC:SYS offset: %lld window: %lld\n", "", 3905 timespec64_to_ns(&ts) - ns, 3906 post_ns - pre_ns); 3907 } 3908 3909 free_page((unsigned long)buf); 3910 return 0; 3911 } 3912 DEFINE_SHOW_ATTRIBUTE(ptp_ocp_summary); 3913 3914 static int 3915 ptp_ocp_tod_status_show(struct seq_file *s, void *data) 3916 { 3917 struct device *dev = s->private; 3918 struct ptp_ocp *bp; 3919 u32 val; 3920 int idx; 3921 3922 bp = dev_get_drvdata(dev); 3923 3924 val = ioread32(&bp->tod->ctrl); 3925 if (!(val & TOD_CTRL_ENABLE)) { 3926 seq_printf(s, "TOD Slave disabled\n"); 3927 return 0; 3928 } 3929 seq_printf(s, "TOD Slave enabled, Control Register 0x%08X\n", val); 3930 3931 idx = val & TOD_CTRL_PROTOCOL ? 4 : 0; 3932 idx += (val >> 16) & 3; 3933 seq_printf(s, "Protocol %s\n", ptp_ocp_tod_proto_name(idx)); 3934 3935 idx = (val >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK; 3936 seq_printf(s, "GNSS %s\n", ptp_ocp_tod_gnss_name(idx)); 3937 3938 val = ioread32(&bp->tod->version); 3939 seq_printf(s, "TOD Version %d.%d.%d\n", 3940 val >> 24, (val >> 16) & 0xff, val & 0xffff); 3941 3942 val = ioread32(&bp->tod->status); 3943 seq_printf(s, "Status register: 0x%08X\n", val); 3944 3945 val = ioread32(&bp->tod->adj_sec); 3946 idx = (val & ~INT_MAX) ? -1 : 1; 3947 idx *= (val & INT_MAX); 3948 seq_printf(s, "Correction seconds: %d\n", idx); 3949 3950 val = ioread32(&bp->tod->utc_status); 3951 seq_printf(s, "UTC status register: 0x%08X\n", val); 3952 seq_printf(s, "UTC offset: %ld valid:%d\n", 3953 val & TOD_STATUS_UTC_MASK, val & TOD_STATUS_UTC_VALID ? 1 : 0); 3954 seq_printf(s, "Leap second info valid:%d, Leap second announce %d\n", 3955 val & TOD_STATUS_LEAP_VALID ? 1 : 0, 3956 val & TOD_STATUS_LEAP_ANNOUNCE ? 1 : 0); 3957 3958 val = ioread32(&bp->tod->leap); 3959 seq_printf(s, "Time to next leap second (in sec): %d\n", (s32) val); 3960 3961 return 0; 3962 } 3963 DEFINE_SHOW_ATTRIBUTE(ptp_ocp_tod_status); 3964 3965 static struct dentry *ptp_ocp_debugfs_root; 3966 3967 static void 3968 ptp_ocp_debugfs_add_device(struct ptp_ocp *bp) 3969 { 3970 struct dentry *d; 3971 3972 d = debugfs_create_dir(dev_name(&bp->dev), ptp_ocp_debugfs_root); 3973 bp->debug_root = d; 3974 debugfs_create_file("summary", 0444, bp->debug_root, 3975 &bp->dev, &ptp_ocp_summary_fops); 3976 if (bp->tod) 3977 debugfs_create_file("tod_status", 0444, bp->debug_root, 3978 &bp->dev, &ptp_ocp_tod_status_fops); 3979 } 3980 3981 static void 3982 ptp_ocp_debugfs_remove_device(struct ptp_ocp *bp) 3983 { 3984 debugfs_remove_recursive(bp->debug_root); 3985 } 3986 3987 static void 3988 ptp_ocp_debugfs_init(void) 3989 { 3990 ptp_ocp_debugfs_root = debugfs_create_dir("timecard", NULL); 3991 } 3992 3993 static void 3994 ptp_ocp_debugfs_fini(void) 3995 { 3996 debugfs_remove_recursive(ptp_ocp_debugfs_root); 3997 } 3998 3999 static void 4000 ptp_ocp_dev_release(struct device *dev) 4001 { 4002 struct ptp_ocp *bp = dev_get_drvdata(dev); 4003 4004 mutex_lock(&ptp_ocp_lock); 4005 idr_remove(&ptp_ocp_idr, bp->id); 4006 mutex_unlock(&ptp_ocp_lock); 4007 } 4008 4009 static int 4010 ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev) 4011 { 4012 int err; 4013 4014 mutex_lock(&ptp_ocp_lock); 4015 err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL); 4016 mutex_unlock(&ptp_ocp_lock); 4017 if (err < 0) { 4018 dev_err(&pdev->dev, "idr_alloc failed: %d\n", err); 4019 return err; 4020 } 4021 bp->id = err; 4022 4023 bp->ptp_info = ptp_ocp_clock_info; 4024 spin_lock_init(&bp->lock); 4025 bp->gnss_port.line = -1; 4026 bp->gnss2_port.line = -1; 4027 bp->mac_port.line = -1; 4028 bp->nmea_port.line = -1; 4029 bp->pdev = pdev; 4030 4031 device_initialize(&bp->dev); 4032 dev_set_name(&bp->dev, "ocp%d", bp->id); 4033 bp->dev.class = &timecard_class; 4034 bp->dev.parent = &pdev->dev; 4035 bp->dev.release = ptp_ocp_dev_release; 4036 dev_set_drvdata(&bp->dev, bp); 4037 4038 err = device_add(&bp->dev); 4039 if (err) { 4040 dev_err(&bp->dev, "device add failed: %d\n", err); 4041 goto out; 4042 } 4043 4044 pci_set_drvdata(pdev, bp); 4045 4046 return 0; 4047 4048 out: 4049 put_device(&bp->dev); 4050 return err; 4051 } 4052 4053 static void 4054 ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link) 4055 { 4056 struct device *dev = &bp->dev; 4057 4058 if (sysfs_create_link(&dev->kobj, &child->kobj, link)) 4059 dev_err(dev, "%s symlink failed\n", link); 4060 } 4061 4062 static void 4063 ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link) 4064 { 4065 struct device *dev, *child; 4066 4067 dev = &bp->pdev->dev; 4068 4069 child = device_find_child_by_name(dev, name); 4070 if (!child) { 4071 dev_err(dev, "Could not find device %s\n", name); 4072 return; 4073 } 4074 4075 ptp_ocp_symlink(bp, child, link); 4076 put_device(child); 4077 } 4078 4079 static int 4080 ptp_ocp_complete(struct ptp_ocp *bp) 4081 { 4082 struct pps_device *pps; 4083 char buf[32]; 4084 4085 if (bp->gnss_port.line != -1) { 4086 sprintf(buf, "ttyS%d", bp->gnss_port.line); 4087 ptp_ocp_link_child(bp, buf, "ttyGNSS"); 4088 } 4089 if (bp->gnss2_port.line != -1) { 4090 sprintf(buf, "ttyS%d", bp->gnss2_port.line); 4091 ptp_ocp_link_child(bp, buf, "ttyGNSS2"); 4092 } 4093 if (bp->mac_port.line != -1) { 4094 sprintf(buf, "ttyS%d", bp->mac_port.line); 4095 ptp_ocp_link_child(bp, buf, "ttyMAC"); 4096 } 4097 if (bp->nmea_port.line != -1) { 4098 sprintf(buf, "ttyS%d", bp->nmea_port.line); 4099 ptp_ocp_link_child(bp, buf, "ttyNMEA"); 4100 } 4101 sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp)); 4102 ptp_ocp_link_child(bp, buf, "ptp"); 4103 4104 pps = pps_lookup_dev(bp->ptp); 4105 if (pps) 4106 ptp_ocp_symlink(bp, pps->dev, "pps"); 4107 4108 ptp_ocp_debugfs_add_device(bp); 4109 4110 return 0; 4111 } 4112 4113 static void 4114 ptp_ocp_phc_info(struct ptp_ocp *bp) 4115 { 4116 struct timespec64 ts; 4117 u32 version, select; 4118 4119 version = ioread32(&bp->reg->version); 4120 select = ioread32(&bp->reg->select); 4121 dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n", 4122 version >> 24, (version >> 16) & 0xff, version & 0xffff, 4123 ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16), 4124 ptp_clock_index(bp->ptp)); 4125 4126 if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL)) 4127 dev_info(&bp->pdev->dev, "Time: %lld.%ld, %s\n", 4128 ts.tv_sec, ts.tv_nsec, 4129 bp->sync ? "in-sync" : "UNSYNCED"); 4130 } 4131 4132 static void 4133 ptp_ocp_serial_info(struct device *dev, const char *name, int port, int baud) 4134 { 4135 if (port != -1) 4136 dev_info(dev, "%5s: /dev/ttyS%-2d @ %6d\n", name, port, baud); 4137 } 4138 4139 static void 4140 ptp_ocp_info(struct ptp_ocp *bp) 4141 { 4142 static int nmea_baud[] = { 4143 1200, 2400, 4800, 9600, 19200, 38400, 4144 57600, 115200, 230400, 460800, 921600, 4145 1000000, 2000000 4146 }; 4147 struct device *dev = &bp->pdev->dev; 4148 u32 reg; 4149 4150 ptp_ocp_phc_info(bp); 4151 4152 ptp_ocp_serial_info(dev, "GNSS", bp->gnss_port.line, 4153 bp->gnss_port.baud); 4154 ptp_ocp_serial_info(dev, "GNSS2", bp->gnss2_port.line, 4155 bp->gnss2_port.baud); 4156 ptp_ocp_serial_info(dev, "MAC", bp->mac_port.line, bp->mac_port.baud); 4157 if (bp->nmea_out && bp->nmea_port.line != -1) { 4158 bp->nmea_port.baud = -1; 4159 4160 reg = ioread32(&bp->nmea_out->uart_baud); 4161 if (reg < ARRAY_SIZE(nmea_baud)) 4162 bp->nmea_port.baud = nmea_baud[reg]; 4163 4164 ptp_ocp_serial_info(dev, "NMEA", bp->nmea_port.line, 4165 bp->nmea_port.baud); 4166 } 4167 } 4168 4169 static void 4170 ptp_ocp_detach_sysfs(struct ptp_ocp *bp) 4171 { 4172 struct device *dev = &bp->dev; 4173 4174 sysfs_remove_link(&dev->kobj, "ttyGNSS"); 4175 sysfs_remove_link(&dev->kobj, "ttyGNSS2"); 4176 sysfs_remove_link(&dev->kobj, "ttyMAC"); 4177 sysfs_remove_link(&dev->kobj, "ptp"); 4178 sysfs_remove_link(&dev->kobj, "pps"); 4179 } 4180 4181 static void 4182 ptp_ocp_detach(struct ptp_ocp *bp) 4183 { 4184 int i; 4185 4186 ptp_ocp_debugfs_remove_device(bp); 4187 ptp_ocp_detach_sysfs(bp); 4188 ptp_ocp_attr_group_del(bp); 4189 if (timer_pending(&bp->watchdog)) 4190 del_timer_sync(&bp->watchdog); 4191 if (bp->ts0) 4192 ptp_ocp_unregister_ext(bp->ts0); 4193 if (bp->ts1) 4194 ptp_ocp_unregister_ext(bp->ts1); 4195 if (bp->ts2) 4196 ptp_ocp_unregister_ext(bp->ts2); 4197 if (bp->ts3) 4198 ptp_ocp_unregister_ext(bp->ts3); 4199 if (bp->ts4) 4200 ptp_ocp_unregister_ext(bp->ts4); 4201 if (bp->pps) 4202 ptp_ocp_unregister_ext(bp->pps); 4203 for (i = 0; i < 4; i++) 4204 if (bp->signal_out[i]) 4205 ptp_ocp_unregister_ext(bp->signal_out[i]); 4206 if (bp->gnss_port.line != -1) 4207 serial8250_unregister_port(bp->gnss_port.line); 4208 if (bp->gnss2_port.line != -1) 4209 serial8250_unregister_port(bp->gnss2_port.line); 4210 if (bp->mac_port.line != -1) 4211 serial8250_unregister_port(bp->mac_port.line); 4212 if (bp->nmea_port.line != -1) 4213 serial8250_unregister_port(bp->nmea_port.line); 4214 platform_device_unregister(bp->spi_flash); 4215 platform_device_unregister(bp->i2c_ctrl); 4216 if (bp->i2c_clk) 4217 clk_hw_unregister_fixed_rate(bp->i2c_clk); 4218 if (bp->n_irqs) 4219 pci_free_irq_vectors(bp->pdev); 4220 if (bp->ptp) 4221 ptp_clock_unregister(bp->ptp); 4222 kfree(bp->ptp_info.pin_config); 4223 device_unregister(&bp->dev); 4224 } 4225 4226 static int ptp_ocp_dpll_lock_status_get(const struct dpll_device *dpll, 4227 void *priv, 4228 enum dpll_lock_status *status, 4229 struct netlink_ext_ack *extack) 4230 { 4231 struct ptp_ocp *bp = priv; 4232 4233 *status = bp->sync ? DPLL_LOCK_STATUS_LOCKED : DPLL_LOCK_STATUS_UNLOCKED; 4234 4235 return 0; 4236 } 4237 4238 static int ptp_ocp_dpll_state_get(const struct dpll_pin *pin, void *pin_priv, 4239 const struct dpll_device *dpll, void *priv, 4240 enum dpll_pin_state *state, 4241 struct netlink_ext_ack *extack) 4242 { 4243 struct ptp_ocp *bp = priv; 4244 int idx; 4245 4246 if (bp->pps_select) { 4247 idx = ioread32(&bp->pps_select->gpio1); 4248 *state = (&bp->sma[idx] == pin_priv) ? DPLL_PIN_STATE_CONNECTED : 4249 DPLL_PIN_STATE_SELECTABLE; 4250 return 0; 4251 } 4252 NL_SET_ERR_MSG(extack, "pin selection is not supported on current HW"); 4253 return -EINVAL; 4254 } 4255 4256 static int ptp_ocp_dpll_mode_get(const struct dpll_device *dpll, void *priv, 4257 enum dpll_mode *mode, struct netlink_ext_ack *extack) 4258 { 4259 *mode = DPLL_MODE_AUTOMATIC; 4260 return 0; 4261 } 4262 4263 static bool ptp_ocp_dpll_mode_supported(const struct dpll_device *dpll, 4264 void *priv, const enum dpll_mode mode, 4265 struct netlink_ext_ack *extack) 4266 { 4267 return mode == DPLL_MODE_AUTOMATIC; 4268 } 4269 4270 static int ptp_ocp_dpll_direction_get(const struct dpll_pin *pin, 4271 void *pin_priv, 4272 const struct dpll_device *dpll, 4273 void *priv, 4274 enum dpll_pin_direction *direction, 4275 struct netlink_ext_ack *extack) 4276 { 4277 struct ptp_ocp_sma_connector *sma = pin_priv; 4278 4279 *direction = sma->mode == SMA_MODE_IN ? 4280 DPLL_PIN_DIRECTION_INPUT : 4281 DPLL_PIN_DIRECTION_OUTPUT; 4282 return 0; 4283 } 4284 4285 static int ptp_ocp_dpll_direction_set(const struct dpll_pin *pin, 4286 void *pin_priv, 4287 const struct dpll_device *dpll, 4288 void *dpll_priv, 4289 enum dpll_pin_direction direction, 4290 struct netlink_ext_ack *extack) 4291 { 4292 struct ptp_ocp_sma_connector *sma = pin_priv; 4293 struct ptp_ocp *bp = dpll_priv; 4294 enum ptp_ocp_sma_mode mode; 4295 int sma_nr = (sma - bp->sma); 4296 4297 if (sma->fixed_dir) 4298 return -EOPNOTSUPP; 4299 mode = direction == DPLL_PIN_DIRECTION_INPUT ? 4300 SMA_MODE_IN : SMA_MODE_OUT; 4301 return ptp_ocp_sma_store_val(bp, 0, mode, sma_nr); 4302 } 4303 4304 static int ptp_ocp_dpll_frequency_set(const struct dpll_pin *pin, 4305 void *pin_priv, 4306 const struct dpll_device *dpll, 4307 void *dpll_priv, u64 frequency, 4308 struct netlink_ext_ack *extack) 4309 { 4310 struct ptp_ocp_sma_connector *sma = pin_priv; 4311 struct ptp_ocp *bp = dpll_priv; 4312 const struct ocp_selector *tbl; 4313 int sma_nr = (sma - bp->sma); 4314 int i; 4315 4316 if (sma->fixed_fcn) 4317 return -EOPNOTSUPP; 4318 4319 tbl = bp->sma_op->tbl[sma->mode]; 4320 for (i = 0; tbl[i].name; i++) 4321 if (tbl[i].frequency == frequency) 4322 return ptp_ocp_sma_store_val(bp, i, sma->mode, sma_nr); 4323 return -EINVAL; 4324 } 4325 4326 static int ptp_ocp_dpll_frequency_get(const struct dpll_pin *pin, 4327 void *pin_priv, 4328 const struct dpll_device *dpll, 4329 void *dpll_priv, u64 *frequency, 4330 struct netlink_ext_ack *extack) 4331 { 4332 struct ptp_ocp_sma_connector *sma = pin_priv; 4333 struct ptp_ocp *bp = dpll_priv; 4334 const struct ocp_selector *tbl; 4335 int sma_nr = (sma - bp->sma); 4336 u32 val; 4337 int i; 4338 4339 val = bp->sma_op->get(bp, sma_nr); 4340 tbl = bp->sma_op->tbl[sma->mode]; 4341 for (i = 0; tbl[i].name; i++) 4342 if (val == tbl[i].value) { 4343 *frequency = tbl[i].frequency; 4344 return 0; 4345 } 4346 4347 return -EINVAL; 4348 } 4349 4350 static const struct dpll_device_ops dpll_ops = { 4351 .lock_status_get = ptp_ocp_dpll_lock_status_get, 4352 .mode_get = ptp_ocp_dpll_mode_get, 4353 .mode_supported = ptp_ocp_dpll_mode_supported, 4354 }; 4355 4356 static const struct dpll_pin_ops dpll_pins_ops = { 4357 .frequency_get = ptp_ocp_dpll_frequency_get, 4358 .frequency_set = ptp_ocp_dpll_frequency_set, 4359 .direction_get = ptp_ocp_dpll_direction_get, 4360 .direction_set = ptp_ocp_dpll_direction_set, 4361 .state_on_dpll_get = ptp_ocp_dpll_state_get, 4362 }; 4363 4364 static void 4365 ptp_ocp_sync_work(struct work_struct *work) 4366 { 4367 struct ptp_ocp *bp; 4368 bool sync; 4369 4370 bp = container_of(work, struct ptp_ocp, sync_work.work); 4371 sync = !!(ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC); 4372 4373 if (bp->sync != sync) 4374 dpll_device_change_ntf(bp->dpll); 4375 4376 bp->sync = sync; 4377 4378 queue_delayed_work(system_power_efficient_wq, &bp->sync_work, HZ); 4379 } 4380 4381 static int 4382 ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id) 4383 { 4384 struct devlink *devlink; 4385 struct ptp_ocp *bp; 4386 int err, i; 4387 u64 clkid; 4388 4389 devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp), &pdev->dev); 4390 if (!devlink) { 4391 dev_err(&pdev->dev, "devlink_alloc failed\n"); 4392 return -ENOMEM; 4393 } 4394 4395 err = pci_enable_device(pdev); 4396 if (err) { 4397 dev_err(&pdev->dev, "pci_enable_device\n"); 4398 goto out_free; 4399 } 4400 4401 bp = devlink_priv(devlink); 4402 err = ptp_ocp_device_init(bp, pdev); 4403 if (err) 4404 goto out_disable; 4405 4406 INIT_DELAYED_WORK(&bp->sync_work, ptp_ocp_sync_work); 4407 4408 /* compat mode. 4409 * Older FPGA firmware only returns 2 irq's. 4410 * allow this - if not all of the IRQ's are returned, skip the 4411 * extra devices and just register the clock. 4412 */ 4413 err = pci_alloc_irq_vectors(pdev, 1, 17, PCI_IRQ_MSI | PCI_IRQ_MSIX); 4414 if (err < 0) { 4415 dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err); 4416 goto out; 4417 } 4418 bp->n_irqs = err; 4419 pci_set_master(pdev); 4420 4421 err = ptp_ocp_register_resources(bp, id->driver_data); 4422 if (err) 4423 goto out; 4424 4425 bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev); 4426 if (IS_ERR(bp->ptp)) { 4427 err = PTR_ERR(bp->ptp); 4428 dev_err(&pdev->dev, "ptp_clock_register: %d\n", err); 4429 bp->ptp = NULL; 4430 goto out; 4431 } 4432 4433 err = ptp_ocp_complete(bp); 4434 if (err) 4435 goto out; 4436 4437 ptp_ocp_info(bp); 4438 devlink_register(devlink); 4439 4440 clkid = pci_get_dsn(pdev); 4441 bp->dpll = dpll_device_get(clkid, 0, THIS_MODULE); 4442 if (IS_ERR(bp->dpll)) { 4443 err = PTR_ERR(bp->dpll); 4444 dev_err(&pdev->dev, "dpll_device_alloc failed\n"); 4445 goto out; 4446 } 4447 4448 err = dpll_device_register(bp->dpll, DPLL_TYPE_PPS, &dpll_ops, bp); 4449 if (err) 4450 goto out; 4451 4452 for (i = 0; i < OCP_SMA_NUM; i++) { 4453 bp->sma[i].dpll_pin = dpll_pin_get(clkid, i, THIS_MODULE, &bp->sma[i].dpll_prop); 4454 if (IS_ERR(bp->sma[i].dpll_pin)) { 4455 err = PTR_ERR(bp->sma[i].dpll_pin); 4456 goto out_dpll; 4457 } 4458 4459 err = dpll_pin_register(bp->dpll, bp->sma[i].dpll_pin, &dpll_pins_ops, 4460 &bp->sma[i]); 4461 if (err) { 4462 dpll_pin_put(bp->sma[i].dpll_pin); 4463 goto out_dpll; 4464 } 4465 } 4466 queue_delayed_work(system_power_efficient_wq, &bp->sync_work, HZ); 4467 4468 return 0; 4469 out_dpll: 4470 while (i) { 4471 --i; 4472 dpll_pin_unregister(bp->dpll, bp->sma[i].dpll_pin, &dpll_pins_ops, &bp->sma[i]); 4473 dpll_pin_put(bp->sma[i].dpll_pin); 4474 } 4475 dpll_device_put(bp->dpll); 4476 out: 4477 ptp_ocp_detach(bp); 4478 out_disable: 4479 pci_disable_device(pdev); 4480 out_free: 4481 devlink_free(devlink); 4482 return err; 4483 } 4484 4485 static void 4486 ptp_ocp_remove(struct pci_dev *pdev) 4487 { 4488 struct ptp_ocp *bp = pci_get_drvdata(pdev); 4489 struct devlink *devlink = priv_to_devlink(bp); 4490 int i; 4491 4492 cancel_delayed_work_sync(&bp->sync_work); 4493 for (i = 0; i < OCP_SMA_NUM; i++) { 4494 if (bp->sma[i].dpll_pin) { 4495 dpll_pin_unregister(bp->dpll, bp->sma[i].dpll_pin, &dpll_pins_ops, bp); 4496 dpll_pin_put(bp->sma[i].dpll_pin); 4497 } 4498 } 4499 dpll_device_unregister(bp->dpll, &dpll_ops, bp); 4500 dpll_device_put(bp->dpll); 4501 devlink_unregister(devlink); 4502 ptp_ocp_detach(bp); 4503 pci_disable_device(pdev); 4504 4505 devlink_free(devlink); 4506 } 4507 4508 static struct pci_driver ptp_ocp_driver = { 4509 .name = KBUILD_MODNAME, 4510 .id_table = ptp_ocp_pcidev_id, 4511 .probe = ptp_ocp_probe, 4512 .remove = ptp_ocp_remove, 4513 }; 4514 4515 static int 4516 ptp_ocp_i2c_notifier_call(struct notifier_block *nb, 4517 unsigned long action, void *data) 4518 { 4519 struct device *dev, *child = data; 4520 struct ptp_ocp *bp; 4521 bool add; 4522 4523 switch (action) { 4524 case BUS_NOTIFY_ADD_DEVICE: 4525 case BUS_NOTIFY_DEL_DEVICE: 4526 add = action == BUS_NOTIFY_ADD_DEVICE; 4527 break; 4528 default: 4529 return 0; 4530 } 4531 4532 if (!i2c_verify_adapter(child)) 4533 return 0; 4534 4535 dev = child; 4536 while ((dev = dev->parent)) 4537 if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME)) 4538 goto found; 4539 return 0; 4540 4541 found: 4542 bp = dev_get_drvdata(dev); 4543 if (add) 4544 ptp_ocp_symlink(bp, child, "i2c"); 4545 else 4546 sysfs_remove_link(&bp->dev.kobj, "i2c"); 4547 4548 return 0; 4549 } 4550 4551 static struct notifier_block ptp_ocp_i2c_notifier = { 4552 .notifier_call = ptp_ocp_i2c_notifier_call, 4553 }; 4554 4555 static int __init 4556 ptp_ocp_init(void) 4557 { 4558 const char *what; 4559 int err; 4560 4561 ptp_ocp_debugfs_init(); 4562 4563 what = "timecard class"; 4564 err = class_register(&timecard_class); 4565 if (err) 4566 goto out; 4567 4568 what = "i2c notifier"; 4569 err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 4570 if (err) 4571 goto out_notifier; 4572 4573 what = "ptp_ocp driver"; 4574 err = pci_register_driver(&ptp_ocp_driver); 4575 if (err) 4576 goto out_register; 4577 4578 return 0; 4579 4580 out_register: 4581 bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 4582 out_notifier: 4583 class_unregister(&timecard_class); 4584 out: 4585 ptp_ocp_debugfs_fini(); 4586 pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err); 4587 return err; 4588 } 4589 4590 static void __exit 4591 ptp_ocp_fini(void) 4592 { 4593 bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 4594 pci_unregister_driver(&ptp_ocp_driver); 4595 class_unregister(&timecard_class); 4596 ptp_ocp_debugfs_fini(); 4597 } 4598 4599 module_init(ptp_ocp_init); 4600 module_exit(ptp_ocp_fini); 4601 4602 MODULE_DESCRIPTION("OpenCompute TimeCard driver"); 4603 MODULE_LICENSE("GPL v2"); 4604