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