root/drivers/net/ethernet/microchip/lan743x_ptp.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. lan743x_gpio_init
  2. lan743x_ptp_wait_till_cmd_done
  3. lan743x_ptp_tx_ts_enqueue_ts
  4. lan743x_ptp_tx_ts_complete
  5. lan743x_ptp_reserve_event_ch
  6. lan743x_ptp_release_event_ch
  7. lan743x_gpio_rsrv_ptp_out
  8. lan743x_gpio_release
  9. lan743x_ptpci_adjfine
  10. lan743x_ptpci_adjfreq
  11. lan743x_ptpci_adjtime
  12. lan743x_ptpci_gettime64
  13. lan743x_ptpci_settime64
  14. lan743x_ptp_perout_off
  15. lan743x_ptp_perout
  16. lan743x_ptpci_enable
  17. lan743x_ptpci_do_aux_work
  18. lan743x_ptp_clock_get
  19. lan743x_ptp_clock_step
  20. lan743x_ptp_isr
  21. lan743x_ptp_tx_ts_enqueue_skb
  22. lan743x_ptp_sync_to_system_clock
  23. lan743x_ptp_update_latency
  24. lan743x_ptp_init
  25. lan743x_ptp_open
  26. lan743x_ptp_close
  27. lan743x_ptp_set_sync_ts_insert
  28. lan743x_ptp_is_enabled
  29. lan743x_ptp_enable
  30. lan743x_ptp_disable
  31. lan743x_ptp_reset
  32. lan743x_ptp_clock_set
  33. lan743x_ptp_request_tx_timestamp
  34. lan743x_ptp_unrequest_tx_timestamp
  35. lan743x_ptp_tx_timestamp_skb
  36. lan743x_ptp_ioctl

   1 /* SPDX-License-Identifier: GPL-2.0+ */
   2 /* Copyright (C) 2018 Microchip Technology Inc. */
   3 
   4 #include <linux/netdevice.h>
   5 #include "lan743x_main.h"
   6 
   7 #include <linux/ptp_clock_kernel.h>
   8 #include <linux/module.h>
   9 #include <linux/pci.h>
  10 #include <linux/net_tstamp.h>
  11 
  12 #include "lan743x_ptp.h"
  13 
  14 #define LAN743X_NUMBER_OF_GPIO                  (12)
  15 #define LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB         (31249999)
  16 #define LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM  (2047999934)
  17 
  18 static bool lan743x_ptp_is_enabled(struct lan743x_adapter *adapter);
  19 static void lan743x_ptp_enable(struct lan743x_adapter *adapter);
  20 static void lan743x_ptp_disable(struct lan743x_adapter *adapter);
  21 static void lan743x_ptp_reset(struct lan743x_adapter *adapter);
  22 static void lan743x_ptp_clock_set(struct lan743x_adapter *adapter,
  23                                   u32 seconds, u32 nano_seconds,
  24                                   u32 sub_nano_seconds);
  25 
  26 int lan743x_gpio_init(struct lan743x_adapter *adapter)
  27 {
  28         struct lan743x_gpio *gpio = &adapter->gpio;
  29 
  30         spin_lock_init(&gpio->gpio_lock);
  31 
  32         gpio->gpio_cfg0 = 0; /* set all direction to input, data = 0 */
  33         gpio->gpio_cfg1 = 0x0FFF0000;/* disable all gpio, set to open drain */
  34         gpio->gpio_cfg2 = 0;/* set all to 1588 low polarity level */
  35         gpio->gpio_cfg3 = 0;/* disable all 1588 output */
  36         lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0);
  37         lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1);
  38         lan743x_csr_write(adapter, GPIO_CFG2, gpio->gpio_cfg2);
  39         lan743x_csr_write(adapter, GPIO_CFG3, gpio->gpio_cfg3);
  40 
  41         return 0;
  42 }
  43 
  44 static void lan743x_ptp_wait_till_cmd_done(struct lan743x_adapter *adapter,
  45                                            u32 bit_mask)
  46 {
  47         int timeout = 1000;
  48         u32 data = 0;
  49 
  50         while (timeout &&
  51                (data = (lan743x_csr_read(adapter, PTP_CMD_CTL) &
  52                bit_mask))) {
  53                 usleep_range(1000, 20000);
  54                 timeout--;
  55         }
  56         if (data) {
  57                 netif_err(adapter, drv, adapter->netdev,
  58                           "timeout waiting for cmd to be done, cmd = 0x%08X\n",
  59                           bit_mask);
  60         }
  61 }
  62 
  63 static void lan743x_ptp_tx_ts_enqueue_ts(struct lan743x_adapter *adapter,
  64                                          u32 seconds, u32 nano_seconds,
  65                                          u32 header)
  66 {
  67         struct lan743x_ptp *ptp = &adapter->ptp;
  68 
  69         spin_lock_bh(&ptp->tx_ts_lock);
  70         if (ptp->tx_ts_queue_size < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
  71                 ptp->tx_ts_seconds_queue[ptp->tx_ts_queue_size] = seconds;
  72                 ptp->tx_ts_nseconds_queue[ptp->tx_ts_queue_size] = nano_seconds;
  73                 ptp->tx_ts_header_queue[ptp->tx_ts_queue_size] = header;
  74                 ptp->tx_ts_queue_size++;
  75         } else {
  76                 netif_err(adapter, drv, adapter->netdev,
  77                           "tx ts queue overflow\n");
  78         }
  79         spin_unlock_bh(&ptp->tx_ts_lock);
  80 }
  81 
  82 static void lan743x_ptp_tx_ts_complete(struct lan743x_adapter *adapter)
  83 {
  84         struct lan743x_ptp *ptp = &adapter->ptp;
  85         struct skb_shared_hwtstamps tstamps;
  86         u32 header, nseconds, seconds;
  87         bool ignore_sync = false;
  88         struct sk_buff *skb;
  89         int c, i;
  90 
  91         spin_lock_bh(&ptp->tx_ts_lock);
  92         c = ptp->tx_ts_skb_queue_size;
  93 
  94         if (c > ptp->tx_ts_queue_size)
  95                 c = ptp->tx_ts_queue_size;
  96         if (c <= 0)
  97                 goto done;
  98 
  99         for (i = 0; i < c; i++) {
 100                 ignore_sync = ((ptp->tx_ts_ignore_sync_queue &
 101                                 BIT(i)) != 0);
 102                 skb = ptp->tx_ts_skb_queue[i];
 103                 nseconds = ptp->tx_ts_nseconds_queue[i];
 104                 seconds = ptp->tx_ts_seconds_queue[i];
 105                 header = ptp->tx_ts_header_queue[i];
 106 
 107                 memset(&tstamps, 0, sizeof(tstamps));
 108                 tstamps.hwtstamp = ktime_set(seconds, nseconds);
 109                 if (!ignore_sync ||
 110                     ((header & PTP_TX_MSG_HEADER_MSG_TYPE_) !=
 111                     PTP_TX_MSG_HEADER_MSG_TYPE_SYNC_))
 112                         skb_tstamp_tx(skb, &tstamps);
 113 
 114                 dev_kfree_skb(skb);
 115 
 116                 ptp->tx_ts_skb_queue[i] = NULL;
 117                 ptp->tx_ts_seconds_queue[i] = 0;
 118                 ptp->tx_ts_nseconds_queue[i] = 0;
 119                 ptp->tx_ts_header_queue[i] = 0;
 120         }
 121 
 122         /* shift queue */
 123         ptp->tx_ts_ignore_sync_queue >>= c;
 124         for (i = c; i < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS; i++) {
 125                 ptp->tx_ts_skb_queue[i - c] = ptp->tx_ts_skb_queue[i];
 126                 ptp->tx_ts_seconds_queue[i - c] = ptp->tx_ts_seconds_queue[i];
 127                 ptp->tx_ts_nseconds_queue[i - c] = ptp->tx_ts_nseconds_queue[i];
 128                 ptp->tx_ts_header_queue[i - c] = ptp->tx_ts_header_queue[i];
 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         ptp->tx_ts_skb_queue_size -= c;
 136         ptp->tx_ts_queue_size -= c;
 137 done:
 138         ptp->pending_tx_timestamps -= c;
 139         spin_unlock_bh(&ptp->tx_ts_lock);
 140 }
 141 
 142 static int lan743x_ptp_reserve_event_ch(struct lan743x_adapter *adapter)
 143 {
 144         struct lan743x_ptp *ptp = &adapter->ptp;
 145         int result = -ENODEV;
 146         int index = 0;
 147 
 148         mutex_lock(&ptp->command_lock);
 149         for (index = 0; index < LAN743X_PTP_NUMBER_OF_EVENT_CHANNELS; index++) {
 150                 if (!(test_bit(index, &ptp->used_event_ch))) {
 151                         ptp->used_event_ch |= BIT(index);
 152                         result = index;
 153                         break;
 154                 }
 155         }
 156         mutex_unlock(&ptp->command_lock);
 157         return result;
 158 }
 159 
 160 static void lan743x_ptp_release_event_ch(struct lan743x_adapter *adapter,
 161                                          int event_channel)
 162 {
 163         struct lan743x_ptp *ptp = &adapter->ptp;
 164 
 165         mutex_lock(&ptp->command_lock);
 166         if (test_bit(event_channel, &ptp->used_event_ch)) {
 167                 ptp->used_event_ch &= ~BIT(event_channel);
 168         } else {
 169                 netif_warn(adapter, drv, adapter->netdev,
 170                            "attempted release on a not used event_channel = %d\n",
 171                            event_channel);
 172         }
 173         mutex_unlock(&ptp->command_lock);
 174 }
 175 
 176 static void lan743x_ptp_clock_get(struct lan743x_adapter *adapter,
 177                                   u32 *seconds, u32 *nano_seconds,
 178                                   u32 *sub_nano_seconds);
 179 static void lan743x_ptp_clock_step(struct lan743x_adapter *adapter,
 180                                    s64 time_step_ns);
 181 
 182 static int lan743x_gpio_rsrv_ptp_out(struct lan743x_adapter *adapter,
 183                                      int bit, int ptp_channel)
 184 {
 185         struct lan743x_gpio *gpio = &adapter->gpio;
 186         unsigned long irq_flags = 0;
 187         int bit_mask = BIT(bit);
 188         int ret = -EBUSY;
 189 
 190         spin_lock_irqsave(&gpio->gpio_lock, irq_flags);
 191 
 192         if (!(gpio->used_bits & bit_mask)) {
 193                 gpio->used_bits |= bit_mask;
 194                 gpio->output_bits |= bit_mask;
 195                 gpio->ptp_bits |= bit_mask;
 196 
 197                 /* set as output, and zero initial value */
 198                 gpio->gpio_cfg0 |= GPIO_CFG0_GPIO_DIR_BIT_(bit);
 199                 gpio->gpio_cfg0 &= ~GPIO_CFG0_GPIO_DATA_BIT_(bit);
 200                 lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0);
 201 
 202                 /* enable gpio, and set buffer type to push pull */
 203                 gpio->gpio_cfg1 &= ~GPIO_CFG1_GPIOEN_BIT_(bit);
 204                 gpio->gpio_cfg1 |= GPIO_CFG1_GPIOBUF_BIT_(bit);
 205                 lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1);
 206 
 207                 /* set 1588 polarity to high */
 208                 gpio->gpio_cfg2 |= GPIO_CFG2_1588_POL_BIT_(bit);
 209                 lan743x_csr_write(adapter, GPIO_CFG2, gpio->gpio_cfg2);
 210 
 211                 if (!ptp_channel) {
 212                         /* use channel A */
 213                         gpio->gpio_cfg3 &= ~GPIO_CFG3_1588_CH_SEL_BIT_(bit);
 214                 } else {
 215                         /* use channel B */
 216                         gpio->gpio_cfg3 |= GPIO_CFG3_1588_CH_SEL_BIT_(bit);
 217                 }
 218                 gpio->gpio_cfg3 |= GPIO_CFG3_1588_OE_BIT_(bit);
 219                 lan743x_csr_write(adapter, GPIO_CFG3, gpio->gpio_cfg3);
 220 
 221                 ret = bit;
 222         }
 223         spin_unlock_irqrestore(&gpio->gpio_lock, irq_flags);
 224         return ret;
 225 }
 226 
 227 static void lan743x_gpio_release(struct lan743x_adapter *adapter, int bit)
 228 {
 229         struct lan743x_gpio *gpio = &adapter->gpio;
 230         unsigned long irq_flags = 0;
 231         int bit_mask = BIT(bit);
 232 
 233         spin_lock_irqsave(&gpio->gpio_lock, irq_flags);
 234         if (gpio->used_bits & bit_mask) {
 235                 gpio->used_bits &= ~bit_mask;
 236                 if (gpio->output_bits & bit_mask) {
 237                         gpio->output_bits &= ~bit_mask;
 238 
 239                         if (gpio->ptp_bits & bit_mask) {
 240                                 gpio->ptp_bits &= ~bit_mask;
 241                                 /* disable ptp output */
 242                                 gpio->gpio_cfg3 &= ~GPIO_CFG3_1588_OE_BIT_(bit);
 243                                 lan743x_csr_write(adapter, GPIO_CFG3,
 244                                                   gpio->gpio_cfg3);
 245                         }
 246                         /* release gpio output */
 247 
 248                         /* disable gpio */
 249                         gpio->gpio_cfg1 |= GPIO_CFG1_GPIOEN_BIT_(bit);
 250                         gpio->gpio_cfg1 &= ~GPIO_CFG1_GPIOBUF_BIT_(bit);
 251                         lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1);
 252 
 253                         /* reset back to input */
 254                         gpio->gpio_cfg0 &= ~GPIO_CFG0_GPIO_DIR_BIT_(bit);
 255                         gpio->gpio_cfg0 &= ~GPIO_CFG0_GPIO_DATA_BIT_(bit);
 256                         lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0);
 257                 }
 258         }
 259         spin_unlock_irqrestore(&gpio->gpio_lock, irq_flags);
 260 }
 261 
 262 static int lan743x_ptpci_adjfine(struct ptp_clock_info *ptpci, long scaled_ppm)
 263 {
 264         struct lan743x_ptp *ptp =
 265                 container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
 266         struct lan743x_adapter *adapter =
 267                 container_of(ptp, struct lan743x_adapter, ptp);
 268         u32 lan743x_rate_adj = 0;
 269         bool positive = true;
 270         u64 u64_delta = 0;
 271 
 272         if ((scaled_ppm < (-LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM)) ||
 273             scaled_ppm > LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM) {
 274                 return -EINVAL;
 275         }
 276         if (scaled_ppm > 0) {
 277                 u64_delta = (u64)scaled_ppm;
 278                 positive = true;
 279         } else {
 280                 u64_delta = (u64)(-scaled_ppm);
 281                 positive = false;
 282         }
 283         u64_delta = (u64_delta << 19);
 284         lan743x_rate_adj = div_u64(u64_delta, 1000000);
 285 
 286         if (positive)
 287                 lan743x_rate_adj |= PTP_CLOCK_RATE_ADJ_DIR_;
 288 
 289         lan743x_csr_write(adapter, PTP_CLOCK_RATE_ADJ,
 290                           lan743x_rate_adj);
 291 
 292         return 0;
 293 }
 294 
 295 static int lan743x_ptpci_adjfreq(struct ptp_clock_info *ptpci, s32 delta_ppb)
 296 {
 297         struct lan743x_ptp *ptp =
 298                 container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
 299         struct lan743x_adapter *adapter =
 300                 container_of(ptp, struct lan743x_adapter, ptp);
 301         u32 lan743x_rate_adj = 0;
 302         bool positive = true;
 303         u32 u32_delta = 0;
 304         u64 u64_delta = 0;
 305 
 306         if ((delta_ppb < (-LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB)) ||
 307             delta_ppb > LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB) {
 308                 return -EINVAL;
 309         }
 310         if (delta_ppb > 0) {
 311                 u32_delta = (u32)delta_ppb;
 312                 positive = true;
 313         } else {
 314                 u32_delta = (u32)(-delta_ppb);
 315                 positive = false;
 316         }
 317         u64_delta = (((u64)u32_delta) << 35);
 318         lan743x_rate_adj = div_u64(u64_delta, 1000000000);
 319 
 320         if (positive)
 321                 lan743x_rate_adj |= PTP_CLOCK_RATE_ADJ_DIR_;
 322 
 323         lan743x_csr_write(adapter, PTP_CLOCK_RATE_ADJ,
 324                           lan743x_rate_adj);
 325 
 326         return 0;
 327 }
 328 
 329 static int lan743x_ptpci_adjtime(struct ptp_clock_info *ptpci, s64 delta)
 330 {
 331         struct lan743x_ptp *ptp =
 332                 container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
 333         struct lan743x_adapter *adapter =
 334                 container_of(ptp, struct lan743x_adapter, ptp);
 335 
 336         lan743x_ptp_clock_step(adapter, delta);
 337 
 338         return 0;
 339 }
 340 
 341 static int lan743x_ptpci_gettime64(struct ptp_clock_info *ptpci,
 342                                    struct timespec64 *ts)
 343 {
 344         struct lan743x_ptp *ptp =
 345                 container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
 346         struct lan743x_adapter *adapter =
 347                 container_of(ptp, struct lan743x_adapter, ptp);
 348         u32 nano_seconds = 0;
 349         u32 seconds = 0;
 350 
 351         lan743x_ptp_clock_get(adapter, &seconds, &nano_seconds, NULL);
 352         ts->tv_sec = seconds;
 353         ts->tv_nsec = nano_seconds;
 354 
 355         return 0;
 356 }
 357 
 358 static int lan743x_ptpci_settime64(struct ptp_clock_info *ptpci,
 359                                    const struct timespec64 *ts)
 360 {
 361         struct lan743x_ptp *ptp =
 362                 container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
 363         struct lan743x_adapter *adapter =
 364                 container_of(ptp, struct lan743x_adapter, ptp);
 365         u32 nano_seconds = 0;
 366         u32 seconds = 0;
 367 
 368         if (ts) {
 369                 if (ts->tv_sec > 0xFFFFFFFFLL ||
 370                     ts->tv_sec < 0) {
 371                         netif_warn(adapter, drv, adapter->netdev,
 372                                    "ts->tv_sec out of range, %lld\n",
 373                                    ts->tv_sec);
 374                         return -ERANGE;
 375                 }
 376                 if (ts->tv_nsec >= 1000000000L ||
 377                     ts->tv_nsec < 0) {
 378                         netif_warn(adapter, drv, adapter->netdev,
 379                                    "ts->tv_nsec out of range, %ld\n",
 380                                    ts->tv_nsec);
 381                         return -ERANGE;
 382                 }
 383                 seconds = ts->tv_sec;
 384                 nano_seconds = ts->tv_nsec;
 385                 lan743x_ptp_clock_set(adapter, seconds, nano_seconds, 0);
 386         } else {
 387                 netif_warn(adapter, drv, adapter->netdev, "ts == NULL\n");
 388                 return -EINVAL;
 389         }
 390 
 391         return 0;
 392 }
 393 
 394 static void lan743x_ptp_perout_off(struct lan743x_adapter *adapter)
 395 {
 396         struct lan743x_ptp *ptp = &adapter->ptp;
 397         u32 general_config = 0;
 398 
 399         if (ptp->perout_gpio_bit >= 0) {
 400                 lan743x_gpio_release(adapter, ptp->perout_gpio_bit);
 401                 ptp->perout_gpio_bit = -1;
 402         }
 403 
 404         if (ptp->perout_event_ch >= 0) {
 405                 /* set target to far in the future, effectively disabling it */
 406                 lan743x_csr_write(adapter,
 407                                   PTP_CLOCK_TARGET_SEC_X(ptp->perout_event_ch),
 408                                   0xFFFF0000);
 409                 lan743x_csr_write(adapter,
 410                                   PTP_CLOCK_TARGET_NS_X(ptp->perout_event_ch),
 411                                   0);
 412 
 413                 general_config = lan743x_csr_read(adapter, PTP_GENERAL_CONFIG);
 414                 general_config |= PTP_GENERAL_CONFIG_RELOAD_ADD_X_
 415                                   (ptp->perout_event_ch);
 416                 lan743x_csr_write(adapter, PTP_GENERAL_CONFIG, general_config);
 417                 lan743x_ptp_release_event_ch(adapter, ptp->perout_event_ch);
 418                 ptp->perout_event_ch = -1;
 419         }
 420 }
 421 
 422 static int lan743x_ptp_perout(struct lan743x_adapter *adapter, int on,
 423                               struct ptp_perout_request *perout)
 424 {
 425         struct lan743x_ptp *ptp = &adapter->ptp;
 426         u32 period_sec = 0, period_nsec = 0;
 427         u32 start_sec = 0, start_nsec = 0;
 428         u32 general_config = 0;
 429         int pulse_width = 0;
 430         int perout_bit = 0;
 431 
 432         /* Reject requests with unsupported flags */
 433         if (perout->flags)
 434                 return -EOPNOTSUPP;
 435 
 436         if (!on) {
 437                 lan743x_ptp_perout_off(adapter);
 438                 return 0;
 439         }
 440 
 441         if (ptp->perout_event_ch >= 0 ||
 442             ptp->perout_gpio_bit >= 0) {
 443                 /* already on, turn off first */
 444                 lan743x_ptp_perout_off(adapter);
 445         }
 446 
 447         ptp->perout_event_ch = lan743x_ptp_reserve_event_ch(adapter);
 448         if (ptp->perout_event_ch < 0) {
 449                 netif_warn(adapter, drv, adapter->netdev,
 450                            "Failed to reserve event channel for PEROUT\n");
 451                 goto failed;
 452         }
 453 
 454         switch (adapter->csr.id_rev & ID_REV_ID_MASK_) {
 455         case ID_REV_ID_LAN7430_:
 456                 perout_bit = 2;/* GPIO 2 is preferred on EVB LAN7430 */
 457                 break;
 458         case ID_REV_ID_LAN7431_:
 459                 perout_bit = 4;/* GPIO 4 is preferred on EVB LAN7431 */
 460                 break;
 461         }
 462 
 463         ptp->perout_gpio_bit = lan743x_gpio_rsrv_ptp_out(adapter,
 464                                                          perout_bit,
 465                                                          ptp->perout_event_ch);
 466 
 467         if (ptp->perout_gpio_bit < 0) {
 468                 netif_warn(adapter, drv, adapter->netdev,
 469                            "Failed to reserve gpio %d for PEROUT\n",
 470                            perout_bit);
 471                 goto failed;
 472         }
 473 
 474         start_sec = perout->start.sec;
 475         start_sec += perout->start.nsec / 1000000000;
 476         start_nsec = perout->start.nsec % 1000000000;
 477 
 478         period_sec = perout->period.sec;
 479         period_sec += perout->period.nsec / 1000000000;
 480         period_nsec = perout->period.nsec % 1000000000;
 481 
 482         if (period_sec == 0) {
 483                 if (period_nsec >= 400000000) {
 484                         pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
 485                 } else if (period_nsec >= 20000000) {
 486                         pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_;
 487                 } else if (period_nsec >= 2000000) {
 488                         pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_;
 489                 } else if (period_nsec >= 200000) {
 490                         pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_;
 491                 } else if (period_nsec >= 20000) {
 492                         pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_;
 493                 } else if (period_nsec >= 200) {
 494                         pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_;
 495                 } else {
 496                         netif_warn(adapter, drv, adapter->netdev,
 497                                    "perout period too small, minimum is 200nS\n");
 498                         goto failed;
 499                 }
 500         } else {
 501                 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
 502         }
 503 
 504         /* turn off by setting target far in future */
 505         lan743x_csr_write(adapter,
 506                           PTP_CLOCK_TARGET_SEC_X(ptp->perout_event_ch),
 507                           0xFFFF0000);
 508         lan743x_csr_write(adapter,
 509                           PTP_CLOCK_TARGET_NS_X(ptp->perout_event_ch), 0);
 510 
 511         /* Configure to pulse every period */
 512         general_config = lan743x_csr_read(adapter, PTP_GENERAL_CONFIG);
 513         general_config &= ~(PTP_GENERAL_CONFIG_CLOCK_EVENT_X_MASK_
 514                           (ptp->perout_event_ch));
 515         general_config |= PTP_GENERAL_CONFIG_CLOCK_EVENT_X_SET_
 516                           (ptp->perout_event_ch, pulse_width);
 517         general_config &= ~PTP_GENERAL_CONFIG_RELOAD_ADD_X_
 518                           (ptp->perout_event_ch);
 519         lan743x_csr_write(adapter, PTP_GENERAL_CONFIG, general_config);
 520 
 521         /* set the reload to one toggle cycle */
 522         lan743x_csr_write(adapter,
 523                           PTP_CLOCK_TARGET_RELOAD_SEC_X(ptp->perout_event_ch),
 524                           period_sec);
 525         lan743x_csr_write(adapter,
 526                           PTP_CLOCK_TARGET_RELOAD_NS_X(ptp->perout_event_ch),
 527                           period_nsec);
 528 
 529         /* set the start time */
 530         lan743x_csr_write(adapter,
 531                           PTP_CLOCK_TARGET_SEC_X(ptp->perout_event_ch),
 532                           start_sec);
 533         lan743x_csr_write(adapter,
 534                           PTP_CLOCK_TARGET_NS_X(ptp->perout_event_ch),
 535                           start_nsec);
 536 
 537         return 0;
 538 
 539 failed:
 540         lan743x_ptp_perout_off(adapter);
 541         return -ENODEV;
 542 }
 543 
 544 static int lan743x_ptpci_enable(struct ptp_clock_info *ptpci,
 545                                 struct ptp_clock_request *request, int on)
 546 {
 547         struct lan743x_ptp *ptp =
 548                 container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
 549         struct lan743x_adapter *adapter =
 550                 container_of(ptp, struct lan743x_adapter, ptp);
 551 
 552         if (request) {
 553                 switch (request->type) {
 554                 case PTP_CLK_REQ_EXTTS:
 555                         return -EINVAL;
 556                 case PTP_CLK_REQ_PEROUT:
 557                         if (request->perout.index == 0)
 558                                 return lan743x_ptp_perout(adapter, on,
 559                                                           &request->perout);
 560                         return -EINVAL;
 561                 case PTP_CLK_REQ_PPS:
 562                         return -EINVAL;
 563                 default:
 564                         netif_err(adapter, drv, adapter->netdev,
 565                                   "request->type == %d, Unknown\n",
 566                                   request->type);
 567                         break;
 568                 }
 569         } else {
 570                 netif_err(adapter, drv, adapter->netdev, "request == NULL\n");
 571         }
 572         return 0;
 573 }
 574 
 575 static long lan743x_ptpci_do_aux_work(struct ptp_clock_info *ptpci)
 576 {
 577         struct lan743x_ptp *ptp =
 578                 container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
 579         struct lan743x_adapter *adapter =
 580                 container_of(ptp, struct lan743x_adapter, ptp);
 581         u32 cap_info, cause, header, nsec, seconds;
 582         bool new_timestamp_available = false;
 583         int count = 0;
 584 
 585         while ((count < 100) &&
 586                (lan743x_csr_read(adapter, PTP_INT_STS) & PTP_INT_BIT_TX_TS_)) {
 587                 count++;
 588                 cap_info = lan743x_csr_read(adapter, PTP_CAP_INFO);
 589 
 590                 if (PTP_CAP_INFO_TX_TS_CNT_GET_(cap_info) > 0) {
 591                         seconds = lan743x_csr_read(adapter,
 592                                                    PTP_TX_EGRESS_SEC);
 593                         nsec = lan743x_csr_read(adapter, PTP_TX_EGRESS_NS);
 594                         cause = (nsec &
 595                                  PTP_TX_EGRESS_NS_CAPTURE_CAUSE_MASK_);
 596                         header = lan743x_csr_read(adapter,
 597                                                   PTP_TX_MSG_HEADER);
 598 
 599                         if (cause == PTP_TX_EGRESS_NS_CAPTURE_CAUSE_SW_) {
 600                                 nsec &= PTP_TX_EGRESS_NS_TS_NS_MASK_;
 601                                 lan743x_ptp_tx_ts_enqueue_ts(adapter,
 602                                                              seconds, nsec,
 603                                                              header);
 604                                 new_timestamp_available = true;
 605                         } else if (cause ==
 606                                 PTP_TX_EGRESS_NS_CAPTURE_CAUSE_AUTO_) {
 607                                 netif_err(adapter, drv, adapter->netdev,
 608                                           "Auto capture cause not supported\n");
 609                         } else {
 610                                 netif_warn(adapter, drv, adapter->netdev,
 611                                            "unknown tx timestamp capture cause\n");
 612                         }
 613                 } else {
 614                         netif_warn(adapter, drv, adapter->netdev,
 615                                    "TX TS INT but no TX TS CNT\n");
 616                 }
 617                 lan743x_csr_write(adapter, PTP_INT_STS, PTP_INT_BIT_TX_TS_);
 618         }
 619 
 620         if (new_timestamp_available)
 621                 lan743x_ptp_tx_ts_complete(adapter);
 622 
 623         lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
 624 
 625         return -1;
 626 }
 627 
 628 static void lan743x_ptp_clock_get(struct lan743x_adapter *adapter,
 629                                   u32 *seconds, u32 *nano_seconds,
 630                                   u32 *sub_nano_seconds)
 631 {
 632         struct lan743x_ptp *ptp = &adapter->ptp;
 633 
 634         mutex_lock(&ptp->command_lock);
 635 
 636         lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_);
 637         lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_READ_);
 638 
 639         if (seconds)
 640                 (*seconds) = lan743x_csr_read(adapter, PTP_CLOCK_SEC);
 641 
 642         if (nano_seconds)
 643                 (*nano_seconds) = lan743x_csr_read(adapter, PTP_CLOCK_NS);
 644 
 645         if (sub_nano_seconds)
 646                 (*sub_nano_seconds) =
 647                 lan743x_csr_read(adapter, PTP_CLOCK_SUBNS);
 648 
 649         mutex_unlock(&ptp->command_lock);
 650 }
 651 
 652 static void lan743x_ptp_clock_step(struct lan743x_adapter *adapter,
 653                                    s64 time_step_ns)
 654 {
 655         struct lan743x_ptp *ptp = &adapter->ptp;
 656         u32 nano_seconds_step = 0;
 657         u64 abs_time_step_ns = 0;
 658         u32 unsigned_seconds = 0;
 659         u32 nano_seconds = 0;
 660         u32 remainder = 0;
 661         s32 seconds = 0;
 662 
 663         if (time_step_ns >  15000000000LL) {
 664                 /* convert to clock set */
 665                 lan743x_ptp_clock_get(adapter, &unsigned_seconds,
 666                                       &nano_seconds, NULL);
 667                 unsigned_seconds += div_u64_rem(time_step_ns, 1000000000LL,
 668                                                 &remainder);
 669                 nano_seconds += remainder;
 670                 if (nano_seconds >= 1000000000) {
 671                         unsigned_seconds++;
 672                         nano_seconds -= 1000000000;
 673                 }
 674                 lan743x_ptp_clock_set(adapter, unsigned_seconds,
 675                                       nano_seconds, 0);
 676                 return;
 677         } else if (time_step_ns < -15000000000LL) {
 678                 /* convert to clock set */
 679                 time_step_ns = -time_step_ns;
 680 
 681                 lan743x_ptp_clock_get(adapter, &unsigned_seconds,
 682                                       &nano_seconds, NULL);
 683                 unsigned_seconds -= div_u64_rem(time_step_ns, 1000000000LL,
 684                                                 &remainder);
 685                 nano_seconds_step = remainder;
 686                 if (nano_seconds < nano_seconds_step) {
 687                         unsigned_seconds--;
 688                         nano_seconds += 1000000000;
 689                 }
 690                 nano_seconds -= nano_seconds_step;
 691                 lan743x_ptp_clock_set(adapter, unsigned_seconds,
 692                                       nano_seconds, 0);
 693                 return;
 694         }
 695 
 696         /* do clock step */
 697         if (time_step_ns >= 0) {
 698                 abs_time_step_ns = (u64)(time_step_ns);
 699                 seconds = (s32)div_u64_rem(abs_time_step_ns, 1000000000,
 700                                            &remainder);
 701                 nano_seconds = (u32)remainder;
 702         } else {
 703                 abs_time_step_ns = (u64)(-time_step_ns);
 704                 seconds = -((s32)div_u64_rem(abs_time_step_ns, 1000000000,
 705                                              &remainder));
 706                 nano_seconds = (u32)remainder;
 707                 if (nano_seconds > 0) {
 708                         /* subtracting nano seconds is not allowed
 709                          * convert to subtracting from seconds,
 710                          * and adding to nanoseconds
 711                          */
 712                         seconds--;
 713                         nano_seconds = (1000000000 - nano_seconds);
 714                 }
 715         }
 716 
 717         if (nano_seconds > 0) {
 718                 /* add 8 ns to cover the likely normal increment */
 719                 nano_seconds += 8;
 720         }
 721 
 722         if (nano_seconds >= 1000000000) {
 723                 /* carry into seconds */
 724                 seconds++;
 725                 nano_seconds -= 1000000000;
 726         }
 727 
 728         while (seconds) {
 729                 mutex_lock(&ptp->command_lock);
 730                 if (seconds > 0) {
 731                         u32 adjustment_value = (u32)seconds;
 732 
 733                         if (adjustment_value > 0xF)
 734                                 adjustment_value = 0xF;
 735                         lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
 736                                           PTP_CLOCK_STEP_ADJ_DIR_ |
 737                                           adjustment_value);
 738                         seconds -= ((s32)adjustment_value);
 739                 } else {
 740                         u32 adjustment_value = (u32)(-seconds);
 741 
 742                         if (adjustment_value > 0xF)
 743                                 adjustment_value = 0xF;
 744                         lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
 745                                           adjustment_value);
 746                         seconds += ((s32)adjustment_value);
 747                 }
 748                 lan743x_csr_write(adapter, PTP_CMD_CTL,
 749                                   PTP_CMD_CTL_PTP_CLOCK_STEP_SEC_);
 750                 lan743x_ptp_wait_till_cmd_done(adapter,
 751                                                PTP_CMD_CTL_PTP_CLOCK_STEP_SEC_);
 752                 mutex_unlock(&ptp->command_lock);
 753         }
 754         if (nano_seconds) {
 755                 mutex_lock(&ptp->command_lock);
 756                 lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
 757                                   PTP_CLOCK_STEP_ADJ_DIR_ |
 758                                   (nano_seconds &
 759                                   PTP_CLOCK_STEP_ADJ_VALUE_MASK_));
 760                 lan743x_csr_write(adapter, PTP_CMD_CTL,
 761                                   PTP_CMD_CTL_PTP_CLK_STP_NSEC_);
 762                 lan743x_ptp_wait_till_cmd_done(adapter,
 763                                                PTP_CMD_CTL_PTP_CLK_STP_NSEC_);
 764                 mutex_unlock(&ptp->command_lock);
 765         }
 766 }
 767 
 768 void lan743x_ptp_isr(void *context)
 769 {
 770         struct lan743x_adapter *adapter = (struct lan743x_adapter *)context;
 771         struct lan743x_ptp *ptp = NULL;
 772         int enable_flag = 1;
 773         u32 ptp_int_sts = 0;
 774 
 775         ptp = &adapter->ptp;
 776 
 777         lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_1588_);
 778 
 779         ptp_int_sts = lan743x_csr_read(adapter, PTP_INT_STS);
 780         ptp_int_sts &= lan743x_csr_read(adapter, PTP_INT_EN_SET);
 781 
 782         if (ptp_int_sts & PTP_INT_BIT_TX_TS_) {
 783                 ptp_schedule_worker(ptp->ptp_clock, 0);
 784                 enable_flag = 0;/* tasklet will re-enable later */
 785         }
 786         if (ptp_int_sts & PTP_INT_BIT_TX_SWTS_ERR_) {
 787                 netif_err(adapter, drv, adapter->netdev,
 788                           "PTP TX Software Timestamp Error\n");
 789                 /* clear int status bit */
 790                 lan743x_csr_write(adapter, PTP_INT_STS,
 791                                   PTP_INT_BIT_TX_SWTS_ERR_);
 792         }
 793         if (ptp_int_sts & PTP_INT_BIT_TIMER_B_) {
 794                 /* clear int status bit */
 795                 lan743x_csr_write(adapter, PTP_INT_STS,
 796                                   PTP_INT_BIT_TIMER_B_);
 797         }
 798         if (ptp_int_sts & PTP_INT_BIT_TIMER_A_) {
 799                 /* clear int status bit */
 800                 lan743x_csr_write(adapter, PTP_INT_STS,
 801                                   PTP_INT_BIT_TIMER_A_);
 802         }
 803 
 804         if (enable_flag) {
 805                 /* re-enable isr */
 806                 lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
 807         }
 808 }
 809 
 810 static void lan743x_ptp_tx_ts_enqueue_skb(struct lan743x_adapter *adapter,
 811                                           struct sk_buff *skb, bool ignore_sync)
 812 {
 813         struct lan743x_ptp *ptp = &adapter->ptp;
 814 
 815         spin_lock_bh(&ptp->tx_ts_lock);
 816         if (ptp->tx_ts_skb_queue_size < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
 817                 ptp->tx_ts_skb_queue[ptp->tx_ts_skb_queue_size] = skb;
 818                 if (ignore_sync)
 819                         ptp->tx_ts_ignore_sync_queue |=
 820                                 BIT(ptp->tx_ts_skb_queue_size);
 821                 ptp->tx_ts_skb_queue_size++;
 822         } else {
 823                 /* this should never happen, so long as the tx channel
 824                  * calls and honors the result from
 825                  * lan743x_ptp_request_tx_timestamp
 826                  */
 827                 netif_err(adapter, drv, adapter->netdev,
 828                           "tx ts skb queue overflow\n");
 829                 dev_kfree_skb(skb);
 830         }
 831         spin_unlock_bh(&ptp->tx_ts_lock);
 832 }
 833 
 834 static void lan743x_ptp_sync_to_system_clock(struct lan743x_adapter *adapter)
 835 {
 836         struct timespec64 ts;
 837 
 838         ktime_get_clocktai_ts64(&ts);
 839 
 840         lan743x_ptp_clock_set(adapter, ts.tv_sec, ts.tv_nsec, 0);
 841 }
 842 
 843 void lan743x_ptp_update_latency(struct lan743x_adapter *adapter,
 844                                 u32 link_speed)
 845 {
 846         switch (link_speed) {
 847         case 10:
 848                 lan743x_csr_write(adapter, PTP_LATENCY,
 849                                   PTP_LATENCY_TX_SET_(0) |
 850                                   PTP_LATENCY_RX_SET_(0));
 851                 break;
 852         case 100:
 853                 lan743x_csr_write(adapter, PTP_LATENCY,
 854                                   PTP_LATENCY_TX_SET_(181) |
 855                                   PTP_LATENCY_RX_SET_(594));
 856                 break;
 857         case 1000:
 858                 lan743x_csr_write(adapter, PTP_LATENCY,
 859                                   PTP_LATENCY_TX_SET_(30) |
 860                                   PTP_LATENCY_RX_SET_(525));
 861                 break;
 862         }
 863 }
 864 
 865 int lan743x_ptp_init(struct lan743x_adapter *adapter)
 866 {
 867         struct lan743x_ptp *ptp = &adapter->ptp;
 868 
 869         mutex_init(&ptp->command_lock);
 870         spin_lock_init(&ptp->tx_ts_lock);
 871         ptp->used_event_ch = 0;
 872         ptp->perout_event_ch = -1;
 873         ptp->perout_gpio_bit = -1;
 874         return 0;
 875 }
 876 
 877 int lan743x_ptp_open(struct lan743x_adapter *adapter)
 878 {
 879         struct lan743x_ptp *ptp = &adapter->ptp;
 880         int ret = -ENODEV;
 881         u32 temp;
 882 
 883         lan743x_ptp_reset(adapter);
 884         lan743x_ptp_sync_to_system_clock(adapter);
 885         temp = lan743x_csr_read(adapter, PTP_TX_MOD2);
 886         temp |= PTP_TX_MOD2_TX_PTP_CLR_UDPV4_CHKSUM_;
 887         lan743x_csr_write(adapter, PTP_TX_MOD2, temp);
 888         lan743x_ptp_enable(adapter);
 889         lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
 890         lan743x_csr_write(adapter, PTP_INT_EN_SET,
 891                           PTP_INT_BIT_TX_SWTS_ERR_ | PTP_INT_BIT_TX_TS_);
 892         ptp->flags |= PTP_FLAG_ISR_ENABLED;
 893 
 894         if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK))
 895                 return 0;
 896 
 897         snprintf(ptp->pin_config[0].name, 32, "lan743x_ptp_pin_0");
 898         ptp->pin_config[0].index = 0;
 899         ptp->pin_config[0].func = PTP_PF_PEROUT;
 900         ptp->pin_config[0].chan = 0;
 901 
 902         ptp->ptp_clock_info.owner = THIS_MODULE;
 903         snprintf(ptp->ptp_clock_info.name, 16, "%pm",
 904                  adapter->netdev->dev_addr);
 905         ptp->ptp_clock_info.max_adj = LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB;
 906         ptp->ptp_clock_info.n_alarm = 0;
 907         ptp->ptp_clock_info.n_ext_ts = 0;
 908         ptp->ptp_clock_info.n_per_out = 1;
 909         ptp->ptp_clock_info.n_pins = 0;
 910         ptp->ptp_clock_info.pps = 0;
 911         ptp->ptp_clock_info.pin_config = NULL;
 912         ptp->ptp_clock_info.adjfine = lan743x_ptpci_adjfine;
 913         ptp->ptp_clock_info.adjfreq = lan743x_ptpci_adjfreq;
 914         ptp->ptp_clock_info.adjtime = lan743x_ptpci_adjtime;
 915         ptp->ptp_clock_info.gettime64 = lan743x_ptpci_gettime64;
 916         ptp->ptp_clock_info.getcrosststamp = NULL;
 917         ptp->ptp_clock_info.settime64 = lan743x_ptpci_settime64;
 918         ptp->ptp_clock_info.enable = lan743x_ptpci_enable;
 919         ptp->ptp_clock_info.do_aux_work = lan743x_ptpci_do_aux_work;
 920         ptp->ptp_clock_info.verify = NULL;
 921 
 922         ptp->ptp_clock = ptp_clock_register(&ptp->ptp_clock_info,
 923                                             &adapter->pdev->dev);
 924 
 925         if (IS_ERR(ptp->ptp_clock)) {
 926                 netif_err(adapter, ifup, adapter->netdev,
 927                           "ptp_clock_register failed\n");
 928                 goto done;
 929         }
 930         ptp->flags |= PTP_FLAG_PTP_CLOCK_REGISTERED;
 931         netif_info(adapter, ifup, adapter->netdev,
 932                    "successfully registered ptp clock\n");
 933 
 934         return 0;
 935 done:
 936         lan743x_ptp_close(adapter);
 937         return ret;
 938 }
 939 
 940 void lan743x_ptp_close(struct lan743x_adapter *adapter)
 941 {
 942         struct lan743x_ptp *ptp = &adapter->ptp;
 943         int index;
 944 
 945         if (IS_ENABLED(CONFIG_PTP_1588_CLOCK) &&
 946             ptp->flags & PTP_FLAG_PTP_CLOCK_REGISTERED) {
 947                 ptp_clock_unregister(ptp->ptp_clock);
 948                 ptp->ptp_clock = NULL;
 949                 ptp->flags &= ~PTP_FLAG_PTP_CLOCK_REGISTERED;
 950                 netif_info(adapter, drv, adapter->netdev,
 951                            "ptp clock unregister\n");
 952         }
 953 
 954         if (ptp->flags & PTP_FLAG_ISR_ENABLED) {
 955                 lan743x_csr_write(adapter, PTP_INT_EN_CLR,
 956                                   PTP_INT_BIT_TX_SWTS_ERR_ |
 957                                   PTP_INT_BIT_TX_TS_);
 958                 lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_1588_);
 959                 ptp->flags &= ~PTP_FLAG_ISR_ENABLED;
 960         }
 961 
 962         /* clean up pending timestamp requests */
 963         lan743x_ptp_tx_ts_complete(adapter);
 964         spin_lock_bh(&ptp->tx_ts_lock);
 965         for (index = 0;
 966                 index < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS;
 967                 index++) {
 968                 struct sk_buff *skb = ptp->tx_ts_skb_queue[index];
 969 
 970                 dev_kfree_skb(skb);
 971                 ptp->tx_ts_skb_queue[index] = NULL;
 972                 ptp->tx_ts_seconds_queue[index] = 0;
 973                 ptp->tx_ts_nseconds_queue[index] = 0;
 974         }
 975         ptp->tx_ts_skb_queue_size = 0;
 976         ptp->tx_ts_queue_size = 0;
 977         ptp->pending_tx_timestamps = 0;
 978         spin_unlock_bh(&ptp->tx_ts_lock);
 979 
 980         lan743x_ptp_disable(adapter);
 981 }
 982 
 983 static void lan743x_ptp_set_sync_ts_insert(struct lan743x_adapter *adapter,
 984                                            bool ts_insert_enable)
 985 {
 986         u32 ptp_tx_mod = lan743x_csr_read(adapter, PTP_TX_MOD);
 987 
 988         if (ts_insert_enable)
 989                 ptp_tx_mod |= PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_;
 990         else
 991                 ptp_tx_mod &= ~PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_;
 992 
 993         lan743x_csr_write(adapter, PTP_TX_MOD, ptp_tx_mod);
 994 }
 995 
 996 static bool lan743x_ptp_is_enabled(struct lan743x_adapter *adapter)
 997 {
 998         if (lan743x_csr_read(adapter, PTP_CMD_CTL) & PTP_CMD_CTL_PTP_ENABLE_)
 999                 return true;
1000         return false;
1001 }
1002 
1003 static void lan743x_ptp_enable(struct lan743x_adapter *adapter)
1004 {
1005         struct lan743x_ptp *ptp = &adapter->ptp;
1006 
1007         mutex_lock(&ptp->command_lock);
1008 
1009         if (lan743x_ptp_is_enabled(adapter)) {
1010                 netif_warn(adapter, drv, adapter->netdev,
1011                            "PTP already enabled\n");
1012                 goto done;
1013         }
1014         lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_ENABLE_);
1015 done:
1016         mutex_unlock(&ptp->command_lock);
1017 }
1018 
1019 static void lan743x_ptp_disable(struct lan743x_adapter *adapter)
1020 {
1021         struct lan743x_ptp *ptp = &adapter->ptp;
1022 
1023         mutex_lock(&ptp->command_lock);
1024         if (!lan743x_ptp_is_enabled(adapter)) {
1025                 netif_warn(adapter, drv, adapter->netdev,
1026                            "PTP already disabled\n");
1027                 goto done;
1028         }
1029         lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_DISABLE_);
1030         lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_ENABLE_);
1031 done:
1032         mutex_unlock(&ptp->command_lock);
1033 }
1034 
1035 static void lan743x_ptp_reset(struct lan743x_adapter *adapter)
1036 {
1037         struct lan743x_ptp *ptp = &adapter->ptp;
1038 
1039         mutex_lock(&ptp->command_lock);
1040 
1041         if (lan743x_ptp_is_enabled(adapter)) {
1042                 netif_err(adapter, drv, adapter->netdev,
1043                           "Attempting reset while enabled\n");
1044                 goto done;
1045         }
1046 
1047         lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_RESET_);
1048         lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_RESET_);
1049 done:
1050         mutex_unlock(&ptp->command_lock);
1051 }
1052 
1053 static void lan743x_ptp_clock_set(struct lan743x_adapter *adapter,
1054                                   u32 seconds, u32 nano_seconds,
1055                                   u32 sub_nano_seconds)
1056 {
1057         struct lan743x_ptp *ptp = &adapter->ptp;
1058 
1059         mutex_lock(&ptp->command_lock);
1060 
1061         lan743x_csr_write(adapter, PTP_CLOCK_SEC, seconds);
1062         lan743x_csr_write(adapter, PTP_CLOCK_NS, nano_seconds);
1063         lan743x_csr_write(adapter, PTP_CLOCK_SUBNS, sub_nano_seconds);
1064 
1065         lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
1066         lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
1067         mutex_unlock(&ptp->command_lock);
1068 }
1069 
1070 bool lan743x_ptp_request_tx_timestamp(struct lan743x_adapter *adapter)
1071 {
1072         struct lan743x_ptp *ptp = &adapter->ptp;
1073         bool result = false;
1074 
1075         spin_lock_bh(&ptp->tx_ts_lock);
1076         if (ptp->pending_tx_timestamps < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
1077                 /* request granted */
1078                 ptp->pending_tx_timestamps++;
1079                 result = true;
1080         }
1081         spin_unlock_bh(&ptp->tx_ts_lock);
1082         return result;
1083 }
1084 
1085 void lan743x_ptp_unrequest_tx_timestamp(struct lan743x_adapter *adapter)
1086 {
1087         struct lan743x_ptp *ptp = &adapter->ptp;
1088 
1089         spin_lock_bh(&ptp->tx_ts_lock);
1090         if (ptp->pending_tx_timestamps > 0)
1091                 ptp->pending_tx_timestamps--;
1092         else
1093                 netif_err(adapter, drv, adapter->netdev,
1094                           "unrequest failed, pending_tx_timestamps==0\n");
1095         spin_unlock_bh(&ptp->tx_ts_lock);
1096 }
1097 
1098 void lan743x_ptp_tx_timestamp_skb(struct lan743x_adapter *adapter,
1099                                   struct sk_buff *skb, bool ignore_sync)
1100 {
1101         lan743x_ptp_tx_ts_enqueue_skb(adapter, skb, ignore_sync);
1102 
1103         lan743x_ptp_tx_ts_complete(adapter);
1104 }
1105 
1106 int lan743x_ptp_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1107 {
1108         struct lan743x_adapter *adapter = netdev_priv(netdev);
1109         struct hwtstamp_config config;
1110         int ret = 0;
1111         int index;
1112 
1113         if (!ifr) {
1114                 netif_err(adapter, drv, adapter->netdev,
1115                           "SIOCSHWTSTAMP, ifr == NULL\n");
1116                 return -EINVAL;
1117         }
1118 
1119         if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1120                 return -EFAULT;
1121 
1122         if (config.flags) {
1123                 netif_warn(adapter, drv, adapter->netdev,
1124                            "ignoring hwtstamp_config.flags == 0x%08X, expected 0\n",
1125                            config.flags);
1126         }
1127 
1128         switch (config.tx_type) {
1129         case HWTSTAMP_TX_OFF:
1130                 for (index = 0; index < LAN743X_MAX_TX_CHANNELS;
1131                         index++)
1132                         lan743x_tx_set_timestamping_mode(&adapter->tx[index],
1133                                                          false, false);
1134                 lan743x_ptp_set_sync_ts_insert(adapter, false);
1135                 break;
1136         case HWTSTAMP_TX_ON:
1137                 for (index = 0; index < LAN743X_MAX_TX_CHANNELS;
1138                         index++)
1139                         lan743x_tx_set_timestamping_mode(&adapter->tx[index],
1140                                                          true, false);
1141                 lan743x_ptp_set_sync_ts_insert(adapter, false);
1142                 break;
1143         case HWTSTAMP_TX_ONESTEP_SYNC:
1144                 for (index = 0; index < LAN743X_MAX_TX_CHANNELS;
1145                         index++)
1146                         lan743x_tx_set_timestamping_mode(&adapter->tx[index],
1147                                                          true, true);
1148 
1149                 lan743x_ptp_set_sync_ts_insert(adapter, true);
1150                 break;
1151         default:
1152                 netif_warn(adapter, drv, adapter->netdev,
1153                            "  tx_type = %d, UNKNOWN\n", config.tx_type);
1154                 ret = -EINVAL;
1155                 break;
1156         }
1157 
1158         if (!ret)
1159                 return copy_to_user(ifr->ifr_data, &config,
1160                         sizeof(config)) ? -EFAULT : 0;
1161         return ret;
1162 }

/* [<][>][^][v][top][bottom][index][help] */