Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0+ */
0002 /* Copyright (C) 2018 Microchip Technology Inc. */
0003 
0004 #include <linux/netdevice.h>
0005 
0006 #include <linux/ptp_clock_kernel.h>
0007 #include <linux/module.h>
0008 #include <linux/pci.h>
0009 #include <linux/net_tstamp.h>
0010 #include "lan743x_main.h"
0011 
0012 #include "lan743x_ptp.h"
0013 
0014 #define LAN743X_LED0_ENABLE     20  /* LED0 offset in HW_CFG */
0015 #define LAN743X_LED_ENABLE(pin)     BIT(LAN743X_LED0_ENABLE + (pin))
0016 
0017 #define LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB     (31249999)
0018 #define LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM  (2047999934)
0019 
0020 static bool lan743x_ptp_is_enabled(struct lan743x_adapter *adapter);
0021 static void lan743x_ptp_enable(struct lan743x_adapter *adapter);
0022 static void lan743x_ptp_disable(struct lan743x_adapter *adapter);
0023 static void lan743x_ptp_reset(struct lan743x_adapter *adapter);
0024 static void lan743x_ptp_clock_set(struct lan743x_adapter *adapter,
0025                   u32 seconds, u32 nano_seconds,
0026                   u32 sub_nano_seconds);
0027 
0028 static int lan743x_get_channel(u32 ch_map)
0029 {
0030     int idx;
0031 
0032     for (idx = 0; idx < 32; idx++) {
0033         if (ch_map & (0x1 << idx))
0034             return idx;
0035     }
0036 
0037     return -EINVAL;
0038 }
0039 
0040 int lan743x_gpio_init(struct lan743x_adapter *adapter)
0041 {
0042     struct lan743x_gpio *gpio = &adapter->gpio;
0043 
0044     spin_lock_init(&gpio->gpio_lock);
0045 
0046     gpio->gpio_cfg0 = 0; /* set all direction to input, data = 0 */
0047     gpio->gpio_cfg1 = 0x0FFF0000;/* disable all gpio, set to open drain */
0048     gpio->gpio_cfg2 = 0;/* set all to 1588 low polarity level */
0049     gpio->gpio_cfg3 = 0;/* disable all 1588 output */
0050     lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0);
0051     lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1);
0052     lan743x_csr_write(adapter, GPIO_CFG2, gpio->gpio_cfg2);
0053     lan743x_csr_write(adapter, GPIO_CFG3, gpio->gpio_cfg3);
0054 
0055     return 0;
0056 }
0057 
0058 static void lan743x_ptp_wait_till_cmd_done(struct lan743x_adapter *adapter,
0059                        u32 bit_mask)
0060 {
0061     int timeout = 1000;
0062     u32 data = 0;
0063 
0064     while (timeout &&
0065            (data = (lan743x_csr_read(adapter, PTP_CMD_CTL) &
0066            bit_mask))) {
0067         usleep_range(1000, 20000);
0068         timeout--;
0069     }
0070     if (data) {
0071         netif_err(adapter, drv, adapter->netdev,
0072               "timeout waiting for cmd to be done, cmd = 0x%08X\n",
0073               bit_mask);
0074     }
0075 }
0076 
0077 static void lan743x_ptp_tx_ts_enqueue_ts(struct lan743x_adapter *adapter,
0078                      u32 seconds, u32 nano_seconds,
0079                      u32 header)
0080 {
0081     struct lan743x_ptp *ptp = &adapter->ptp;
0082 
0083     spin_lock_bh(&ptp->tx_ts_lock);
0084     if (ptp->tx_ts_queue_size < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
0085         ptp->tx_ts_seconds_queue[ptp->tx_ts_queue_size] = seconds;
0086         ptp->tx_ts_nseconds_queue[ptp->tx_ts_queue_size] = nano_seconds;
0087         ptp->tx_ts_header_queue[ptp->tx_ts_queue_size] = header;
0088         ptp->tx_ts_queue_size++;
0089     } else {
0090         netif_err(adapter, drv, adapter->netdev,
0091               "tx ts queue overflow\n");
0092     }
0093     spin_unlock_bh(&ptp->tx_ts_lock);
0094 }
0095 
0096 static void lan743x_ptp_tx_ts_complete(struct lan743x_adapter *adapter)
0097 {
0098     struct lan743x_ptp *ptp = &adapter->ptp;
0099     struct skb_shared_hwtstamps tstamps;
0100     u32 header, nseconds, seconds;
0101     bool ignore_sync = false;
0102     struct sk_buff *skb;
0103     int c, i;
0104 
0105     spin_lock_bh(&ptp->tx_ts_lock);
0106     c = ptp->tx_ts_skb_queue_size;
0107 
0108     if (c > ptp->tx_ts_queue_size)
0109         c = ptp->tx_ts_queue_size;
0110     if (c <= 0)
0111         goto done;
0112 
0113     for (i = 0; i < c; i++) {
0114         ignore_sync = ((ptp->tx_ts_ignore_sync_queue &
0115                 BIT(i)) != 0);
0116         skb = ptp->tx_ts_skb_queue[i];
0117         nseconds = ptp->tx_ts_nseconds_queue[i];
0118         seconds = ptp->tx_ts_seconds_queue[i];
0119         header = ptp->tx_ts_header_queue[i];
0120 
0121         memset(&tstamps, 0, sizeof(tstamps));
0122         tstamps.hwtstamp = ktime_set(seconds, nseconds);
0123         if (!ignore_sync ||
0124             ((header & PTP_TX_MSG_HEADER_MSG_TYPE_) !=
0125             PTP_TX_MSG_HEADER_MSG_TYPE_SYNC_))
0126             skb_tstamp_tx(skb, &tstamps);
0127 
0128         dev_kfree_skb(skb);
0129 
0130         ptp->tx_ts_skb_queue[i] = NULL;
0131         ptp->tx_ts_seconds_queue[i] = 0;
0132         ptp->tx_ts_nseconds_queue[i] = 0;
0133         ptp->tx_ts_header_queue[i] = 0;
0134     }
0135 
0136     /* shift queue */
0137     ptp->tx_ts_ignore_sync_queue >>= c;
0138     for (i = c; i < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS; i++) {
0139         ptp->tx_ts_skb_queue[i - c] = ptp->tx_ts_skb_queue[i];
0140         ptp->tx_ts_seconds_queue[i - c] = ptp->tx_ts_seconds_queue[i];
0141         ptp->tx_ts_nseconds_queue[i - c] = ptp->tx_ts_nseconds_queue[i];
0142         ptp->tx_ts_header_queue[i - c] = ptp->tx_ts_header_queue[i];
0143 
0144         ptp->tx_ts_skb_queue[i] = NULL;
0145         ptp->tx_ts_seconds_queue[i] = 0;
0146         ptp->tx_ts_nseconds_queue[i] = 0;
0147         ptp->tx_ts_header_queue[i] = 0;
0148     }
0149     ptp->tx_ts_skb_queue_size -= c;
0150     ptp->tx_ts_queue_size -= c;
0151 done:
0152     ptp->pending_tx_timestamps -= c;
0153     spin_unlock_bh(&ptp->tx_ts_lock);
0154 }
0155 
0156 static int lan743x_ptp_reserve_event_ch(struct lan743x_adapter *adapter,
0157                     int event_channel)
0158 {
0159     struct lan743x_ptp *ptp = &adapter->ptp;
0160     int result = -ENODEV;
0161 
0162     mutex_lock(&ptp->command_lock);
0163     if (!(test_bit(event_channel, &ptp->used_event_ch))) {
0164         ptp->used_event_ch |= BIT(event_channel);
0165         result = event_channel;
0166     } else {
0167         netif_warn(adapter, drv, adapter->netdev,
0168                "attempted to reserved a used event_channel = %d\n",
0169                event_channel);
0170     }
0171     mutex_unlock(&ptp->command_lock);
0172     return result;
0173 }
0174 
0175 static void lan743x_ptp_release_event_ch(struct lan743x_adapter *adapter,
0176                      int event_channel)
0177 {
0178     struct lan743x_ptp *ptp = &adapter->ptp;
0179 
0180     mutex_lock(&ptp->command_lock);
0181     if (test_bit(event_channel, &ptp->used_event_ch)) {
0182         ptp->used_event_ch &= ~BIT(event_channel);
0183     } else {
0184         netif_warn(adapter, drv, adapter->netdev,
0185                "attempted release on a not used event_channel = %d\n",
0186                event_channel);
0187     }
0188     mutex_unlock(&ptp->command_lock);
0189 }
0190 
0191 static void lan743x_ptp_clock_get(struct lan743x_adapter *adapter,
0192                   u32 *seconds, u32 *nano_seconds,
0193                   u32 *sub_nano_seconds);
0194 static void lan743x_ptp_io_clock_get(struct lan743x_adapter *adapter,
0195                      u32 *sec, u32 *nsec, u32 *sub_nsec);
0196 static void lan743x_ptp_clock_step(struct lan743x_adapter *adapter,
0197                    s64 time_step_ns);
0198 
0199 static void lan743x_led_mux_enable(struct lan743x_adapter *adapter,
0200                    int pin, bool enable)
0201 {
0202     struct lan743x_ptp *ptp = &adapter->ptp;
0203 
0204     if (ptp->leds_multiplexed &&
0205         ptp->led_enabled[pin]) {
0206         u32 val = lan743x_csr_read(adapter, HW_CFG);
0207 
0208         if (enable)
0209             val |= LAN743X_LED_ENABLE(pin);
0210         else
0211             val &= ~LAN743X_LED_ENABLE(pin);
0212 
0213         lan743x_csr_write(adapter, HW_CFG, val);
0214     }
0215 }
0216 
0217 static void lan743x_led_mux_save(struct lan743x_adapter *adapter)
0218 {
0219     struct lan743x_ptp *ptp = &adapter->ptp;
0220     u32 id_rev = adapter->csr.id_rev & ID_REV_ID_MASK_;
0221 
0222     if (id_rev == ID_REV_ID_LAN7430_) {
0223         int i;
0224         u32 val = lan743x_csr_read(adapter, HW_CFG);
0225 
0226         for (i = 0; i < LAN7430_N_LED; i++) {
0227             bool led_enabled = (val & LAN743X_LED_ENABLE(i)) != 0;
0228 
0229             ptp->led_enabled[i] = led_enabled;
0230         }
0231         ptp->leds_multiplexed = true;
0232     } else {
0233         ptp->leds_multiplexed = false;
0234     }
0235 }
0236 
0237 static void lan743x_led_mux_restore(struct lan743x_adapter *adapter)
0238 {
0239     u32 id_rev = adapter->csr.id_rev & ID_REV_ID_MASK_;
0240 
0241     if (id_rev == ID_REV_ID_LAN7430_) {
0242         int i;
0243 
0244         for (i = 0; i < LAN7430_N_LED; i++)
0245             lan743x_led_mux_enable(adapter, i, true);
0246     }
0247 }
0248 
0249 static int lan743x_gpio_rsrv_ptp_out(struct lan743x_adapter *adapter,
0250                      int pin, int event_channel)
0251 {
0252     struct lan743x_gpio *gpio = &adapter->gpio;
0253     unsigned long irq_flags = 0;
0254     int bit_mask = BIT(pin);
0255     int ret = -EBUSY;
0256 
0257     spin_lock_irqsave(&gpio->gpio_lock, irq_flags);
0258 
0259     if (!(gpio->used_bits & bit_mask)) {
0260         gpio->used_bits |= bit_mask;
0261         gpio->output_bits |= bit_mask;
0262         gpio->ptp_bits |= bit_mask;
0263 
0264         /* assign pin to GPIO function */
0265         lan743x_led_mux_enable(adapter, pin, false);
0266 
0267         /* set as output, and zero initial value */
0268         gpio->gpio_cfg0 |= GPIO_CFG0_GPIO_DIR_BIT_(pin);
0269         gpio->gpio_cfg0 &= ~GPIO_CFG0_GPIO_DATA_BIT_(pin);
0270         lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0);
0271 
0272         /* enable gpio, and set buffer type to push pull */
0273         gpio->gpio_cfg1 &= ~GPIO_CFG1_GPIOEN_BIT_(pin);
0274         gpio->gpio_cfg1 |= GPIO_CFG1_GPIOBUF_BIT_(pin);
0275         lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1);
0276 
0277         /* set 1588 polarity to high */
0278         gpio->gpio_cfg2 |= GPIO_CFG2_1588_POL_BIT_(pin);
0279         lan743x_csr_write(adapter, GPIO_CFG2, gpio->gpio_cfg2);
0280 
0281         if (event_channel == 0) {
0282             /* use channel A */
0283             gpio->gpio_cfg3 &= ~GPIO_CFG3_1588_CH_SEL_BIT_(pin);
0284         } else {
0285             /* use channel B */
0286             gpio->gpio_cfg3 |= GPIO_CFG3_1588_CH_SEL_BIT_(pin);
0287         }
0288         gpio->gpio_cfg3 |= GPIO_CFG3_1588_OE_BIT_(pin);
0289         lan743x_csr_write(adapter, GPIO_CFG3, gpio->gpio_cfg3);
0290 
0291         ret = pin;
0292     }
0293     spin_unlock_irqrestore(&gpio->gpio_lock, irq_flags);
0294     return ret;
0295 }
0296 
0297 static void lan743x_gpio_release(struct lan743x_adapter *adapter, int pin)
0298 {
0299     struct lan743x_gpio *gpio = &adapter->gpio;
0300     unsigned long irq_flags = 0;
0301     int bit_mask = BIT(pin);
0302 
0303     spin_lock_irqsave(&gpio->gpio_lock, irq_flags);
0304     if (gpio->used_bits & bit_mask) {
0305         gpio->used_bits &= ~bit_mask;
0306         if (gpio->output_bits & bit_mask) {
0307             gpio->output_bits &= ~bit_mask;
0308 
0309             if (gpio->ptp_bits & bit_mask) {
0310                 gpio->ptp_bits &= ~bit_mask;
0311                 /* disable ptp output */
0312                 gpio->gpio_cfg3 &= ~GPIO_CFG3_1588_OE_BIT_(pin);
0313                 lan743x_csr_write(adapter, GPIO_CFG3,
0314                           gpio->gpio_cfg3);
0315             }
0316             /* release gpio output */
0317 
0318             /* disable gpio */
0319             gpio->gpio_cfg1 |= GPIO_CFG1_GPIOEN_BIT_(pin);
0320             gpio->gpio_cfg1 &= ~GPIO_CFG1_GPIOBUF_BIT_(pin);
0321             lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1);
0322 
0323             /* reset back to input */
0324             gpio->gpio_cfg0 &= ~GPIO_CFG0_GPIO_DIR_BIT_(pin);
0325             gpio->gpio_cfg0 &= ~GPIO_CFG0_GPIO_DATA_BIT_(pin);
0326             lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0);
0327 
0328             /* assign pin to original function */
0329             lan743x_led_mux_enable(adapter, pin, true);
0330         }
0331     }
0332     spin_unlock_irqrestore(&gpio->gpio_lock, irq_flags);
0333 }
0334 
0335 static int lan743x_ptpci_adjfine(struct ptp_clock_info *ptpci, long scaled_ppm)
0336 {
0337     struct lan743x_ptp *ptp =
0338         container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
0339     struct lan743x_adapter *adapter =
0340         container_of(ptp, struct lan743x_adapter, ptp);
0341     u32 lan743x_rate_adj = 0;
0342     bool positive = true;
0343     u64 u64_delta = 0;
0344 
0345     if ((scaled_ppm < (-LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM)) ||
0346         scaled_ppm > LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM) {
0347         return -EINVAL;
0348     }
0349     if (scaled_ppm > 0) {
0350         u64_delta = (u64)scaled_ppm;
0351         positive = true;
0352     } else {
0353         u64_delta = (u64)(-scaled_ppm);
0354         positive = false;
0355     }
0356     u64_delta = (u64_delta << 19);
0357     lan743x_rate_adj = div_u64(u64_delta, 1000000);
0358 
0359     if (positive)
0360         lan743x_rate_adj |= PTP_CLOCK_RATE_ADJ_DIR_;
0361 
0362     lan743x_csr_write(adapter, PTP_CLOCK_RATE_ADJ,
0363               lan743x_rate_adj);
0364 
0365     return 0;
0366 }
0367 
0368 static int lan743x_ptpci_adjfreq(struct ptp_clock_info *ptpci, s32 delta_ppb)
0369 {
0370     struct lan743x_ptp *ptp =
0371         container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
0372     struct lan743x_adapter *adapter =
0373         container_of(ptp, struct lan743x_adapter, ptp);
0374     u32 lan743x_rate_adj = 0;
0375     bool positive = true;
0376     u32 u32_delta = 0;
0377     u64 u64_delta = 0;
0378 
0379     if ((delta_ppb < (-LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB)) ||
0380         delta_ppb > LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB) {
0381         return -EINVAL;
0382     }
0383     if (delta_ppb > 0) {
0384         u32_delta = (u32)delta_ppb;
0385         positive = true;
0386     } else {
0387         u32_delta = (u32)(-delta_ppb);
0388         positive = false;
0389     }
0390     u64_delta = (((u64)u32_delta) << 35);
0391     lan743x_rate_adj = div_u64(u64_delta, 1000000000);
0392 
0393     if (positive)
0394         lan743x_rate_adj |= PTP_CLOCK_RATE_ADJ_DIR_;
0395 
0396     lan743x_csr_write(adapter, PTP_CLOCK_RATE_ADJ,
0397               lan743x_rate_adj);
0398 
0399     return 0;
0400 }
0401 
0402 static int lan743x_ptpci_adjtime(struct ptp_clock_info *ptpci, s64 delta)
0403 {
0404     struct lan743x_ptp *ptp =
0405         container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
0406     struct lan743x_adapter *adapter =
0407         container_of(ptp, struct lan743x_adapter, ptp);
0408 
0409     lan743x_ptp_clock_step(adapter, delta);
0410 
0411     return 0;
0412 }
0413 
0414 static int lan743x_ptpci_gettime64(struct ptp_clock_info *ptpci,
0415                    struct timespec64 *ts)
0416 {
0417     struct lan743x_ptp *ptp =
0418         container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
0419     struct lan743x_adapter *adapter =
0420         container_of(ptp, struct lan743x_adapter, ptp);
0421     u32 nano_seconds = 0;
0422     u32 seconds = 0;
0423 
0424     if (adapter->is_pci11x1x)
0425         lan743x_ptp_io_clock_get(adapter, &seconds, &nano_seconds,
0426                      NULL);
0427     else
0428         lan743x_ptp_clock_get(adapter, &seconds, &nano_seconds, NULL);
0429     ts->tv_sec = seconds;
0430     ts->tv_nsec = nano_seconds;
0431 
0432     return 0;
0433 }
0434 
0435 static int lan743x_ptpci_settime64(struct ptp_clock_info *ptpci,
0436                    const struct timespec64 *ts)
0437 {
0438     struct lan743x_ptp *ptp =
0439         container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
0440     struct lan743x_adapter *adapter =
0441         container_of(ptp, struct lan743x_adapter, ptp);
0442     u32 nano_seconds = 0;
0443     u32 seconds = 0;
0444 
0445     if (ts) {
0446         if (ts->tv_sec > 0xFFFFFFFFLL ||
0447             ts->tv_sec < 0) {
0448             netif_warn(adapter, drv, adapter->netdev,
0449                    "ts->tv_sec out of range, %lld\n",
0450                    ts->tv_sec);
0451             return -ERANGE;
0452         }
0453         if (ts->tv_nsec >= 1000000000L ||
0454             ts->tv_nsec < 0) {
0455             netif_warn(adapter, drv, adapter->netdev,
0456                    "ts->tv_nsec out of range, %ld\n",
0457                    ts->tv_nsec);
0458             return -ERANGE;
0459         }
0460         seconds = ts->tv_sec;
0461         nano_seconds = ts->tv_nsec;
0462         lan743x_ptp_clock_set(adapter, seconds, nano_seconds, 0);
0463     } else {
0464         netif_warn(adapter, drv, adapter->netdev, "ts == NULL\n");
0465         return -EINVAL;
0466     }
0467 
0468     return 0;
0469 }
0470 
0471 static void lan743x_ptp_perout_off(struct lan743x_adapter *adapter,
0472                    unsigned int index)
0473 {
0474     struct lan743x_ptp *ptp = &adapter->ptp;
0475     u32 general_config = 0;
0476     struct lan743x_ptp_perout *perout = &ptp->perout[index];
0477 
0478     if (perout->gpio_pin >= 0) {
0479         lan743x_gpio_release(adapter, perout->gpio_pin);
0480         perout->gpio_pin = -1;
0481     }
0482 
0483     if (perout->event_ch >= 0) {
0484         /* set target to far in the future, effectively disabling it */
0485         lan743x_csr_write(adapter,
0486                   PTP_CLOCK_TARGET_SEC_X(perout->event_ch),
0487                   0xFFFF0000);
0488         lan743x_csr_write(adapter,
0489                   PTP_CLOCK_TARGET_NS_X(perout->event_ch),
0490                   0);
0491 
0492         general_config = lan743x_csr_read(adapter, PTP_GENERAL_CONFIG);
0493         general_config |= PTP_GENERAL_CONFIG_RELOAD_ADD_X_
0494                   (perout->event_ch);
0495         lan743x_csr_write(adapter, PTP_GENERAL_CONFIG, general_config);
0496         lan743x_ptp_release_event_ch(adapter, perout->event_ch);
0497         perout->event_ch = -1;
0498     }
0499 }
0500 
0501 static int lan743x_ptp_perout(struct lan743x_adapter *adapter, int on,
0502                   struct ptp_perout_request *perout_request)
0503 {
0504     struct lan743x_ptp *ptp = &adapter->ptp;
0505     u32 period_sec = 0, period_nsec = 0;
0506     u32 start_sec = 0, start_nsec = 0;
0507     u32 general_config = 0;
0508     int pulse_width = 0;
0509     int perout_pin = 0;
0510     unsigned int index = perout_request->index;
0511     struct lan743x_ptp_perout *perout = &ptp->perout[index];
0512     int ret = 0;
0513 
0514     /* Reject requests with unsupported flags */
0515     if (perout_request->flags & ~PTP_PEROUT_DUTY_CYCLE)
0516         return -EOPNOTSUPP;
0517 
0518     if (on) {
0519         perout_pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_PEROUT,
0520                       perout_request->index);
0521         if (perout_pin < 0)
0522             return -EBUSY;
0523     } else {
0524         lan743x_ptp_perout_off(adapter, index);
0525         return 0;
0526     }
0527 
0528     if (perout->event_ch >= 0 ||
0529         perout->gpio_pin >= 0) {
0530         /* already on, turn off first */
0531         lan743x_ptp_perout_off(adapter, index);
0532     }
0533 
0534     perout->event_ch = lan743x_ptp_reserve_event_ch(adapter, index);
0535 
0536     if (perout->event_ch < 0) {
0537         netif_warn(adapter, drv, adapter->netdev,
0538                "Failed to reserve event channel %d for PEROUT\n",
0539                index);
0540         ret = -EBUSY;
0541         goto failed;
0542     }
0543 
0544     perout->gpio_pin = lan743x_gpio_rsrv_ptp_out(adapter,
0545                              perout_pin,
0546                              perout->event_ch);
0547 
0548     if (perout->gpio_pin < 0) {
0549         netif_warn(adapter, drv, adapter->netdev,
0550                "Failed to reserve gpio %d for PEROUT\n",
0551                perout_pin);
0552         ret = -EBUSY;
0553         goto failed;
0554     }
0555 
0556     start_sec = perout_request->start.sec;
0557     start_sec += perout_request->start.nsec / 1000000000;
0558     start_nsec = perout_request->start.nsec % 1000000000;
0559 
0560     period_sec = perout_request->period.sec;
0561     period_sec += perout_request->period.nsec / 1000000000;
0562     period_nsec = perout_request->period.nsec % 1000000000;
0563 
0564     if (perout_request->flags & PTP_PEROUT_DUTY_CYCLE) {
0565         struct timespec64 ts_on, ts_period;
0566         s64 wf_high, period64, half;
0567         s32 reminder;
0568 
0569         ts_on.tv_sec = perout_request->on.sec;
0570         ts_on.tv_nsec = perout_request->on.nsec;
0571         wf_high = timespec64_to_ns(&ts_on);
0572         ts_period.tv_sec = perout_request->period.sec;
0573         ts_period.tv_nsec = perout_request->period.nsec;
0574         period64 = timespec64_to_ns(&ts_period);
0575 
0576         if (period64 < 200) {
0577             netif_warn(adapter, drv, adapter->netdev,
0578                    "perout period too small, minimum is 200nS\n");
0579             ret = -EOPNOTSUPP;
0580             goto failed;
0581         }
0582         if (wf_high >= period64) {
0583             netif_warn(adapter, drv, adapter->netdev,
0584                    "pulse width must be smaller than period\n");
0585             ret = -EINVAL;
0586             goto failed;
0587         }
0588 
0589         /* Check if we can do 50% toggle on an even value of period.
0590          * If the period number is odd, then check if the requested
0591          * pulse width is the same as one of pre-defined width values.
0592          * Otherwise, return failure.
0593          */
0594         half = div_s64_rem(period64, 2, &reminder);
0595         if (!reminder) {
0596             if (half == wf_high) {
0597                 /* It's 50% match. Use the toggle option */
0598                 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_TOGGLE_;
0599                 /* In this case, devide period value by 2 */
0600                 ts_period = ns_to_timespec64(div_s64(period64, 2));
0601                 period_sec = ts_period.tv_sec;
0602                 period_nsec = ts_period.tv_nsec;
0603 
0604                 goto program;
0605             }
0606         }
0607         /* if we can't do toggle, then the width option needs to be the exact match */
0608         if (wf_high == 200000000) {
0609             pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
0610         } else if (wf_high == 10000000) {
0611             pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_;
0612         } else if (wf_high == 1000000) {
0613             pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_;
0614         } else if (wf_high == 100000) {
0615             pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_;
0616         } else if (wf_high == 10000) {
0617             pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_;
0618         } else if (wf_high == 100) {
0619             pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_;
0620         } else {
0621             netif_warn(adapter, drv, adapter->netdev,
0622                    "duty cycle specified is not supported\n");
0623             ret = -EOPNOTSUPP;
0624             goto failed;
0625         }
0626     } else {
0627         if (period_sec == 0) {
0628             if (period_nsec >= 400000000) {
0629                 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
0630             } else if (period_nsec >= 20000000) {
0631                 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_;
0632             } else if (period_nsec >= 2000000) {
0633                 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_;
0634             } else if (period_nsec >= 200000) {
0635                 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_;
0636             } else if (period_nsec >= 20000) {
0637                 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_;
0638             } else if (period_nsec >= 200) {
0639                 pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_;
0640             } else {
0641                 netif_warn(adapter, drv, adapter->netdev,
0642                        "perout period too small, minimum is 200nS\n");
0643                 ret = -EOPNOTSUPP;
0644                 goto failed;
0645             }
0646         } else {
0647             pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
0648         }
0649     }
0650 program:
0651 
0652     /* turn off by setting target far in future */
0653     lan743x_csr_write(adapter,
0654               PTP_CLOCK_TARGET_SEC_X(perout->event_ch),
0655               0xFFFF0000);
0656     lan743x_csr_write(adapter,
0657               PTP_CLOCK_TARGET_NS_X(perout->event_ch), 0);
0658 
0659     /* Configure to pulse every period */
0660     general_config = lan743x_csr_read(adapter, PTP_GENERAL_CONFIG);
0661     general_config &= ~(PTP_GENERAL_CONFIG_CLOCK_EVENT_X_MASK_
0662               (perout->event_ch));
0663     general_config |= PTP_GENERAL_CONFIG_CLOCK_EVENT_X_SET_
0664               (perout->event_ch, pulse_width);
0665     general_config &= ~PTP_GENERAL_CONFIG_RELOAD_ADD_X_
0666               (perout->event_ch);
0667     lan743x_csr_write(adapter, PTP_GENERAL_CONFIG, general_config);
0668 
0669     /* set the reload to one toggle cycle */
0670     lan743x_csr_write(adapter,
0671               PTP_CLOCK_TARGET_RELOAD_SEC_X(perout->event_ch),
0672               period_sec);
0673     lan743x_csr_write(adapter,
0674               PTP_CLOCK_TARGET_RELOAD_NS_X(perout->event_ch),
0675               period_nsec);
0676 
0677     /* set the start time */
0678     lan743x_csr_write(adapter,
0679               PTP_CLOCK_TARGET_SEC_X(perout->event_ch),
0680               start_sec);
0681     lan743x_csr_write(adapter,
0682               PTP_CLOCK_TARGET_NS_X(perout->event_ch),
0683               start_nsec);
0684 
0685     return 0;
0686 
0687 failed:
0688     lan743x_ptp_perout_off(adapter, index);
0689     return ret;
0690 }
0691 
0692 static void lan743x_ptp_io_perout_off(struct lan743x_adapter *adapter,
0693                       u32 index)
0694 {
0695     struct lan743x_ptp *ptp = &adapter->ptp;
0696     int perout_pin;
0697     int event_ch;
0698     u32 gen_cfg;
0699     int val;
0700 
0701     event_ch = ptp->ptp_io_perout[index];
0702     if (event_ch >= 0) {
0703         /* set target to far in the future, effectively disabling it */
0704         lan743x_csr_write(adapter,
0705                   PTP_CLOCK_TARGET_SEC_X(event_ch),
0706                   0xFFFF0000);
0707         lan743x_csr_write(adapter,
0708                   PTP_CLOCK_TARGET_NS_X(event_ch),
0709                   0);
0710 
0711         gen_cfg = lan743x_csr_read(adapter, HS_PTP_GENERAL_CONFIG);
0712         gen_cfg &= ~(HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_X_MASK_
0713                     (event_ch));
0714         gen_cfg &= ~(HS_PTP_GENERAL_CONFIG_EVENT_POL_X_(event_ch));
0715         gen_cfg |= HS_PTP_GENERAL_CONFIG_RELOAD_ADD_X_(event_ch);
0716         lan743x_csr_write(adapter, HS_PTP_GENERAL_CONFIG, gen_cfg);
0717         if (event_ch)
0718             lan743x_csr_write(adapter, PTP_INT_STS,
0719                       PTP_INT_TIMER_INT_B_);
0720         else
0721             lan743x_csr_write(adapter, PTP_INT_STS,
0722                       PTP_INT_TIMER_INT_A_);
0723         lan743x_ptp_release_event_ch(adapter, event_ch);
0724         ptp->ptp_io_perout[index] = -1;
0725     }
0726 
0727     perout_pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_PEROUT, index);
0728 
0729     /* Deselect Event output */
0730     val = lan743x_csr_read(adapter, PTP_IO_EVENT_OUTPUT_CFG);
0731 
0732     /* Disables the output of Local Time Target compare events */
0733     val &= ~PTP_IO_EVENT_OUTPUT_CFG_EN_(perout_pin);
0734     lan743x_csr_write(adapter, PTP_IO_EVENT_OUTPUT_CFG, val);
0735 
0736     /* Configured as an opendrain driver*/
0737     val = lan743x_csr_read(adapter, PTP_IO_PIN_CFG);
0738     val &= ~PTP_IO_PIN_CFG_OBUF_TYPE_(perout_pin);
0739     lan743x_csr_write(adapter, PTP_IO_PIN_CFG, val);
0740     /* Dummy read to make sure write operation success */
0741     val = lan743x_csr_read(adapter, PTP_IO_PIN_CFG);
0742 }
0743 
0744 static int lan743x_ptp_io_perout(struct lan743x_adapter *adapter, int on,
0745                  struct ptp_perout_request *perout_request)
0746 {
0747     struct lan743x_ptp *ptp = &adapter->ptp;
0748     u32 period_sec, period_nsec;
0749     u32 start_sec, start_nsec;
0750     u32 pulse_sec, pulse_nsec;
0751     int pulse_width;
0752     int perout_pin;
0753     int event_ch;
0754     u32 gen_cfg;
0755     u32 index;
0756     int val;
0757 
0758     index = perout_request->index;
0759     event_ch = ptp->ptp_io_perout[index];
0760 
0761     if (on) {
0762         perout_pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_PEROUT, index);
0763         if (perout_pin < 0)
0764             return -EBUSY;
0765     } else {
0766         lan743x_ptp_io_perout_off(adapter, index);
0767         return 0;
0768     }
0769 
0770     if (event_ch >= LAN743X_PTP_N_EVENT_CHAN) {
0771         /* already on, turn off first */
0772         lan743x_ptp_io_perout_off(adapter, index);
0773     }
0774 
0775     event_ch = lan743x_ptp_reserve_event_ch(adapter, index);
0776     if (event_ch < 0) {
0777         netif_warn(adapter, drv, adapter->netdev,
0778                "Failed to reserve event channel %d for PEROUT\n",
0779                index);
0780         goto failed;
0781     }
0782     ptp->ptp_io_perout[index] = event_ch;
0783 
0784     if (perout_request->flags & PTP_PEROUT_DUTY_CYCLE) {
0785         pulse_sec = perout_request->on.sec;
0786         pulse_sec += perout_request->on.nsec / 1000000000;
0787         pulse_nsec = perout_request->on.nsec % 1000000000;
0788     } else {
0789         pulse_sec = perout_request->period.sec;
0790         pulse_sec += perout_request->period.nsec / 1000000000;
0791         pulse_nsec = perout_request->period.nsec % 1000000000;
0792     }
0793 
0794     if (pulse_sec == 0) {
0795         if (pulse_nsec >= 400000000) {
0796             pulse_width = PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
0797         } else if (pulse_nsec >= 200000000) {
0798             pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_100MS_;
0799         } else if (pulse_nsec >= 100000000) {
0800             pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_50MS_;
0801         } else if (pulse_nsec >= 20000000) {
0802             pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_10MS_;
0803         } else if (pulse_nsec >= 10000000) {
0804             pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_5MS_;
0805         } else if (pulse_nsec >= 2000000) {
0806             pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_1MS_;
0807         } else if (pulse_nsec >= 1000000) {
0808             pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_500US_;
0809         } else if (pulse_nsec >= 200000) {
0810             pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_100US_;
0811         } else if (pulse_nsec >= 100000) {
0812             pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_50US_;
0813         } else if (pulse_nsec >= 20000) {
0814             pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_10US_;
0815         } else if (pulse_nsec >= 10000) {
0816             pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_5US_;
0817         } else if (pulse_nsec >= 2000) {
0818             pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_1US_;
0819         } else if (pulse_nsec >= 1000) {
0820             pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_500NS_;
0821         } else if (pulse_nsec >= 200) {
0822             pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_100NS_;
0823         } else {
0824             netif_warn(adapter, drv, adapter->netdev,
0825                    "perout period too small, min is 200nS\n");
0826             goto failed;
0827         }
0828     } else {
0829         pulse_width = HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_200MS_;
0830     }
0831 
0832     /* turn off by setting target far in future */
0833     lan743x_csr_write(adapter,
0834               PTP_CLOCK_TARGET_SEC_X(event_ch),
0835               0xFFFF0000);
0836     lan743x_csr_write(adapter,
0837               PTP_CLOCK_TARGET_NS_X(event_ch), 0);
0838 
0839     /* Configure to pulse every period */
0840     gen_cfg = lan743x_csr_read(adapter, HS_PTP_GENERAL_CONFIG);
0841     gen_cfg &= ~(HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_X_MASK_(event_ch));
0842     gen_cfg |= HS_PTP_GENERAL_CONFIG_CLOCK_EVENT_X_SET_
0843               (event_ch, pulse_width);
0844     gen_cfg |= HS_PTP_GENERAL_CONFIG_EVENT_POL_X_(event_ch);
0845     gen_cfg &= ~(HS_PTP_GENERAL_CONFIG_RELOAD_ADD_X_(event_ch));
0846     lan743x_csr_write(adapter, HS_PTP_GENERAL_CONFIG, gen_cfg);
0847 
0848     /* set the reload to one toggle cycle */
0849     period_sec = perout_request->period.sec;
0850     period_sec += perout_request->period.nsec / 1000000000;
0851     period_nsec = perout_request->period.nsec % 1000000000;
0852     lan743x_csr_write(adapter,
0853               PTP_CLOCK_TARGET_RELOAD_SEC_X(event_ch),
0854               period_sec);
0855     lan743x_csr_write(adapter,
0856               PTP_CLOCK_TARGET_RELOAD_NS_X(event_ch),
0857               period_nsec);
0858 
0859     start_sec = perout_request->start.sec;
0860     start_sec += perout_request->start.nsec / 1000000000;
0861     start_nsec = perout_request->start.nsec % 1000000000;
0862 
0863     /* set the start time */
0864     lan743x_csr_write(adapter,
0865               PTP_CLOCK_TARGET_SEC_X(event_ch),
0866               start_sec);
0867     lan743x_csr_write(adapter,
0868               PTP_CLOCK_TARGET_NS_X(event_ch),
0869               start_nsec);
0870 
0871     /* Enable LTC Target Read */
0872     val = lan743x_csr_read(adapter, PTP_CMD_CTL);
0873     val |= PTP_CMD_CTL_PTP_LTC_TARGET_READ_;
0874     lan743x_csr_write(adapter, PTP_CMD_CTL, val);
0875 
0876     /* Configure as an push/pull driver */
0877     val = lan743x_csr_read(adapter, PTP_IO_PIN_CFG);
0878     val |= PTP_IO_PIN_CFG_OBUF_TYPE_(perout_pin);
0879     lan743x_csr_write(adapter, PTP_IO_PIN_CFG, val);
0880 
0881     /* Select Event output */
0882     val = lan743x_csr_read(adapter, PTP_IO_EVENT_OUTPUT_CFG);
0883     if (event_ch)
0884         /* Channel B as the output */
0885         val |= PTP_IO_EVENT_OUTPUT_CFG_SEL_(perout_pin);
0886     else
0887         /* Channel A as the output */
0888         val &= ~PTP_IO_EVENT_OUTPUT_CFG_SEL_(perout_pin);
0889 
0890     /* Enables the output of Local Time Target compare events */
0891     val |= PTP_IO_EVENT_OUTPUT_CFG_EN_(perout_pin);
0892     lan743x_csr_write(adapter, PTP_IO_EVENT_OUTPUT_CFG, val);
0893 
0894     return 0;
0895 
0896 failed:
0897     lan743x_ptp_io_perout_off(adapter, index);
0898     return -ENODEV;
0899 }
0900 
0901 static void lan743x_ptp_io_extts_off(struct lan743x_adapter *adapter,
0902                      u32 index)
0903 {
0904     struct lan743x_ptp *ptp = &adapter->ptp;
0905     struct lan743x_extts *extts;
0906     int val;
0907 
0908     extts = &ptp->extts[index];
0909     /* PTP Interrupt Enable Clear Register */
0910     if (extts->flags & PTP_FALLING_EDGE)
0911         val = PTP_INT_EN_FE_EN_CLR_(index);
0912     else
0913         val = PTP_INT_EN_RE_EN_CLR_(index);
0914     lan743x_csr_write(adapter, PTP_INT_EN_CLR, val);
0915 
0916     /* Disables PTP-IO edge lock */
0917     val = lan743x_csr_read(adapter, PTP_IO_CAP_CONFIG);
0918     if (extts->flags & PTP_FALLING_EDGE) {
0919         val &= ~PTP_IO_CAP_CONFIG_LOCK_FE_(index);
0920         val &= ~PTP_IO_CAP_CONFIG_FE_CAP_EN_(index);
0921     } else {
0922         val &= ~PTP_IO_CAP_CONFIG_LOCK_RE_(index);
0923         val &= ~PTP_IO_CAP_CONFIG_RE_CAP_EN_(index);
0924     }
0925     lan743x_csr_write(adapter, PTP_IO_CAP_CONFIG, val);
0926 
0927     /* PTP-IO De-select register */
0928     val = lan743x_csr_read(adapter, PTP_IO_SEL);
0929     val &= ~PTP_IO_SEL_MASK_;
0930     lan743x_csr_write(adapter, PTP_IO_SEL, val);
0931 
0932     /* Clear timestamp */
0933     memset(&extts->ts, 0, sizeof(struct timespec64));
0934     extts->flags = 0;
0935 }
0936 
0937 static int lan743x_ptp_io_event_cap_en(struct lan743x_adapter *adapter,
0938                        u32 flags, u32 channel)
0939 {
0940     struct lan743x_ptp *ptp = &adapter->ptp;
0941     int val;
0942 
0943     if ((flags & PTP_EXTTS_EDGES) ==  PTP_EXTTS_EDGES)
0944         return -EOPNOTSUPP;
0945 
0946     mutex_lock(&ptp->command_lock);
0947     /* PTP-IO Event Capture Enable */
0948     val = lan743x_csr_read(adapter, PTP_IO_CAP_CONFIG);
0949     if (flags & PTP_FALLING_EDGE) {
0950         val &= ~PTP_IO_CAP_CONFIG_LOCK_RE_(channel);
0951         val &= ~PTP_IO_CAP_CONFIG_RE_CAP_EN_(channel);
0952         val |= PTP_IO_CAP_CONFIG_LOCK_FE_(channel);
0953         val |= PTP_IO_CAP_CONFIG_FE_CAP_EN_(channel);
0954     } else {
0955         /* Rising eventing as Default */
0956         val &= ~PTP_IO_CAP_CONFIG_LOCK_FE_(channel);
0957         val &= ~PTP_IO_CAP_CONFIG_FE_CAP_EN_(channel);
0958         val |= PTP_IO_CAP_CONFIG_LOCK_RE_(channel);
0959         val |= PTP_IO_CAP_CONFIG_RE_CAP_EN_(channel);
0960     }
0961     lan743x_csr_write(adapter, PTP_IO_CAP_CONFIG, val);
0962 
0963     /* PTP-IO Select */
0964     val = lan743x_csr_read(adapter, PTP_IO_SEL);
0965     val &= ~PTP_IO_SEL_MASK_;
0966     val |= channel << PTP_IO_SEL_SHIFT_;
0967     lan743x_csr_write(adapter, PTP_IO_SEL, val);
0968 
0969     /* PTP Interrupt Enable Register */
0970     if (flags & PTP_FALLING_EDGE)
0971         val = PTP_INT_EN_FE_EN_SET_(channel);
0972     else
0973         val = PTP_INT_EN_RE_EN_SET_(channel);
0974     lan743x_csr_write(adapter, PTP_INT_EN_SET, val);
0975 
0976     mutex_unlock(&ptp->command_lock);
0977 
0978     return 0;
0979 }
0980 
0981 static int lan743x_ptp_io_extts(struct lan743x_adapter *adapter, int on,
0982                 struct ptp_extts_request *extts_request)
0983 {
0984     struct lan743x_ptp *ptp = &adapter->ptp;
0985     u32 flags = extts_request->flags;
0986     u32 index = extts_request->index;
0987     struct lan743x_extts *extts;
0988     int extts_pin;
0989     int ret = 0;
0990 
0991     extts = &ptp->extts[index];
0992 
0993     if (on) {
0994         extts_pin = ptp_find_pin(ptp->ptp_clock, PTP_PF_EXTTS, index);
0995         if (extts_pin < 0)
0996             return -EBUSY;
0997 
0998         ret = lan743x_ptp_io_event_cap_en(adapter, flags, index);
0999         if (!ret)
1000             extts->flags = flags;
1001     } else {
1002         lan743x_ptp_io_extts_off(adapter, index);
1003     }
1004 
1005     return ret;
1006 }
1007 
1008 static int lan743x_ptpci_enable(struct ptp_clock_info *ptpci,
1009                 struct ptp_clock_request *request, int on)
1010 {
1011     struct lan743x_ptp *ptp =
1012         container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
1013     struct lan743x_adapter *adapter =
1014         container_of(ptp, struct lan743x_adapter, ptp);
1015 
1016     if (request) {
1017         switch (request->type) {
1018         case PTP_CLK_REQ_EXTTS:
1019             if (request->extts.index < ptpci->n_ext_ts)
1020                 return lan743x_ptp_io_extts(adapter, on,
1021                              &request->extts);
1022             return -EINVAL;
1023         case PTP_CLK_REQ_PEROUT:
1024             if (request->perout.index < ptpci->n_per_out) {
1025                 if (adapter->is_pci11x1x)
1026                     return lan743x_ptp_io_perout(adapter, on,
1027                                  &request->perout);
1028                 else
1029                     return lan743x_ptp_perout(adapter, on,
1030                               &request->perout);
1031             }
1032             return -EINVAL;
1033         case PTP_CLK_REQ_PPS:
1034             return -EINVAL;
1035         default:
1036             netif_err(adapter, drv, adapter->netdev,
1037                   "request->type == %d, Unknown\n",
1038                   request->type);
1039             break;
1040         }
1041     } else {
1042         netif_err(adapter, drv, adapter->netdev, "request == NULL\n");
1043     }
1044     return 0;
1045 }
1046 
1047 static int lan743x_ptpci_verify_pin_config(struct ptp_clock_info *ptp,
1048                        unsigned int pin,
1049                        enum ptp_pin_function func,
1050                        unsigned int chan)
1051 {
1052     int result = 0;
1053 
1054     /* Confirm the requested function is supported. Parameter
1055      * validation is done by the caller.
1056      */
1057     switch (func) {
1058     case PTP_PF_NONE:
1059     case PTP_PF_PEROUT:
1060     case PTP_PF_EXTTS:
1061         break;
1062     case PTP_PF_PHYSYNC:
1063     default:
1064         result = -1;
1065         break;
1066     }
1067     return result;
1068 }
1069 
1070 static void lan743x_ptp_io_event_clock_get(struct lan743x_adapter *adapter,
1071                        bool fe, u8 channel,
1072                        struct timespec64 *ts)
1073 {
1074     struct lan743x_ptp *ptp = &adapter->ptp;
1075     struct lan743x_extts *extts;
1076     u32 sec, nsec;
1077 
1078     mutex_lock(&ptp->command_lock);
1079     if (fe) {
1080         sec = lan743x_csr_read(adapter, PTP_IO_FE_LTC_SEC_CAP_X);
1081         nsec = lan743x_csr_read(adapter, PTP_IO_FE_LTC_NS_CAP_X);
1082     } else {
1083         sec = lan743x_csr_read(adapter, PTP_IO_RE_LTC_SEC_CAP_X);
1084         nsec = lan743x_csr_read(adapter, PTP_IO_RE_LTC_NS_CAP_X);
1085     }
1086 
1087     mutex_unlock(&ptp->command_lock);
1088 
1089     /* Update Local timestamp */
1090     extts = &ptp->extts[channel];
1091     extts->ts.tv_sec = sec;
1092     extts->ts.tv_nsec = nsec;
1093     ts->tv_sec = sec;
1094     ts->tv_nsec = nsec;
1095 }
1096 
1097 static long lan743x_ptpci_do_aux_work(struct ptp_clock_info *ptpci)
1098 {
1099     struct lan743x_ptp *ptp =
1100         container_of(ptpci, struct lan743x_ptp, ptp_clock_info);
1101     struct lan743x_adapter *adapter =
1102         container_of(ptp, struct lan743x_adapter, ptp);
1103     u32 cap_info, cause, header, nsec, seconds;
1104     bool new_timestamp_available = false;
1105     struct ptp_clock_event ptp_event;
1106     struct timespec64 ts;
1107     int ptp_int_sts;
1108     int count = 0;
1109     int channel;
1110     s64 ns;
1111 
1112     ptp_int_sts = lan743x_csr_read(adapter, PTP_INT_STS);
1113     while ((count < 100) && ptp_int_sts) {
1114         count++;
1115 
1116         if (ptp_int_sts & PTP_INT_BIT_TX_TS_) {
1117             cap_info = lan743x_csr_read(adapter, PTP_CAP_INFO);
1118 
1119             if (PTP_CAP_INFO_TX_TS_CNT_GET_(cap_info) > 0) {
1120                 seconds = lan743x_csr_read(adapter,
1121                                PTP_TX_EGRESS_SEC);
1122                 nsec = lan743x_csr_read(adapter,
1123                             PTP_TX_EGRESS_NS);
1124                 cause = (nsec &
1125                      PTP_TX_EGRESS_NS_CAPTURE_CAUSE_MASK_);
1126                 header = lan743x_csr_read(adapter,
1127                               PTP_TX_MSG_HEADER);
1128 
1129                 if (cause ==
1130                     PTP_TX_EGRESS_NS_CAPTURE_CAUSE_SW_) {
1131                     nsec &= PTP_TX_EGRESS_NS_TS_NS_MASK_;
1132                     lan743x_ptp_tx_ts_enqueue_ts(adapter,
1133                                      seconds,
1134                                      nsec,
1135                                      header);
1136                     new_timestamp_available = true;
1137                 } else if (cause ==
1138                        PTP_TX_EGRESS_NS_CAPTURE_CAUSE_AUTO_) {
1139                     netif_err(adapter, drv, adapter->netdev,
1140                           "Auto capture cause not supported\n");
1141                 } else {
1142                     netif_warn(adapter, drv, adapter->netdev,
1143                            "unknown tx timestamp capture cause\n");
1144                 }
1145             } else {
1146                 netif_warn(adapter, drv, adapter->netdev,
1147                        "TX TS INT but no TX TS CNT\n");
1148             }
1149             lan743x_csr_write(adapter, PTP_INT_STS,
1150                       PTP_INT_BIT_TX_TS_);
1151         }
1152 
1153         if (ptp_int_sts & PTP_INT_IO_FE_MASK_) {
1154             do {
1155                 channel = lan743x_get_channel((ptp_int_sts &
1156                             PTP_INT_IO_FE_MASK_) >>
1157                             PTP_INT_IO_FE_SHIFT_);
1158                 if (channel >= 0 &&
1159                     channel < PCI11X1X_PTP_IO_MAX_CHANNELS) {
1160                     lan743x_ptp_io_event_clock_get(adapter,
1161                                        true,
1162                                        channel,
1163                                        &ts);
1164                     /* PTP Falling Event post */
1165                     ns = timespec64_to_ns(&ts);
1166                     ptp_event.timestamp = ns;
1167                     ptp_event.index = channel;
1168                     ptp_event.type = PTP_CLOCK_EXTTS;
1169                     ptp_clock_event(ptp->ptp_clock,
1170                             &ptp_event);
1171                     lan743x_csr_write(adapter, PTP_INT_STS,
1172                               PTP_INT_IO_FE_SET_
1173                               (channel));
1174                     ptp_int_sts &= ~(1 <<
1175                              (PTP_INT_IO_FE_SHIFT_ +
1176                               channel));
1177                 } else {
1178                     /* Clear falling event interrupts */
1179                     lan743x_csr_write(adapter, PTP_INT_STS,
1180                               PTP_INT_IO_FE_MASK_);
1181                     ptp_int_sts &= ~PTP_INT_IO_FE_MASK_;
1182                 }
1183             } while (ptp_int_sts & PTP_INT_IO_FE_MASK_);
1184         }
1185 
1186         if (ptp_int_sts & PTP_INT_IO_RE_MASK_) {
1187             do {
1188                 channel = lan743x_get_channel((ptp_int_sts &
1189                                PTP_INT_IO_RE_MASK_) >>
1190                                PTP_INT_IO_RE_SHIFT_);
1191                 if (channel >= 0 &&
1192                     channel < PCI11X1X_PTP_IO_MAX_CHANNELS) {
1193                     lan743x_ptp_io_event_clock_get(adapter,
1194                                        false,
1195                                        channel,
1196                                        &ts);
1197                     /* PTP Rising Event post */
1198                     ns = timespec64_to_ns(&ts);
1199                     ptp_event.timestamp = ns;
1200                     ptp_event.index = channel;
1201                     ptp_event.type = PTP_CLOCK_EXTTS;
1202                     ptp_clock_event(ptp->ptp_clock,
1203                             &ptp_event);
1204                     lan743x_csr_write(adapter, PTP_INT_STS,
1205                               PTP_INT_IO_RE_SET_
1206                               (channel));
1207                     ptp_int_sts &= ~(1 <<
1208                              (PTP_INT_IO_RE_SHIFT_ +
1209                               channel));
1210                 } else {
1211                     /* Clear Rising event interrupt */
1212                     lan743x_csr_write(adapter, PTP_INT_STS,
1213                               PTP_INT_IO_RE_MASK_);
1214                     ptp_int_sts &= ~PTP_INT_IO_RE_MASK_;
1215                 }
1216             } while (ptp_int_sts & PTP_INT_IO_RE_MASK_);
1217         }
1218 
1219         ptp_int_sts = lan743x_csr_read(adapter, PTP_INT_STS);
1220     }
1221 
1222     if (new_timestamp_available)
1223         lan743x_ptp_tx_ts_complete(adapter);
1224 
1225     lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
1226 
1227     return -1;
1228 }
1229 
1230 static void lan743x_ptp_clock_get(struct lan743x_adapter *adapter,
1231                   u32 *seconds, u32 *nano_seconds,
1232                   u32 *sub_nano_seconds)
1233 {
1234     struct lan743x_ptp *ptp = &adapter->ptp;
1235 
1236     mutex_lock(&ptp->command_lock);
1237 
1238     lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_);
1239     lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_READ_);
1240 
1241     if (seconds)
1242         (*seconds) = lan743x_csr_read(adapter, PTP_CLOCK_SEC);
1243 
1244     if (nano_seconds)
1245         (*nano_seconds) = lan743x_csr_read(adapter, PTP_CLOCK_NS);
1246 
1247     if (sub_nano_seconds)
1248         (*sub_nano_seconds) =
1249         lan743x_csr_read(adapter, PTP_CLOCK_SUBNS);
1250 
1251     mutex_unlock(&ptp->command_lock);
1252 }
1253 
1254 static void lan743x_ptp_io_clock_get(struct lan743x_adapter *adapter,
1255                      u32 *sec, u32 *nsec, u32 *sub_nsec)
1256 {
1257     struct lan743x_ptp *ptp = &adapter->ptp;
1258 
1259     mutex_lock(&ptp->command_lock);
1260     lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_READ_);
1261     lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_READ_);
1262 
1263     if (sec)
1264         (*sec) = lan743x_csr_read(adapter, PTP_LTC_RD_SEC_LO);
1265 
1266     if (nsec)
1267         (*nsec) = lan743x_csr_read(adapter, PTP_LTC_RD_NS);
1268 
1269     if (sub_nsec)
1270         (*sub_nsec) =
1271         lan743x_csr_read(adapter, PTP_LTC_RD_SUBNS);
1272 
1273     mutex_unlock(&ptp->command_lock);
1274 }
1275 
1276 static void lan743x_ptp_clock_step(struct lan743x_adapter *adapter,
1277                    s64 time_step_ns)
1278 {
1279     struct lan743x_ptp *ptp = &adapter->ptp;
1280     u32 nano_seconds_step = 0;
1281     u64 abs_time_step_ns = 0;
1282     u32 unsigned_seconds = 0;
1283     u32 nano_seconds = 0;
1284     u32 remainder = 0;
1285     s32 seconds = 0;
1286 
1287     if (time_step_ns >  15000000000LL) {
1288         /* convert to clock set */
1289         if (adapter->is_pci11x1x)
1290             lan743x_ptp_io_clock_get(adapter, &unsigned_seconds,
1291                          &nano_seconds, NULL);
1292         else
1293             lan743x_ptp_clock_get(adapter, &unsigned_seconds,
1294                           &nano_seconds, NULL);
1295         unsigned_seconds += div_u64_rem(time_step_ns, 1000000000LL,
1296                         &remainder);
1297         nano_seconds += remainder;
1298         if (nano_seconds >= 1000000000) {
1299             unsigned_seconds++;
1300             nano_seconds -= 1000000000;
1301         }
1302         lan743x_ptp_clock_set(adapter, unsigned_seconds,
1303                       nano_seconds, 0);
1304         return;
1305     } else if (time_step_ns < -15000000000LL) {
1306         /* convert to clock set */
1307         time_step_ns = -time_step_ns;
1308 
1309         if (adapter->is_pci11x1x) {
1310             lan743x_ptp_io_clock_get(adapter, &unsigned_seconds,
1311                          &nano_seconds, NULL);
1312         } else {
1313             lan743x_ptp_clock_get(adapter, &unsigned_seconds,
1314                           &nano_seconds, NULL);
1315         }
1316         unsigned_seconds -= div_u64_rem(time_step_ns, 1000000000LL,
1317                         &remainder);
1318         nano_seconds_step = remainder;
1319         if (nano_seconds < nano_seconds_step) {
1320             unsigned_seconds--;
1321             nano_seconds += 1000000000;
1322         }
1323         nano_seconds -= nano_seconds_step;
1324         lan743x_ptp_clock_set(adapter, unsigned_seconds,
1325                       nano_seconds, 0);
1326         return;
1327     }
1328 
1329     /* do clock step */
1330     if (time_step_ns >= 0) {
1331         abs_time_step_ns = (u64)(time_step_ns);
1332         seconds = (s32)div_u64_rem(abs_time_step_ns, 1000000000,
1333                        &remainder);
1334         nano_seconds = (u32)remainder;
1335     } else {
1336         abs_time_step_ns = (u64)(-time_step_ns);
1337         seconds = -((s32)div_u64_rem(abs_time_step_ns, 1000000000,
1338                          &remainder));
1339         nano_seconds = (u32)remainder;
1340         if (nano_seconds > 0) {
1341             /* subtracting nano seconds is not allowed
1342              * convert to subtracting from seconds,
1343              * and adding to nanoseconds
1344              */
1345             seconds--;
1346             nano_seconds = (1000000000 - nano_seconds);
1347         }
1348     }
1349 
1350     if (nano_seconds > 0) {
1351         /* add 8 ns to cover the likely normal increment */
1352         nano_seconds += 8;
1353     }
1354 
1355     if (nano_seconds >= 1000000000) {
1356         /* carry into seconds */
1357         seconds++;
1358         nano_seconds -= 1000000000;
1359     }
1360 
1361     while (seconds) {
1362         mutex_lock(&ptp->command_lock);
1363         if (seconds > 0) {
1364             u32 adjustment_value = (u32)seconds;
1365 
1366             if (adjustment_value > 0xF)
1367                 adjustment_value = 0xF;
1368             lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
1369                       PTP_CLOCK_STEP_ADJ_DIR_ |
1370                       adjustment_value);
1371             seconds -= ((s32)adjustment_value);
1372         } else {
1373             u32 adjustment_value = (u32)(-seconds);
1374 
1375             if (adjustment_value > 0xF)
1376                 adjustment_value = 0xF;
1377             lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
1378                       adjustment_value);
1379             seconds += ((s32)adjustment_value);
1380         }
1381         lan743x_csr_write(adapter, PTP_CMD_CTL,
1382                   PTP_CMD_CTL_PTP_CLOCK_STEP_SEC_);
1383         lan743x_ptp_wait_till_cmd_done(adapter,
1384                            PTP_CMD_CTL_PTP_CLOCK_STEP_SEC_);
1385         mutex_unlock(&ptp->command_lock);
1386     }
1387     if (nano_seconds) {
1388         mutex_lock(&ptp->command_lock);
1389         lan743x_csr_write(adapter, PTP_CLOCK_STEP_ADJ,
1390                   PTP_CLOCK_STEP_ADJ_DIR_ |
1391                   (nano_seconds &
1392                   PTP_CLOCK_STEP_ADJ_VALUE_MASK_));
1393         lan743x_csr_write(adapter, PTP_CMD_CTL,
1394                   PTP_CMD_CTL_PTP_CLK_STP_NSEC_);
1395         lan743x_ptp_wait_till_cmd_done(adapter,
1396                            PTP_CMD_CTL_PTP_CLK_STP_NSEC_);
1397         mutex_unlock(&ptp->command_lock);
1398     }
1399 }
1400 
1401 void lan743x_ptp_isr(void *context)
1402 {
1403     struct lan743x_adapter *adapter = (struct lan743x_adapter *)context;
1404     struct lan743x_ptp *ptp = NULL;
1405     int enable_flag = 1;
1406     u32 ptp_int_sts = 0;
1407 
1408     ptp = &adapter->ptp;
1409 
1410     lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_1588_);
1411 
1412     ptp_int_sts = lan743x_csr_read(adapter, PTP_INT_STS);
1413     ptp_int_sts &= lan743x_csr_read(adapter, PTP_INT_EN_SET);
1414 
1415     if (ptp_int_sts & PTP_INT_BIT_TX_TS_) {
1416         ptp_schedule_worker(ptp->ptp_clock, 0);
1417         enable_flag = 0;/* tasklet will re-enable later */
1418     }
1419     if (ptp_int_sts & PTP_INT_BIT_TX_SWTS_ERR_) {
1420         netif_err(adapter, drv, adapter->netdev,
1421               "PTP TX Software Timestamp Error\n");
1422         /* clear int status bit */
1423         lan743x_csr_write(adapter, PTP_INT_STS,
1424                   PTP_INT_BIT_TX_SWTS_ERR_);
1425     }
1426     if (ptp_int_sts & PTP_INT_BIT_TIMER_B_) {
1427         /* clear int status bit */
1428         lan743x_csr_write(adapter, PTP_INT_STS,
1429                   PTP_INT_BIT_TIMER_B_);
1430     }
1431     if (ptp_int_sts & PTP_INT_BIT_TIMER_A_) {
1432         /* clear int status bit */
1433         lan743x_csr_write(adapter, PTP_INT_STS,
1434                   PTP_INT_BIT_TIMER_A_);
1435     }
1436 
1437     if (enable_flag) {
1438         /* re-enable isr */
1439         lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
1440     }
1441 }
1442 
1443 static void lan743x_ptp_tx_ts_enqueue_skb(struct lan743x_adapter *adapter,
1444                       struct sk_buff *skb, bool ignore_sync)
1445 {
1446     struct lan743x_ptp *ptp = &adapter->ptp;
1447 
1448     spin_lock_bh(&ptp->tx_ts_lock);
1449     if (ptp->tx_ts_skb_queue_size < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
1450         ptp->tx_ts_skb_queue[ptp->tx_ts_skb_queue_size] = skb;
1451         if (ignore_sync)
1452             ptp->tx_ts_ignore_sync_queue |=
1453                 BIT(ptp->tx_ts_skb_queue_size);
1454         ptp->tx_ts_skb_queue_size++;
1455     } else {
1456         /* this should never happen, so long as the tx channel
1457          * calls and honors the result from
1458          * lan743x_ptp_request_tx_timestamp
1459          */
1460         netif_err(adapter, drv, adapter->netdev,
1461               "tx ts skb queue overflow\n");
1462         dev_kfree_skb(skb);
1463     }
1464     spin_unlock_bh(&ptp->tx_ts_lock);
1465 }
1466 
1467 static void lan743x_ptp_sync_to_system_clock(struct lan743x_adapter *adapter)
1468 {
1469     struct timespec64 ts;
1470 
1471     ktime_get_clocktai_ts64(&ts);
1472 
1473     lan743x_ptp_clock_set(adapter, ts.tv_sec, ts.tv_nsec, 0);
1474 }
1475 
1476 void lan743x_ptp_update_latency(struct lan743x_adapter *adapter,
1477                 u32 link_speed)
1478 {
1479     switch (link_speed) {
1480     case 10:
1481         lan743x_csr_write(adapter, PTP_LATENCY,
1482                   PTP_LATENCY_TX_SET_(0) |
1483                   PTP_LATENCY_RX_SET_(0));
1484         break;
1485     case 100:
1486         lan743x_csr_write(adapter, PTP_LATENCY,
1487                   PTP_LATENCY_TX_SET_(181) |
1488                   PTP_LATENCY_RX_SET_(594));
1489         break;
1490     case 1000:
1491         lan743x_csr_write(adapter, PTP_LATENCY,
1492                   PTP_LATENCY_TX_SET_(30) |
1493                   PTP_LATENCY_RX_SET_(525));
1494         break;
1495     }
1496 }
1497 
1498 int lan743x_ptp_init(struct lan743x_adapter *adapter)
1499 {
1500     struct lan743x_ptp *ptp = &adapter->ptp;
1501     int i;
1502 
1503     mutex_init(&ptp->command_lock);
1504     spin_lock_init(&ptp->tx_ts_lock);
1505     ptp->used_event_ch = 0;
1506 
1507     for (i = 0; i < LAN743X_PTP_N_EVENT_CHAN; i++) {
1508         ptp->perout[i].event_ch = -1;
1509         ptp->perout[i].gpio_pin = -1;
1510     }
1511 
1512     lan743x_led_mux_save(adapter);
1513 
1514     return 0;
1515 }
1516 
1517 int lan743x_ptp_open(struct lan743x_adapter *adapter)
1518 {
1519     struct lan743x_ptp *ptp = &adapter->ptp;
1520     int ret = -ENODEV;
1521     u32 temp;
1522     int i;
1523     int n_pins;
1524 
1525     lan743x_ptp_reset(adapter);
1526     lan743x_ptp_sync_to_system_clock(adapter);
1527     temp = lan743x_csr_read(adapter, PTP_TX_MOD2);
1528     temp |= PTP_TX_MOD2_TX_PTP_CLR_UDPV4_CHKSUM_;
1529     lan743x_csr_write(adapter, PTP_TX_MOD2, temp);
1530     lan743x_ptp_enable(adapter);
1531     lan743x_csr_write(adapter, INT_EN_SET, INT_BIT_1588_);
1532     lan743x_csr_write(adapter, PTP_INT_EN_SET,
1533               PTP_INT_BIT_TX_SWTS_ERR_ | PTP_INT_BIT_TX_TS_);
1534     ptp->flags |= PTP_FLAG_ISR_ENABLED;
1535 
1536     if (!IS_ENABLED(CONFIG_PTP_1588_CLOCK))
1537         return 0;
1538 
1539     switch (adapter->csr.id_rev & ID_REV_ID_MASK_) {
1540     case ID_REV_ID_LAN7430_:
1541         n_pins = LAN7430_N_GPIO;
1542         break;
1543     case ID_REV_ID_LAN7431_:
1544     case ID_REV_ID_A011_:
1545     case ID_REV_ID_A041_:
1546         n_pins = LAN7431_N_GPIO;
1547         break;
1548     default:
1549         netif_warn(adapter, drv, adapter->netdev,
1550                "Unknown LAN743x (%08x). Assuming no GPIO\n",
1551                adapter->csr.id_rev);
1552         n_pins = 0;
1553         break;
1554     }
1555 
1556     if (n_pins > LAN743X_PTP_N_GPIO)
1557         n_pins = LAN743X_PTP_N_GPIO;
1558 
1559     for (i = 0; i < n_pins; i++) {
1560         struct ptp_pin_desc *ptp_pin = &ptp->pin_config[i];
1561 
1562         snprintf(ptp_pin->name,
1563              sizeof(ptp_pin->name), "lan743x_ptp_pin_%02d", i);
1564         ptp_pin->index = i;
1565         ptp_pin->func = PTP_PF_NONE;
1566     }
1567 
1568     ptp->ptp_clock_info.owner = THIS_MODULE;
1569     snprintf(ptp->ptp_clock_info.name, 16, "%pm",
1570          adapter->netdev->dev_addr);
1571     ptp->ptp_clock_info.max_adj = LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB;
1572     ptp->ptp_clock_info.n_alarm = 0;
1573     ptp->ptp_clock_info.n_ext_ts = LAN743X_PTP_N_EXTTS;
1574     ptp->ptp_clock_info.n_per_out = LAN743X_PTP_N_EVENT_CHAN;
1575     ptp->ptp_clock_info.n_pins = n_pins;
1576     ptp->ptp_clock_info.pps = LAN743X_PTP_N_PPS;
1577     ptp->ptp_clock_info.pin_config = ptp->pin_config;
1578     ptp->ptp_clock_info.adjfine = lan743x_ptpci_adjfine;
1579     ptp->ptp_clock_info.adjfreq = lan743x_ptpci_adjfreq;
1580     ptp->ptp_clock_info.adjtime = lan743x_ptpci_adjtime;
1581     ptp->ptp_clock_info.gettime64 = lan743x_ptpci_gettime64;
1582     ptp->ptp_clock_info.getcrosststamp = NULL;
1583     ptp->ptp_clock_info.settime64 = lan743x_ptpci_settime64;
1584     ptp->ptp_clock_info.enable = lan743x_ptpci_enable;
1585     ptp->ptp_clock_info.do_aux_work = lan743x_ptpci_do_aux_work;
1586     ptp->ptp_clock_info.verify = lan743x_ptpci_verify_pin_config;
1587 
1588     ptp->ptp_clock = ptp_clock_register(&ptp->ptp_clock_info,
1589                         &adapter->pdev->dev);
1590 
1591     if (IS_ERR(ptp->ptp_clock)) {
1592         netif_err(adapter, ifup, adapter->netdev,
1593               "ptp_clock_register failed\n");
1594         goto done;
1595     }
1596     ptp->flags |= PTP_FLAG_PTP_CLOCK_REGISTERED;
1597     netif_info(adapter, ifup, adapter->netdev,
1598            "successfully registered ptp clock\n");
1599 
1600     return 0;
1601 done:
1602     lan743x_ptp_close(adapter);
1603     return ret;
1604 }
1605 
1606 void lan743x_ptp_close(struct lan743x_adapter *adapter)
1607 {
1608     struct lan743x_ptp *ptp = &adapter->ptp;
1609     int index;
1610 
1611     if (IS_ENABLED(CONFIG_PTP_1588_CLOCK) &&
1612         (ptp->flags & PTP_FLAG_PTP_CLOCK_REGISTERED)) {
1613         ptp_clock_unregister(ptp->ptp_clock);
1614         ptp->ptp_clock = NULL;
1615         ptp->flags &= ~PTP_FLAG_PTP_CLOCK_REGISTERED;
1616         netif_info(adapter, drv, adapter->netdev,
1617                "ptp clock unregister\n");
1618     }
1619 
1620     if (ptp->flags & PTP_FLAG_ISR_ENABLED) {
1621         lan743x_csr_write(adapter, PTP_INT_EN_CLR,
1622                   PTP_INT_BIT_TX_SWTS_ERR_ |
1623                   PTP_INT_BIT_TX_TS_);
1624         lan743x_csr_write(adapter, INT_EN_CLR, INT_BIT_1588_);
1625         ptp->flags &= ~PTP_FLAG_ISR_ENABLED;
1626     }
1627 
1628     /* clean up pending timestamp requests */
1629     lan743x_ptp_tx_ts_complete(adapter);
1630     spin_lock_bh(&ptp->tx_ts_lock);
1631     for (index = 0;
1632         index < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS;
1633         index++) {
1634         struct sk_buff *skb = ptp->tx_ts_skb_queue[index];
1635 
1636         dev_kfree_skb(skb);
1637         ptp->tx_ts_skb_queue[index] = NULL;
1638         ptp->tx_ts_seconds_queue[index] = 0;
1639         ptp->tx_ts_nseconds_queue[index] = 0;
1640     }
1641     ptp->tx_ts_skb_queue_size = 0;
1642     ptp->tx_ts_queue_size = 0;
1643     ptp->pending_tx_timestamps = 0;
1644     spin_unlock_bh(&ptp->tx_ts_lock);
1645 
1646     lan743x_led_mux_restore(adapter);
1647 
1648     lan743x_ptp_disable(adapter);
1649 }
1650 
1651 static void lan743x_ptp_set_sync_ts_insert(struct lan743x_adapter *adapter,
1652                        bool ts_insert_enable)
1653 {
1654     u32 ptp_tx_mod = lan743x_csr_read(adapter, PTP_TX_MOD);
1655 
1656     if (ts_insert_enable)
1657         ptp_tx_mod |= PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_;
1658     else
1659         ptp_tx_mod &= ~PTP_TX_MOD_TX_PTP_SYNC_TS_INSERT_;
1660 
1661     lan743x_csr_write(adapter, PTP_TX_MOD, ptp_tx_mod);
1662 }
1663 
1664 static bool lan743x_ptp_is_enabled(struct lan743x_adapter *adapter)
1665 {
1666     if (lan743x_csr_read(adapter, PTP_CMD_CTL) & PTP_CMD_CTL_PTP_ENABLE_)
1667         return true;
1668     return false;
1669 }
1670 
1671 static void lan743x_ptp_enable(struct lan743x_adapter *adapter)
1672 {
1673     struct lan743x_ptp *ptp = &adapter->ptp;
1674 
1675     mutex_lock(&ptp->command_lock);
1676 
1677     if (lan743x_ptp_is_enabled(adapter)) {
1678         netif_warn(adapter, drv, adapter->netdev,
1679                "PTP already enabled\n");
1680         goto done;
1681     }
1682     lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_ENABLE_);
1683 done:
1684     mutex_unlock(&ptp->command_lock);
1685 }
1686 
1687 static void lan743x_ptp_disable(struct lan743x_adapter *adapter)
1688 {
1689     struct lan743x_ptp *ptp = &adapter->ptp;
1690 
1691     mutex_lock(&ptp->command_lock);
1692     if (!lan743x_ptp_is_enabled(adapter)) {
1693         netif_warn(adapter, drv, adapter->netdev,
1694                "PTP already disabled\n");
1695         goto done;
1696     }
1697     lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_DISABLE_);
1698     lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_ENABLE_);
1699 done:
1700     mutex_unlock(&ptp->command_lock);
1701 }
1702 
1703 static void lan743x_ptp_reset(struct lan743x_adapter *adapter)
1704 {
1705     struct lan743x_ptp *ptp = &adapter->ptp;
1706 
1707     mutex_lock(&ptp->command_lock);
1708 
1709     if (lan743x_ptp_is_enabled(adapter)) {
1710         netif_err(adapter, drv, adapter->netdev,
1711               "Attempting reset while enabled\n");
1712         goto done;
1713     }
1714 
1715     lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_RESET_);
1716     lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_RESET_);
1717 done:
1718     mutex_unlock(&ptp->command_lock);
1719 }
1720 
1721 static void lan743x_ptp_clock_set(struct lan743x_adapter *adapter,
1722                   u32 seconds, u32 nano_seconds,
1723                   u32 sub_nano_seconds)
1724 {
1725     struct lan743x_ptp *ptp = &adapter->ptp;
1726 
1727     mutex_lock(&ptp->command_lock);
1728 
1729     lan743x_csr_write(adapter, PTP_CLOCK_SEC, seconds);
1730     lan743x_csr_write(adapter, PTP_CLOCK_NS, nano_seconds);
1731     lan743x_csr_write(adapter, PTP_CLOCK_SUBNS, sub_nano_seconds);
1732 
1733     lan743x_csr_write(adapter, PTP_CMD_CTL, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
1734     lan743x_ptp_wait_till_cmd_done(adapter, PTP_CMD_CTL_PTP_CLOCK_LOAD_);
1735     mutex_unlock(&ptp->command_lock);
1736 }
1737 
1738 bool lan743x_ptp_request_tx_timestamp(struct lan743x_adapter *adapter)
1739 {
1740     struct lan743x_ptp *ptp = &adapter->ptp;
1741     bool result = false;
1742 
1743     spin_lock_bh(&ptp->tx_ts_lock);
1744     if (ptp->pending_tx_timestamps < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
1745         /* request granted */
1746         ptp->pending_tx_timestamps++;
1747         result = true;
1748     }
1749     spin_unlock_bh(&ptp->tx_ts_lock);
1750     return result;
1751 }
1752 
1753 void lan743x_ptp_unrequest_tx_timestamp(struct lan743x_adapter *adapter)
1754 {
1755     struct lan743x_ptp *ptp = &adapter->ptp;
1756 
1757     spin_lock_bh(&ptp->tx_ts_lock);
1758     if (ptp->pending_tx_timestamps > 0)
1759         ptp->pending_tx_timestamps--;
1760     else
1761         netif_err(adapter, drv, adapter->netdev,
1762               "unrequest failed, pending_tx_timestamps==0\n");
1763     spin_unlock_bh(&ptp->tx_ts_lock);
1764 }
1765 
1766 void lan743x_ptp_tx_timestamp_skb(struct lan743x_adapter *adapter,
1767                   struct sk_buff *skb, bool ignore_sync)
1768 {
1769     lan743x_ptp_tx_ts_enqueue_skb(adapter, skb, ignore_sync);
1770 
1771     lan743x_ptp_tx_ts_complete(adapter);
1772 }
1773 
1774 int lan743x_ptp_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1775 {
1776     struct lan743x_adapter *adapter = netdev_priv(netdev);
1777     struct hwtstamp_config config;
1778     int ret = 0;
1779     int index;
1780 
1781     if (!ifr) {
1782         netif_err(adapter, drv, adapter->netdev,
1783               "SIOCSHWTSTAMP, ifr == NULL\n");
1784         return -EINVAL;
1785     }
1786 
1787     if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1788         return -EFAULT;
1789 
1790     switch (config.tx_type) {
1791     case HWTSTAMP_TX_OFF:
1792         for (index = 0; index < adapter->used_tx_channels;
1793              index++)
1794             lan743x_tx_set_timestamping_mode(&adapter->tx[index],
1795                              false, false);
1796         lan743x_ptp_set_sync_ts_insert(adapter, false);
1797         break;
1798     case HWTSTAMP_TX_ON:
1799         for (index = 0; index < adapter->used_tx_channels;
1800             index++)
1801             lan743x_tx_set_timestamping_mode(&adapter->tx[index],
1802                              true, false);
1803         lan743x_ptp_set_sync_ts_insert(adapter, false);
1804         break;
1805     case HWTSTAMP_TX_ONESTEP_SYNC:
1806         for (index = 0; index < adapter->used_tx_channels;
1807             index++)
1808             lan743x_tx_set_timestamping_mode(&adapter->tx[index],
1809                              true, true);
1810 
1811         lan743x_ptp_set_sync_ts_insert(adapter, true);
1812         break;
1813     case HWTSTAMP_TX_ONESTEP_P2P:
1814         ret = -ERANGE;
1815         break;
1816     default:
1817         netif_warn(adapter, drv, adapter->netdev,
1818                "  tx_type = %d, UNKNOWN\n", config.tx_type);
1819         ret = -EINVAL;
1820         break;
1821     }
1822 
1823     if (!ret)
1824         return copy_to_user(ifr->ifr_data, &config,
1825             sizeof(config)) ? -EFAULT : 0;
1826     return ret;
1827 }