| 1 |
// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-3-Clause) |
| 2 |
/* |
| 3 |
* Copyright (c) 2014-2025, Advanced Micro Devices, Inc. |
| 4 |
* Copyright (c) 2014, Synopsys, Inc. |
| 5 |
* All rights reserved |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <linux/clk.h> |
| 9 |
#include <linux/clocksource.h> |
| 10 |
#include <linux/ptp_clock_kernel.h> |
| 11 |
#include <linux/net_tstamp.h> |
| 12 |
|
| 13 |
#include "xgbe.h" |
| 14 |
#include "xgbe-common.h" |
| 15 |
|
| 16 |
static int xgbe_adjfine(struct ptp_clock_info *info, long scaled_ppm) |
| 17 |
{ |
| 18 |
struct xgbe_prv_data *pdata = container_of(info, |
| 19 |
struct xgbe_prv_data, |
| 20 |
ptp_clock_info); |
| 21 |
unsigned long flags; |
| 22 |
u64 addend; |
| 23 |
|
| 24 |
addend = adjust_by_scaled_ppm(pdata->tstamp_addend, scaled_ppm); |
| 25 |
|
| 26 |
spin_lock_irqsave(&pdata->tstamp_lock, flags); |
| 27 |
|
| 28 |
xgbe_update_tstamp_addend(pdata, addend); |
| 29 |
|
| 30 |
spin_unlock_irqrestore(&pdata->tstamp_lock, flags); |
| 31 |
|
| 32 |
return 0; |
| 33 |
} |
| 34 |
|
| 35 |
static int xgbe_adjtime(struct ptp_clock_info *info, s64 delta) |
| 36 |
{ |
| 37 |
struct xgbe_prv_data *pdata = container_of(info, |
| 38 |
struct xgbe_prv_data, |
| 39 |
ptp_clock_info); |
| 40 |
unsigned int neg_adjust = 0; |
| 41 |
unsigned int sec, nsec; |
| 42 |
u32 quotient, reminder; |
| 43 |
unsigned long flags; |
| 44 |
|
| 45 |
if (delta < 0) { |
| 46 |
neg_adjust = 1; |
| 47 |
delta = -delta; |
| 48 |
} |
| 49 |
|
| 50 |
quotient = div_u64_rem(delta, 1000000000ULL, &reminder); |
| 51 |
sec = quotient; |
| 52 |
nsec = reminder; |
| 53 |
|
| 54 |
/* Negative adjustment for Hw timer register. */ |
| 55 |
if (neg_adjust) { |
| 56 |
sec = -sec; |
| 57 |
if (XGMAC_IOREAD_BITS(pdata, MAC_TSCR, TSCTRLSSR)) |
| 58 |
nsec = (1000000000UL - nsec); |
| 59 |
else |
| 60 |
nsec = (0x80000000UL - nsec); |
| 61 |
} |
| 62 |
nsec = (neg_adjust << 31) | nsec; |
| 63 |
|
| 64 |
spin_lock_irqsave(&pdata->tstamp_lock, flags); |
| 65 |
xgbe_update_tstamp_time(pdata, sec, nsec); |
| 66 |
spin_unlock_irqrestore(&pdata->tstamp_lock, flags); |
| 67 |
|
| 68 |
return 0; |
| 69 |
} |
| 70 |
|
| 71 |
static int xgbe_gettimex(struct ptp_clock_info *info, struct timespec64 *ts, |
| 72 |
struct ptp_system_timestamp *sts) |
| 73 |
{ |
| 74 |
struct xgbe_prv_data *pdata = container_of(info, |
| 75 |
struct xgbe_prv_data, |
| 76 |
ptp_clock_info); |
| 77 |
unsigned long flags; |
| 78 |
u64 nsec; |
| 79 |
|
| 80 |
spin_lock_irqsave(&pdata->tstamp_lock, flags); |
| 81 |
ptp_read_system_prets(sts); |
| 82 |
nsec = xgbe_get_tstamp_time(pdata); |
| 83 |
ptp_read_system_postts(sts); |
| 84 |
spin_unlock_irqrestore(&pdata->tstamp_lock, flags); |
| 85 |
|
| 86 |
*ts = ns_to_timespec64(nsec); |
| 87 |
|
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
static int xgbe_settime(struct ptp_clock_info *info, |
| 92 |
const struct timespec64 *ts) |
| 93 |
{ |
| 94 |
struct xgbe_prv_data *pdata = container_of(info, |
| 95 |
struct xgbe_prv_data, |
| 96 |
ptp_clock_info); |
| 97 |
unsigned long flags; |
| 98 |
|
| 99 |
spin_lock_irqsave(&pdata->tstamp_lock, flags); |
| 100 |
xgbe_set_tstamp_time(pdata, ts->tv_sec, ts->tv_nsec); |
| 101 |
spin_unlock_irqrestore(&pdata->tstamp_lock, flags); |
| 102 |
|
| 103 |
return 0; |
| 104 |
} |
| 105 |
|
| 106 |
static int xgbe_enable(struct ptp_clock_info *info, |
| 107 |
struct ptp_clock_request *request, int on) |
| 108 |
{ |
| 109 |
struct xgbe_prv_data *pdata = container_of(info, struct xgbe_prv_data, |
| 110 |
ptp_clock_info); |
| 111 |
struct xgbe_pps_config *pps_cfg; |
| 112 |
unsigned long flags; |
| 113 |
int ret; |
| 114 |
|
| 115 |
dev_dbg(pdata->dev, "rq->type %d on %d\n", request->type, on); |
| 116 |
|
| 117 |
if (request->type != PTP_CLK_REQ_PEROUT) |
| 118 |
return -EOPNOTSUPP; |
| 119 |
|
| 120 |
pps_cfg = &pdata->pps[request->perout.index]; |
| 121 |
|
| 122 |
pps_cfg->start.tv_sec = request->perout.start.sec; |
| 123 |
pps_cfg->start.tv_nsec = request->perout.start.nsec; |
| 124 |
pps_cfg->period.tv_sec = request->perout.period.sec; |
| 125 |
pps_cfg->period.tv_nsec = request->perout.period.nsec; |
| 126 |
|
| 127 |
spin_lock_irqsave(&pdata->tstamp_lock, flags); |
| 128 |
ret = xgbe_pps_config(pdata, pps_cfg, request->perout.index, on); |
| 129 |
spin_unlock_irqrestore(&pdata->tstamp_lock, flags); |
| 130 |
|
| 131 |
return ret; |
| 132 |
} |
| 133 |
|
| 134 |
void xgbe_ptp_register(struct xgbe_prv_data *pdata) |
| 135 |
{ |
| 136 |
struct ptp_clock_info *info = &pdata->ptp_clock_info; |
| 137 |
struct ptp_clock *clock; |
| 138 |
|
| 139 |
snprintf(info->name, sizeof(info->name), "%s", |
| 140 |
netdev_name(pdata->netdev)); |
| 141 |
info->owner = THIS_MODULE; |
| 142 |
info->max_adj = pdata->ptpclk_rate; |
| 143 |
info->adjfine = xgbe_adjfine; |
| 144 |
info->adjtime = xgbe_adjtime; |
| 145 |
info->gettimex64 = xgbe_gettimex; |
| 146 |
info->settime64 = xgbe_settime; |
| 147 |
info->n_per_out = pdata->hw_feat.pps_out_num; |
| 148 |
info->n_ext_ts = pdata->hw_feat.aux_snap_num; |
| 149 |
info->enable = xgbe_enable; |
| 150 |
|
| 151 |
clock = ptp_clock_register(info, pdata->dev); |
| 152 |
if (IS_ERR(clock)) { |
| 153 |
dev_err(pdata->dev, "ptp_clock_register failed\n"); |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
pdata->ptp_clock = clock; |
| 158 |
|
| 159 |
/* Disable all timestamping to start */ |
| 160 |
XGMAC_IOWRITE(pdata, MAC_TSCR, 0); |
| 161 |
pdata->tstamp_config.tx_type = HWTSTAMP_TX_OFF; |
| 162 |
pdata->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE; |
| 163 |
} |
| 164 |
|
| 165 |
void xgbe_ptp_unregister(struct xgbe_prv_data *pdata) |
| 166 |
{ |
| 167 |
if (pdata->ptp_clock) |
| 168 |
ptp_clock_unregister(pdata->ptp_clock); |
| 169 |
} |