1 /* SPDX-License-Identifier: GPL-2.0+ */ 2 /* Copyright (C) 2018 Microchip Technology Inc. */ 3 4 #include <linux/netdevice.h> 5 6 #include <linux/ptp_clock_kernel.h> 7 #include <linux/module.h> 8 #include <linux/pci.h> 9 #include <linux/net_tstamp.h> 10 #include "lan743x_main.h" 11 12 #include "lan743x_ptp.h" 13 14 #define LAN743X_LED0_ENABLE 20 /* LED0 offset in HW_CFG */ 15 #define LAN743X_LED_ENABLE(pin) BIT(LAN743X_LED0_ENABLE + (pin)) 16 17 #define LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB (31249999) 18 #define LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM (2047999934) 19 20 static bool lan743x_ptp_is_enabled(struct lan743x_adapter *adapter); 21 static void lan743x_ptp_enable(struct lan743x_adapter *adapter); 22 static void lan743x_ptp_disable(struct lan743x_adapter *adapter); 23 static void lan743x_ptp_reset(struct lan743x_adapter *adapter); 24 static void lan743x_ptp_clock_set(struct lan743x_adapter *adapter, 25 u32 seconds, u32 nano_seconds, 26 u32 sub_nano_seconds); 27 28 static int lan743x_get_channel(u32 ch_map) 29 { 30 int idx; 31 32 for (idx = 0; idx < 32; idx++) { 33 if (ch_map & (0x1 << idx)) 34 return idx; 35 } 36 37 return -EINVAL; 38 } 39 40 int lan743x_gpio_init(struct lan743x_adapter *adapter) 41 { 42 struct lan743x_gpio *gpio = &adapter->gpio; 43 44 spin_lock_init(&gpio->gpio_lock); 45 46 gpio->gpio_cfg0 = 0; /* set all direction to input, data = 0 */ 47 gpio->gpio_cfg1 = 0x0FFF0000;/* disable all gpio, set to open drain */ 48 gpio->gpio_cfg2 = 0;/* set all to 1588 low polarity level */ 49 gpio->gpio_cfg3 = 0;/* disable all 1588 output */ 50 lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0); 51 lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1); 52 lan743x_csr_write(adapter, GPIO_CFG2, gpio->gpio_cfg2); 53 lan743x_csr_write(adapter, GPIO_CFG3, gpio->gpio_cfg3); 54 55 return 0; 56 } 57 58 static void lan743x_ptp_wait_till_cmd_done(struct lan743x_adapter *adapter, 59 u32 bit_mask) 60 { 61 int timeout = 1000; 62 u32 data = 0; 63 64 while (timeout && 65 (data = (lan743x_csr_read(adapter, PTP_CMD_CTL) & 66 bit_mask))) { 67 usleep_range(1000, 20000); 68 timeout--; 69 } 70 if (data) { 71 netif_err(adapter, drv, adapter->netdev, 72 "timeout waiting for cmd to be done, cmd = 0x%08X\n", 73 bit_mask); 74 } 75 } 76 77 static void lan743x_ptp_tx_ts_enqueue_ts(struct lan743x_adapter *adapter, 78 u32 seconds, u32 nano_seconds, 79 u32 header) 80 { 81 struct lan743x_ptp *ptp = &adapter->ptp; 82 83 spin_lock_bh(&ptp->tx_ts_lock); 84 if (ptp->tx_ts_queue_size < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) { 85 ptp->tx_ts_seconds_queue[ptp->tx_ts_queue_size] = seconds; 86 ptp->tx_ts_nseconds_queue[ptp->tx_ts_queue_size] = nano_seconds; 87 ptp->tx_ts_header_queue[ptp->tx_ts_queue_size] = header; 88 ptp->tx_ts_queue_size++; 89 } else { 90 netif_err(adapter, drv, adapter->netdev, 91 "tx ts queue overflow\n"); 92 } 93 spin_unlock_bh(&ptp->tx_ts_lock); 94 } 95 96 static void lan743x_ptp_tx_ts_complete(struct lan743x_adapter *adapter) 97 { 98 struct lan743x_ptp *ptp = &adapter->ptp; 99 struct skb_shared_hwtstamps tstamps; 100 u32 header, nseconds, seconds; 101 bool ignore_sync = false; 102 struct sk_buff *skb; 103 int c, i; 104 105 spin_lock_bh(&ptp->tx_ts_lock); 106 c = ptp->tx_ts_skb_queue_size; 107 108 if (c > ptp->tx_ts_queue_size) 109 c = ptp->tx_ts_queue_size; 110 if (c <= 0) 111 goto done; 112 113 for (i = 0; i < c; i++) { 114 ignore_sync = ((ptp->tx_ts_ignore_sync_queue & 115 BIT(i)) != 0); 116 skb = ptp->tx_ts_skb_queue[i]; 117 nseconds = ptp->tx_ts_nseconds_queue[i]; 118 seconds = ptp->tx_ts_seconds_queue[i]; 119 header = ptp->tx_ts_header_queue[i]; 120 121 memset(&tstamps, 0, sizeof(tstamps)); 122 tstamps.hwtstamp = ktime_set(seconds, nseconds); 123 if (!ignore_sync || 124 ((header & PTP_TX_MSG_HEADER_MSG_TYPE_) != 125 PTP_TX_MSG_HEADER_MSG_TYPE_SYNC_)) 126 skb_tstamp_tx(skb, &tstamps); 127 128 dev_kfree_skb(skb); 129 130 ptp->tx_ts_skb_queue[i] = NULL; 131 ptp->tx_ts_seconds_queue[i] = 0; 132 ptp->tx_ts_nseconds_queue[i] = 0; 133 ptp->tx_ts_header_queue[i] = 0; 134 } 135 136 /* shift queue */ 137 ptp->tx_ts_ignore_sync_queue >>= c; 138 for (i = c; i < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS; i++) { 139 ptp->tx_ts_skb_queue[i - c] = ptp->tx_ts_skb_queue[i]; 140 ptp->tx_ts_seconds_queue[i - c] = ptp->tx_ts_seconds_queue[i]; 141 ptp->tx_ts_nseconds_queue[i - c] = ptp->tx_ts_nseconds_queue[i]; 142 ptp->tx_ts_header_queue[i - c] = ptp->tx_ts_header_queue[i]; 143 144 ptp->tx_ts_skb_queue[i] = NULL; 145 ptp->tx_ts_seconds_queue[i] = 0; 146 ptp->tx_ts_nseconds_queue[i] = 0; 147 ptp->tx_ts_header_queue[i] = 0; 148 } 149 ptp->tx_ts_skb_queue_size -= c; 150 ptp->tx_ts_queue_size -= c; 151 done: 152 ptp->pending_tx_timestamps -= c; 153 spin_unlock_bh(&ptp->tx_ts_lock); 154 } 155 156 static int lan743x_ptp_reserve_event_ch(struct lan743x_adapter *adapter, 157 int event_channel) 158 { 159 struct lan743x_ptp *ptp = &adapter->ptp; 160 int result = -ENODEV; 161 162 mutex_lock(&ptp->command_lock); 163 if (!(test_bit(event_channel, &ptp->used_event_ch))) { 164 ptp->used_event_ch |= BIT(event_channel); 165 result = event_channel; 166 } else { 167 netif_warn(adapter, drv, adapter->netdev, 168 "attempted to reserved a used event_channel = %d\n", 169 event_channel); 170 } 171 mutex_unlock(&ptp->command_lock); 172 return result; 173 } 174 175 static void lan743x_ptp_release_event_ch(struct lan743x_adapter *adapter, 176 int event_channel) 177 { 178 struct lan743x_ptp *ptp = &adapter->ptp; 179 180 mutex_lock(&ptp->command_lock); 181 if (test_bit(event_channel, &ptp->used_event_ch)) { 182 ptp->used_event_ch &= ~BIT(event_channel); 183 } else { 184 netif_warn(adapter, drv, adapter->netdev, 185 "attempted release on a not used event_channel = %d\n", 186 event_channel); 187 } 188 mutex_unlock(&ptp->command_lock); 189 } 190 191 static void lan743x_ptp_clock_get(struct lan743x_adapter *adapter, 192 u32 *seconds, u32 *nano_seconds, 193 u32 *sub_nano_seconds); 194 static void lan743x_ptp_io_clock_get(struct lan743x_adapter *adapter, 195 u32 *sec, u32 *nsec, u32 *sub_nsec); 196 static void lan743x_ptp_clock_step(struct lan743x_adapter *adapter, 197 s64 time_step_ns); 198 199 static void lan743x_led_mux_enable(struct lan743x_adapter *adapter, 200 int pin, bool enable) 201 { 202 struct lan743x_ptp *ptp = &adapter->ptp; 203 204 if (ptp->leds_multiplexed && 205 ptp->led_enabled[pin]) { 206 u32 val = lan743x_csr_read(adapter, HW_CFG); 207 208 if (enable) 209 val |= LAN743X_LED_ENABLE(pin); 210 else 211 val &= ~LAN743X_LED_ENABLE(pin); 212 213 lan743x_csr_write(adapter, HW_CFG, val); 214 } 215 } 216 217 static void lan743x_led_mux_save(struct lan743x_adapter *adapter) 218 { 219 struct lan743x_ptp *ptp = &adapter->ptp; 220 u32 id_rev = adapter->csr.id_rev & ID_REV_ID_MASK_; 221 222 if (id_rev == ID_REV_ID_LAN7430_) { 223 int i; 224 u32 val = lan743x_csr_read(adapter, HW_CFG); 225 226 for (i = 0; i < LAN7430_N_LED; i++) { 227 bool led_enabled = (val & LAN743X_LED_ENABLE(i)) != 0; 228 229 ptp->led_enabled[i] = led_enabled; 230 } 231 ptp->leds_multiplexed = true; 232 } else { 233 ptp->leds_multiplexed = false; 234 } 235 } 236 237 static void lan743x_led_mux_restore(struct lan743x_adapter *adapter) 238 { 239 u32 id_rev = adapter->csr.id_rev & ID_REV_ID_MASK_; 240 241 if (id_rev == ID_REV_ID_LAN7430_) { 242 int i; 243 244 for (i = 0; i < LAN7430_N_LED; i++) 245 lan743x_led_mux_enable(adapter, i, true); 246 } 247 } 248 249 static int lan743x_gpio_rsrv_ptp_out(struct lan743x_adapter *adapter, 250 int pin, int event_channel) 251 { 252 struct lan743x_gpio *gpio = &adapter->gpio; 253 unsigned long irq_flags = 0; 254 int bit_mask = BIT(pin); 255 int ret = -EBUSY; 256 257 spin_lock_irqsave(&gpio->gpio_lock, irq_flags); 258 259 if (!(gpio->used_bits & bit_mask)) { 260 gpio->used_bits |= bit_mask; 261 gpio->output_bits |= bit_mask; 262 gpio->ptp_bits |= bit_mask; 263 264 /* assign pin to GPIO function */ 265 lan743x_led_mux_enable(adapter, pin, false); 266 267 /* set as output, and zero initial value */ 268 gpio->gpio_cfg0 |= GPIO_CFG0_GPIO_DIR_BIT_(pin); 269 gpio->gpio_cfg0 &= ~GPIO_CFG0_GPIO_DATA_BIT_(pin); 270 lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0); 271 272 /* enable gpio, and set buffer type to push pull */ 273 gpio->gpio_cfg1 &= ~GPIO_CFG1_GPIOEN_BIT_(pin); 274 gpio->gpio_cfg1 |= GPIO_CFG1_GPIOBUF_BIT_(pin); 275 lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1); 276 277 /* set 1588 polarity to high */ 278 gpio->gpio_cfg2 |= GPIO_CFG2_1588_POL_BIT_(pin); 279 lan743x_csr_write(adapter, GPIO_CFG2, gpio->gpio_cfg2); 280 281 if (event_channel == 0) { 282 /* use channel A */ 283 gpio->gpio_cfg3 &= ~GPIO_CFG3_1588_CH_SEL_BIT_(pin); 284 } else { 285 /* use channel B */ 286 gpio->gpio_cfg3 |= GPIO_CFG3_1588_CH_SEL_BIT_(pin); 287 } 288 gpio->gpio_cfg3 |= GPIO_CFG3_1588_OE_BIT_(pin); 289 lan743x_csr_write(adapter, GPIO_CFG3, gpio->gpio_cfg3); 290 291 ret = pin; 292 } 293 spin_unlock_irqrestore(&gpio->gpio_lock, irq_flags); 294 return ret; 295 } 296 297 static void lan743x_gpio_release(struct lan743x_adapter *adapter, int pin) 298 { 299 struct lan743x_gpio *gpio = &adapter->gpio; 300 unsigned long irq_flags = 0; 301 int bit_mask = BIT(pin); 302 303 spin_lock_irqsave(&gpio->gpio_lock, irq_flags); 304 if (gpio->used_bits & bit_mask) { 305 gpio->used_bits &= ~bit_mask; 306 if (gpio->output_bits & bit_mask) { 307 gpio->output_bits &= ~bit_mask; 308 309 if (gpio->ptp_bits & bit_mask) { 310 gpio->ptp_bits &= ~bit_mask; 311 /* disable ptp output */ 312 gpio->gpio_cfg3 &= ~GPIO_CFG3_1588_OE_BIT_(pin); 313 lan743x_csr_write(adapter, GPIO_CFG3, 314 gpio->gpio_cfg3); 315 } 316 /* release gpio output */ 317 318 /* disable gpio */ 319 gpio->gpio_cfg1 |= GPIO_CFG1_GPIOEN_BIT_(pin); 320 gpio->gpio_cfg1 &= ~GPIO_CFG1_GPIOBUF_BIT_(pin); 321 lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1); 322 323 /* reset back to input */ 324 gpio->gpio_cfg0 &= ~GPIO_CFG0_GPIO_DIR_BIT_(pin); 325 gpio->gpio_cfg0 &= ~GPIO_CFG0_GPIO_DATA_BIT_(pin); 326 lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0); 327 328 /* assign pin to original function */ 329 lan743x_led_mux_enable(adapter, pin, true); 330 } 331 } 332 spin_unlock_irqrestore(&gpio->gpio_lock, irq_flags); 333 } 334 335 static int lan743x_ptpci_adjfine(struct ptp_clock_info *ptpci, long scaled_ppm) 336 { 337 struct lan743x_ptp *ptp = 338 container_of(ptpci, struct lan743x_ptp, ptp_clock_info); 339 struct lan743x_adapter *adapter = 340 container_of(ptp, struct lan743x_adapter, ptp); 341 u32 lan743x_rate_adj = 0; 342 bool positive = true; 343 u64 u64_delta = 0; 344 345 if ((scaled_ppm < (-LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM)) || 346 scaled_ppm > LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM) { 347 return -EINVAL; 348 } 349 if (scaled_ppm > 0) { 350 u64_delta = (u64)scaled_ppm; 351 positive = true; 352 } else { 353 u64_delta = (u64)(-scaled_ppm); 354 positive = false; 355 } 356 u64_delta = (u64_delta << 19); 357 lan743x_rate_adj = div_u64(u64_delta, 1000000); 358 359 if (positive) 360 lan743x_rate_adj |= PTP_CLOCK_RATE_ADJ_DIR_; 361 362 lan743x_csr_write(adapter, PTP_CLOCK_RATE_ADJ, 363 lan743x_rate_adj); 364 365 return 0; 366 } 367 368 static int lan743x_ptpci_adjtime(struct ptp_clock_info *ptpci, s64 delta) 369 { 370 struct lan743x_ptp *ptp = 371 container_of(ptpci, struct lan743x_ptp, ptp_clock_info); 372 struct lan743x_adapter *adapter = 373 container_of(ptp, struct lan743x_adapter, ptp); 374 375 lan743x_ptp_clock_step(adapter, delta); 376 377 return 0; 378 } 379 380 static int lan743x_ptpci_gettime64(struct ptp_clock_info *ptpci, 381 struct timespec64 *ts) 382 { 383 struct lan743x_ptp *ptp = 384 container_of(ptpci, struct lan743x_ptp, ptp_clock_info); 385 struct lan743x_adapter *adapter = 386 container_of(ptp, struct lan743x_adapter, ptp); 387 u32 nano_seconds = 0; 388 u32 seconds = 0; 389 390 if (adapter->is_pci11x1x) 391 lan743x_ptp_io_clock_get(adapter, &seconds, &nano_seconds, 392 NULL); 393 else 394 lan743x_ptp_clock_get(adapter, &seconds, &nano_seconds, NULL); 395 ts->tv_sec = seconds; 396 ts->tv_nsec = nano_seconds; 397 398 return 0; 399 } 400 401 static int lan743x_ptpci_settime64(struct ptp_clock_info *ptpci, 402 const struct timespec64 *ts) 403 { 404 struct lan743x_ptp *ptp = 405 container_of(ptpci, struct lan743x_ptp, ptp_clock_info); 406 struct lan743x_adapter *adapter = 407 container_of(ptp, struct lan743x_adapter, ptp); 408 u32 nano_seconds = 0; 409 u32 seconds = 0; 410 411 if (ts) { 412 if (ts->tv_sec > 0xFFFFFFFFLL || 413 ts->tv_sec < 0) { 414 netif_warn(adapter, drv, adapter->netdev, 415 "ts->tv_sec out of range, %lld\n", 416 ts->tv_sec); 417 return -ERANGE; 418 } 419 if (ts->tv_nsec >= 1000000000L || 420 ts->tv_nsec < 0) { 421 netif_warn(adapter, drv, adapter->netdev, 422 "ts->tv_nsec out of range, %ld\n", 423 ts->tv_nsec); 424 return -ERANGE; 425 } 426 seconds = ts->tv_sec; 427 nano_seconds = ts->tv_nsec; 428 lan743x_ptp_clock_set(adapter, seconds, nano_seconds, 0); 429 } else { 430 netif_warn(adapter, drv, adapter->netdev, "ts == NULL\n"); 431 return -EINVAL; 432 } 433 434 return 0; 435 } 436 437 static void lan743x_ptp_perout_off(struct lan743x_adapter *adapter, 438 unsigned int index) 439 { 440 struct lan743x_ptp *ptp = &adapter->ptp; 441 u32 general_config = 0; 442 struct lan743x_ptp_perout *perout = &ptp->perout[index]; 443 444 if (perout->gpio_pin >= 0) { 445 lan743x_gpio_release(adapter, perout->gpio_pin); 446 perout->gpio_pin = -1; 447 } 448 449 if (perout->event_ch >= 0) { 450 /* set target to far in the future, effectively disabling it */ 451 lan743x_csr_write(adapter, 452 PTP_CLOCK_TARGET_SEC_X(perout->event_ch), 453 0xFFFF0000); 454 lan743x_csr_write(adapter, 455 PTP_CLOCK_TARGET_NS_X(perout->event_ch), 456 0); 457 458 general_config = lan743x_csr_read(adapter, PTP_GENERAL_CONFIG); 459 general_config |= PTP_GENERAL_CONFIG_RELOAD_ADD_X_ 460 (perout->event_ch); 461 lan743x_csr_write(adapter, PTP_GENERAL_CONFIG, general_config); 462 lan743x_ptp_release_event_ch(adapter, perout->event_ch); 463 perout->event_ch = -1; 464 } 465 } 466 467 static int lan743x_ptp_perout(struct lan743x_adapter *adapter, int on, 468 struct ptp_perout_request *perout_request) 469 { 470 struct lan743x_ptp *ptp = &adapter->ptp; 471 u32 period_sec = 0, period_nsec = 0; 472 u32 start_sec = 0, start_nsec = 0; 473 u32 general_config = 0; 474 int pulse_width = 0; 475 int perout_pin = 0; 476 unsigned int index = perout_request->index; 477 struct lan743x_ptp_perout *perout = &ptp->perout[index]; 478 int ret = 0; 479 480 /* Reject requests with unsupported flags */ 481 if (perout_request->flags & ~PTP_PEROUT_DUTY_CYCLE) 482 return -EOPNOTSUPP; 483 484 if (on) { 485 perout_pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_PEROUT, 486 perout_request->index); 487 if (perout_pin < 0) 488 return -EBUSY; 489 } else { 490 lan743x_ptp_perout_off(adapter, index); 491 return 0; 492 } 493 494 if (perout->event_ch >= 0 || 495 perout->gpio_pin >= 0) { 496 /* already on, turn off first */ 497 lan743x_ptp_perout_off(adapter, index); 498 } 499 500 perout->event_ch = lan743x_ptp_reserve_event_ch(adapter, index); 501 502 if (perout->event_ch < 0) { 503 netif_warn(adapter, drv, adapter->netdev, 504 "Failed to reserve event channel %d for PEROUT\n", 505 index); 506 ret = -EBUSY; 507 goto failed; 508 } 509 510 perout->gpio_pin = lan743x_gpio_rsrv_ptp_out(adapter, 511 perout_pin, 512 perout->event_ch); 513 514 if (perout->gpio_pin < 0) { 515 netif_warn(adapter, drv, adapter->netdev, 516 "Failed to reserve gpio %d for PEROUT\n", 517 perout_pin); 518 ret = -EBUSY; 519 goto failed; 520 } 521 522 start_sec = perout_request->start.sec; 523 start_sec += perout_request->start.nsec / 1000000000; 524 start_nsec = perout_request->start.nsec % 1000000000; 525 526 period_sec = perout_request->period.sec; 527 period_sec += perout_request->period.nsec / 1000000000; 528 period_nsec = perout_request->period.nsec % 1000000000; 529 530 if (perout_request->flags & PTP_PEROUT_DUTY_CYCLE) { 531 struct timespec64 ts_on, ts_period; 532 s64 wf_high, period64, half; 533 s32 reminder; 534 535 ts_on.tv_sec = perout_request->on.sec; 536 ts_on.tv_nsec = perout_request->on.nsec; 537 wf_high = timespec64_to_ns(&ts_on); 538 ts_period.tv_sec = perout_request->period.sec; 539 ts_period.tv_nsec = perout_request->period.nsec; 540 period64 = timespec64_to_ns(&ts_period); 541 542 if (period64 < 200) { 543 netif_warn(adapter, drv, adapter->netdev, 544 "perout period too small, minimum is 200nS\n"); 545 ret = -EOPNOTSUPP; 546 goto failed; 547 } 548 if (wf_high >= period64) { 549 netif_warn(adapter, drv, adapter->netdev, 550 "pulse width must be smaller than period\n"); 551 ret = -EINVAL; 552 goto failed; 553 } 554 555 /* Check if we can do 50% toggle on an even value of period. 556 * If the period number is odd, then check if the requested 557 * pulse width is the same as one of pre-defined width values. 558 * Otherwise, return failure. 559 */ 560 half = div_s64_rem(period64, 2, &reminder); 561 if (!reminder) { 562 if (half == wf_high) { 563 /* It's 50% match. Use the toggle option */ 564 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_TOGGLE_; 565 /* In this case, devide period value by 2 */ 566 ts_period = ns_to_timespec64(div_s64(period64, 2)); 567 period_sec = ts_period.tv_sec; 568 period_nsec = ts_period.tv_nsec; 569 570 goto program; 571 } 572 } 573 /* if we can't do toggle, then the width option needs to be the exact match */ 574 if (wf_high == 200000000) { 575 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_; 576 } else if (wf_high == 10000000) { 577 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_; 578 } else if (wf_high == 1000000) { 579 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_; 580 } else if (wf_high == 100000) { 581 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_; 582 } else if (wf_high == 10000) { 583 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_; 584 } else if (wf_high == 100) { 585 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_; 586 } else { 587 netif_warn(adapter, drv, adapter->netdev, 588 "duty cycle specified is not supported\n"); 589 ret = -EOPNOTSUPP; 590 goto failed; 591 } 592 } else { 593 if (period_sec == 0) { 594 if (period_nsec >= 400000000) { 595 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_; 596 } else if (period_nsec >= 20000000) { 597 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_; 598 } else if (period_nsec >= 2000000) { 599 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_; 600 } else if (period_nsec >= 200000) { 601 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_; 602 } else if (period_nsec >= 20000) { 603 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_; 604 } else if (period_nsec >= 200) { 605 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_; 606 } else { 607 netif_warn(adapter, drv, adapter->netdev, 608 "perout period too small, minimum is 200nS\n"); 609 ret = -EOPNOTSUPP; 610 goto failed; 611 } 612 } else { 613 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_; 614 } 615 } 616 program: 617 618 /* turn off by setting target far in future */ 619 lan743x_csr_write(adapter, 620 PTP_CLOCK_TARGET_SEC_X(perout->event_ch), 621 0xFFFF0000); 622 lan743x_csr_write(adapter, 623 PTP_CLOCK_TARGET_NS_X(perout->event_ch), 0); 624 625 /* Configure to pulse every period */ 626 general_config = lan743x_csr_read(adapter, PTP_GENERAL_CONFIG); 627 general_config &= ~(PTP_GENERAL_CONFIG_CLOCK_EVENT_X_MASK_ 628 (perout->event_ch)); 629 general_config |= PTP_GENERAL_CONFIG_CLOCK_EVENT_X_SET_ 630 (perout->event_ch, pulse_width); 631 general_config &= ~PTP_GENERAL_CONFIG_RELOAD_ADD_X_ 632 (perout->event_ch); 633 lan743x_csr_write(adapter, PTP_GENERAL_CONFIG, general_config); 634 635 /* set the reload to one toggle cycle */ 636 lan743x_csr_write(adapter, 637 PTP_CLOCK_TARGET_RELOAD_SEC_X(perout->event_ch), 638 period_sec); 639 lan743x_csr_write(adapter, 640 PTP_CLOCK_TARGET_RELOAD_NS_X(perout->event_ch), 641 period_nsec); 642 643 /* set the start time */ 644 lan743x_csr_write(adapter, 645 PTP_CLOCK_TARGET_SEC_X(perout->event_ch), 646 start_sec); 647 lan743x_csr_write(adapter, 648 PTP_CLOCK_TARGET_NS_X(perout->event_ch), 649 start_nsec); 650 651 return 0; 652 653 failed: 654 lan743x_ptp_perout_off(adapter, index); 655 return ret; 656 } 657 658 static void lan743x_ptp_io_perout_off(struct lan743x_adapter *adapter, 659 u32 index) 660 { 661 struct lan743x_ptp *ptp = &adapter->ptp; 662 int perout_pin; 663 int event_ch; 664 u32 gen_cfg; 665 int val; 666 667 event_ch = ptp->ptp_io_perout[index]; 668 if (event_ch >= 0) { 669 /* set target to far in the future, effectively disabling it */ 670 lan743x_csr_write(adapter, 671 PTP_CLOCK_TARGET_SEC_X(event_ch), 672 0xFFFF0000); 673 lan743x_csr_write(adapter, 674 PTP_CLOCK_TARGET_NS_X(event_ch), 675 0); 676 677 gen_cfg = lan743x_csr_read(adapter, HS_PTP_GENERAL_CONFIG); 678 gen_cfg &= ~(HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_X_MASK_ 679 (event_ch)); 680 gen_cfg &= ~(HS_PTP_GENERAL_CONFIG_EVENT_POL_X_(event_ch)); 681 gen_cfg |= HS_PTP_GENERAL_CONFIG_RELOAD_ADD_X_(event_ch); 682 lan743x_csr_write(adapter, HS_PTP_GENERAL_CONFIG, gen_cfg); 683 if (event_ch) 684 lan743x_csr_write(adapter, PTP_INT_STS, 685 PTP_INT_TIMER_INT_B_); 686 else 687 lan743x_csr_write(adapter, PTP_INT_STS, 688 PTP_INT_TIMER_INT_A_); 689 lan743x_ptp_release_event_ch(adapter, event_ch); 690 ptp->ptp_io_perout[index] = -1; 691 } 692 693 perout_pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_PEROUT, index); 694 695 /* Deselect Event output */ 696 val = lan743x_csr_read(adapter, PTP_IO_EVENT_OUTPUT_CFG); 697 698 /* Disables the output of Local Time Target compare events */ 699 val &= ~PTP_IO_EVENT_OUTPUT_CFG_EN_(perout_pin); 700 lan743x_csr_write(adapter, PTP_IO_EVENT_OUTPUT_CFG, val); 701 702 /* Configured as an opendrain driver*/ 703 val = lan743x_csr_read(adapter, PTP_IO_PIN_CFG); 704 val &= ~PTP_IO_PIN_CFG_OBUF_TYPE_(perout_pin); 705 lan743x_csr_write(adapter, PTP_IO_PIN_CFG, val); 706 /* Dummy read to make sure write operation success */ 707 val = lan743x_csr_read(adapter, PTP_IO_PIN_CFG); 708 } 709 710 static int lan743x_ptp_io_perout(struct lan743x_adapter *adapter, int on, 711 struct ptp_perout_request *perout_request) 712 { 713 struct lan743x_ptp *ptp = &adapter->ptp; 714 u32 period_sec, period_nsec; 715 u32 start_sec, start_nsec; 716 u32 pulse_sec, pulse_nsec; 717 int pulse_width; 718 int perout_pin; 719 int event_ch; 720 u32 gen_cfg; 721 u32 index; 722 int val; 723 724 index = perout_request->index; 725 event_ch = ptp->ptp_io_perout[index]; 726 727 if (on) { 728 perout_pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_PEROUT, index); 729 if (perout_pin < 0) 730 return -EBUSY; 731 } else { 732 lan743x_ptp_io_perout_off(adapter, index); 733 return 0; 734 } 735 736 if (event_ch >= LAN743X_PTP_N_EVENT_CHAN) { 737 /* already on, turn off first */ 738 lan743x_ptp_io_perout_off(adapter, index); 739 } 740 741 event_ch = lan743x_ptp_reserve_event_ch(adapter, index); 742 if (event_ch < 0) { 743 netif_warn(adapter, drv, adapter->netdev, 744 "Failed to reserve event channel %d for PEROUT\n", 745 index); 746 goto failed; 747 } 748 ptp->ptp_io_perout[index] = event_ch; 749 750 if (perout_request->flags & PTP_PEROUT_DUTY_CYCLE) { 751 pulse_sec = perout_request->on.sec; 752 pulse_sec += perout_request->on.nsec / 1000000000; 753 pulse_nsec = perout_request->on.nsec % 1000000000; 754 } else { 755 pulse_sec = perout_request->period.sec; 756 pulse_sec += perout_request->period.nsec / 1000000000; 757 pulse_nsec = perout_request->period.nsec % 1000000000; 758 } 759 760 if (pulse_sec == 0) { 761 if (pulse_nsec >= 400000000) { 762 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_; 763 } else if (pulse_nsec >= 200000000) { 764 pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_100MS_; 765 } else if (pulse_nsec >= 100000000) { 766 pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_50MS_; 767 } else if (pulse_nsec >= 20000000) { 768 pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_; 769 } else if (pulse_nsec >= 10000000) { 770 pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_5MS_; 771 } else if (pulse_nsec >= 2000000) { 772 pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_; 773 } else if (pulse_nsec >= 1000000) { 774 pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_500US_; 775 } else if (pulse_nsec >= 200000) { 776 pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_; 777 } else if (pulse_nsec >= 100000) { 778 pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_50US_; 779 } else if (pulse_nsec >= 20000) { 780 pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_; 781 } else if (pulse_nsec >= 10000) { 782 pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_5US_; 783 } else if (pulse_nsec >= 2000) { 784 pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_1US_; 785 } else if (pulse_nsec >= 1000) { 786 pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_500NS_; 787 } else if (pulse_nsec >= 200) { 788 pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_; 789 } else { 790 netif_warn(adapter, drv, adapter->netdev, 791 "perout period too small, min is 200nS\n"); 792 goto failed; 793 } 794 } else { 795 pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_; 796 } 797 798 /* turn off by setting target far in future */ 799 lan743x_csr_write(adapter, 800 PTP_CLOCK_TARGET_SEC_X(event_ch), 801 0xFFFF0000); 802 lan743x_csr_write(adapter, 803 PTP_CLOCK_TARGET_NS_X(event_ch), 0); 804 805 /* Configure to pulse every period */ 806 gen_cfg = lan743x_csr_read(adapter, HS_PTP_GENERAL_CONFIG); 807 gen_cfg &= ~(HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_X_MASK_(event_ch)); 808 gen_cfg |= HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_X_SET_ 809 (event_ch, pulse_width); 810 gen_cfg |= HS_PTP_GENERAL_CONFIG_EVENT_POL_X_(event_ch); 811 gen_cfg &= ~(HS_PTP_GENERAL_CONFIG_RELOAD_ADD_X_(event_ch)); 812 lan743x_csr_write(adapter, HS_PTP_GENERAL_CONFIG, gen_cfg); 813 814 /* set the reload to one toggle cycle */ 815 period_sec = perout_request->period.sec; 816 period_sec += perout_request->period.nsec / 1000000000; 817 period_nsec = perout_request->period.nsec % 1000000000; 818 lan743x_csr_write(adapter, 819 PTP_CLOCK_TARGET_RELOAD_SEC_X(event_ch), 820 period_sec); 821 lan743x_csr_write(adapter, 822 PTP_CLOCK_TARGET_RELOAD_NS_X(event_ch), 823 period_nsec); 824 825 start_sec = perout_request->start.sec; 826 start_sec += perout_request->start.nsec / 1000000000; 827 start_nsec = perout_request->start.nsec % 1000000000; 828 829 /* set the start time */ 830 lan743x_csr_write(adapter, 831 PTP_CLOCK_TARGET_SEC_X(event_ch), 832 start_sec); 833 lan743x_csr_write(adapter, 834 PTP_CLOCK_TARGET_NS_X(event_ch), 835 start_nsec); 836 837 /* Enable LTC Target Read */ 838 val = lan743x_csr_read(adapter, PTP_CMD_CTL); 839 val |= PTP_CMD_CTL_PTP_LTC_TARGET_READ_; 840 lan743x_csr_write(adapter, PTP_CMD_CTL, val); 841 842 /* Configure as an push/pull driver */ 843 val = lan743x_csr_read(adapter, PTP_IO_PIN_CFG); 844 val |= PTP_IO_PIN_CFG_OBUF_TYPE_(perout_pin); 845 lan743x_csr_write(adapter, PTP_IO_PIN_CFG, val); 846 847 /* Select Event output */ 848 val = lan743x_csr_read(adapter, PTP_IO_EVENT_OUTPUT_CFG); 849 if (event_ch) 850 /* Channel B as the output */ 851 val |= PTP_IO_EVENT_OUTPUT_CFG_SEL_(perout_pin); 852 else 853 /* Channel A as the output */ 854 val &= ~PTP_IO_EVENT_OUTPUT_CFG_SEL_(perout_pin); 855 856 /* Enables the output of Local Time Target compare events */ 857 val |= PTP_IO_EVENT_OUTPUT_CFG_EN_(perout_pin); 858 lan743x_csr_write(adapter, PTP_IO_EVENT_OUTPUT_CFG, val); 859 860 return 0; 861 862 failed: 863 lan743x_ptp_io_perout_off(adapter, index); 864 return -ENODEV; 865 } 866 867 static void lan743x_ptp_io_extts_off(struct lan743x_adapter *adapter, 868 u32 index) 869 { 870 struct lan743x_ptp *ptp = &adapter->ptp; 871 struct lan743x_extts *extts; 872 int val; 873 874 extts = &ptp->extts[index]; 875 /* PTP Interrupt Enable Clear Register */ 876 if (extts->flags & PTP_FALLING_EDGE) 877 val = PTP_INT_EN_FE_EN_CLR_(index); 878 else 879 val = PTP_INT_EN_RE_EN_CLR_(index); 880 lan743x_csr_write(adapter, PTP_INT_EN_CLR, val); 881 882 /* Disables PTP-IO edge lock */ 883 val = lan743x_csr_read(adapter, PTP_IO_CAP_CONFIG); 884 if (extts->flags & PTP_FALLING_EDGE) { 885 val &= ~PTP_IO_CAP_CONFIG_LOCK_FE_(index); 886 val &= ~PTP_IO_CAP_CONFIG_FE_CAP_EN_(index); 887 } else { 888 val &= ~PTP_IO_CAP_CONFIG_LOCK_RE_(index); 889 val &= ~PTP_IO_CAP_CONFIG_RE_CAP_EN_(index); 890 } 891 lan743x_csr_write(adapter, PTP_IO_CAP_CONFIG, val); 892 893 /* PTP-IO De-select register */ 894 val = lan743x_csr_read(adapter, PTP_IO_SEL); 895 val &= ~PTP_IO_SEL_MASK_; 896 lan743x_csr_write(adapter, PTP_IO_SEL, val); 897 898 /* Clear timestamp */ 899 memset(&extts->ts, 0, sizeof(struct timespec64)); 900 extts->flags = 0; 901 } 902 903 static int lan743x_ptp_io_event_cap_en(struct lan743x_adapter *adapter, 904 u32 flags, u32 channel) 905 { 906 struct lan743x_ptp *ptp = &adapter->ptp; 907 int val; 908 909 if ((flags & PTP_EXTTS_EDGES) == PTP_EXTTS_EDGES) 910 return -EOPNOTSUPP; 911 912 mutex_lock(&ptp->command_lock); 913 /* PTP-IO Event Capture Enable */ 914 val = lan743x_csr_read(adapter, PTP_IO_CAP_CONFIG); 915 if (flags & PTP_FALLING_EDGE) { 916 val &= ~PTP_IO_CAP_CONFIG_LOCK_RE_(channel); 917 val &= ~PTP_IO_CAP_CONFIG_RE_CAP_EN_(channel); 918 val |= PTP_IO_CAP_CONFIG_LOCK_FE_(channel); 919 val |= PTP_IO_CAP_CONFIG_FE_CAP_EN_(channel); 920 } else { 921 /* Rising eventing as Default */ 922 val &= ~PTP_IO_CAP_CONFIG_LOCK_FE_(channel); 923 val &= ~PTP_IO_CAP_CONFIG_FE_CAP_EN_(channel); 924 val |= PTP_IO_CAP_CONFIG_LOCK_RE_(channel); 925 val |= PTP_IO_CAP_CONFIG_RE_CAP_EN_(channel); 926 } 927 lan743x_csr_write(adapter, PTP_IO_CAP_CONFIG, val); 928 929 /* PTP-IO Select */ 930 val = lan743x_csr_read(adapter, PTP_IO_SEL); 931 val &= ~PTP_IO_SEL_MASK_; 932 val |= channel << PTP_IO_SEL_SHIFT_; 933 lan743x_csr_write(adapter, PTP_IO_SEL, val); 934 935 /* PTP Interrupt Enable Register */ 936 if (flags & PTP_FALLING_EDGE) 937 val = PTP_INT_EN_FE_EN_SET_(channel); 938 else 939 val = PTP_INT_EN_RE_EN_SET_(channel); 940 lan743x_csr_write(adapter, PTP_INT_EN_SET, val); 941 942 mutex_unlock(&ptp->command_lock); 943 944 return 0; 945 } 946 947 static int lan743x_ptp_io_extts(struct lan743x_adapter *adapter, int on, 948 struct ptp_extts_request *extts_request) 949 { 950 struct lan743x_ptp *ptp = &adapter->ptp; 951 u32 flags = extts_request->flags; 952 u32 index = extts_request->index; 953 struct lan743x_extts *extts; 954 int extts_pin; 955 int ret = 0; 956 957 extts = &ptp->extts[index]; 958 959 if (on) { 960 extts_pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_EXTTS, index); 961 if (extts_pin < 0) 962 return -EBUSY; 963 964 ret = lan743x_ptp_io_event_cap_en(adapter, flags, index); 965 if (!ret) 966 extts->flags = flags; 967 } else { 968 lan743x_ptp_io_extts_off(adapter, index); 969 } 970 971 return ret; 972 } 973 974 static int lan743x_ptpci_enable(struct ptp_clock_info *ptpci, 975 struct ptp_clock_request *request, int on) 976 { 977 struct lan743x_ptp *ptp = 978 container_of(ptpci, struct lan743x_ptp, ptp_clock_info); 979 struct lan743x_adapter *adapter = 980 container_of(ptp, struct lan743x_adapter, ptp); 981 982 if (request) { 983 switch (request->type) { 984 case PTP_CLK_REQ_EXTTS: 985 if (request->extts.index < ptpci->n_ext_ts) 986 return lan743x_ptp_io_extts(adapter, on, 987 &request->extts); 988 return -EINVAL; 989 case PTP_CLK_REQ_PEROUT: 990 if (request->perout.index < ptpci->n_per_out) { 991 if (adapter->is_pci11x1x) 992 return lan743x_ptp_io_perout(adapter, on, 993 &request->perout); 994 else 995 return lan743x_ptp_perout(adapter, on, 996 &request->perout); 997 } 998 return -EINVAL; 999 case PTP_CLK_REQ_PPS: 1000 return -EINVAL; 1001 default: 1002 netif_err(adapter, drv, adapter->netdev, 1003 "request->type == %d, Unknown\n", 1004 request->type); 1005 break; 1006 } 1007 } else { 1008 netif_err(adapter, drv, adapter->netdev, "request == NULL\n"); 1009 } 1010 return 0; 1011 } 1012 1013 static int lan743x_ptpci_verify_pin_config(struct ptp_clock_info *ptp, 1014 unsigned int pin, 1015 enum ptp_pin_function func, 1016 unsigned int chan) 1017 { 1018 struct lan743x_ptp *lan_ptp = 1019 container_of(ptp, struct lan743x_ptp, ptp_clock_info); 1020 struct lan743x_adapter *adapter = 1021 container_of(lan_ptp, struct lan743x_adapter, ptp); 1022 int result = 0; 1023 1024 /* Confirm the requested function is supported. Parameter 1025 * validation is done by the caller. 1026 */ 1027 switch (func) { 1028 case PTP_PF_NONE: 1029 case PTP_PF_PEROUT: 1030 break; 1031 case PTP_PF_EXTTS: 1032 if (!adapter->is_pci11x1x) 1033 result = -1; 1034 break; 1035 case PTP_PF_PHYSYNC: 1036 default: 1037 result = -1; 1038 break; 1039 } 1040 return result; 1041 } 1042 1043 static void lan743x_ptp_io_event_clock_get(struct lan743x_adapter *adapter, 1044 bool fe, u8 channel, 1045 struct timespec64 *ts) 1046 { 1047 struct lan743x_ptp *ptp = &adapter->ptp; 1048 struct lan743x_extts *extts; 1049 u32 sec, nsec; 1050 1051 mutex_lock(&ptp->command_lock); 1052 if (fe) { 1053 sec = lan743x_csr_read(adapter, PTP_IO_FE_LTC_SEC_CAP_X); 1054 nsec = lan743x_csr_read(adapter, PTP_IO_FE_LTC_NS_CAP_X); 1055 } else { 1056 sec = lan743x_csr_read(adapter, PTP_IO_RE_LTC_SEC_CAP_X); 1057 nsec = lan743x_csr_read(adapter, PTP_IO_RE_LTC_NS_CAP_X); 1058 } 1059 1060 mutex_unlock(&ptp->command_lock); 1061 1062 /* Update Local timestamp */ 1063 extts = &ptp->extts[channel]; 1064 extts->ts.tv_sec = sec; 1065 extts->ts.tv_nsec = nsec; 1066 ts->tv_sec = sec; 1067 ts->tv_nsec = nsec; 1068 } 1069 1070 static long lan743x_ptpci_do_aux_work(struct ptp_clock_info *ptpci) 1071 { 1072 struct lan743x_ptp *ptp = 1073 container_of(ptpci, struct lan743x_ptp, ptp_clock_info); 1074 struct lan743x_adapter *adapter = 1075 container_of(ptp, struct lan743x_adapter, ptp); 1076 u32 cap_info, cause, header, nsec, seconds; 1077 bool new_timestamp_available = false; 1078 struct ptp_clock_event ptp_event; 1079 struct timespec64 ts; 1080 int ptp_int_sts; 1081 int count = 0; 1082 int channel; 1083 s64 ns; 1084 1085 ptp_int_sts = lan743x_csr_read(adapter, PTP_INT_STS); 1086 while ((count < 100) && ptp_int_sts) { 1087 count++; 1088 1089 if (ptp_int_sts & PTP_INT_BIT_TX_TS_) { 1090 cap_info = lan743x_csr_read(adapter, PTP_CAP_INFO); 1091 1092 if (PTP_CAP_INFO_TX_TS_CNT_GET_(cap_info) > 0) { 1093 seconds = lan743x_csr_read(adapter, 1094 PTP_TX_EGRESS_SEC); 1095 nsec = lan743x_csr_read(adapter, 1096 PTP_TX_EGRESS_NS); 1097 cause = (nsec & 1098 PTP_TX_EGRESS_NS_CAPTURE_CAUSE_MASK_); 1099 header = lan743x_csr_read(adapter, 1100 PTP_TX_MSG_HEADER); 1101 1102 if (cause == 1103 PTP_TX_EGRESS_NS_CAPTURE_CAUSE_SW_) { 1104 nsec &= PTP_TX_EGRESS_NS_TS_NS_MASK_; 1105 lan743x_ptp_tx_ts_enqueue_ts(adapter, 1106 seconds, 1107 nsec, 1108 header); 1109 new_timestamp_available = true; 1110 } else if (cause == 1111 PTP_TX_EGRESS_NS_CAPTURE_CAUSE_AUTO_) { 1112 netif_err(adapter, drv, adapter->netdev, 1113 "Auto capture cause not supported\n"); 1114 } else { 1115 netif_warn(adapter, drv, adapter->netdev, 1116 "unknown tx timestamp capture cause\n"); 1117 } 1118 } else { 1119 netif_warn(adapter, drv, adapter->netdev, 1120 "TX TS INT but no TX TS CNT\n"); 1121 } 1122 lan743x_csr_write(adapter, PTP_INT_STS, 1123 PTP_INT_BIT_TX_TS_); 1124 } 1125 1126 if (ptp_int_sts & PTP_INT_IO_FE_MASK_) { 1127 do { 1128 channel = lan743x_get_channel((ptp_int_sts & 1129 PTP_INT_IO_FE_MASK_) >> 1130 PTP_INT_IO_FE_SHIFT_); 1131 if (channel >= 0 && 1132 channel < PCI11X1X_PTP_IO_MAX_CHANNELS) { 1133 lan743x_ptp_io_event_clock_get(adapter, 1134 true, 1135 channel, 1136 &ts); 1137 /* PTP Falling Event post */ 1138 ns = timespec64_to_ns(&ts); 1139 ptp_event.timestamp = ns; 1140 ptp_event.index = channel; 1141 ptp_event.type = PTP_CLOCK_EXTTS; 1142 ptp_clock_event(ptp->ptp_clock, 1143 &ptp_event); 1144 lan743x_csr_write(adapter, PTP_INT_STS, 1145 PTP_INT_IO_FE_SET_ 1146 (channel)); 1147 ptp_int_sts &= ~(1 << 1148 (PTP_INT_IO_FE_SHIFT_ + 1149 channel)); 1150 } else { 1151 /* Clear falling event interrupts */ 1152 lan743x_csr_write(adapter, PTP_INT_STS, 1153 PTP_INT_IO_FE_MASK_); 1154 ptp_int_sts &= ~PTP_INT_IO_FE_MASK_; 1155 } 1156 } while (ptp_int_sts & PTP_INT_IO_FE_MASK_); 1157 } 1158 1159 if (ptp_int_sts & PTP_INT_IO_RE_MASK_) { 1160 do { 1161 channel = lan743x_get_channel((ptp_int_sts & 1162 PTP_INT_IO_RE_MASK_) >> 1163 PTP_INT_IO_RE_SHIFT_); 1164 if (channel >= 0 && 1165 channel < PCI11X1X_PTP_IO_MAX_CHANNELS) { 1166 lan743x_ptp_io_event_clock_get(adapter, 1167 false, 1168 channel, 1169 &ts); 1170 /* PTP Rising Event post */ 1171 ns = timespec64_to_ns(&ts); 1172 ptp_event.timestamp = ns; 1173 ptp_event.index = channel; 1174 ptp_event.type = PTP_CLOCK_EXTTS; 1175 ptp_clock_event(ptp->ptp_clock, 1176 &ptp_event); 1177 lan743x_csr_write(adapter, PTP_INT_STS, 1178 PTP_INT_IO_RE_SET_ 1179 (channel)); 1180 ptp_int_sts &= ~(1 << 1181 (PTP_INT_IO_RE_SHIFT_ + 1182 channel)); 1183 } else { 1184 /* Clear Rising event interrupt */ 1185 lan743x_csr_write(adapter, PTP_INT_STS, 1186 PTP_INT_IO_RE_MASK_); 1187 ptp_int_sts &= ~PTP_INT_IO_RE_MASK_; 1188 } 1189 } while (ptp_int_sts & PTP_INT_IO_RE_MASK_); 1190 } 1191 1192 ptp_int_sts = lan743x_csr_read(adapter, PTP_INT_STS); 1193 } 1194 1195 if (new_timestamp_available) 1196 lan743x_ptp_tx_ts_complete(adapter); 1197 1198 lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_); 1199 1200 return -1; 1201 } 1202 1203 static void lan743x_ptp_clock_get(struct lan743x_adapter *adapter, 1204 u32 *seconds, u32 *nano_seconds, 1205 u32 *sub_nano_seconds) 1206 { 1207 struct lan743x_ptp *ptp = &adapter->ptp; 1208 1209 mutex_lock(&ptp->command_lock); 1210 1211 lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_); 1212 lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_READ_); 1213 1214 if (seconds) 1215 (*seconds) = lan743x_csr_read(adapter, PTP_CLOCK_SEC); 1216 1217 if (nano_seconds) 1218 (*nano_seconds) = lan743x_csr_read(adapter, PTP_CLOCK_NS); 1219 1220 if (sub_nano_seconds) 1221 (*sub_nano_seconds) = 1222 lan743x_csr_read(adapter, PTP_CLOCK_SUBNS); 1223 1224 mutex_unlock(&ptp->command_lock); 1225 } 1226 1227 static void lan743x_ptp_io_clock_get(struct lan743x_adapter *adapter, 1228 u32 *sec, u32 *nsec, u32 *sub_nsec) 1229 { 1230 struct lan743x_ptp *ptp = &adapter->ptp; 1231 1232 mutex_lock(&ptp->command_lock); 1233 lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_); 1234 lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_READ_); 1235 1236 if (sec) 1237 (*sec) = lan743x_csr_read(adapter, PTP_LTC_RD_SEC_LO); 1238 1239 if (nsec) 1240 (*nsec) = lan743x_csr_read(adapter, PTP_LTC_RD_NS); 1241 1242 if (sub_nsec) 1243 (*sub_nsec) = 1244 lan743x_csr_read(adapter, PTP_LTC_RD_SUBNS); 1245 1246 mutex_unlock(&ptp->command_lock); 1247 } 1248 1249 static void lan743x_ptp_clock_step(struct lan743x_adapter *adapter, 1250 s64 time_step_ns) 1251 { 1252 struct lan743x_ptp *ptp = &adapter->ptp; 1253 u32 nano_seconds_step = 0; 1254 u64 abs_time_step_ns = 0; 1255 u32 unsigned_seconds = 0; 1256 u32 nano_seconds = 0; 1257 u32 remainder = 0; 1258 s32 seconds = 0; 1259 1260 if (time_step_ns > 15000000000LL) { 1261 /* convert to clock set */ 1262 if (adapter->is_pci11x1x) 1263 lan743x_ptp_io_clock_get(adapter, &unsigned_seconds, 1264 &nano_seconds, NULL); 1265 else 1266 lan743x_ptp_clock_get(adapter, &unsigned_seconds, 1267 &nano_seconds, NULL); 1268 unsigned_seconds += div_u64_rem(time_step_ns, 1000000000LL, 1269 &remainder); 1270 nano_seconds += remainder; 1271 if (nano_seconds >= 1000000000) { 1272 unsigned_seconds++; 1273 nano_seconds -= 1000000000; 1274 } 1275 lan743x_ptp_clock_set(adapter, unsigned_seconds, 1276 nano_seconds, 0); 1277 return; 1278 } else if (time_step_ns < -15000000000LL) { 1279 /* convert to clock set */ 1280 time_step_ns = -time_step_ns; 1281 1282 if (adapter->is_pci11x1x) { 1283 lan743x_ptp_io_clock_get(adapter, &unsigned_seconds, 1284 &nano_seconds, NULL); 1285 } else { 1286 lan743x_ptp_clock_get(adapter, &unsigned_seconds, 1287 &nano_seconds, NULL); 1288 } 1289 unsigned_seconds -= div_u64_rem(time_step_ns, 1000000000LL, 1290 &remainder); 1291 nano_seconds_step = remainder; 1292 if (nano_seconds < nano_seconds_step) { 1293 unsigned_seconds--; 1294 nano_seconds += 1000000000; 1295 } 1296 nano_seconds -= nano_seconds_step; 1297 lan743x_ptp_clock_set(adapter, unsigned_seconds, 1298 nano_seconds, 0); 1299 return; 1300 } 1301 1302 /* do clock step */ 1303 if (time_step_ns >= 0) { 1304 abs_time_step_ns = (u64)(time_step_ns); 1305 seconds = (s32)div_u64_rem(abs_time_step_ns, 1000000000, 1306 &remainder); 1307 nano_seconds = (u32)remainder; 1308 } else { 1309 abs_time_step_ns = (u64)(-time_step_ns); 1310 seconds = -((s32)div_u64_rem(abs_time_step_ns, 1000000000, 1311 &remainder)); 1312 nano_seconds = (u32)remainder; 1313 if (nano_seconds > 0) { 1314 /* subtracting nano seconds is not allowed 1315 * convert to subtracting from seconds, 1316 * and adding to nanoseconds 1317 */ 1318 seconds--; 1319 nano_seconds = (1000000000 - nano_seconds); 1320 } 1321 } 1322 1323 if (nano_seconds > 0) { 1324 /* add 8 ns to cover the likely normal increment */ 1325 nano_seconds += 8; 1326 } 1327 1328 if (nano_seconds >= 1000000000) { 1329 /* carry into seconds */ 1330 seconds++; 1331 nano_seconds -= 1000000000; 1332 } 1333 1334 while (seconds) { 1335 mutex_lock(&ptp->command_lock); 1336 if (seconds > 0) { 1337 u32 adjustment_value = (u32)seconds; 1338 1339 if (adjustment_value > 0xF) 1340 adjustment_value = 0xF; 1341 lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ, 1342 PTP_CLOCK_STEP_ADJ_DIR_ | 1343 adjustment_value); 1344 seconds -= ((s32)adjustment_value); 1345 } else { 1346 u32 adjustment_value = (u32)(-seconds); 1347 1348 if (adjustment_value > 0xF) 1349 adjustment_value = 0xF; 1350 lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ, 1351 adjustment_value); 1352 seconds += ((s32)adjustment_value); 1353 } 1354 lan743x_csr_write(adapter, PTP_CMD_CTL, 1355 PTP_CMD_CTL_PTP_CLOCK_STEP_SEC_); 1356 lan743x_ptp_wait_till_cmd_done(adapter, 1357 PTP_CMD_CTL_PTP_CLOCK_STEP_SEC_); 1358 mutex_unlock(&ptp->command_lock); 1359 } 1360 if (nano_seconds) { 1361 mutex_lock(&ptp->command_lock); 1362 lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ, 1363 PTP_CLOCK_STEP_ADJ_DIR_ | 1364 (nano_seconds & 1365 PTP_CLOCK_STEP_ADJ_VALUE_MASK_)); 1366 lan743x_csr_write(adapter, PTP_CMD_CTL, 1367 PTP_CMD_CTL_PTP_CLK_STP_NSEC_); 1368 lan743x_ptp_wait_till_cmd_done(adapter, 1369 PTP_CMD_CTL_PTP_CLK_STP_NSEC_); 1370 mutex_unlock(&ptp->command_lock); 1371 } 1372 } 1373 1374 void lan743x_ptp_isr(void *context) 1375 { 1376 struct lan743x_adapter *adapter = (struct lan743x_adapter *)context; 1377 struct lan743x_ptp *ptp = NULL; 1378 int enable_flag = 1; 1379 u32 ptp_int_sts = 0; 1380 1381 ptp = &adapter->ptp; 1382 1383 lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_1588_); 1384 1385 ptp_int_sts = lan743x_csr_read(adapter, PTP_INT_STS); 1386 ptp_int_sts &= lan743x_csr_read(adapter, PTP_INT_EN_SET); 1387 1388 if (ptp_int_sts & PTP_INT_BIT_TX_TS_) { 1389 ptp_schedule_worker(ptp->ptp_clock, 0); 1390 enable_flag = 0;/* tasklet will re-enable later */ 1391 } 1392 if (ptp_int_sts & PTP_INT_BIT_TX_SWTS_ERR_) { 1393 netif_err(adapter, drv, adapter->netdev, 1394 "PTP TX Software Timestamp Error\n"); 1395 /* clear int status bit */ 1396 lan743x_csr_write(adapter, PTP_INT_STS, 1397 PTP_INT_BIT_TX_SWTS_ERR_); 1398 } 1399 if (ptp_int_sts & PTP_INT_BIT_TIMER_B_) { 1400 /* clear int status bit */ 1401 lan743x_csr_write(adapter, PTP_INT_STS, 1402 PTP_INT_BIT_TIMER_B_); 1403 } 1404 if (ptp_int_sts & PTP_INT_BIT_TIMER_A_) { 1405 /* clear int status bit */ 1406 lan743x_csr_write(adapter, PTP_INT_STS, 1407 PTP_INT_BIT_TIMER_A_); 1408 } 1409 1410 if (enable_flag) { 1411 /* re-enable isr */ 1412 lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_); 1413 } 1414 } 1415 1416 static void lan743x_ptp_tx_ts_enqueue_skb(struct lan743x_adapter *adapter, 1417 struct sk_buff *skb, bool ignore_sync) 1418 { 1419 struct lan743x_ptp *ptp = &adapter->ptp; 1420 1421 spin_lock_bh(&ptp->tx_ts_lock); 1422 if (ptp->tx_ts_skb_queue_size < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) { 1423 ptp->tx_ts_skb_queue[ptp->tx_ts_skb_queue_size] = skb; 1424 if (ignore_sync) 1425 ptp->tx_ts_ignore_sync_queue |= 1426 BIT(ptp->tx_ts_skb_queue_size); 1427 ptp->tx_ts_skb_queue_size++; 1428 } else { 1429 /* this should never happen, so long as the tx channel 1430 * calls and honors the result from 1431 * lan743x_ptp_request_tx_timestamp 1432 */ 1433 netif_err(adapter, drv, adapter->netdev, 1434 "tx ts skb queue overflow\n"); 1435 dev_kfree_skb(skb); 1436 } 1437 spin_unlock_bh(&ptp->tx_ts_lock); 1438 } 1439 1440 static void lan743x_ptp_sync_to_system_clock(struct lan743x_adapter *adapter) 1441 { 1442 struct timespec64 ts; 1443 1444 ktime_get_clocktai_ts64(&ts); 1445 1446 lan743x_ptp_clock_set(adapter, ts.tv_sec, ts.tv_nsec, 0); 1447 } 1448 1449 void lan743x_ptp_update_latency(struct lan743x_adapter *adapter, 1450 u32 link_speed) 1451 { 1452 switch (link_speed) { 1453 case 10: 1454 lan743x_csr_write(adapter, PTP_LATENCY, 1455 PTP_LATENCY_TX_SET_(0) | 1456 PTP_LATENCY_RX_SET_(0)); 1457 break; 1458 case 100: 1459 lan743x_csr_write(adapter, PTP_LATENCY, 1460 PTP_LATENCY_TX_SET_(181) | 1461 PTP_LATENCY_RX_SET_(594)); 1462 break; 1463 case 1000: 1464 lan743x_csr_write(adapter, PTP_LATENCY, 1465 PTP_LATENCY_TX_SET_(30) | 1466 PTP_LATENCY_RX_SET_(525)); 1467 break; 1468 } 1469 } 1470 1471 int lan743x_ptp_init(struct lan743x_adapter *adapter) 1472 { 1473 struct lan743x_ptp *ptp = &adapter->ptp; 1474 int i; 1475 1476 mutex_init(&ptp->command_lock); 1477 spin_lock_init(&ptp->tx_ts_lock); 1478 ptp->used_event_ch = 0; 1479 1480 for (i = 0; i < LAN743X_PTP_N_EVENT_CHAN; i++) { 1481 ptp->perout[i].event_ch = -1; 1482 ptp->perout[i].gpio_pin = -1; 1483 } 1484 1485 lan743x_led_mux_save(adapter); 1486 1487 return 0; 1488 } 1489 1490 int lan743x_ptp_open(struct lan743x_adapter *adapter) 1491 { 1492 struct lan743x_ptp *ptp = &adapter->ptp; 1493 int ret = -ENODEV; 1494 u32 temp; 1495 int i; 1496 int n_pins; 1497 1498 lan743x_ptp_reset(adapter); 1499 lan743x_ptp_sync_to_system_clock(adapter); 1500 temp = lan743x_csr_read(adapter, PTP_TX_MOD2); 1501 temp |= PTP_TX_MOD2_TX_PTP_CLR_UDPV4_CHKSUM_; 1502 lan743x_csr_write(adapter, PTP_TX_MOD2, temp); 1503 lan743x_ptp_enable(adapter); 1504 lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_); 1505 lan743x_csr_write(adapter, PTP_INT_EN_SET, 1506 PTP_INT_BIT_TX_SWTS_ERR_ | PTP_INT_BIT_TX_TS_); 1507 ptp->flags |= PTP_FLAG_ISR_ENABLED; 1508 1509 if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK)) 1510 return 0; 1511 1512 switch (adapter->csr.id_rev & ID_REV_ID_MASK_) { 1513 case ID_REV_ID_LAN7430_: 1514 n_pins = LAN7430_N_GPIO; 1515 break; 1516 case ID_REV_ID_LAN7431_: 1517 case ID_REV_ID_A011_: 1518 case ID_REV_ID_A041_: 1519 n_pins = LAN7431_N_GPIO; 1520 break; 1521 default: 1522 netif_warn(adapter, drv, adapter->netdev, 1523 "Unknown LAN743x (%08x). Assuming no GPIO\n", 1524 adapter->csr.id_rev); 1525 n_pins = 0; 1526 break; 1527 } 1528 1529 if (n_pins > LAN743X_PTP_N_GPIO) 1530 n_pins = LAN743X_PTP_N_GPIO; 1531 1532 for (i = 0; i < n_pins; i++) { 1533 struct ptp_pin_desc *ptp_pin = &ptp->pin_config[i]; 1534 1535 snprintf(ptp_pin->name, 1536 sizeof(ptp_pin->name), "lan743x_ptp_pin_%02d", i); 1537 ptp_pin->index = i; 1538 ptp_pin->func = PTP_PF_NONE; 1539 } 1540 1541 ptp->ptp_clock_info.owner = THIS_MODULE; 1542 snprintf(ptp->ptp_clock_info.name, 16, "%pm", 1543 adapter->netdev->dev_addr); 1544 ptp->ptp_clock_info.max_adj = LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB; 1545 ptp->ptp_clock_info.n_alarm = 0; 1546 ptp->ptp_clock_info.n_ext_ts = LAN743X_PTP_N_EXTTS; 1547 ptp->ptp_clock_info.n_per_out = LAN743X_PTP_N_EVENT_CHAN; 1548 ptp->ptp_clock_info.n_pins = n_pins; 1549 ptp->ptp_clock_info.pps = LAN743X_PTP_N_PPS; 1550 ptp->ptp_clock_info.pin_config = ptp->pin_config; 1551 ptp->ptp_clock_info.adjfine = lan743x_ptpci_adjfine; 1552 ptp->ptp_clock_info.adjtime = lan743x_ptpci_adjtime; 1553 ptp->ptp_clock_info.gettime64 = lan743x_ptpci_gettime64; 1554 ptp->ptp_clock_info.getcrosststamp = NULL; 1555 ptp->ptp_clock_info.settime64 = lan743x_ptpci_settime64; 1556 ptp->ptp_clock_info.enable = lan743x_ptpci_enable; 1557 ptp->ptp_clock_info.do_aux_work = lan743x_ptpci_do_aux_work; 1558 ptp->ptp_clock_info.verify = lan743x_ptpci_verify_pin_config; 1559 1560 ptp->ptp_clock = ptp_clock_register(&ptp->ptp_clock_info, 1561 &adapter->pdev->dev); 1562 1563 if (IS_ERR(ptp->ptp_clock)) { 1564 netif_err(adapter, ifup, adapter->netdev, 1565 "ptp_clock_register failed\n"); 1566 goto done; 1567 } 1568 ptp->flags |= PTP_FLAG_PTP_CLOCK_REGISTERED; 1569 netif_info(adapter, ifup, adapter->netdev, 1570 "successfully registered ptp clock\n"); 1571 1572 return 0; 1573 done: 1574 lan743x_ptp_close(adapter); 1575 return ret; 1576 } 1577 1578 void lan743x_ptp_close(struct lan743x_adapter *adapter) 1579 { 1580 struct lan743x_ptp *ptp = &adapter->ptp; 1581 int index; 1582 1583 if (IS_ENABLED(CONFIG_PTP_1588_CLOCK) && 1584 (ptp->flags & PTP_FLAG_PTP_CLOCK_REGISTERED)) { 1585 ptp_clock_unregister(ptp->ptp_clock); 1586 ptp->ptp_clock = NULL; 1587 ptp->flags &= ~PTP_FLAG_PTP_CLOCK_REGISTERED; 1588 netif_info(adapter, drv, adapter->netdev, 1589 "ptp clock unregister\n"); 1590 } 1591 1592 if (ptp->flags & PTP_FLAG_ISR_ENABLED) { 1593 lan743x_csr_write(adapter, PTP_INT_EN_CLR, 1594 PTP_INT_BIT_TX_SWTS_ERR_ | 1595 PTP_INT_BIT_TX_TS_); 1596 lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_1588_); 1597 ptp->flags &= ~PTP_FLAG_ISR_ENABLED; 1598 } 1599 1600 /* clean up pending timestamp requests */ 1601 lan743x_ptp_tx_ts_complete(adapter); 1602 spin_lock_bh(&ptp->tx_ts_lock); 1603 for (index = 0; 1604 index < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS; 1605 index++) { 1606 struct sk_buff *skb = ptp->tx_ts_skb_queue[index]; 1607 1608 dev_kfree_skb(skb); 1609 ptp->tx_ts_skb_queue[index] = NULL; 1610 ptp->tx_ts_seconds_queue[index] = 0; 1611 ptp->tx_ts_nseconds_queue[index] = 0; 1612 } 1613 ptp->tx_ts_skb_queue_size = 0; 1614 ptp->tx_ts_queue_size = 0; 1615 ptp->pending_tx_timestamps = 0; 1616 spin_unlock_bh(&ptp->tx_ts_lock); 1617 1618 lan743x_led_mux_restore(adapter); 1619 1620 lan743x_ptp_disable(adapter); 1621 } 1622 1623 static void lan743x_ptp_set_sync_ts_insert(struct lan743x_adapter *adapter, 1624 bool ts_insert_enable) 1625 { 1626 u32 ptp_tx_mod = lan743x_csr_read(adapter, PTP_TX_MOD); 1627 1628 if (ts_insert_enable) 1629 ptp_tx_mod |= PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_; 1630 else 1631 ptp_tx_mod &= ~PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_; 1632 1633 lan743x_csr_write(adapter, PTP_TX_MOD, ptp_tx_mod); 1634 } 1635 1636 static bool lan743x_ptp_is_enabled(struct lan743x_adapter *adapter) 1637 { 1638 if (lan743x_csr_read(adapter, PTP_CMD_CTL) & PTP_CMD_CTL_PTP_ENABLE_) 1639 return true; 1640 return false; 1641 } 1642 1643 static void lan743x_ptp_enable(struct lan743x_adapter *adapter) 1644 { 1645 struct lan743x_ptp *ptp = &adapter->ptp; 1646 1647 mutex_lock(&ptp->command_lock); 1648 1649 if (lan743x_ptp_is_enabled(adapter)) { 1650 netif_warn(adapter, drv, adapter->netdev, 1651 "PTP already enabled\n"); 1652 goto done; 1653 } 1654 lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_ENABLE_); 1655 done: 1656 mutex_unlock(&ptp->command_lock); 1657 } 1658 1659 static void lan743x_ptp_disable(struct lan743x_adapter *adapter) 1660 { 1661 struct lan743x_ptp *ptp = &adapter->ptp; 1662 1663 mutex_lock(&ptp->command_lock); 1664 if (!lan743x_ptp_is_enabled(adapter)) { 1665 netif_warn(adapter, drv, adapter->netdev, 1666 "PTP already disabled\n"); 1667 goto done; 1668 } 1669 lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_DISABLE_); 1670 lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_ENABLE_); 1671 done: 1672 mutex_unlock(&ptp->command_lock); 1673 } 1674 1675 static void lan743x_ptp_reset(struct lan743x_adapter *adapter) 1676 { 1677 struct lan743x_ptp *ptp = &adapter->ptp; 1678 1679 mutex_lock(&ptp->command_lock); 1680 1681 if (lan743x_ptp_is_enabled(adapter)) { 1682 netif_err(adapter, drv, adapter->netdev, 1683 "Attempting reset while enabled\n"); 1684 goto done; 1685 } 1686 1687 lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_RESET_); 1688 lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_RESET_); 1689 done: 1690 mutex_unlock(&ptp->command_lock); 1691 } 1692 1693 static void lan743x_ptp_clock_set(struct lan743x_adapter *adapter, 1694 u32 seconds, u32 nano_seconds, 1695 u32 sub_nano_seconds) 1696 { 1697 struct lan743x_ptp *ptp = &adapter->ptp; 1698 1699 mutex_lock(&ptp->command_lock); 1700 1701 lan743x_csr_write(adapter, PTP_CLOCK_SEC, seconds); 1702 lan743x_csr_write(adapter, PTP_CLOCK_NS, nano_seconds); 1703 lan743x_csr_write(adapter, PTP_CLOCK_SUBNS, sub_nano_seconds); 1704 1705 lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_LOAD_); 1706 lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_LOAD_); 1707 mutex_unlock(&ptp->command_lock); 1708 } 1709 1710 bool lan743x_ptp_request_tx_timestamp(struct lan743x_adapter *adapter) 1711 { 1712 struct lan743x_ptp *ptp = &adapter->ptp; 1713 bool result = false; 1714 1715 spin_lock_bh(&ptp->tx_ts_lock); 1716 if (ptp->pending_tx_timestamps < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) { 1717 /* request granted */ 1718 ptp->pending_tx_timestamps++; 1719 result = true; 1720 } 1721 spin_unlock_bh(&ptp->tx_ts_lock); 1722 return result; 1723 } 1724 1725 void lan743x_ptp_unrequest_tx_timestamp(struct lan743x_adapter *adapter) 1726 { 1727 struct lan743x_ptp *ptp = &adapter->ptp; 1728 1729 spin_lock_bh(&ptp->tx_ts_lock); 1730 if (ptp->pending_tx_timestamps > 0) 1731 ptp->pending_tx_timestamps--; 1732 else 1733 netif_err(adapter, drv, adapter->netdev, 1734 "unrequest failed, pending_tx_timestamps==0\n"); 1735 spin_unlock_bh(&ptp->tx_ts_lock); 1736 } 1737 1738 void lan743x_ptp_tx_timestamp_skb(struct lan743x_adapter *adapter, 1739 struct sk_buff *skb, bool ignore_sync) 1740 { 1741 lan743x_ptp_tx_ts_enqueue_skb(adapter, skb, ignore_sync); 1742 1743 lan743x_ptp_tx_ts_complete(adapter); 1744 } 1745 1746 int lan743x_ptp_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd) 1747 { 1748 struct lan743x_adapter *adapter = netdev_priv(netdev); 1749 struct hwtstamp_config config; 1750 int ret = 0; 1751 int index; 1752 1753 if (!ifr) { 1754 netif_err(adapter, drv, adapter->netdev, 1755 "SIOCSHWTSTAMP, ifr == NULL\n"); 1756 return -EINVAL; 1757 } 1758 1759 if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 1760 return -EFAULT; 1761 1762 switch (config.tx_type) { 1763 case HWTSTAMP_TX_OFF: 1764 for (index = 0; index < adapter->used_tx_channels; 1765 index++) 1766 lan743x_tx_set_timestamping_mode(&adapter->tx[index], 1767 false, false); 1768 lan743x_ptp_set_sync_ts_insert(adapter, false); 1769 break; 1770 case HWTSTAMP_TX_ON: 1771 for (index = 0; index < adapter->used_tx_channels; 1772 index++) 1773 lan743x_tx_set_timestamping_mode(&adapter->tx[index], 1774 true, false); 1775 lan743x_ptp_set_sync_ts_insert(adapter, false); 1776 break; 1777 case HWTSTAMP_TX_ONESTEP_SYNC: 1778 for (index = 0; index < adapter->used_tx_channels; 1779 index++) 1780 lan743x_tx_set_timestamping_mode(&adapter->tx[index], 1781 true, true); 1782 1783 lan743x_ptp_set_sync_ts_insert(adapter, true); 1784 break; 1785 case HWTSTAMP_TX_ONESTEP_P2P: 1786 ret = -ERANGE; 1787 break; 1788 default: 1789 netif_warn(adapter, drv, adapter->netdev, 1790 " tx_type = %d, UNKNOWN\n", config.tx_type); 1791 ret = -EINVAL; 1792 break; 1793 } 1794 1795 if (!ret) 1796 return copy_to_user(ifr->ifr_data, &config, 1797 sizeof(config)) ? -EFAULT : 0; 1798 return ret; 1799 } 1800