1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (c) 2020 Facebook */ 3 4 #include <linux/err.h> 5 #include <linux/kernel.h> 6 #include <linux/module.h> 7 #include <linux/debugfs.h> 8 #include <linux/init.h> 9 #include <linux/pci.h> 10 #include <linux/serial_8250.h> 11 #include <linux/clkdev.h> 12 #include <linux/clk-provider.h> 13 #include <linux/platform_device.h> 14 #include <linux/ptp_clock_kernel.h> 15 #include <linux/spi/spi.h> 16 #include <linux/spi/xilinx_spi.h> 17 #include <net/devlink.h> 18 #include <linux/i2c.h> 19 #include <linux/mtd/mtd.h> 20 21 #ifndef PCI_VENDOR_ID_FACEBOOK 22 #define PCI_VENDOR_ID_FACEBOOK 0x1d9b 23 #endif 24 25 #ifndef PCI_DEVICE_ID_FACEBOOK_TIMECARD 26 #define PCI_DEVICE_ID_FACEBOOK_TIMECARD 0x0400 27 #endif 28 29 static struct class timecard_class = { 30 .owner = THIS_MODULE, 31 .name = "timecard", 32 }; 33 34 struct ocp_reg { 35 u32 ctrl; 36 u32 status; 37 u32 select; 38 u32 version; 39 u32 time_ns; 40 u32 time_sec; 41 u32 __pad0[2]; 42 u32 adjust_ns; 43 u32 adjust_sec; 44 u32 __pad1[2]; 45 u32 offset_ns; 46 u32 offset_window_ns; 47 u32 __pad2[2]; 48 u32 drift_ns; 49 u32 drift_window_ns; 50 u32 __pad3[6]; 51 u32 servo_offset_p; 52 u32 servo_offset_i; 53 u32 servo_drift_p; 54 u32 servo_drift_i; 55 }; 56 57 #define OCP_CTRL_ENABLE BIT(0) 58 #define OCP_CTRL_ADJUST_TIME BIT(1) 59 #define OCP_CTRL_ADJUST_OFFSET BIT(2) 60 #define OCP_CTRL_ADJUST_DRIFT BIT(3) 61 #define OCP_CTRL_ADJUST_SERVO BIT(8) 62 #define OCP_CTRL_READ_TIME_REQ BIT(30) 63 #define OCP_CTRL_READ_TIME_DONE BIT(31) 64 65 #define OCP_STATUS_IN_SYNC BIT(0) 66 #define OCP_STATUS_IN_HOLDOVER BIT(1) 67 68 #define OCP_SELECT_CLK_NONE 0 69 #define OCP_SELECT_CLK_REG 0xfe 70 71 struct tod_reg { 72 u32 ctrl; 73 u32 status; 74 u32 uart_polarity; 75 u32 version; 76 u32 adj_sec; 77 u32 __pad0[3]; 78 u32 uart_baud; 79 u32 __pad1[3]; 80 u32 utc_status; 81 u32 leap; 82 }; 83 84 #define TOD_CTRL_PROTOCOL BIT(28) 85 #define TOD_CTRL_DISABLE_FMT_A BIT(17) 86 #define TOD_CTRL_DISABLE_FMT_B BIT(16) 87 #define TOD_CTRL_ENABLE BIT(0) 88 #define TOD_CTRL_GNSS_MASK ((1U << 4) - 1) 89 #define TOD_CTRL_GNSS_SHIFT 24 90 91 #define TOD_STATUS_UTC_MASK 0xff 92 #define TOD_STATUS_UTC_VALID BIT(8) 93 #define TOD_STATUS_LEAP_VALID BIT(16) 94 95 struct ts_reg { 96 u32 enable; 97 u32 error; 98 u32 polarity; 99 u32 version; 100 u32 __pad0[4]; 101 u32 cable_delay; 102 u32 __pad1[3]; 103 u32 intr; 104 u32 intr_mask; 105 u32 event_count; 106 u32 __pad2[1]; 107 u32 ts_count; 108 u32 time_ns; 109 u32 time_sec; 110 u32 data_width; 111 u32 data; 112 }; 113 114 struct pps_reg { 115 u32 ctrl; 116 u32 status; 117 u32 __pad0[6]; 118 u32 cable_delay; 119 }; 120 121 #define PPS_STATUS_FILTER_ERR BIT(0) 122 #define PPS_STATUS_SUPERV_ERR BIT(1) 123 124 struct img_reg { 125 u32 version; 126 }; 127 128 struct gpio_reg { 129 u32 gpio1; 130 u32 __pad0; 131 u32 gpio2; 132 u32 __pad1; 133 }; 134 135 struct irig_master_reg { 136 u32 ctrl; 137 u32 status; 138 u32 __pad0; 139 u32 version; 140 u32 adj_sec; 141 u32 mode_ctrl; 142 }; 143 144 #define IRIG_M_CTRL_ENABLE BIT(0) 145 146 struct irig_slave_reg { 147 u32 ctrl; 148 u32 status; 149 u32 __pad0; 150 u32 version; 151 u32 adj_sec; 152 u32 mode_ctrl; 153 }; 154 155 #define IRIG_S_CTRL_ENABLE BIT(0) 156 157 struct dcf_master_reg { 158 u32 ctrl; 159 u32 status; 160 u32 __pad0; 161 u32 version; 162 u32 adj_sec; 163 }; 164 165 #define DCF_M_CTRL_ENABLE BIT(0) 166 167 struct dcf_slave_reg { 168 u32 ctrl; 169 u32 status; 170 u32 __pad0; 171 u32 version; 172 u32 adj_sec; 173 }; 174 175 #define DCF_S_CTRL_ENABLE BIT(0) 176 177 struct ptp_ocp_flash_info { 178 const char *name; 179 int pci_offset; 180 int data_size; 181 void *data; 182 }; 183 184 struct ptp_ocp_i2c_info { 185 const char *name; 186 unsigned long fixed_rate; 187 size_t data_size; 188 void *data; 189 }; 190 191 struct ptp_ocp_ext_info { 192 int index; 193 irqreturn_t (*irq_fcn)(int irq, void *priv); 194 int (*enable)(void *priv, u32 req, bool enable); 195 }; 196 197 struct ptp_ocp_ext_src { 198 void __iomem *mem; 199 struct ptp_ocp *bp; 200 struct ptp_ocp_ext_info *info; 201 int irq_vec; 202 }; 203 204 struct ptp_ocp { 205 struct pci_dev *pdev; 206 struct device dev; 207 spinlock_t lock; 208 struct ocp_reg __iomem *reg; 209 struct tod_reg __iomem *tod; 210 struct pps_reg __iomem *pps_to_ext; 211 struct pps_reg __iomem *pps_to_clk; 212 struct gpio_reg __iomem *pps_select; 213 struct gpio_reg __iomem *sma; 214 struct irig_master_reg __iomem *irig_out; 215 struct irig_slave_reg __iomem *irig_in; 216 struct dcf_master_reg __iomem *dcf_out; 217 struct dcf_slave_reg __iomem *dcf_in; 218 struct tod_reg __iomem *nmea_out; 219 struct ptp_ocp_ext_src *pps; 220 struct ptp_ocp_ext_src *ts0; 221 struct ptp_ocp_ext_src *ts1; 222 struct ptp_ocp_ext_src *ts2; 223 struct img_reg __iomem *image; 224 struct ptp_clock *ptp; 225 struct ptp_clock_info ptp_info; 226 struct platform_device *i2c_ctrl; 227 struct platform_device *spi_flash; 228 struct clk_hw *i2c_clk; 229 struct timer_list watchdog; 230 struct dentry *debug_root; 231 time64_t gnss_lost; 232 int id; 233 int n_irqs; 234 int gnss_port; 235 int gnss2_port; 236 int mac_port; /* miniature atomic clock */ 237 int nmea_port; 238 u8 serial[6]; 239 bool has_serial; 240 u32 pps_req_map; 241 int flash_start; 242 u32 utc_tai_offset; 243 u32 ts_window_adjust; 244 }; 245 246 #define OCP_REQ_TIMESTAMP BIT(0) 247 #define OCP_REQ_PPS BIT(1) 248 249 struct ocp_resource { 250 unsigned long offset; 251 int size; 252 int irq_vec; 253 int (*setup)(struct ptp_ocp *bp, struct ocp_resource *r); 254 void *extra; 255 unsigned long bp_offset; 256 const char * const name; 257 }; 258 259 static int ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r); 260 static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r); 261 static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r); 262 static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r); 263 static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r); 264 static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r); 265 static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv); 266 static int ptp_ocp_ts_enable(void *priv, u32 req, bool enable); 267 268 #define bp_assign_entry(bp, res, val) ({ \ 269 uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset; \ 270 *(typeof(val) *)addr = val; \ 271 }) 272 273 #define OCP_RES_LOCATION(member) \ 274 .name = #member, .bp_offset = offsetof(struct ptp_ocp, member) 275 276 #define OCP_MEM_RESOURCE(member) \ 277 OCP_RES_LOCATION(member), .setup = ptp_ocp_register_mem 278 279 #define OCP_SERIAL_RESOURCE(member) \ 280 OCP_RES_LOCATION(member), .setup = ptp_ocp_register_serial 281 282 #define OCP_I2C_RESOURCE(member) \ 283 OCP_RES_LOCATION(member), .setup = ptp_ocp_register_i2c 284 285 #define OCP_SPI_RESOURCE(member) \ 286 OCP_RES_LOCATION(member), .setup = ptp_ocp_register_spi 287 288 #define OCP_EXT_RESOURCE(member) \ 289 OCP_RES_LOCATION(member), .setup = ptp_ocp_register_ext 290 291 /* This is the MSI vector mapping used. 292 * 0: TS3 (and PPS) 293 * 1: TS0 294 * 2: TS1 295 * 3: GNSS 296 * 4: GNSS2 297 * 5: MAC 298 * 6: TS2 299 * 7: I2C controller 300 * 8: HWICAP (notused) 301 * 9: SPI Flash 302 * 10: NMEA 303 */ 304 305 static struct ocp_resource ocp_fb_resource[] = { 306 { 307 OCP_MEM_RESOURCE(reg), 308 .offset = 0x01000000, .size = 0x10000, 309 }, 310 { 311 OCP_EXT_RESOURCE(ts0), 312 .offset = 0x01010000, .size = 0x10000, .irq_vec = 1, 313 .extra = &(struct ptp_ocp_ext_info) { 314 .index = 0, 315 .irq_fcn = ptp_ocp_ts_irq, 316 .enable = ptp_ocp_ts_enable, 317 }, 318 }, 319 { 320 OCP_EXT_RESOURCE(ts1), 321 .offset = 0x01020000, .size = 0x10000, .irq_vec = 2, 322 .extra = &(struct ptp_ocp_ext_info) { 323 .index = 1, 324 .irq_fcn = ptp_ocp_ts_irq, 325 .enable = ptp_ocp_ts_enable, 326 }, 327 }, 328 { 329 OCP_EXT_RESOURCE(ts2), 330 .offset = 0x01060000, .size = 0x10000, .irq_vec = 6, 331 .extra = &(struct ptp_ocp_ext_info) { 332 .index = 2, 333 .irq_fcn = ptp_ocp_ts_irq, 334 .enable = ptp_ocp_ts_enable, 335 }, 336 }, 337 { 338 OCP_EXT_RESOURCE(pps), 339 .offset = 0x010C0000, .size = 0x10000, .irq_vec = 0, 340 .extra = &(struct ptp_ocp_ext_info) { 341 .index = 3, 342 .irq_fcn = ptp_ocp_ts_irq, 343 .enable = ptp_ocp_ts_enable, 344 }, 345 }, 346 { 347 OCP_MEM_RESOURCE(pps_to_ext), 348 .offset = 0x01030000, .size = 0x10000, 349 }, 350 { 351 OCP_MEM_RESOURCE(pps_to_clk), 352 .offset = 0x01040000, .size = 0x10000, 353 }, 354 { 355 OCP_MEM_RESOURCE(tod), 356 .offset = 0x01050000, .size = 0x10000, 357 }, 358 { 359 OCP_MEM_RESOURCE(irig_in), 360 .offset = 0x01070000, .size = 0x10000, 361 }, 362 { 363 OCP_MEM_RESOURCE(irig_out), 364 .offset = 0x01080000, .size = 0x10000, 365 }, 366 { 367 OCP_MEM_RESOURCE(dcf_in), 368 .offset = 0x01090000, .size = 0x10000, 369 }, 370 { 371 OCP_MEM_RESOURCE(dcf_out), 372 .offset = 0x010A0000, .size = 0x10000, 373 }, 374 { 375 OCP_MEM_RESOURCE(nmea_out), 376 .offset = 0x010B0000, .size = 0x10000, 377 }, 378 { 379 OCP_MEM_RESOURCE(image), 380 .offset = 0x00020000, .size = 0x1000, 381 }, 382 { 383 OCP_MEM_RESOURCE(pps_select), 384 .offset = 0x00130000, .size = 0x1000, 385 }, 386 { 387 OCP_MEM_RESOURCE(sma), 388 .offset = 0x00140000, .size = 0x1000, 389 }, 390 { 391 OCP_I2C_RESOURCE(i2c_ctrl), 392 .offset = 0x00150000, .size = 0x10000, .irq_vec = 7, 393 .extra = &(struct ptp_ocp_i2c_info) { 394 .name = "xiic-i2c", 395 .fixed_rate = 50000000, 396 }, 397 }, 398 { 399 OCP_SERIAL_RESOURCE(gnss_port), 400 .offset = 0x00160000 + 0x1000, .irq_vec = 3, 401 }, 402 { 403 OCP_SERIAL_RESOURCE(gnss2_port), 404 .offset = 0x00170000 + 0x1000, .irq_vec = 4, 405 }, 406 { 407 OCP_SERIAL_RESOURCE(mac_port), 408 .offset = 0x00180000 + 0x1000, .irq_vec = 5, 409 }, 410 { 411 OCP_SERIAL_RESOURCE(nmea_port), 412 .offset = 0x00190000 + 0x1000, .irq_vec = 10, 413 }, 414 { 415 OCP_SPI_RESOURCE(spi_flash), 416 .offset = 0x00310000, .size = 0x10000, .irq_vec = 9, 417 .extra = &(struct ptp_ocp_flash_info) { 418 .name = "xilinx_spi", .pci_offset = 0, 419 .data_size = sizeof(struct xspi_platform_data), 420 .data = &(struct xspi_platform_data) { 421 .num_chipselect = 1, 422 .bits_per_word = 8, 423 .num_devices = 1, 424 .devices = &(struct spi_board_info) { 425 .modalias = "spi-nor", 426 }, 427 }, 428 }, 429 }, 430 { 431 .setup = ptp_ocp_fb_board_init, 432 }, 433 { } 434 }; 435 436 static const struct pci_device_id ptp_ocp_pcidev_id[] = { 437 { PCI_DEVICE_DATA(FACEBOOK, TIMECARD, &ocp_fb_resource) }, 438 { 0 } 439 }; 440 MODULE_DEVICE_TABLE(pci, ptp_ocp_pcidev_id); 441 442 static DEFINE_MUTEX(ptp_ocp_lock); 443 static DEFINE_IDR(ptp_ocp_idr); 444 445 struct ocp_selector { 446 const char *name; 447 int value; 448 }; 449 450 static struct ocp_selector ptp_ocp_clock[] = { 451 { .name = "NONE", .value = 0 }, 452 { .name = "TOD", .value = 1 }, 453 { .name = "IRIG", .value = 2 }, 454 { .name = "PPS", .value = 3 }, 455 { .name = "PTP", .value = 4 }, 456 { .name = "RTC", .value = 5 }, 457 { .name = "DCF", .value = 6 }, 458 { .name = "REGS", .value = 0xfe }, 459 { .name = "EXT", .value = 0xff }, 460 { } 461 }; 462 463 static struct ocp_selector ptp_ocp_sma_in[] = { 464 { .name = "10Mhz", .value = 0x00 }, 465 { .name = "PPS1", .value = 0x01 }, 466 { .name = "PPS2", .value = 0x02 }, 467 { .name = "TS1", .value = 0x04 }, 468 { .name = "TS2", .value = 0x08 }, 469 { .name = "IRIG", .value = 0x10 }, 470 { .name = "DCF", .value = 0x20 }, 471 { } 472 }; 473 474 static struct ocp_selector ptp_ocp_sma_out[] = { 475 { .name = "10Mhz", .value = 0x00 }, 476 { .name = "PHC", .value = 0x01 }, 477 { .name = "MAC", .value = 0x02 }, 478 { .name = "GNSS", .value = 0x04 }, 479 { .name = "GNSS2", .value = 0x08 }, 480 { .name = "IRIG", .value = 0x10 }, 481 { .name = "DCF", .value = 0x20 }, 482 { } 483 }; 484 485 static const char * 486 ptp_ocp_select_name_from_val(struct ocp_selector *tbl, int val) 487 { 488 int i; 489 490 for (i = 0; tbl[i].name; i++) 491 if (tbl[i].value == val) 492 return tbl[i].name; 493 return NULL; 494 } 495 496 static int 497 ptp_ocp_select_val_from_name(struct ocp_selector *tbl, const char *name) 498 { 499 const char *select; 500 int i; 501 502 for (i = 0; tbl[i].name; i++) { 503 select = tbl[i].name; 504 if (!strncasecmp(name, select, strlen(select))) 505 return tbl[i].value; 506 } 507 return -EINVAL; 508 } 509 510 static ssize_t 511 ptp_ocp_select_table_show(struct ocp_selector *tbl, char *buf) 512 { 513 ssize_t count; 514 int i; 515 516 count = 0; 517 for (i = 0; tbl[i].name; i++) 518 count += sysfs_emit_at(buf, count, "%s ", tbl[i].name); 519 if (count) 520 count--; 521 count += sysfs_emit_at(buf, count, "\n"); 522 return count; 523 } 524 525 static int 526 __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts, 527 struct ptp_system_timestamp *sts) 528 { 529 u32 ctrl, time_sec, time_ns; 530 int i; 531 532 ptp_read_system_prets(sts); 533 534 ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE; 535 iowrite32(ctrl, &bp->reg->ctrl); 536 537 for (i = 0; i < 100; i++) { 538 ctrl = ioread32(&bp->reg->ctrl); 539 if (ctrl & OCP_CTRL_READ_TIME_DONE) 540 break; 541 } 542 ptp_read_system_postts(sts); 543 544 if (sts && bp->ts_window_adjust) { 545 s64 ns = timespec64_to_ns(&sts->post_ts); 546 547 sts->post_ts = ns_to_timespec64(ns - bp->ts_window_adjust); 548 } 549 550 time_ns = ioread32(&bp->reg->time_ns); 551 time_sec = ioread32(&bp->reg->time_sec); 552 553 ts->tv_sec = time_sec; 554 ts->tv_nsec = time_ns; 555 556 return ctrl & OCP_CTRL_READ_TIME_DONE ? 0 : -ETIMEDOUT; 557 } 558 559 static int 560 ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts, 561 struct ptp_system_timestamp *sts) 562 { 563 struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 564 unsigned long flags; 565 int err; 566 567 spin_lock_irqsave(&bp->lock, flags); 568 err = __ptp_ocp_gettime_locked(bp, ts, sts); 569 spin_unlock_irqrestore(&bp->lock, flags); 570 571 return err; 572 } 573 574 static void 575 __ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts) 576 { 577 u32 ctrl, time_sec, time_ns; 578 u32 select; 579 580 time_ns = ts->tv_nsec; 581 time_sec = ts->tv_sec; 582 583 select = ioread32(&bp->reg->select); 584 iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 585 586 iowrite32(time_ns, &bp->reg->adjust_ns); 587 iowrite32(time_sec, &bp->reg->adjust_sec); 588 589 ctrl = OCP_CTRL_ADJUST_TIME | OCP_CTRL_ENABLE; 590 iowrite32(ctrl, &bp->reg->ctrl); 591 592 /* restore clock selection */ 593 iowrite32(select >> 16, &bp->reg->select); 594 } 595 596 static int 597 ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts) 598 { 599 struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 600 unsigned long flags; 601 602 spin_lock_irqsave(&bp->lock, flags); 603 __ptp_ocp_settime_locked(bp, ts); 604 spin_unlock_irqrestore(&bp->lock, flags); 605 606 return 0; 607 } 608 609 static void 610 __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u64 adj_val) 611 { 612 u32 select, ctrl; 613 614 select = ioread32(&bp->reg->select); 615 iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 616 617 iowrite32(adj_val, &bp->reg->offset_ns); 618 iowrite32(adj_val & 0x7f, &bp->reg->offset_window_ns); 619 620 ctrl = OCP_CTRL_ADJUST_OFFSET | OCP_CTRL_ENABLE; 621 iowrite32(ctrl, &bp->reg->ctrl); 622 623 /* restore clock selection */ 624 iowrite32(select >> 16, &bp->reg->select); 625 } 626 627 static int 628 ptp_ocp_adjtime(struct ptp_clock_info *ptp_info, s64 delta_ns) 629 { 630 struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 631 unsigned long flags; 632 u32 adj_ns, sign; 633 634 sign = delta_ns < 0 ? BIT(31) : 0; 635 adj_ns = sign ? -delta_ns : delta_ns; 636 637 spin_lock_irqsave(&bp->lock, flags); 638 __ptp_ocp_adjtime_locked(bp, sign | adj_ns); 639 spin_unlock_irqrestore(&bp->lock, flags); 640 641 return 0; 642 } 643 644 static int 645 ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm) 646 { 647 if (scaled_ppm == 0) 648 return 0; 649 650 return -EOPNOTSUPP; 651 } 652 653 static int 654 ptp_ocp_null_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns) 655 { 656 return -EOPNOTSUPP; 657 } 658 659 static int 660 ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq, 661 int on) 662 { 663 struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info); 664 struct ptp_ocp_ext_src *ext = NULL; 665 u32 req; 666 int err; 667 668 switch (rq->type) { 669 case PTP_CLK_REQ_EXTTS: 670 req = OCP_REQ_TIMESTAMP; 671 switch (rq->extts.index) { 672 case 0: 673 ext = bp->ts0; 674 break; 675 case 1: 676 ext = bp->ts1; 677 break; 678 case 2: 679 ext = bp->ts2; 680 break; 681 case 3: 682 ext = bp->pps; 683 break; 684 } 685 break; 686 case PTP_CLK_REQ_PPS: 687 req = OCP_REQ_PPS; 688 ext = bp->pps; 689 break; 690 case PTP_CLK_REQ_PEROUT: 691 if (on && 692 (rq->perout.period.sec != 1 || rq->perout.period.nsec != 0)) 693 return -EINVAL; 694 /* This is a request for 1PPS on an output SMA. 695 * Allow, but assume manual configuration. 696 */ 697 return 0; 698 default: 699 return -EOPNOTSUPP; 700 } 701 702 err = -ENXIO; 703 if (ext) 704 err = ext->info->enable(ext, req, on); 705 706 return err; 707 } 708 709 static const struct ptp_clock_info ptp_ocp_clock_info = { 710 .owner = THIS_MODULE, 711 .name = KBUILD_MODNAME, 712 .max_adj = 100000000, 713 .gettimex64 = ptp_ocp_gettimex, 714 .settime64 = ptp_ocp_settime, 715 .adjtime = ptp_ocp_adjtime, 716 .adjfine = ptp_ocp_null_adjfine, 717 .adjphase = ptp_ocp_null_adjphase, 718 .enable = ptp_ocp_enable, 719 .pps = true, 720 .n_ext_ts = 4, 721 .n_per_out = 1, 722 }; 723 724 static void 725 __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp) 726 { 727 u32 ctrl, select; 728 729 select = ioread32(&bp->reg->select); 730 iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select); 731 732 iowrite32(0, &bp->reg->drift_ns); 733 734 ctrl = OCP_CTRL_ADJUST_DRIFT | OCP_CTRL_ENABLE; 735 iowrite32(ctrl, &bp->reg->ctrl); 736 737 /* restore clock selection */ 738 iowrite32(select >> 16, &bp->reg->select); 739 } 740 741 static void 742 ptp_ocp_watchdog(struct timer_list *t) 743 { 744 struct ptp_ocp *bp = from_timer(bp, t, watchdog); 745 unsigned long flags; 746 u32 status; 747 748 status = ioread32(&bp->pps_to_clk->status); 749 750 if (status & PPS_STATUS_SUPERV_ERR) { 751 iowrite32(status, &bp->pps_to_clk->status); 752 if (!bp->gnss_lost) { 753 spin_lock_irqsave(&bp->lock, flags); 754 __ptp_ocp_clear_drift_locked(bp); 755 spin_unlock_irqrestore(&bp->lock, flags); 756 bp->gnss_lost = ktime_get_real_seconds(); 757 } 758 759 } else if (bp->gnss_lost) { 760 bp->gnss_lost = 0; 761 } 762 763 mod_timer(&bp->watchdog, jiffies + HZ); 764 } 765 766 static void 767 ptp_ocp_estimate_pci_timing(struct ptp_ocp *bp) 768 { 769 ktime_t start, end; 770 ktime_t delay; 771 u32 ctrl; 772 773 ctrl = ioread32(&bp->reg->ctrl); 774 ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE; 775 776 iowrite32(ctrl, &bp->reg->ctrl); 777 778 start = ktime_get_ns(); 779 780 ctrl = ioread32(&bp->reg->ctrl); 781 782 end = ktime_get_ns(); 783 784 delay = end - start; 785 bp->ts_window_adjust = (delay >> 5) * 3; 786 } 787 788 static int 789 ptp_ocp_init_clock(struct ptp_ocp *bp) 790 { 791 struct timespec64 ts; 792 bool sync; 793 u32 ctrl; 794 795 ctrl = OCP_CTRL_ENABLE; 796 iowrite32(ctrl, &bp->reg->ctrl); 797 798 /* NO DRIFT Correction */ 799 /* offset_p:i 1/8, offset_i: 1/16, drift_p: 0, drift_i: 0 */ 800 iowrite32(0x2000, &bp->reg->servo_offset_p); 801 iowrite32(0x1000, &bp->reg->servo_offset_i); 802 iowrite32(0, &bp->reg->servo_drift_p); 803 iowrite32(0, &bp->reg->servo_drift_i); 804 805 /* latch servo values */ 806 ctrl |= OCP_CTRL_ADJUST_SERVO; 807 iowrite32(ctrl, &bp->reg->ctrl); 808 809 if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) { 810 dev_err(&bp->pdev->dev, "clock not enabled\n"); 811 return -ENODEV; 812 } 813 814 ptp_ocp_estimate_pci_timing(bp); 815 816 sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC; 817 if (!sync) { 818 ktime_get_clocktai_ts64(&ts); 819 ptp_ocp_settime(&bp->ptp_info, &ts); 820 } 821 822 /* If there is a clock supervisor, then enable the watchdog */ 823 if (bp->pps_to_clk) { 824 timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0); 825 mod_timer(&bp->watchdog, jiffies + HZ); 826 } 827 828 return 0; 829 } 830 831 static void 832 ptp_ocp_utc_distribute(struct ptp_ocp *bp, u32 val) 833 { 834 unsigned long flags; 835 836 spin_lock_irqsave(&bp->lock, flags); 837 838 bp->utc_tai_offset = val; 839 840 if (bp->irig_out) 841 iowrite32(val, &bp->irig_out->adj_sec); 842 if (bp->dcf_out) 843 iowrite32(val, &bp->dcf_out->adj_sec); 844 if (bp->nmea_out) 845 iowrite32(val, &bp->nmea_out->adj_sec); 846 847 spin_unlock_irqrestore(&bp->lock, flags); 848 } 849 850 static void 851 ptp_ocp_tod_init(struct ptp_ocp *bp) 852 { 853 u32 ctrl, reg; 854 855 ctrl = ioread32(&bp->tod->ctrl); 856 ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE; 857 ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B); 858 iowrite32(ctrl, &bp->tod->ctrl); 859 860 reg = ioread32(&bp->tod->utc_status); 861 if (reg & TOD_STATUS_UTC_VALID) 862 ptp_ocp_utc_distribute(bp, reg & TOD_STATUS_UTC_MASK); 863 } 864 865 static void 866 ptp_ocp_tod_info(struct ptp_ocp *bp) 867 { 868 static const char * const proto_name[] = { 869 "NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none", 870 "UBX", "UBX_UTC", "UBX_LS", "UBX_none" 871 }; 872 static const char * const gnss_name[] = { 873 "ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU", 874 }; 875 u32 version, ctrl, reg; 876 int idx; 877 878 version = ioread32(&bp->tod->version); 879 dev_info(&bp->pdev->dev, "TOD Version %d.%d.%d\n", 880 version >> 24, (version >> 16) & 0xff, version & 0xffff); 881 882 ctrl = ioread32(&bp->tod->ctrl); 883 idx = ctrl & TOD_CTRL_PROTOCOL ? 4 : 0; 884 idx += (ctrl >> 16) & 3; 885 dev_info(&bp->pdev->dev, "control: %x\n", ctrl); 886 dev_info(&bp->pdev->dev, "TOD Protocol %s %s\n", proto_name[idx], 887 ctrl & TOD_CTRL_ENABLE ? "enabled" : ""); 888 889 idx = (ctrl >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK; 890 if (idx < ARRAY_SIZE(gnss_name)) 891 dev_info(&bp->pdev->dev, "GNSS %s\n", gnss_name[idx]); 892 893 reg = ioread32(&bp->tod->status); 894 dev_info(&bp->pdev->dev, "status: %x\n", reg); 895 896 reg = ioread32(&bp->tod->adj_sec); 897 dev_info(&bp->pdev->dev, "correction: %d\n", reg); 898 899 reg = ioread32(&bp->tod->utc_status); 900 dev_info(&bp->pdev->dev, "utc_status: %x\n", reg); 901 dev_info(&bp->pdev->dev, "utc_offset: %d valid:%d leap_valid:%d\n", 902 reg & TOD_STATUS_UTC_MASK, reg & TOD_STATUS_UTC_VALID ? 1 : 0, 903 reg & TOD_STATUS_LEAP_VALID ? 1 : 0); 904 } 905 906 static int 907 ptp_ocp_firstchild(struct device *dev, void *data) 908 { 909 return 1; 910 } 911 912 static int 913 ptp_ocp_read_i2c(struct i2c_adapter *adap, u8 addr, u8 reg, u8 sz, u8 *data) 914 { 915 struct i2c_msg msgs[2] = { 916 { 917 .addr = addr, 918 .len = 1, 919 .buf = ®, 920 }, 921 { 922 .addr = addr, 923 .flags = I2C_M_RD, 924 .len = 2, 925 .buf = data, 926 }, 927 }; 928 int err; 929 u8 len; 930 931 /* xiic-i2c for some stupid reason only does 2 byte reads. */ 932 while (sz) { 933 len = min_t(u8, sz, 2); 934 msgs[1].len = len; 935 err = i2c_transfer(adap, msgs, 2); 936 if (err != msgs[1].len) 937 return err; 938 msgs[1].buf += len; 939 reg += len; 940 sz -= len; 941 } 942 return 0; 943 } 944 945 static void 946 ptp_ocp_get_serial_number(struct ptp_ocp *bp) 947 { 948 struct i2c_adapter *adap; 949 struct device *dev; 950 int err; 951 952 if (!bp->i2c_ctrl) 953 return; 954 955 dev = device_find_child(&bp->i2c_ctrl->dev, NULL, ptp_ocp_firstchild); 956 if (!dev) { 957 dev_err(&bp->pdev->dev, "Can't find I2C adapter\n"); 958 return; 959 } 960 961 adap = i2c_verify_adapter(dev); 962 if (!adap) { 963 dev_err(&bp->pdev->dev, "device '%s' isn't an I2C adapter\n", 964 dev_name(dev)); 965 goto out; 966 } 967 968 err = ptp_ocp_read_i2c(adap, 0x58, 0x9A, 6, bp->serial); 969 if (err) { 970 dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", err); 971 goto out; 972 } 973 974 bp->has_serial = true; 975 976 out: 977 put_device(dev); 978 } 979 980 static struct device * 981 ptp_ocp_find_flash(struct ptp_ocp *bp) 982 { 983 struct device *dev, *last; 984 985 last = NULL; 986 dev = &bp->spi_flash->dev; 987 988 while ((dev = device_find_child(dev, NULL, ptp_ocp_firstchild))) { 989 if (!strcmp("mtd", dev_bus_name(dev))) 990 break; 991 put_device(last); 992 last = dev; 993 } 994 put_device(last); 995 996 return dev; 997 } 998 999 static int 1000 ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev, 1001 const struct firmware *fw) 1002 { 1003 struct mtd_info *mtd = dev_get_drvdata(dev); 1004 struct ptp_ocp *bp = devlink_priv(devlink); 1005 size_t off, len, resid, wrote; 1006 struct erase_info erase; 1007 size_t base, blksz; 1008 int err = 0; 1009 1010 off = 0; 1011 base = bp->flash_start; 1012 blksz = 4096; 1013 resid = fw->size; 1014 1015 while (resid) { 1016 devlink_flash_update_status_notify(devlink, "Flashing", 1017 NULL, off, fw->size); 1018 1019 len = min_t(size_t, resid, blksz); 1020 erase.addr = base + off; 1021 erase.len = blksz; 1022 1023 err = mtd_erase(mtd, &erase); 1024 if (err) 1025 goto out; 1026 1027 err = mtd_write(mtd, base + off, len, &wrote, &fw->data[off]); 1028 if (err) 1029 goto out; 1030 1031 off += blksz; 1032 resid -= len; 1033 } 1034 out: 1035 return err; 1036 } 1037 1038 static int 1039 ptp_ocp_devlink_flash_update(struct devlink *devlink, 1040 struct devlink_flash_update_params *params, 1041 struct netlink_ext_ack *extack) 1042 { 1043 struct ptp_ocp *bp = devlink_priv(devlink); 1044 struct device *dev; 1045 const char *msg; 1046 int err; 1047 1048 dev = ptp_ocp_find_flash(bp); 1049 if (!dev) { 1050 dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n"); 1051 return -ENODEV; 1052 } 1053 1054 devlink_flash_update_status_notify(devlink, "Preparing to flash", 1055 NULL, 0, 0); 1056 1057 err = ptp_ocp_devlink_flash(devlink, dev, params->fw); 1058 1059 msg = err ? "Flash error" : "Flash complete"; 1060 devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0); 1061 1062 put_device(dev); 1063 return err; 1064 } 1065 1066 static int 1067 ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req, 1068 struct netlink_ext_ack *extack) 1069 { 1070 struct ptp_ocp *bp = devlink_priv(devlink); 1071 char buf[32]; 1072 int err; 1073 1074 err = devlink_info_driver_name_put(req, KBUILD_MODNAME); 1075 if (err) 1076 return err; 1077 1078 if (bp->image) { 1079 u32 ver = ioread32(&bp->image->version); 1080 1081 if (ver & 0xffff) { 1082 sprintf(buf, "%d", ver); 1083 err = devlink_info_version_running_put(req, 1084 "fw", 1085 buf); 1086 } else { 1087 sprintf(buf, "%d", ver >> 16); 1088 err = devlink_info_version_running_put(req, 1089 "loader", 1090 buf); 1091 } 1092 if (err) 1093 return err; 1094 } 1095 1096 if (!bp->has_serial) 1097 ptp_ocp_get_serial_number(bp); 1098 1099 if (bp->has_serial) { 1100 sprintf(buf, "%pM", bp->serial); 1101 err = devlink_info_serial_number_put(req, buf); 1102 if (err) 1103 return err; 1104 } 1105 1106 return 0; 1107 } 1108 1109 static const struct devlink_ops ptp_ocp_devlink_ops = { 1110 .flash_update = ptp_ocp_devlink_flash_update, 1111 .info_get = ptp_ocp_devlink_info_get, 1112 }; 1113 1114 static void __iomem * 1115 __ptp_ocp_get_mem(struct ptp_ocp *bp, unsigned long start, int size) 1116 { 1117 struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp"); 1118 1119 return devm_ioremap_resource(&bp->pdev->dev, &res); 1120 } 1121 1122 static void __iomem * 1123 ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r) 1124 { 1125 unsigned long start; 1126 1127 start = pci_resource_start(bp->pdev, 0) + r->offset; 1128 return __ptp_ocp_get_mem(bp, start, r->size); 1129 } 1130 1131 static void 1132 ptp_ocp_set_irq_resource(struct resource *res, int irq) 1133 { 1134 struct resource r = DEFINE_RES_IRQ(irq); 1135 *res = r; 1136 } 1137 1138 static void 1139 ptp_ocp_set_mem_resource(struct resource *res, unsigned long start, int size) 1140 { 1141 struct resource r = DEFINE_RES_MEM(start, size); 1142 *res = r; 1143 } 1144 1145 static int 1146 ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r) 1147 { 1148 struct ptp_ocp_flash_info *info; 1149 struct pci_dev *pdev = bp->pdev; 1150 struct platform_device *p; 1151 struct resource res[2]; 1152 unsigned long start; 1153 int id; 1154 1155 start = pci_resource_start(pdev, 0) + r->offset; 1156 ptp_ocp_set_mem_resource(&res[0], start, r->size); 1157 ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1158 1159 info = r->extra; 1160 id = pci_dev_id(pdev) << 1; 1161 id += info->pci_offset; 1162 1163 p = platform_device_register_resndata(&pdev->dev, info->name, id, 1164 res, 2, info->data, 1165 info->data_size); 1166 if (IS_ERR(p)) 1167 return PTR_ERR(p); 1168 1169 bp_assign_entry(bp, r, p); 1170 1171 return 0; 1172 } 1173 1174 static struct platform_device * 1175 ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id) 1176 { 1177 struct ptp_ocp_i2c_info *info; 1178 struct resource res[2]; 1179 unsigned long start; 1180 1181 info = r->extra; 1182 start = pci_resource_start(pdev, 0) + r->offset; 1183 ptp_ocp_set_mem_resource(&res[0], start, r->size); 1184 ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec)); 1185 1186 return platform_device_register_resndata(&pdev->dev, info->name, 1187 id, res, 2, 1188 info->data, info->data_size); 1189 } 1190 1191 static int 1192 ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r) 1193 { 1194 struct pci_dev *pdev = bp->pdev; 1195 struct ptp_ocp_i2c_info *info; 1196 struct platform_device *p; 1197 struct clk_hw *clk; 1198 char buf[32]; 1199 int id; 1200 1201 info = r->extra; 1202 id = pci_dev_id(bp->pdev); 1203 1204 sprintf(buf, "AXI.%d", id); 1205 clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0, 1206 info->fixed_rate); 1207 if (IS_ERR(clk)) 1208 return PTR_ERR(clk); 1209 bp->i2c_clk = clk; 1210 1211 sprintf(buf, "%s.%d", info->name, id); 1212 devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf); 1213 p = ptp_ocp_i2c_bus(bp->pdev, r, id); 1214 if (IS_ERR(p)) 1215 return PTR_ERR(p); 1216 1217 bp_assign_entry(bp, r, p); 1218 1219 return 0; 1220 } 1221 1222 static irqreturn_t 1223 ptp_ocp_ts_irq(int irq, void *priv) 1224 { 1225 struct ptp_ocp_ext_src *ext = priv; 1226 struct ts_reg __iomem *reg = ext->mem; 1227 struct ptp_clock_event ev; 1228 u32 sec, nsec; 1229 1230 if (ext == ext->bp->pps) { 1231 if (ext->bp->pps_req_map & OCP_REQ_PPS) { 1232 ev.type = PTP_CLOCK_PPS; 1233 ptp_clock_event(ext->bp->ptp, &ev); 1234 } 1235 1236 if ((ext->bp->pps_req_map & ~OCP_REQ_PPS) == 0) 1237 goto out; 1238 } 1239 1240 /* XXX should fix API - this converts s/ns -> ts -> s/ns */ 1241 sec = ioread32(®->time_sec); 1242 nsec = ioread32(®->time_ns); 1243 1244 ev.type = PTP_CLOCK_EXTTS; 1245 ev.index = ext->info->index; 1246 ev.timestamp = sec * NSEC_PER_SEC + nsec; 1247 1248 ptp_clock_event(ext->bp->ptp, &ev); 1249 1250 out: 1251 iowrite32(1, ®->intr); /* write 1 to ack */ 1252 1253 return IRQ_HANDLED; 1254 } 1255 1256 static int 1257 ptp_ocp_ts_enable(void *priv, u32 req, bool enable) 1258 { 1259 struct ptp_ocp_ext_src *ext = priv; 1260 struct ts_reg __iomem *reg = ext->mem; 1261 struct ptp_ocp *bp = ext->bp; 1262 1263 if (ext == bp->pps) { 1264 u32 old_map = bp->pps_req_map; 1265 1266 if (enable) 1267 bp->pps_req_map |= req; 1268 else 1269 bp->pps_req_map &= ~req; 1270 1271 /* if no state change, just return */ 1272 if ((!!old_map ^ !!bp->pps_req_map) == 0) 1273 return 0; 1274 } 1275 1276 if (enable) { 1277 iowrite32(1, ®->enable); 1278 iowrite32(1, ®->intr_mask); 1279 iowrite32(1, ®->intr); 1280 } else { 1281 iowrite32(0, ®->intr_mask); 1282 iowrite32(0, ®->enable); 1283 } 1284 1285 return 0; 1286 } 1287 1288 static void 1289 ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext) 1290 { 1291 ext->info->enable(ext, ~0, false); 1292 pci_free_irq(ext->bp->pdev, ext->irq_vec, ext); 1293 kfree(ext); 1294 } 1295 1296 static int 1297 ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r) 1298 { 1299 struct pci_dev *pdev = bp->pdev; 1300 struct ptp_ocp_ext_src *ext; 1301 int err; 1302 1303 ext = kzalloc(sizeof(*ext), GFP_KERNEL); 1304 if (!ext) 1305 return -ENOMEM; 1306 1307 err = -EINVAL; 1308 ext->mem = ptp_ocp_get_mem(bp, r); 1309 if (!ext->mem) 1310 goto out; 1311 1312 ext->bp = bp; 1313 ext->info = r->extra; 1314 ext->irq_vec = r->irq_vec; 1315 1316 err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL, 1317 ext, "ocp%d.%s", bp->id, r->name); 1318 if (err) { 1319 dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec); 1320 goto out; 1321 } 1322 1323 bp_assign_entry(bp, r, ext); 1324 1325 return 0; 1326 1327 out: 1328 kfree(ext); 1329 return err; 1330 } 1331 1332 static int 1333 ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r) 1334 { 1335 struct pci_dev *pdev = bp->pdev; 1336 struct uart_8250_port uart; 1337 1338 /* Setting UPF_IOREMAP and leaving port.membase unspecified lets 1339 * the serial port device claim and release the pci resource. 1340 */ 1341 memset(&uart, 0, sizeof(uart)); 1342 uart.port.dev = &pdev->dev; 1343 uart.port.iotype = UPIO_MEM; 1344 uart.port.regshift = 2; 1345 uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset; 1346 uart.port.irq = pci_irq_vector(pdev, r->irq_vec); 1347 uart.port.uartclk = 50000000; 1348 uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP; 1349 uart.port.type = PORT_16550A; 1350 1351 return serial8250_register_8250_port(&uart); 1352 } 1353 1354 static int 1355 ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r) 1356 { 1357 int port; 1358 1359 port = ptp_ocp_serial_line(bp, r); 1360 if (port < 0) 1361 return port; 1362 1363 bp_assign_entry(bp, r, port); 1364 1365 return 0; 1366 } 1367 1368 static int 1369 ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r) 1370 { 1371 void __iomem *mem; 1372 1373 mem = ptp_ocp_get_mem(bp, r); 1374 if (!mem) 1375 return -EINVAL; 1376 1377 bp_assign_entry(bp, r, mem); 1378 1379 return 0; 1380 } 1381 1382 static void 1383 ptp_ocp_nmea_out_init(struct ptp_ocp *bp) 1384 { 1385 if (!bp->nmea_out) 1386 return; 1387 1388 iowrite32(0, &bp->nmea_out->ctrl); /* disable */ 1389 iowrite32(7, &bp->nmea_out->uart_baud); /* 115200 */ 1390 iowrite32(1, &bp->nmea_out->ctrl); /* enable */ 1391 } 1392 1393 /* FB specific board initializers; last "resource" registered. */ 1394 static int 1395 ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r) 1396 { 1397 bp->flash_start = 1024 * 4096; 1398 1399 ptp_ocp_tod_init(bp); 1400 ptp_ocp_nmea_out_init(bp); 1401 1402 return ptp_ocp_init_clock(bp); 1403 } 1404 1405 static bool 1406 ptp_ocp_allow_irq(struct ptp_ocp *bp, struct ocp_resource *r) 1407 { 1408 bool allow = !r->irq_vec || r->irq_vec < bp->n_irqs; 1409 1410 if (!allow) 1411 dev_err(&bp->pdev->dev, "irq %d out of range, skipping %s\n", 1412 r->irq_vec, r->name); 1413 return allow; 1414 } 1415 1416 static int 1417 ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data) 1418 { 1419 struct ocp_resource *r, *table; 1420 int err = 0; 1421 1422 table = (struct ocp_resource *)driver_data; 1423 for (r = table; r->setup; r++) { 1424 if (!ptp_ocp_allow_irq(bp, r)) 1425 continue; 1426 err = r->setup(bp, r); 1427 if (err) { 1428 dev_err(&bp->pdev->dev, 1429 "Could not register %s: err %d\n", 1430 r->name, err); 1431 break; 1432 } 1433 } 1434 return err; 1435 } 1436 1437 static void 1438 ptp_ocp_enable_fpga(u32 __iomem *reg, u32 bit, bool enable) 1439 { 1440 u32 ctrl; 1441 bool on; 1442 1443 ctrl = ioread32(reg); 1444 on = ctrl & bit; 1445 if (on ^ enable) { 1446 ctrl &= ~bit; 1447 ctrl |= enable ? bit : 0; 1448 iowrite32(ctrl, reg); 1449 } 1450 } 1451 1452 static void 1453 ptp_ocp_irig_out(struct ptp_ocp *bp, bool enable) 1454 { 1455 return ptp_ocp_enable_fpga(&bp->irig_out->ctrl, 1456 IRIG_M_CTRL_ENABLE, enable); 1457 } 1458 1459 static void 1460 ptp_ocp_irig_in(struct ptp_ocp *bp, bool enable) 1461 { 1462 return ptp_ocp_enable_fpga(&bp->irig_in->ctrl, 1463 IRIG_S_CTRL_ENABLE, enable); 1464 } 1465 1466 static void 1467 ptp_ocp_dcf_out(struct ptp_ocp *bp, bool enable) 1468 { 1469 return ptp_ocp_enable_fpga(&bp->dcf_out->ctrl, 1470 DCF_M_CTRL_ENABLE, enable); 1471 } 1472 1473 static void 1474 ptp_ocp_dcf_in(struct ptp_ocp *bp, bool enable) 1475 { 1476 return ptp_ocp_enable_fpga(&bp->dcf_in->ctrl, 1477 DCF_S_CTRL_ENABLE, enable); 1478 } 1479 1480 static void 1481 __handle_signal_outputs(struct ptp_ocp *bp, u32 val) 1482 { 1483 ptp_ocp_irig_out(bp, val & 0x00100010); 1484 ptp_ocp_dcf_out(bp, val & 0x00200020); 1485 } 1486 1487 static void 1488 __handle_signal_inputs(struct ptp_ocp *bp, u32 val) 1489 { 1490 ptp_ocp_irig_in(bp, val & 0x00100010); 1491 ptp_ocp_dcf_in(bp, val & 0x00200020); 1492 } 1493 1494 /* 1495 * ANT0 == gps (in) 1496 * ANT1 == sma1 (in) 1497 * ANT2 == sma2 (in) 1498 * ANT3 == sma3 (out) 1499 * ANT4 == sma4 (out) 1500 */ 1501 1502 enum ptp_ocp_sma_mode { 1503 SMA_MODE_IN, 1504 SMA_MODE_OUT, 1505 }; 1506 1507 static struct ptp_ocp_sma_connector { 1508 enum ptp_ocp_sma_mode mode; 1509 bool fixed_mode; 1510 u16 default_out_idx; 1511 } ptp_ocp_sma_map[4] = { 1512 { 1513 .mode = SMA_MODE_IN, 1514 .fixed_mode = true, 1515 }, 1516 { 1517 .mode = SMA_MODE_IN, 1518 .fixed_mode = true, 1519 }, 1520 { 1521 .mode = SMA_MODE_OUT, 1522 .fixed_mode = true, 1523 .default_out_idx = 0, /* 10Mhz */ 1524 }, 1525 { 1526 .mode = SMA_MODE_OUT, 1527 .fixed_mode = true, 1528 .default_out_idx = 1, /* PHC */ 1529 }, 1530 }; 1531 1532 static ssize_t 1533 ptp_ocp_show_output(u32 val, char *buf, int default_idx) 1534 { 1535 const char *name; 1536 ssize_t count; 1537 1538 count = sysfs_emit(buf, "OUT: "); 1539 name = ptp_ocp_select_name_from_val(ptp_ocp_sma_out, val); 1540 if (!name) 1541 name = ptp_ocp_sma_out[default_idx].name; 1542 count += sysfs_emit_at(buf, count, "%s\n", name); 1543 return count; 1544 } 1545 1546 static ssize_t 1547 ptp_ocp_show_inputs(u32 val, char *buf, const char *zero_in) 1548 { 1549 const char *name; 1550 ssize_t count; 1551 int i; 1552 1553 count = sysfs_emit(buf, "IN: "); 1554 for (i = 0; i < ARRAY_SIZE(ptp_ocp_sma_in); i++) { 1555 if (val & ptp_ocp_sma_in[i].value) { 1556 name = ptp_ocp_sma_in[i].name; 1557 count += sysfs_emit_at(buf, count, "%s ", name); 1558 } 1559 } 1560 if (!val && zero_in) 1561 count += sysfs_emit_at(buf, count, "%s ", zero_in); 1562 if (count) 1563 count--; 1564 count += sysfs_emit_at(buf, count, "\n"); 1565 return count; 1566 } 1567 1568 static int 1569 sma_parse_inputs(const char *buf, enum ptp_ocp_sma_mode *mode) 1570 { 1571 struct ocp_selector *tbl[] = { ptp_ocp_sma_in, ptp_ocp_sma_out }; 1572 int idx, count, dir; 1573 char **argv; 1574 int ret; 1575 1576 argv = argv_split(GFP_KERNEL, buf, &count); 1577 if (!argv) 1578 return -ENOMEM; 1579 1580 ret = -EINVAL; 1581 if (!count) 1582 goto out; 1583 1584 idx = 0; 1585 dir = *mode == SMA_MODE_IN ? 0 : 1; 1586 if (!strcasecmp("IN:", argv[idx])) { 1587 dir = 0; 1588 idx++; 1589 } 1590 if (!strcasecmp("OUT:", argv[0])) { 1591 dir = 1; 1592 idx++; 1593 } 1594 *mode = dir == 0 ? SMA_MODE_IN : SMA_MODE_OUT; 1595 1596 ret = 0; 1597 for (; idx < count; idx++) 1598 ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]); 1599 if (ret < 0) 1600 ret = -EINVAL; 1601 1602 out: 1603 argv_free(argv); 1604 return ret; 1605 } 1606 1607 static ssize_t 1608 ptp_ocp_sma_show(struct ptp_ocp *bp, int sma_nr, u32 val, char *buf, 1609 const char *zero_in) 1610 { 1611 struct ptp_ocp_sma_connector *sma = &ptp_ocp_sma_map[sma_nr - 1]; 1612 1613 if (sma->mode == SMA_MODE_IN) 1614 return ptp_ocp_show_inputs(val, buf, zero_in); 1615 1616 return ptp_ocp_show_output(val, buf, sma->default_out_idx); 1617 } 1618 1619 static ssize_t 1620 sma1_show(struct device *dev, struct device_attribute *attr, char *buf) 1621 { 1622 struct ptp_ocp *bp = dev_get_drvdata(dev); 1623 u32 val; 1624 1625 val = ioread32(&bp->sma->gpio1) & 0x3f; 1626 return ptp_ocp_sma_show(bp, 1, val, buf, ptp_ocp_sma_in[0].name); 1627 } 1628 1629 static ssize_t 1630 sma2_show(struct device *dev, struct device_attribute *attr, char *buf) 1631 { 1632 struct ptp_ocp *bp = dev_get_drvdata(dev); 1633 u32 val; 1634 1635 val = (ioread32(&bp->sma->gpio1) >> 16) & 0x3f; 1636 return ptp_ocp_sma_show(bp, 2, val, buf, NULL); 1637 } 1638 1639 static ssize_t 1640 sma3_show(struct device *dev, struct device_attribute *attr, char *buf) 1641 { 1642 struct ptp_ocp *bp = dev_get_drvdata(dev); 1643 u32 val; 1644 1645 val = ioread32(&bp->sma->gpio2) & 0x3f; 1646 return ptp_ocp_sma_show(bp, 3, val, buf, NULL); 1647 } 1648 1649 static ssize_t 1650 sma4_show(struct device *dev, struct device_attribute *attr, char *buf) 1651 { 1652 struct ptp_ocp *bp = dev_get_drvdata(dev); 1653 u32 val; 1654 1655 val = (ioread32(&bp->sma->gpio2) >> 16) & 0x3f; 1656 return ptp_ocp_sma_show(bp, 4, val, buf, NULL); 1657 } 1658 1659 static void 1660 ptp_ocp_sma_store_output(struct ptp_ocp *bp, u32 val, u32 shift) 1661 { 1662 unsigned long flags; 1663 u32 gpio, mask; 1664 1665 mask = 0xffff << (16 - shift); 1666 1667 spin_lock_irqsave(&bp->lock, flags); 1668 1669 gpio = ioread32(&bp->sma->gpio2); 1670 gpio = (gpio & mask) | (val << shift); 1671 1672 __handle_signal_outputs(bp, gpio); 1673 1674 iowrite32(gpio, &bp->sma->gpio2); 1675 1676 spin_unlock_irqrestore(&bp->lock, flags); 1677 } 1678 1679 static void 1680 ptp_ocp_sma_store_inputs(struct ptp_ocp *bp, u32 val, u32 shift) 1681 { 1682 unsigned long flags; 1683 u32 gpio, mask; 1684 1685 mask = 0xffff << (16 - shift); 1686 1687 spin_lock_irqsave(&bp->lock, flags); 1688 1689 gpio = ioread32(&bp->sma->gpio1); 1690 gpio = (gpio & mask) | (val << shift); 1691 1692 __handle_signal_inputs(bp, gpio); 1693 1694 iowrite32(gpio, &bp->sma->gpio1); 1695 1696 spin_unlock_irqrestore(&bp->lock, flags); 1697 } 1698 1699 static ssize_t 1700 ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr, u32 shift) 1701 { 1702 struct ptp_ocp_sma_connector *sma = &ptp_ocp_sma_map[sma_nr - 1]; 1703 enum ptp_ocp_sma_mode mode; 1704 int val; 1705 1706 mode = sma->mode; 1707 val = sma_parse_inputs(buf, &mode); 1708 if (val < 0) 1709 return val; 1710 1711 if (mode != sma->mode && sma->fixed_mode) 1712 return -EOPNOTSUPP; 1713 1714 if (mode != sma->mode) { 1715 pr_err("Mode changes not supported yet.\n"); 1716 return -EOPNOTSUPP; 1717 } 1718 1719 if (sma->mode == SMA_MODE_IN) 1720 ptp_ocp_sma_store_inputs(bp, val, shift); 1721 else 1722 ptp_ocp_sma_store_output(bp, val, shift); 1723 1724 return 0; 1725 } 1726 1727 static ssize_t 1728 sma1_store(struct device *dev, struct device_attribute *attr, 1729 const char *buf, size_t count) 1730 { 1731 struct ptp_ocp *bp = dev_get_drvdata(dev); 1732 int err; 1733 1734 err = ptp_ocp_sma_store(bp, buf, 1, 0); 1735 return err ? err : count; 1736 } 1737 1738 static ssize_t 1739 sma2_store(struct device *dev, struct device_attribute *attr, 1740 const char *buf, size_t count) 1741 { 1742 struct ptp_ocp *bp = dev_get_drvdata(dev); 1743 int err; 1744 1745 err = ptp_ocp_sma_store(bp, buf, 2, 16); 1746 return err ? err : count; 1747 } 1748 1749 static ssize_t 1750 sma3_store(struct device *dev, struct device_attribute *attr, 1751 const char *buf, size_t count) 1752 { 1753 struct ptp_ocp *bp = dev_get_drvdata(dev); 1754 int err; 1755 1756 err = ptp_ocp_sma_store(bp, buf, 3, 0); 1757 return err ? err : count; 1758 } 1759 1760 static ssize_t 1761 sma4_store(struct device *dev, struct device_attribute *attr, 1762 const char *buf, size_t count) 1763 { 1764 struct ptp_ocp *bp = dev_get_drvdata(dev); 1765 int err; 1766 1767 err = ptp_ocp_sma_store(bp, buf, 4, 16); 1768 return err ? err : count; 1769 } 1770 static DEVICE_ATTR_RW(sma1); 1771 static DEVICE_ATTR_RW(sma2); 1772 static DEVICE_ATTR_RW(sma3); 1773 static DEVICE_ATTR_RW(sma4); 1774 1775 static ssize_t 1776 available_sma_inputs_show(struct device *dev, 1777 struct device_attribute *attr, char *buf) 1778 { 1779 return ptp_ocp_select_table_show(ptp_ocp_sma_in, buf); 1780 } 1781 static DEVICE_ATTR_RO(available_sma_inputs); 1782 1783 static ssize_t 1784 available_sma_outputs_show(struct device *dev, 1785 struct device_attribute *attr, char *buf) 1786 { 1787 return ptp_ocp_select_table_show(ptp_ocp_sma_out, buf); 1788 } 1789 static DEVICE_ATTR_RO(available_sma_outputs); 1790 1791 static ssize_t 1792 serialnum_show(struct device *dev, struct device_attribute *attr, char *buf) 1793 { 1794 struct ptp_ocp *bp = dev_get_drvdata(dev); 1795 1796 if (!bp->has_serial) 1797 ptp_ocp_get_serial_number(bp); 1798 1799 return sysfs_emit(buf, "%pM\n", bp->serial); 1800 } 1801 static DEVICE_ATTR_RO(serialnum); 1802 1803 static ssize_t 1804 gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf) 1805 { 1806 struct ptp_ocp *bp = dev_get_drvdata(dev); 1807 ssize_t ret; 1808 1809 if (bp->gnss_lost) 1810 ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost); 1811 else 1812 ret = sysfs_emit(buf, "SYNC\n"); 1813 1814 return ret; 1815 } 1816 static DEVICE_ATTR_RO(gnss_sync); 1817 1818 static ssize_t 1819 utc_tai_offset_show(struct device *dev, 1820 struct device_attribute *attr, char *buf) 1821 { 1822 struct ptp_ocp *bp = dev_get_drvdata(dev); 1823 1824 return sysfs_emit(buf, "%d\n", bp->utc_tai_offset); 1825 } 1826 1827 static ssize_t 1828 utc_tai_offset_store(struct device *dev, 1829 struct device_attribute *attr, 1830 const char *buf, size_t count) 1831 { 1832 struct ptp_ocp *bp = dev_get_drvdata(dev); 1833 int err; 1834 u32 val; 1835 1836 err = kstrtou32(buf, 0, &val); 1837 if (err) 1838 return err; 1839 1840 ptp_ocp_utc_distribute(bp, val); 1841 1842 return count; 1843 } 1844 static DEVICE_ATTR_RW(utc_tai_offset); 1845 1846 static ssize_t 1847 ts_window_adjust_show(struct device *dev, 1848 struct device_attribute *attr, char *buf) 1849 { 1850 struct ptp_ocp *bp = dev_get_drvdata(dev); 1851 1852 return sysfs_emit(buf, "%d\n", bp->ts_window_adjust); 1853 } 1854 1855 static ssize_t 1856 ts_window_adjust_store(struct device *dev, 1857 struct device_attribute *attr, 1858 const char *buf, size_t count) 1859 { 1860 struct ptp_ocp *bp = dev_get_drvdata(dev); 1861 int err; 1862 u32 val; 1863 1864 err = kstrtou32(buf, 0, &val); 1865 if (err) 1866 return err; 1867 1868 bp->ts_window_adjust = val; 1869 1870 return count; 1871 } 1872 static DEVICE_ATTR_RW(ts_window_adjust); 1873 1874 static ssize_t 1875 irig_b_mode_show(struct device *dev, struct device_attribute *attr, char *buf) 1876 { 1877 struct ptp_ocp *bp = dev_get_drvdata(dev); 1878 u32 val; 1879 1880 val = ioread32(&bp->irig_out->ctrl); 1881 val = (val >> 16) & 0x07; 1882 return sysfs_emit(buf, "%d\n", val); 1883 } 1884 1885 static ssize_t 1886 irig_b_mode_store(struct device *dev, 1887 struct device_attribute *attr, 1888 const char *buf, size_t count) 1889 { 1890 struct ptp_ocp *bp = dev_get_drvdata(dev); 1891 unsigned long flags; 1892 int err; 1893 u32 reg; 1894 u8 val; 1895 1896 err = kstrtou8(buf, 0, &val); 1897 if (err) 1898 return err; 1899 if (val > 7) 1900 return -EINVAL; 1901 1902 reg = ((val & 0x7) << 16); 1903 1904 spin_lock_irqsave(&bp->lock, flags); 1905 iowrite32(0, &bp->irig_out->ctrl); /* disable */ 1906 iowrite32(reg, &bp->irig_out->ctrl); /* change mode */ 1907 iowrite32(reg | IRIG_M_CTRL_ENABLE, &bp->irig_out->ctrl); 1908 spin_unlock_irqrestore(&bp->lock, flags); 1909 1910 return count; 1911 } 1912 static DEVICE_ATTR_RW(irig_b_mode); 1913 1914 static ssize_t 1915 clock_source_show(struct device *dev, struct device_attribute *attr, char *buf) 1916 { 1917 struct ptp_ocp *bp = dev_get_drvdata(dev); 1918 const char *p; 1919 u32 select; 1920 1921 select = ioread32(&bp->reg->select); 1922 p = ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16); 1923 1924 return sysfs_emit(buf, "%s\n", p); 1925 } 1926 1927 static ssize_t 1928 clock_source_store(struct device *dev, struct device_attribute *attr, 1929 const char *buf, size_t count) 1930 { 1931 struct ptp_ocp *bp = dev_get_drvdata(dev); 1932 unsigned long flags; 1933 int val; 1934 1935 val = ptp_ocp_select_val_from_name(ptp_ocp_clock, buf); 1936 if (val < 0) 1937 return val; 1938 1939 spin_lock_irqsave(&bp->lock, flags); 1940 iowrite32(val, &bp->reg->select); 1941 spin_unlock_irqrestore(&bp->lock, flags); 1942 1943 return count; 1944 } 1945 static DEVICE_ATTR_RW(clock_source); 1946 1947 static ssize_t 1948 available_clock_sources_show(struct device *dev, 1949 struct device_attribute *attr, char *buf) 1950 { 1951 return ptp_ocp_select_table_show(ptp_ocp_clock, buf); 1952 } 1953 static DEVICE_ATTR_RO(available_clock_sources); 1954 1955 static struct attribute *timecard_attrs[] = { 1956 &dev_attr_serialnum.attr, 1957 &dev_attr_gnss_sync.attr, 1958 &dev_attr_clock_source.attr, 1959 &dev_attr_available_clock_sources.attr, 1960 &dev_attr_sma1.attr, 1961 &dev_attr_sma2.attr, 1962 &dev_attr_sma3.attr, 1963 &dev_attr_sma4.attr, 1964 &dev_attr_available_sma_inputs.attr, 1965 &dev_attr_available_sma_outputs.attr, 1966 &dev_attr_irig_b_mode.attr, 1967 &dev_attr_utc_tai_offset.attr, 1968 &dev_attr_ts_window_adjust.attr, 1969 NULL, 1970 }; 1971 ATTRIBUTE_GROUPS(timecard); 1972 1973 static const char * 1974 gpio_map(u32 gpio, u32 bit, const char *pri, const char *sec, const char *def) 1975 { 1976 const char *ans; 1977 1978 if (gpio & (1 << bit)) 1979 ans = pri; 1980 else if (gpio & (1 << (bit + 16))) 1981 ans = sec; 1982 else 1983 ans = def; 1984 return ans; 1985 } 1986 1987 static void 1988 gpio_multi_map(char *buf, u32 gpio, u32 bit, 1989 const char *pri, const char *sec, const char *def) 1990 { 1991 char *ans = buf; 1992 1993 strcpy(ans, def); 1994 if (gpio & (1 << bit)) 1995 ans += sprintf(ans, "%s ", pri); 1996 if (gpio & (1 << (bit + 16))) 1997 ans += sprintf(ans, "%s ", sec); 1998 } 1999 2000 static int 2001 ptp_ocp_summary_show(struct seq_file *s, void *data) 2002 { 2003 struct device *dev = s->private; 2004 struct ptp_system_timestamp sts; 2005 u32 sma_in, sma_out, ctrl, val; 2006 struct ts_reg __iomem *ts_reg; 2007 struct timespec64 ts; 2008 struct ptp_ocp *bp; 2009 const char *src; 2010 bool on, map; 2011 char *buf; 2012 2013 buf = (char *)__get_free_page(GFP_KERNEL); 2014 if (!buf) 2015 return -ENOMEM; 2016 2017 bp = dev_get_drvdata(dev); 2018 sma_in = ioread32(&bp->sma->gpio1); 2019 sma_out = ioread32(&bp->sma->gpio2); 2020 2021 seq_printf(s, "%7s: /dev/ptp%d\n", "PTP", ptp_clock_index(bp->ptp)); 2022 2023 sma1_show(dev, NULL, buf); 2024 seq_printf(s, " sma1: %s", buf); 2025 2026 sma2_show(dev, NULL, buf); 2027 seq_printf(s, " sma2: %s", buf); 2028 2029 sma3_show(dev, NULL, buf); 2030 seq_printf(s, " sma3: %s", buf); 2031 2032 sma4_show(dev, NULL, buf); 2033 seq_printf(s, " sma4: %s", buf); 2034 2035 if (bp->ts0) { 2036 ts_reg = bp->ts0->mem; 2037 on = ioread32(&ts_reg->enable); 2038 src = "GNSS"; 2039 seq_printf(s, "%7s: %s, src: %s\n", "TS0", 2040 on ? " ON" : "OFF", src); 2041 } 2042 2043 if (bp->ts1) { 2044 ts_reg = bp->ts1->mem; 2045 on = ioread32(&ts_reg->enable); 2046 src = gpio_map(sma_in, 2, "sma1", "sma2", "----"); 2047 seq_printf(s, "%7s: %s, src: %s\n", "TS1", 2048 on ? " ON" : "OFF", src); 2049 } 2050 2051 if (bp->ts2) { 2052 ts_reg = bp->ts2->mem; 2053 on = ioread32(&ts_reg->enable); 2054 src = gpio_map(sma_in, 3, "sma1", "sma2", "----"); 2055 seq_printf(s, "%7s: %s, src: %s\n", "TS2", 2056 on ? " ON" : "OFF", src); 2057 } 2058 2059 if (bp->pps) { 2060 ts_reg = bp->pps->mem; 2061 src = "PHC"; 2062 on = ioread32(&ts_reg->enable); 2063 map = !!(bp->pps_req_map & OCP_REQ_TIMESTAMP); 2064 seq_printf(s, "%7s: %s, src: %s\n", "TS3", 2065 on && map ? " ON" : "OFF", src); 2066 2067 map = !!(bp->pps_req_map & OCP_REQ_PPS); 2068 seq_printf(s, "%7s: %s, src: %s\n", "PPS", 2069 on && map ? " ON" : "OFF", src); 2070 } 2071 2072 if (bp->irig_out) { 2073 ctrl = ioread32(&bp->irig_out->ctrl); 2074 on = ctrl & IRIG_M_CTRL_ENABLE; 2075 val = ioread32(&bp->irig_out->status); 2076 gpio_multi_map(buf, sma_out, 4, "sma3", "sma4", "----"); 2077 seq_printf(s, "%7s: %s, error: %d, mode %d, out: %s\n", "IRIG", 2078 on ? " ON" : "OFF", val, (ctrl >> 16), buf); 2079 } 2080 2081 if (bp->irig_in) { 2082 on = ioread32(&bp->irig_in->ctrl) & IRIG_S_CTRL_ENABLE; 2083 val = ioread32(&bp->irig_in->status); 2084 src = gpio_map(sma_in, 4, "sma1", "sma2", "----"); 2085 seq_printf(s, "%7s: %s, error: %d, src: %s\n", "IRIG in", 2086 on ? " ON" : "OFF", val, src); 2087 } 2088 2089 if (bp->dcf_out) { 2090 on = ioread32(&bp->dcf_out->ctrl) & DCF_M_CTRL_ENABLE; 2091 val = ioread32(&bp->dcf_out->status); 2092 gpio_multi_map(buf, sma_out, 5, "sma3", "sma4", "----"); 2093 seq_printf(s, "%7s: %s, error: %d, out: %s\n", "DCF", 2094 on ? " ON" : "OFF", val, buf); 2095 } 2096 2097 if (bp->dcf_in) { 2098 on = ioread32(&bp->dcf_in->ctrl) & DCF_S_CTRL_ENABLE; 2099 val = ioread32(&bp->dcf_in->status); 2100 src = gpio_map(sma_in, 5, "sma1", "sma2", "----"); 2101 seq_printf(s, "%7s: %s, error: %d, src: %s\n", "DCF in", 2102 on ? " ON" : "OFF", val, src); 2103 } 2104 2105 if (bp->nmea_out) { 2106 on = ioread32(&bp->nmea_out->ctrl) & 1; 2107 val = ioread32(&bp->nmea_out->status); 2108 seq_printf(s, "%7s: %s, error: %d\n", "NMEA", 2109 on ? " ON" : "OFF", val); 2110 } 2111 2112 /* compute src for PPS1, used below. */ 2113 if (bp->pps_select) { 2114 val = ioread32(&bp->pps_select->gpio1); 2115 if (val & 0x01) 2116 src = gpio_map(sma_in, 0, "sma1", "sma2", "----"); 2117 else if (val & 0x02) 2118 src = "MAC"; 2119 else if (val & 0x04) 2120 src = "GNSS"; 2121 else 2122 src = "----"; 2123 } else { 2124 src = "?"; 2125 } 2126 2127 /* assumes automatic switchover/selection */ 2128 val = ioread32(&bp->reg->select); 2129 switch (val >> 16) { 2130 case 0: 2131 sprintf(buf, "----"); 2132 break; 2133 case 2: 2134 sprintf(buf, "IRIG"); 2135 break; 2136 case 3: 2137 sprintf(buf, "%s via PPS1", src); 2138 break; 2139 case 6: 2140 sprintf(buf, "DCF"); 2141 break; 2142 default: 2143 strcpy(buf, "unknown"); 2144 break; 2145 } 2146 val = ioread32(&bp->reg->status); 2147 seq_printf(s, "%7s: %s, state: %s\n", "PHC src", buf, 2148 val & OCP_STATUS_IN_SYNC ? "sync" : "unsynced"); 2149 2150 /* reuses PPS1 src from earlier */ 2151 seq_printf(s, "MAC PPS1 src: %s\n", src); 2152 2153 src = gpio_map(sma_in, 1, "sma1", "sma2", "GNSS2"); 2154 seq_printf(s, "MAC PPS2 src: %s\n", src); 2155 2156 if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts)) { 2157 struct timespec64 sys_ts; 2158 s64 pre_ns, post_ns, ns; 2159 2160 pre_ns = timespec64_to_ns(&sts.pre_ts); 2161 post_ns = timespec64_to_ns(&sts.post_ts); 2162 ns = (pre_ns + post_ns) / 2; 2163 ns += (s64)bp->utc_tai_offset * NSEC_PER_SEC; 2164 sys_ts = ns_to_timespec64(ns); 2165 2166 seq_printf(s, "%7s: %lld.%ld == %ptT TAI\n", "PHC", 2167 ts.tv_sec, ts.tv_nsec, &ts); 2168 seq_printf(s, "%7s: %lld.%ld == %ptT UTC offset %d\n", "SYS", 2169 sys_ts.tv_sec, sys_ts.tv_nsec, &sys_ts, 2170 bp->utc_tai_offset); 2171 seq_printf(s, "%7s: PHC:SYS offset: %lld window: %lld\n", "", 2172 timespec64_to_ns(&ts) - ns, 2173 post_ns - pre_ns); 2174 } 2175 2176 free_page((unsigned long)buf); 2177 return 0; 2178 } 2179 DEFINE_SHOW_ATTRIBUTE(ptp_ocp_summary); 2180 2181 static struct dentry *ptp_ocp_debugfs_root; 2182 2183 static void 2184 ptp_ocp_debugfs_add_device(struct ptp_ocp *bp) 2185 { 2186 struct dentry *d; 2187 2188 d = debugfs_create_dir(dev_name(&bp->dev), ptp_ocp_debugfs_root); 2189 bp->debug_root = d; 2190 debugfs_create_file("summary", 0444, bp->debug_root, 2191 &bp->dev, &ptp_ocp_summary_fops); 2192 } 2193 2194 static void 2195 ptp_ocp_debugfs_remove_device(struct ptp_ocp *bp) 2196 { 2197 debugfs_remove_recursive(bp->debug_root); 2198 } 2199 2200 static void 2201 ptp_ocp_debugfs_init(void) 2202 { 2203 ptp_ocp_debugfs_root = debugfs_create_dir("timecard", NULL); 2204 } 2205 2206 static void 2207 ptp_ocp_debugfs_fini(void) 2208 { 2209 debugfs_remove_recursive(ptp_ocp_debugfs_root); 2210 } 2211 2212 static void 2213 ptp_ocp_dev_release(struct device *dev) 2214 { 2215 struct ptp_ocp *bp = dev_get_drvdata(dev); 2216 2217 mutex_lock(&ptp_ocp_lock); 2218 idr_remove(&ptp_ocp_idr, bp->id); 2219 mutex_unlock(&ptp_ocp_lock); 2220 } 2221 2222 static int 2223 ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev) 2224 { 2225 int err; 2226 2227 mutex_lock(&ptp_ocp_lock); 2228 err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL); 2229 mutex_unlock(&ptp_ocp_lock); 2230 if (err < 0) { 2231 dev_err(&pdev->dev, "idr_alloc failed: %d\n", err); 2232 return err; 2233 } 2234 bp->id = err; 2235 2236 bp->ptp_info = ptp_ocp_clock_info; 2237 spin_lock_init(&bp->lock); 2238 bp->gnss_port = -1; 2239 bp->gnss2_port = -1; 2240 bp->mac_port = -1; 2241 bp->nmea_port = -1; 2242 bp->pdev = pdev; 2243 2244 device_initialize(&bp->dev); 2245 dev_set_name(&bp->dev, "ocp%d", bp->id); 2246 bp->dev.class = &timecard_class; 2247 bp->dev.parent = &pdev->dev; 2248 bp->dev.release = ptp_ocp_dev_release; 2249 dev_set_drvdata(&bp->dev, bp); 2250 2251 err = device_add(&bp->dev); 2252 if (err) { 2253 dev_err(&bp->dev, "device add failed: %d\n", err); 2254 goto out; 2255 } 2256 2257 pci_set_drvdata(pdev, bp); 2258 2259 return 0; 2260 2261 out: 2262 ptp_ocp_dev_release(&bp->dev); 2263 put_device(&bp->dev); 2264 return err; 2265 } 2266 2267 static void 2268 ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link) 2269 { 2270 struct device *dev = &bp->dev; 2271 2272 if (sysfs_create_link(&dev->kobj, &child->kobj, link)) 2273 dev_err(dev, "%s symlink failed\n", link); 2274 } 2275 2276 static void 2277 ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link) 2278 { 2279 struct device *dev, *child; 2280 2281 dev = &bp->pdev->dev; 2282 2283 child = device_find_child_by_name(dev, name); 2284 if (!child) { 2285 dev_err(dev, "Could not find device %s\n", name); 2286 return; 2287 } 2288 2289 ptp_ocp_symlink(bp, child, link); 2290 put_device(child); 2291 } 2292 2293 static int 2294 ptp_ocp_complete(struct ptp_ocp *bp) 2295 { 2296 struct pps_device *pps; 2297 char buf[32]; 2298 2299 if (bp->gnss_port != -1) { 2300 sprintf(buf, "ttyS%d", bp->gnss_port); 2301 ptp_ocp_link_child(bp, buf, "ttyGNSS"); 2302 } 2303 if (bp->gnss2_port != -1) { 2304 sprintf(buf, "ttyS%d", bp->gnss2_port); 2305 ptp_ocp_link_child(bp, buf, "ttyGNSS2"); 2306 } 2307 if (bp->mac_port != -1) { 2308 sprintf(buf, "ttyS%d", bp->mac_port); 2309 ptp_ocp_link_child(bp, buf, "ttyMAC"); 2310 } 2311 if (bp->nmea_port != -1) { 2312 sprintf(buf, "ttyS%d", bp->nmea_port); 2313 ptp_ocp_link_child(bp, buf, "ttyNMEA"); 2314 } 2315 sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp)); 2316 ptp_ocp_link_child(bp, buf, "ptp"); 2317 2318 pps = pps_lookup_dev(bp->ptp); 2319 if (pps) 2320 ptp_ocp_symlink(bp, pps->dev, "pps"); 2321 2322 if (device_add_groups(&bp->dev, timecard_groups)) 2323 pr_err("device add groups failed\n"); 2324 2325 ptp_ocp_debugfs_add_device(bp); 2326 2327 return 0; 2328 } 2329 2330 static void 2331 ptp_ocp_phc_info(struct ptp_ocp *bp) 2332 { 2333 struct timespec64 ts; 2334 u32 version, select; 2335 bool sync; 2336 2337 version = ioread32(&bp->reg->version); 2338 select = ioread32(&bp->reg->select); 2339 dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n", 2340 version >> 24, (version >> 16) & 0xff, version & 0xffff, 2341 ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16), 2342 ptp_clock_index(bp->ptp)); 2343 2344 sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC; 2345 if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL)) 2346 dev_info(&bp->pdev->dev, "Time: %lld.%ld, %s\n", 2347 ts.tv_sec, ts.tv_nsec, 2348 sync ? "in-sync" : "UNSYNCED"); 2349 } 2350 2351 static void 2352 ptp_ocp_serial_info(struct device *dev, const char *name, int port, int baud) 2353 { 2354 if (port != -1) 2355 dev_info(dev, "%5s: /dev/ttyS%-2d @ %6d\n", name, port, baud); 2356 } 2357 2358 static void 2359 ptp_ocp_info(struct ptp_ocp *bp) 2360 { 2361 static int nmea_baud[] = { 2362 1200, 2400, 4800, 9600, 19200, 38400, 2363 57600, 115200, 230400, 460800, 921600, 2364 1000000, 2000000 2365 }; 2366 struct device *dev = &bp->pdev->dev; 2367 u32 reg; 2368 2369 ptp_ocp_phc_info(bp); 2370 if (bp->tod) 2371 ptp_ocp_tod_info(bp); 2372 2373 if (bp->image) { 2374 u32 ver = ioread32(&bp->image->version); 2375 2376 dev_info(dev, "version %x\n", ver); 2377 if (ver & 0xffff) 2378 dev_info(dev, "regular image, version %d\n", 2379 ver & 0xffff); 2380 else 2381 dev_info(dev, "golden image, version %d\n", 2382 ver >> 16); 2383 } 2384 ptp_ocp_serial_info(dev, "GNSS", bp->gnss_port, 115200); 2385 ptp_ocp_serial_info(dev, "GNSS2", bp->gnss2_port, 115200); 2386 ptp_ocp_serial_info(dev, "MAC", bp->mac_port, 57600); 2387 if (bp->nmea_out && bp->nmea_port != -1) { 2388 int baud = -1; 2389 2390 reg = ioread32(&bp->nmea_out->uart_baud); 2391 if (reg < ARRAY_SIZE(nmea_baud)) 2392 baud = nmea_baud[reg]; 2393 ptp_ocp_serial_info(dev, "NMEA", bp->nmea_port, baud); 2394 } 2395 } 2396 2397 static void 2398 ptp_ocp_detach_sysfs(struct ptp_ocp *bp) 2399 { 2400 struct device *dev = &bp->dev; 2401 2402 sysfs_remove_link(&dev->kobj, "ttyGNSS"); 2403 sysfs_remove_link(&dev->kobj, "ttyMAC"); 2404 sysfs_remove_link(&dev->kobj, "ptp"); 2405 sysfs_remove_link(&dev->kobj, "pps"); 2406 device_remove_groups(dev, timecard_groups); 2407 } 2408 2409 static void 2410 ptp_ocp_detach(struct ptp_ocp *bp) 2411 { 2412 ptp_ocp_debugfs_remove_device(bp); 2413 ptp_ocp_detach_sysfs(bp); 2414 if (timer_pending(&bp->watchdog)) 2415 del_timer_sync(&bp->watchdog); 2416 if (bp->ts0) 2417 ptp_ocp_unregister_ext(bp->ts0); 2418 if (bp->ts1) 2419 ptp_ocp_unregister_ext(bp->ts1); 2420 if (bp->ts2) 2421 ptp_ocp_unregister_ext(bp->ts2); 2422 if (bp->pps) 2423 ptp_ocp_unregister_ext(bp->pps); 2424 if (bp->gnss_port != -1) 2425 serial8250_unregister_port(bp->gnss_port); 2426 if (bp->gnss2_port != -1) 2427 serial8250_unregister_port(bp->gnss2_port); 2428 if (bp->mac_port != -1) 2429 serial8250_unregister_port(bp->mac_port); 2430 if (bp->nmea_port != -1) 2431 serial8250_unregister_port(bp->nmea_port); 2432 if (bp->spi_flash) 2433 platform_device_unregister(bp->spi_flash); 2434 if (bp->i2c_ctrl) 2435 platform_device_unregister(bp->i2c_ctrl); 2436 if (bp->i2c_clk) 2437 clk_hw_unregister_fixed_rate(bp->i2c_clk); 2438 if (bp->n_irqs) 2439 pci_free_irq_vectors(bp->pdev); 2440 if (bp->ptp) 2441 ptp_clock_unregister(bp->ptp); 2442 device_unregister(&bp->dev); 2443 } 2444 2445 static int 2446 ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id) 2447 { 2448 struct devlink *devlink; 2449 struct ptp_ocp *bp; 2450 int err; 2451 2452 devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp), &pdev->dev); 2453 if (!devlink) { 2454 dev_err(&pdev->dev, "devlink_alloc failed\n"); 2455 return -ENOMEM; 2456 } 2457 2458 err = pci_enable_device(pdev); 2459 if (err) { 2460 dev_err(&pdev->dev, "pci_enable_device\n"); 2461 goto out_unregister; 2462 } 2463 2464 bp = devlink_priv(devlink); 2465 err = ptp_ocp_device_init(bp, pdev); 2466 if (err) 2467 goto out_disable; 2468 2469 /* compat mode. 2470 * Older FPGA firmware only returns 2 irq's. 2471 * allow this - if not all of the IRQ's are returned, skip the 2472 * extra devices and just register the clock. 2473 */ 2474 err = pci_alloc_irq_vectors(pdev, 1, 11, PCI_IRQ_MSI | PCI_IRQ_MSIX); 2475 if (err < 0) { 2476 dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err); 2477 goto out; 2478 } 2479 bp->n_irqs = err; 2480 pci_set_master(pdev); 2481 2482 err = ptp_ocp_register_resources(bp, id->driver_data); 2483 if (err) 2484 goto out; 2485 2486 bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev); 2487 if (IS_ERR(bp->ptp)) { 2488 err = PTR_ERR(bp->ptp); 2489 dev_err(&pdev->dev, "ptp_clock_register: %d\n", err); 2490 bp->ptp = NULL; 2491 goto out; 2492 } 2493 2494 err = ptp_ocp_complete(bp); 2495 if (err) 2496 goto out; 2497 2498 ptp_ocp_info(bp); 2499 devlink_register(devlink); 2500 return 0; 2501 2502 out: 2503 ptp_ocp_detach(bp); 2504 pci_set_drvdata(pdev, NULL); 2505 out_disable: 2506 pci_disable_device(pdev); 2507 out_unregister: 2508 devlink_free(devlink); 2509 return err; 2510 } 2511 2512 static void 2513 ptp_ocp_remove(struct pci_dev *pdev) 2514 { 2515 struct ptp_ocp *bp = pci_get_drvdata(pdev); 2516 struct devlink *devlink = priv_to_devlink(bp); 2517 2518 devlink_unregister(devlink); 2519 ptp_ocp_detach(bp); 2520 pci_set_drvdata(pdev, NULL); 2521 pci_disable_device(pdev); 2522 2523 devlink_free(devlink); 2524 } 2525 2526 static struct pci_driver ptp_ocp_driver = { 2527 .name = KBUILD_MODNAME, 2528 .id_table = ptp_ocp_pcidev_id, 2529 .probe = ptp_ocp_probe, 2530 .remove = ptp_ocp_remove, 2531 }; 2532 2533 static int 2534 ptp_ocp_i2c_notifier_call(struct notifier_block *nb, 2535 unsigned long action, void *data) 2536 { 2537 struct device *dev, *child = data; 2538 struct ptp_ocp *bp; 2539 bool add; 2540 2541 switch (action) { 2542 case BUS_NOTIFY_ADD_DEVICE: 2543 case BUS_NOTIFY_DEL_DEVICE: 2544 add = action == BUS_NOTIFY_ADD_DEVICE; 2545 break; 2546 default: 2547 return 0; 2548 } 2549 2550 if (!i2c_verify_adapter(child)) 2551 return 0; 2552 2553 dev = child; 2554 while ((dev = dev->parent)) 2555 if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME)) 2556 goto found; 2557 return 0; 2558 2559 found: 2560 bp = dev_get_drvdata(dev); 2561 if (add) 2562 ptp_ocp_symlink(bp, child, "i2c"); 2563 else 2564 sysfs_remove_link(&bp->dev.kobj, "i2c"); 2565 2566 return 0; 2567 } 2568 2569 static struct notifier_block ptp_ocp_i2c_notifier = { 2570 .notifier_call = ptp_ocp_i2c_notifier_call, 2571 }; 2572 2573 static int __init 2574 ptp_ocp_init(void) 2575 { 2576 const char *what; 2577 int err; 2578 2579 ptp_ocp_debugfs_init(); 2580 2581 what = "timecard class"; 2582 err = class_register(&timecard_class); 2583 if (err) 2584 goto out; 2585 2586 what = "i2c notifier"; 2587 err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2588 if (err) 2589 goto out_notifier; 2590 2591 what = "ptp_ocp driver"; 2592 err = pci_register_driver(&ptp_ocp_driver); 2593 if (err) 2594 goto out_register; 2595 2596 return 0; 2597 2598 out_register: 2599 bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2600 out_notifier: 2601 class_unregister(&timecard_class); 2602 out: 2603 ptp_ocp_debugfs_fini(); 2604 pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err); 2605 return err; 2606 } 2607 2608 static void __exit 2609 ptp_ocp_fini(void) 2610 { 2611 bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier); 2612 pci_unregister_driver(&ptp_ocp_driver); 2613 class_unregister(&timecard_class); 2614 ptp_ocp_debugfs_fini(); 2615 } 2616 2617 module_init(ptp_ocp_init); 2618 module_exit(ptp_ocp_fini); 2619 2620 MODULE_DESCRIPTION("OpenCompute TimeCard driver"); 2621 MODULE_LICENSE("GPL v2"); 2622