2025-04-07 15:59:13 +05:30
|
|
|
// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-3-Clause)
|
2014-07-29 08:57:19 -05:00
|
|
|
/*
|
2025-04-07 15:59:13 +05:30
|
|
|
* Copyright (c) 2014-2025, Advanced Micro Devices, Inc.
|
|
|
|
* Copyright (c) 2014, Synopsys, Inc.
|
|
|
|
* All rights reserved
|
2014-07-29 08:57:19 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/clocksource.h>
|
|
|
|
#include <linux/ptp_clock_kernel.h>
|
|
|
|
#include <linux/net_tstamp.h>
|
|
|
|
|
|
|
|
#include "xgbe.h"
|
|
|
|
#include "xgbe-common.h"
|
|
|
|
|
2022-10-28 04:04:20 -07:00
|
|
|
static int xgbe_adjfine(struct ptp_clock_info *info, long scaled_ppm)
|
2014-07-29 08:57:19 -05:00
|
|
|
{
|
|
|
|
struct xgbe_prv_data *pdata = container_of(info,
|
|
|
|
struct xgbe_prv_data,
|
|
|
|
ptp_clock_info);
|
|
|
|
unsigned long flags;
|
2022-10-28 04:04:20 -07:00
|
|
|
u64 addend;
|
2014-07-29 08:57:19 -05:00
|
|
|
|
2022-10-28 04:04:20 -07:00
|
|
|
addend = adjust_by_scaled_ppm(pdata->tstamp_addend, scaled_ppm);
|
2014-07-29 08:57:19 -05:00
|
|
|
|
|
|
|
spin_lock_irqsave(&pdata->tstamp_lock, flags);
|
|
|
|
|
2025-07-19 00:26:27 +05:30
|
|
|
xgbe_update_tstamp_addend(pdata, addend);
|
2014-07-29 08:57:19 -05:00
|
|
|
|
|
|
|
spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int xgbe_adjtime(struct ptp_clock_info *info, s64 delta)
|
|
|
|
{
|
|
|
|
struct xgbe_prv_data *pdata = container_of(info,
|
|
|
|
struct xgbe_prv_data,
|
|
|
|
ptp_clock_info);
|
2025-07-19 00:26:28 +05:30
|
|
|
unsigned int neg_adjust = 0;
|
|
|
|
unsigned int sec, nsec;
|
|
|
|
u32 quotient, reminder;
|
2014-07-29 08:57:19 -05:00
|
|
|
unsigned long flags;
|
|
|
|
|
2025-07-19 00:26:28 +05:30
|
|
|
if (delta < 0) {
|
|
|
|
neg_adjust = 1;
|
|
|
|
delta = -delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
quotient = div_u64_rem(delta, 1000000000ULL, &reminder);
|
|
|
|
sec = quotient;
|
|
|
|
nsec = reminder;
|
|
|
|
|
|
|
|
/* Negative adjustment for Hw timer register. */
|
|
|
|
if (neg_adjust) {
|
|
|
|
sec = -sec;
|
|
|
|
if (XGMAC_IOREAD_BITS(pdata, MAC_TSCR, TSCTRLSSR))
|
|
|
|
nsec = (1000000000UL - nsec);
|
|
|
|
else
|
|
|
|
nsec = (0x80000000UL - nsec);
|
|
|
|
}
|
|
|
|
nsec = (neg_adjust << 31) | nsec;
|
|
|
|
|
2014-07-29 08:57:19 -05:00
|
|
|
spin_lock_irqsave(&pdata->tstamp_lock, flags);
|
2025-07-19 00:26:28 +05:30
|
|
|
xgbe_update_tstamp_time(pdata, sec, nsec);
|
2014-07-29 08:57:19 -05:00
|
|
|
spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2025-07-19 00:26:28 +05:30
|
|
|
static int xgbe_gettimex(struct ptp_clock_info *info, struct timespec64 *ts,
|
|
|
|
struct ptp_system_timestamp *sts)
|
2014-07-29 08:57:19 -05:00
|
|
|
{
|
|
|
|
struct xgbe_prv_data *pdata = container_of(info,
|
|
|
|
struct xgbe_prv_data,
|
|
|
|
ptp_clock_info);
|
|
|
|
unsigned long flags;
|
|
|
|
u64 nsec;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&pdata->tstamp_lock, flags);
|
2025-07-19 00:26:28 +05:30
|
|
|
ptp_read_system_prets(sts);
|
|
|
|
nsec = xgbe_get_tstamp_time(pdata);
|
|
|
|
ptp_read_system_postts(sts);
|
2014-07-29 08:57:19 -05:00
|
|
|
spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
|
|
|
|
|
2015-03-29 23:11:55 +02:00
|
|
|
*ts = ns_to_timespec64(nsec);
|
2014-07-29 08:57:19 -05:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-29 23:11:55 +02:00
|
|
|
static int xgbe_settime(struct ptp_clock_info *info,
|
|
|
|
const struct timespec64 *ts)
|
2014-07-29 08:57:19 -05:00
|
|
|
{
|
|
|
|
struct xgbe_prv_data *pdata = container_of(info,
|
|
|
|
struct xgbe_prv_data,
|
|
|
|
ptp_clock_info);
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&pdata->tstamp_lock, flags);
|
2025-07-19 00:26:28 +05:30
|
|
|
xgbe_set_tstamp_time(pdata, ts->tv_sec, ts->tv_nsec);
|
2014-07-29 08:57:19 -05:00
|
|
|
spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int xgbe_enable(struct ptp_clock_info *info,
|
|
|
|
struct ptp_clock_request *request, int on)
|
|
|
|
{
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
void xgbe_ptp_register(struct xgbe_prv_data *pdata)
|
|
|
|
{
|
|
|
|
struct ptp_clock_info *info = &pdata->ptp_clock_info;
|
|
|
|
struct ptp_clock *clock;
|
|
|
|
|
|
|
|
snprintf(info->name, sizeof(info->name), "%s",
|
|
|
|
netdev_name(pdata->netdev));
|
|
|
|
info->owner = THIS_MODULE;
|
2015-01-16 12:47:16 -06:00
|
|
|
info->max_adj = pdata->ptpclk_rate;
|
2022-10-28 04:04:20 -07:00
|
|
|
info->adjfine = xgbe_adjfine;
|
2014-07-29 08:57:19 -05:00
|
|
|
info->adjtime = xgbe_adjtime;
|
2025-07-19 00:26:28 +05:30
|
|
|
info->gettimex64 = xgbe_gettimex;
|
2015-03-29 23:11:55 +02:00
|
|
|
info->settime64 = xgbe_settime;
|
2014-07-29 08:57:19 -05:00
|
|
|
info->enable = xgbe_enable;
|
|
|
|
|
|
|
|
clock = ptp_clock_register(info, pdata->dev);
|
|
|
|
if (IS_ERR(clock)) {
|
|
|
|
dev_err(pdata->dev, "ptp_clock_register failed\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pdata->ptp_clock = clock;
|
|
|
|
|
|
|
|
/* Disable all timestamping to start */
|
2017-06-28 13:41:49 -05:00
|
|
|
XGMAC_IOWRITE(pdata, MAC_TSCR, 0);
|
2014-07-29 08:57:19 -05:00
|
|
|
pdata->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
|
|
|
|
pdata->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void xgbe_ptp_unregister(struct xgbe_prv_data *pdata)
|
|
|
|
{
|
|
|
|
if (pdata->ptp_clock)
|
|
|
|
ptp_clock_unregister(pdata->ptp_clock);
|
|
|
|
}
|