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