/* SPDX-License-Identifier: GPL-2.0+ */
/* Copyright (C) 2018 Microchip Technology Inc. */
#include <linux/netdevice.h>
#include "lan743x_main.h"
#include <linux/ptp_clock_kernel.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/net_tstamp.h>
#include "lan743x_ptp.h"
#define LAN743X_NUMBER_OF_GPIO (12)
#define LAN743X_PTP_MAX_FREQ_ADJ_IN_PPB (31249999)
#define LAN743X_PTP_MAX_FINE_ADJ_IN_SCALED_PPM (2047999934)
static bool lan743x_ptp_is_enabled(struct lan743x_adapter *adapter);
static void lan743x_ptp_enable(struct lan743x_adapter *adapter);
static void lan743x_ptp_disable(struct lan743x_adapter *adapter);
static void lan743x_ptp_reset(struct lan743x_adapter *adapter);
static void lan743x_ptp_clock_set(struct lan743x_adapter *adapter,
u32 seconds, u32 nano_seconds,
u32 sub_nano_seconds);
int lan743x_gpio_init(struct lan743x_adapter *adapter)
{
struct lan743x_gpio *gpio = &adapter->gpio;
spin_lock_init(&gpio->gpio_lock);
gpio->gpio_cfg0 = 0; /* set all direction to input, data = 0 */
gpio->gpio_cfg1 = 0x0FFF0000;/* disable all gpio, set to open drain */
gpio->gpio_cfg2 = 0;/* set all to 1588 low polarity level */
gpio->gpio_cfg3 = 0;/* disable all 1588 output */
lan743x_csr_write(adapter, GPIO_CFG0, gpio->gpio_cfg0);
lan743x_csr_write(adapter, GPIO_CFG1, gpio->gpio_cfg1);
lan743x_csr_write(adapter, GPIO_CFG2, gpio->gpio_cfg2);
lan743x_csr_write(adapter, GPIO_CFG3, gpio->gpio_cfg3);
return 0;
}
static void lan743x_ptp_wait_till_cmd_done(struct lan743x_adapter *adapter,
u32 bit_mask)
{
int timeout = 1000;
u32 data = 0;
while (timeout &&
(data = (lan743x_csr_read(adapter, PTP_CMD_CTL) &
bit_mask))) {
usleep_range(1000, 20000);
timeout--;
}
if (data) {
netif_err(adapter, drv, adapter->netdev,
"timeout waiting for cmd to be done, cmd = 0x%08X\n",
bit_mask);
}
}
static void lan743x_ptp_tx_ts_enqueue_ts(struct lan743x_adapter *adapter,
u32 seconds, u32 nano_seconds,
u32 header)
{
struct lan743x_ptp *ptp = &adapter->ptp;
spin_lock_bh(&ptp->tx_ts_lock);
if (ptp->tx_ts_queue_size < LAN743X_PTP_NUMBER_OF_TX_TIMESTAMPS) {
ptp->tx_ts_seconds_queue[ptp->tx_ts_queue_size] = seconds;
ptp->tx_ts_nseconds_queue[ptp->tx_ts_queue_size] = nano_seconds;
ptp->tx_ts_header_queue[ptp->tx_ts_queue_size] = header;
ptp->tx_ts_queue_size++;
} else {
netif_err(adapter, drv, adapter->netdev,
"tx ts queue overflow\n");
}
spin_unlock_bh(&ptp->tx_ts_lock);
}
static void lan743x_ptp_tx_ts_complete(struct lan743x_adapter *adapter)
{
struct lan743x_ptp *ptp = &adapter->ptp;
struct skb_shared_hwtstamps tstamps;
u32 header, nseconds, seconds;
bool ignore_sync = false;
struct sk_buff *skb;
int c, i;
spin_lock_bh(&ptp->tx_ts_lock);
c = ptp->tx_ts_skb_queue_size;
if (