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