2019-05-27 08:55:01 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-02-14 01:07:45 +01:00
|
|
|
/*
|
|
|
|
* Marvell 88E6xxx Switch PTP support
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 Marvell Semiconductor
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017 National Instruments
|
|
|
|
* Erik Hons <erik.hons@ni.com>
|
|
|
|
* Brandon Streiff <brandon.streiff@ni.com>
|
|
|
|
* Dane Wagner <dane.wagner@ni.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "chip.h"
|
2023-01-13 16:12:58 +01:00
|
|
|
#include "global1.h"
|
2018-02-14 01:07:45 +01:00
|
|
|
#include "global2.h"
|
2018-07-18 22:38:23 +02:00
|
|
|
#include "hwtstamp.h"
|
2018-02-14 01:07:45 +01:00
|
|
|
#include "ptp.h"
|
|
|
|
|
2019-07-31 10:23:51 +02:00
|
|
|
#define MV88E6XXX_MAX_ADJ_PPB 1000000
|
|
|
|
|
2024-10-20 14:38:28 +08:00
|
|
|
struct mv88e6xxx_cc_coeffs {
|
|
|
|
u32 cc_shift;
|
|
|
|
u32 cc_mult;
|
|
|
|
u32 cc_mult_num;
|
|
|
|
u32 cc_mult_dem;
|
|
|
|
};
|
|
|
|
|
2019-07-31 10:23:51 +02:00
|
|
|
/* Family MV88E6250:
|
|
|
|
* Raw timestamps are in units of 10-ns clock periods.
|
|
|
|
*
|
|
|
|
* clkadj = scaled_ppm * 10*2^28 / (10^6 * 2^16)
|
|
|
|
* simplifies to
|
|
|
|
* clkadj = scaled_ppm * 2^7 / 5^5
|
|
|
|
*/
|
2024-10-20 14:38:29 +08:00
|
|
|
#define MV88E6XXX_CC_10NS_SHIFT 28
|
|
|
|
static const struct mv88e6xxx_cc_coeffs mv88e6xxx_cc_10ns_coeffs = {
|
|
|
|
.cc_shift = MV88E6XXX_CC_10NS_SHIFT,
|
|
|
|
.cc_mult = 10 << MV88E6XXX_CC_10NS_SHIFT,
|
2024-10-20 14:38:28 +08:00
|
|
|
.cc_mult_num = 1 << 7,
|
|
|
|
.cc_mult_dem = 3125ULL,
|
|
|
|
};
|
2019-07-31 10:23:51 +02:00
|
|
|
|
2024-10-20 14:38:30 +08:00
|
|
|
/* Other families except MV88E6393X in internal clock mode:
|
2019-07-31 10:23:51 +02:00
|
|
|
* Raw timestamps are in units of 8-ns clock periods.
|
|
|
|
*
|
|
|
|
* clkadj = scaled_ppm * 8*2^28 / (10^6 * 2^16)
|
|
|
|
* simplifies to
|
|
|
|
* clkadj = scaled_ppm * 2^9 / 5^6
|
|
|
|
*/
|
2024-10-20 14:38:29 +08:00
|
|
|
#define MV88E6XXX_CC_8NS_SHIFT 28
|
|
|
|
static const struct mv88e6xxx_cc_coeffs mv88e6xxx_cc_8ns_coeffs = {
|
|
|
|
.cc_shift = MV88E6XXX_CC_8NS_SHIFT,
|
|
|
|
.cc_mult = 8 << MV88E6XXX_CC_8NS_SHIFT,
|
2024-10-20 14:38:28 +08:00
|
|
|
.cc_mult_num = 1 << 9,
|
|
|
|
.cc_mult_dem = 15625ULL
|
|
|
|
};
|
2018-02-14 01:07:45 +01:00
|
|
|
|
2024-10-20 14:38:30 +08:00
|
|
|
/* Family MV88E6393X using internal clock:
|
|
|
|
* Raw timestamps are in units of 4-ns clock periods.
|
|
|
|
*
|
|
|
|
* clkadj = scaled_ppm * 4*2^28 / (10^6 * 2^16)
|
|
|
|
* simplifies to
|
|
|
|
* clkadj = scaled_ppm * 2^8 / 5^6
|
|
|
|
*/
|
|
|
|
#define MV88E6XXX_CC_4NS_SHIFT 28
|
|
|
|
static const struct mv88e6xxx_cc_coeffs mv88e6xxx_cc_4ns_coeffs = {
|
|
|
|
.cc_shift = MV88E6XXX_CC_4NS_SHIFT,
|
|
|
|
.cc_mult = 4 << MV88E6XXX_CC_4NS_SHIFT,
|
|
|
|
.cc_mult_num = 1 << 8,
|
|
|
|
.cc_mult_dem = 15625ULL
|
|
|
|
};
|
|
|
|
|
2018-02-14 01:07:45 +01:00
|
|
|
#define TAI_EVENT_WORK_INTERVAL msecs_to_jiffies(100)
|
|
|
|
|
|
|
|
#define cc_to_chip(cc) container_of(cc, struct mv88e6xxx_chip, tstamp_cc)
|
|
|
|
#define dw_overflow_to_chip(dw) container_of(dw, struct mv88e6xxx_chip, \
|
|
|
|
overflow_work)
|
2018-02-14 01:07:47 +01:00
|
|
|
#define dw_tai_event_to_chip(dw) container_of(dw, struct mv88e6xxx_chip, \
|
|
|
|
tai_event_work)
|
2018-02-14 01:07:45 +01:00
|
|
|
|
|
|
|
static int mv88e6xxx_tai_read(struct mv88e6xxx_chip *chip, int addr,
|
|
|
|
u16 *data, int len)
|
|
|
|
{
|
|
|
|
if (!chip->info->ops->avb_ops->tai_read)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
return chip->info->ops->avb_ops->tai_read(chip, addr, data, len);
|
|
|
|
}
|
|
|
|
|
2018-02-14 01:07:47 +01:00
|
|
|
static int mv88e6xxx_tai_write(struct mv88e6xxx_chip *chip, int addr, u16 data)
|
|
|
|
{
|
|
|
|
if (!chip->info->ops->avb_ops->tai_write)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
return chip->info->ops->avb_ops->tai_write(chip, addr, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: places where this are called should be using pinctrl */
|
2018-07-18 22:38:20 +02:00
|
|
|
static int mv88e6352_set_gpio_func(struct mv88e6xxx_chip *chip, int pin,
|
2018-02-14 01:07:47 +01:00
|
|
|
int func, int input)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!chip->info->ops->gpio_ops)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
err = chip->info->ops->gpio_ops->set_dir(chip, pin, input);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
return chip->info->ops->gpio_ops->set_pctl(chip, pin, func);
|
|
|
|
}
|
|
|
|
|
2024-10-20 14:38:29 +08:00
|
|
|
static const struct mv88e6xxx_cc_coeffs *
|
|
|
|
mv88e6xxx_cc_coeff_get(struct mv88e6xxx_chip *chip)
|
|
|
|
{
|
|
|
|
u16 period_ps;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = mv88e6xxx_tai_read(chip, MV88E6XXX_TAI_CLOCK_PERIOD, &period_ps, 1);
|
|
|
|
if (err) {
|
|
|
|
dev_err(chip->dev, "failed to read cycle counter period: %d\n",
|
|
|
|
err);
|
|
|
|
return ERR_PTR(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (period_ps) {
|
2024-10-20 14:38:30 +08:00
|
|
|
case 4000:
|
|
|
|
return &mv88e6xxx_cc_4ns_coeffs;
|
2024-10-20 14:38:29 +08:00
|
|
|
case 8000:
|
|
|
|
return &mv88e6xxx_cc_8ns_coeffs;
|
|
|
|
case 10000:
|
|
|
|
return &mv88e6xxx_cc_10ns_coeffs;
|
|
|
|
default:
|
|
|
|
dev_err(chip->dev, "unexpected cycle counter period of %u ps\n",
|
|
|
|
period_ps);
|
|
|
|
return ERR_PTR(-ENODEV);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-07-01 14:32:25 +02:00
|
|
|
static u64 mv88e6352_ptp_clock_read(struct cyclecounter *cc)
|
2018-02-14 01:07:45 +01:00
|
|
|
{
|
|
|
|
struct mv88e6xxx_chip *chip = cc_to_chip(cc);
|
|
|
|
u16 phc_time[2];
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = mv88e6xxx_tai_read(chip, MV88E6XXX_TAI_TIME_LO, phc_time,
|
|
|
|
ARRAY_SIZE(phc_time));
|
|
|
|
if (err)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return ((u32)phc_time[1] << 16) | phc_time[0];
|
|
|
|
}
|
|
|
|
|
2025-07-01 14:32:25 +02:00
|
|
|
static u64 mv88e6165_ptp_clock_read(struct cyclecounter *cc)
|
2018-07-18 22:38:22 +02:00
|
|
|
{
|
|
|
|
struct mv88e6xxx_chip *chip = cc_to_chip(cc);
|
|
|
|
u16 phc_time[2];
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = mv88e6xxx_tai_read(chip, MV88E6XXX_PTP_GC_TIME_LO, phc_time,
|
|
|
|
ARRAY_SIZE(phc_time));
|
|
|
|
if (err)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return ((u32)phc_time[1] << 16) | phc_time[0];
|
|
|
|
}
|
|
|
|
|
2018-07-18 22:38:20 +02:00
|
|
|
/* mv88e6352_config_eventcap - configure TAI event capture
|
2018-02-14 01:07:47 +01:00
|
|
|
* @event: PTP_CLOCK_PPS (internal) or PTP_CLOCK_EXTTS (external)
|
|
|
|
* @rising: zero for falling-edge trigger, else rising-edge trigger
|
|
|
|
*
|
|
|
|
* This will also reset the capture sequence counter.
|
|
|
|
*/
|
2018-07-18 22:38:20 +02:00
|
|
|
static int mv88e6352_config_eventcap(struct mv88e6xxx_chip *chip, int event,
|
2018-02-14 01:07:47 +01:00
|
|
|
int rising)
|
|
|
|
{
|
|
|
|
u16 global_config;
|
|
|
|
u16 cap_config;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
chip->evcap_config = MV88E6XXX_TAI_CFG_CAP_OVERWRITE |
|
|
|
|
MV88E6XXX_TAI_CFG_CAP_CTR_START;
|
|
|
|
if (!rising)
|
|
|
|
chip->evcap_config |= MV88E6XXX_TAI_CFG_EVREQ_FALLING;
|
|
|
|
|
|
|
|
global_config = (chip->evcap_config | chip->trig_config);
|
|
|
|
err = mv88e6xxx_tai_write(chip, MV88E6XXX_TAI_CFG, global_config);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (event == PTP_CLOCK_PPS) {
|
|
|
|
cap_config = MV88E6XXX_TAI_EVENT_STATUS_CAP_TRIG;
|
|
|
|
} else if (event == PTP_CLOCK_EXTTS) {
|
|
|
|
/* if STATUS_CAP_TRIG is unset we capture PTP_EVREQ events */
|
|
|
|
cap_config = 0;
|
|
|
|
} else {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the capture config; this also clears the capture counter */
|
|
|
|
err = mv88e6xxx_tai_write(chip, MV88E6XXX_TAI_EVENT_STATUS,
|
|
|
|
cap_config);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2018-07-18 22:38:20 +02:00
|
|
|
static void mv88e6352_tai_event_work(struct work_struct *ugly)
|
2018-02-14 01:07:47 +01:00
|
|
|
{
|
|
|
|
struct delayed_work *dw = to_delayed_work(ugly);
|
|
|
|
struct mv88e6xxx_chip *chip = dw_tai_event_to_chip(dw);
|
|
|
|
struct ptp_clock_event ev;
|
|
|
|
u16 status[4];
|
|
|
|
u32 raw_ts;
|
|
|
|
int err;
|
|
|
|
|
2019-06-20 13:50:42 +00:00
|
|
|
mv88e6xxx_reg_lock(chip);
|
2018-02-14 01:07:47 +01:00
|
|
|
err = mv88e6xxx_tai_read(chip, MV88E6XXX_TAI_EVENT_STATUS,
|
|
|
|
status, ARRAY_SIZE(status));
|
2019-06-20 13:50:42 +00:00
|
|
|
mv88e6xxx_reg_unlock(chip);
|
2018-02-14 01:07:47 +01:00
|
|
|
|
|
|
|
if (err) {
|
|
|
|
dev_err(chip->dev, "failed to read TAI status register\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (status[0] & MV88E6XXX_TAI_EVENT_STATUS_ERROR) {
|
|
|
|
dev_warn(chip->dev, "missed event capture\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!(status[0] & MV88E6XXX_TAI_EVENT_STATUS_VALID))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
raw_ts = ((u32)status[2] << 16) | status[1];
|
|
|
|
|
|
|
|
/* Clear the valid bit so the next timestamp can come in */
|
|
|
|
status[0] &= ~MV88E6XXX_TAI_EVENT_STATUS_VALID;
|
2019-06-20 13:50:42 +00:00
|
|
|
mv88e6xxx_reg_lock(chip);
|
2018-02-14 01:07:47 +01:00
|
|
|
err = mv88e6xxx_tai_write(chip, MV88E6XXX_TAI_EVENT_STATUS, status[0]);
|
2019-06-20 13:50:42 +00:00
|
|
|
mv88e6xxx_reg_unlock(chip);
|
2023-10-20 17:00:04 +08:00
|
|
|
if (err) {
|
|
|
|
dev_err(chip->dev, "failed to write TAI status register\n");
|
|
|
|
return;
|
|
|
|
}
|
2018-02-14 01:07:47 +01:00
|
|
|
|
|
|
|
/* This is an external timestamp */
|
|
|
|
ev.type = PTP_CLOCK_EXTTS;
|
|
|
|
|
|
|
|
/* We only have one timestamping channel. */
|
|
|
|
ev.index = 0;
|
2019-06-20 13:50:42 +00:00
|
|
|
mv88e6xxx_reg_lock(chip);
|
2018-02-14 01:07:47 +01:00
|
|
|
ev.timestamp = timecounter_cyc2time(&chip->tstamp_tc, raw_ts);
|
2019-06-20 13:50:42 +00:00
|
|
|
mv88e6xxx_reg_unlock(chip);
|
2018-02-14 01:07:47 +01:00
|
|
|
|
|
|
|
ptp_clock_event(chip->ptp_clock, &ev);
|
|
|
|
out:
|
|
|
|
schedule_delayed_work(&chip->tai_event_work, TAI_EVENT_WORK_INTERVAL);
|
|
|
|
}
|
|
|
|
|
2018-02-14 01:07:45 +01:00
|
|
|
static int mv88e6xxx_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
|
|
|
|
{
|
|
|
|
struct mv88e6xxx_chip *chip = ptp_to_chip(ptp);
|
|
|
|
int neg_adj = 0;
|
|
|
|
u32 diff, mult;
|
|
|
|
u64 adj;
|
|
|
|
|
|
|
|
if (scaled_ppm < 0) {
|
|
|
|
neg_adj = 1;
|
|
|
|
scaled_ppm = -scaled_ppm;
|
|
|
|
}
|
2019-07-31 10:23:51 +02:00
|
|
|
|
2024-10-20 14:38:29 +08:00
|
|
|
mult = chip->cc_coeffs->cc_mult;
|
|
|
|
adj = chip->cc_coeffs->cc_mult_num;
|
2018-02-14 01:07:45 +01:00
|
|
|
adj *= scaled_ppm;
|
2024-10-20 14:38:29 +08:00
|
|
|
diff = div_u64(adj, chip->cc_coeffs->cc_mult_dem);
|
2018-02-14 01:07:45 +01:00
|
|
|
|
2019-06-20 13:50:42 +00:00
|
|
|
mv88e6xxx_reg_lock(chip);
|
2018-02-14 01:07:45 +01:00
|
|
|
|
|
|
|
timecounter_read(&chip->tstamp_tc);
|
|
|
|
chip->tstamp_cc.mult = neg_adj ? mult - diff : mult + diff;
|
|
|
|
|
2019-06-20 13:50:42 +00:00
|
|
|
mv88e6xxx_reg_unlock(chip);
|
2018-02-14 01:07:45 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mv88e6xxx_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
|
|
|
|
{
|
|
|
|
struct mv88e6xxx_chip *chip = ptp_to_chip(ptp);
|
|
|
|
|
2019-06-20 13:50:42 +00:00
|
|
|
mv88e6xxx_reg_lock(chip);
|
2018-02-14 01:07:45 +01:00
|
|
|
timecounter_adjtime(&chip->tstamp_tc, delta);
|
2019-06-20 13:50:42 +00:00
|
|
|
mv88e6xxx_reg_unlock(chip);
|
2018-02-14 01:07:45 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mv88e6xxx_ptp_gettime(struct ptp_clock_info *ptp,
|
|
|
|
struct timespec64 *ts)
|
|
|
|
{
|
|
|
|
struct mv88e6xxx_chip *chip = ptp_to_chip(ptp);
|
|
|
|
u64 ns;
|
|
|
|
|
2019-06-20 13:50:42 +00:00
|
|
|
mv88e6xxx_reg_lock(chip);
|
2018-02-14 01:07:45 +01:00
|
|
|
ns = timecounter_read(&chip->tstamp_tc);
|
2019-06-20 13:50:42 +00:00
|
|
|
mv88e6xxx_reg_unlock(chip);
|
2018-02-14 01:07:45 +01:00
|
|
|
|
|
|
|
*ts = ns_to_timespec64(ns);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mv88e6xxx_ptp_settime(struct ptp_clock_info *ptp,
|
|
|
|
const struct timespec64 *ts)
|
|
|
|
{
|
|
|
|
struct mv88e6xxx_chip *chip = ptp_to_chip(ptp);
|
|
|
|
u64 ns;
|
|
|
|
|
|
|
|
ns = timespec64_to_ns(ts);
|
|
|
|
|
2019-06-20 13:50:42 +00:00
|
|
|
mv88e6xxx_reg_lock(chip);
|
2018-02-14 01:07:45 +01:00
|
|
|
timecounter_init(&chip->tstamp_tc, &chip->tstamp_cc, ns);
|
2019-06-20 13:50:42 +00:00
|
|
|
mv88e6xxx_reg_unlock(chip);
|
2018-02-14 01:07:45 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-18 22:38:20 +02:00
|
|
|
static int mv88e6352_ptp_enable_extts(struct mv88e6xxx_chip *chip,
|
2018-02-14 01:07:47 +01:00
|
|
|
struct ptp_clock_request *rq, int on)
|
|
|
|
{
|
|
|
|
int rising = (rq->extts.flags & PTP_RISING_EDGE);
|
|
|
|
int func;
|
|
|
|
int pin;
|
|
|
|
int err;
|
|
|
|
|
2019-11-14 10:45:03 -08:00
|
|
|
/* Reject requests to enable time stamping on both edges. */
|
|
|
|
if ((rq->extts.flags & PTP_STRICT_FLAGS) &&
|
|
|
|
(rq->extts.flags & PTP_ENABLE_FEATURE) &&
|
|
|
|
(rq->extts.flags & PTP_EXTTS_EDGES) == PTP_EXTTS_EDGES)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
2018-02-14 01:07:47 +01:00
|
|
|
pin = ptp_find_pin(chip->ptp_clock, PTP_PF_EXTTS, rq->extts.index);
|
|
|
|
|
|
|
|
if (pin < 0)
|
|
|
|
return -EBUSY;
|
|
|
|
|
2019-06-20 13:50:42 +00:00
|
|
|
mv88e6xxx_reg_lock(chip);
|
2018-02-14 01:07:47 +01:00
|
|
|
|
|
|
|
if (on) {
|
|
|
|
func = MV88E6352_G2_SCRATCH_GPIO_PCTL_EVREQ;
|
|
|
|
|
2018-07-18 22:38:20 +02:00
|
|
|
err = mv88e6352_set_gpio_func(chip, pin, func, true);
|
2018-02-14 01:07:47 +01:00
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
schedule_delayed_work(&chip->tai_event_work,
|
|
|
|
TAI_EVENT_WORK_INTERVAL);
|
|
|
|
|
2018-07-18 22:38:20 +02:00
|
|
|
err = mv88e6352_config_eventcap(chip, PTP_CLOCK_EXTTS, rising);
|
2018-02-14 01:07:47 +01:00
|
|
|
} else {
|
|
|
|
func = MV88E6352_G2_SCRATCH_GPIO_PCTL_GPIO;
|
|
|
|
|
2018-07-18 22:38:20 +02:00
|
|
|
err = mv88e6352_set_gpio_func(chip, pin, func, true);
|
2018-02-14 01:07:47 +01:00
|
|
|
|
|
|
|
cancel_delayed_work_sync(&chip->tai_event_work);
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2019-06-20 13:50:42 +00:00
|
|
|
mv88e6xxx_reg_unlock(chip);
|
2018-02-14 01:07:47 +01:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2018-07-18 22:38:20 +02:00
|
|
|
static int mv88e6352_ptp_enable(struct ptp_clock_info *ptp,
|
2018-02-14 01:07:45 +01:00
|
|
|
struct ptp_clock_request *rq, int on)
|
|
|
|
{
|
2018-02-14 01:07:47 +01:00
|
|
|
struct mv88e6xxx_chip *chip = ptp_to_chip(ptp);
|
|
|
|
|
|
|
|
switch (rq->type) {
|
|
|
|
case PTP_CLK_REQ_EXTTS:
|
2018-07-18 22:38:20 +02:00
|
|
|
return mv88e6352_ptp_enable_extts(chip, rq, on);
|
2018-02-14 01:07:47 +01:00
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
2018-02-14 01:07:45 +01:00
|
|
|
}
|
|
|
|
|
2018-07-18 22:38:20 +02:00
|
|
|
static int mv88e6352_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin,
|
2018-02-14 01:07:45 +01:00
|
|
|
enum ptp_pin_function func, unsigned int chan)
|
|
|
|
{
|
2018-02-14 01:07:47 +01:00
|
|
|
switch (func) {
|
|
|
|
case PTP_PF_NONE:
|
|
|
|
case PTP_PF_EXTTS:
|
|
|
|
break;
|
|
|
|
case PTP_PF_PEROUT:
|
|
|
|
case PTP_PF_PHYSYNC:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
return 0;
|
2018-02-14 01:07:45 +01:00
|
|
|
}
|
|
|
|
|
2019-07-31 10:23:50 +02:00
|
|
|
const struct mv88e6xxx_ptp_ops mv88e6165_ptp_ops = {
|
|
|
|
.clock_read = mv88e6165_ptp_clock_read,
|
|
|
|
.global_enable = mv88e6165_global_enable,
|
|
|
|
.global_disable = mv88e6165_global_disable,
|
|
|
|
.arr0_sts_reg = MV88E6165_PORT_PTP_ARR0_STS,
|
|
|
|
.arr1_sts_reg = MV88E6165_PORT_PTP_ARR1_STS,
|
|
|
|
.dep_sts_reg = MV88E6165_PORT_PTP_DEP_STS,
|
|
|
|
.rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ),
|
2019-07-31 10:23:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const struct mv88e6xxx_ptp_ops mv88e6250_ptp_ops = {
|
|
|
|
.clock_read = mv88e6352_ptp_clock_read,
|
|
|
|
.ptp_enable = mv88e6352_ptp_enable,
|
|
|
|
.ptp_verify = mv88e6352_ptp_verify,
|
|
|
|
.event_work = mv88e6352_tai_event_work,
|
|
|
|
.port_enable = mv88e6352_hwtstamp_port_enable,
|
|
|
|
.port_disable = mv88e6352_hwtstamp_port_disable,
|
|
|
|
.n_ext_ts = 1,
|
|
|
|
.arr0_sts_reg = MV88E6XXX_PORT_PTP_ARR0_STS,
|
|
|
|
.arr1_sts_reg = MV88E6XXX_PORT_PTP_ARR1_STS,
|
|
|
|
.dep_sts_reg = MV88E6XXX_PORT_PTP_DEP_STS,
|
|
|
|
.rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ),
|
2019-07-31 10:23:50 +02:00
|
|
|
};
|
|
|
|
|
2018-07-18 22:38:20 +02:00
|
|
|
const struct mv88e6xxx_ptp_ops mv88e6352_ptp_ops = {
|
|
|
|
.clock_read = mv88e6352_ptp_clock_read,
|
|
|
|
.ptp_enable = mv88e6352_ptp_enable,
|
|
|
|
.ptp_verify = mv88e6352_ptp_verify,
|
|
|
|
.event_work = mv88e6352_tai_event_work,
|
2018-07-18 22:38:23 +02:00
|
|
|
.port_enable = mv88e6352_hwtstamp_port_enable,
|
|
|
|
.port_disable = mv88e6352_hwtstamp_port_disable,
|
2018-07-18 22:38:20 +02:00
|
|
|
.n_ext_ts = 1,
|
2018-07-18 22:38:23 +02:00
|
|
|
.arr0_sts_reg = MV88E6XXX_PORT_PTP_ARR0_STS,
|
|
|
|
.arr1_sts_reg = MV88E6XXX_PORT_PTP_ARR1_STS,
|
|
|
|
.dep_sts_reg = MV88E6XXX_PORT_PTP_DEP_STS,
|
2018-07-18 22:38:24 +02:00
|
|
|
.rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
|
2018-07-18 22:38:25 +02:00
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ),
|
2018-07-18 22:38:22 +02:00
|
|
|
};
|
|
|
|
|
2023-01-13 16:12:58 +01:00
|
|
|
const struct mv88e6xxx_ptp_ops mv88e6390_ptp_ops = {
|
|
|
|
.clock_read = mv88e6352_ptp_clock_read,
|
|
|
|
.ptp_enable = mv88e6352_ptp_enable,
|
|
|
|
.ptp_verify = mv88e6352_ptp_verify,
|
|
|
|
.event_work = mv88e6352_tai_event_work,
|
|
|
|
.port_enable = mv88e6352_hwtstamp_port_enable,
|
|
|
|
.port_disable = mv88e6352_hwtstamp_port_disable,
|
|
|
|
.set_ptp_cpu_port = mv88e6390_g1_set_ptp_cpu_port,
|
|
|
|
.n_ext_ts = 1,
|
|
|
|
.arr0_sts_reg = MV88E6XXX_PORT_PTP_ARR0_STS,
|
|
|
|
.arr1_sts_reg = MV88E6XXX_PORT_PTP_ARR1_STS,
|
|
|
|
.dep_sts_reg = MV88E6XXX_PORT_PTP_DEP_STS,
|
|
|
|
.rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_EVENT) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_SYNC) |
|
|
|
|
(1 << HWTSTAMP_FILTER_PTP_V2_DELAY_REQ),
|
|
|
|
};
|
|
|
|
|
2025-07-01 14:32:25 +02:00
|
|
|
static u64 mv88e6xxx_ptp_clock_read(struct cyclecounter *cc)
|
2018-07-18 22:38:20 +02:00
|
|
|
{
|
|
|
|
struct mv88e6xxx_chip *chip = cc_to_chip(cc);
|
|
|
|
|
|
|
|
if (chip->info->ops->ptp_ops->clock_read)
|
|
|
|
return chip->info->ops->ptp_ops->clock_read(cc);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-10-20 14:38:30 +08:00
|
|
|
/* With a 250MHz input clock, the 32-bit timestamp counter overflows in ~17.2
|
2018-02-14 01:07:45 +01:00
|
|
|
* seconds; this task forces periodic reads so that we don't miss any.
|
|
|
|
*/
|
2024-10-20 14:38:30 +08:00
|
|
|
#define MV88E6XXX_TAI_OVERFLOW_PERIOD (HZ * 8)
|
2018-02-14 01:07:45 +01:00
|
|
|
static void mv88e6xxx_ptp_overflow_check(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct delayed_work *dw = to_delayed_work(work);
|
|
|
|
struct mv88e6xxx_chip *chip = dw_overflow_to_chip(dw);
|
|
|
|
struct timespec64 ts;
|
|
|
|
|
|
|
|
mv88e6xxx_ptp_gettime(&chip->ptp_clock_info, &ts);
|
|
|
|
|
|
|
|
schedule_delayed_work(&chip->overflow_work,
|
|
|
|
MV88E6XXX_TAI_OVERFLOW_PERIOD);
|
|
|
|
}
|
|
|
|
|
|
|
|
int mv88e6xxx_ptp_setup(struct mv88e6xxx_chip *chip)
|
|
|
|
{
|
2018-07-18 22:38:20 +02:00
|
|
|
const struct mv88e6xxx_ptp_ops *ptp_ops = chip->info->ops->ptp_ops;
|
2018-02-14 01:07:47 +01:00
|
|
|
int i;
|
|
|
|
|
2018-02-14 01:07:45 +01:00
|
|
|
/* Set up the cycle counter */
|
2024-10-20 14:38:29 +08:00
|
|
|
chip->cc_coeffs = mv88e6xxx_cc_coeff_get(chip);
|
|
|
|
if (IS_ERR(chip->cc_coeffs))
|
|
|
|
return PTR_ERR(chip->cc_coeffs);
|
|
|
|
|
2018-02-14 01:07:45 +01:00
|
|
|
memset(&chip->tstamp_cc, 0, sizeof(chip->tstamp_cc));
|
|
|
|
chip->tstamp_cc.read = mv88e6xxx_ptp_clock_read;
|
|
|
|
chip->tstamp_cc.mask = CYCLECOUNTER_MASK(32);
|
2024-10-20 14:38:29 +08:00
|
|
|
chip->tstamp_cc.mult = chip->cc_coeffs->cc_mult;
|
|
|
|
chip->tstamp_cc.shift = chip->cc_coeffs->cc_shift;
|
2018-02-14 01:07:45 +01:00
|
|
|
|
|
|
|
timecounter_init(&chip->tstamp_tc, &chip->tstamp_cc,
|
|
|
|
ktime_to_ns(ktime_get_real()));
|
|
|
|
|
|
|
|
INIT_DELAYED_WORK(&chip->overflow_work, mv88e6xxx_ptp_overflow_check);
|
2018-07-18 22:38:20 +02:00
|
|
|
if (ptp_ops->event_work)
|
|
|
|
INIT_DELAYED_WORK(&chip->tai_event_work, ptp_ops->event_work);
|
2018-02-14 01:07:45 +01:00
|
|
|
|
|
|
|
chip->ptp_clock_info.owner = THIS_MODULE;
|
|
|
|
snprintf(chip->ptp_clock_info.name, sizeof(chip->ptp_clock_info.name),
|
2019-02-21 20:09:27 -08:00
|
|
|
"%s", dev_name(chip->dev));
|
2018-02-14 01:07:45 +01:00
|
|
|
|
2018-07-18 22:38:20 +02:00
|
|
|
chip->ptp_clock_info.n_ext_ts = ptp_ops->n_ext_ts;
|
2018-02-14 01:07:47 +01:00
|
|
|
chip->ptp_clock_info.n_per_out = 0;
|
|
|
|
chip->ptp_clock_info.n_pins = mv88e6xxx_num_gpio(chip);
|
|
|
|
chip->ptp_clock_info.pps = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < chip->ptp_clock_info.n_pins; ++i) {
|
|
|
|
struct ptp_pin_desc *ppd = &chip->pin_config[i];
|
|
|
|
|
|
|
|
snprintf(ppd->name, sizeof(ppd->name), "mv88e6xxx_gpio%d", i);
|
|
|
|
ppd->index = i;
|
|
|
|
ppd->func = PTP_PF_NONE;
|
|
|
|
}
|
|
|
|
chip->ptp_clock_info.pin_config = chip->pin_config;
|
|
|
|
|
2019-07-31 10:23:51 +02:00
|
|
|
chip->ptp_clock_info.max_adj = MV88E6XXX_MAX_ADJ_PPB;
|
2018-02-14 01:07:45 +01:00
|
|
|
chip->ptp_clock_info.adjfine = mv88e6xxx_ptp_adjfine;
|
|
|
|
chip->ptp_clock_info.adjtime = mv88e6xxx_ptp_adjtime;
|
|
|
|
chip->ptp_clock_info.gettime64 = mv88e6xxx_ptp_gettime;
|
|
|
|
chip->ptp_clock_info.settime64 = mv88e6xxx_ptp_settime;
|
2018-07-18 22:38:20 +02:00
|
|
|
chip->ptp_clock_info.enable = ptp_ops->ptp_enable;
|
|
|
|
chip->ptp_clock_info.verify = ptp_ops->ptp_verify;
|
2018-02-14 01:07:50 +01:00
|
|
|
chip->ptp_clock_info.do_aux_work = mv88e6xxx_hwtstamp_work;
|
2018-02-14 01:07:45 +01:00
|
|
|
|
net: ptp: introduce .supported_extts_flags to ptp_clock_info
The PTP_EXTTS_REQUEST(2) ioctl has a flags field which specifies how the
external timestamp request should behave. This includes which edge of the
signal to timestamp, as well as a specialized "offset" mode. It is expected
that more flags will be added in the future.
Driver authors routinely do not check the flags, often accepting requests
with flags which they do not support. Even drivers which do check flags may
not be future-proofed to reject flags not yet defined. Thus, any future
flag additions often require manually updating drivers to reject these
flags.
This approach of hoping we catch flag checks during review, or playing
whack-a-mole after the fact is the wrong approach.
Introduce the "supported_extts_flags" field to the ptp_clock_info
structure. This field defines the set of flags the device actually
supports.
Update the core character device logic to check this field and reject
unsupported requests. Getting this right is somewhat tricky. First, to
avoid unnecessary repetition and make basic functionality work when
.supported_extts_flags is 0, the core always accepts the PTP_ENABLE_FEATURE
flag. This flag is used to set the 'on' parameter to the .enable function
and is thus always 'supported' by all drivers.
For backwards compatibility, the PTP_RISING_EDGE and PTP_FALLING_EDGE flags
are merely "hints" when using the old PTP_EXTTS_REQUEST ioctl, and are not
expected to be enforced. If the user issues PTP_EXTTS_REQUEST2, the
PTP_STRICT_FLAGS flag is added which is supposed to inform the driver to
strictly validate the flags and reject unsupported requests. To handle
this, first check if the driver reports PTP_STRICT_FLAGS support. If it
does not, then always allow the PTP_RISING_EDGE and PTP_FALLING_EDGE flags.
This keeps backwards compatibility with the original PTP_EXTTS_REQUEST
ioctl where these flags are not guaranteed to be honored.
This way, drivers which do not set the supported_extts_flags will continue
to accept requests for the original PTP_EXTTS_REQUEST ioctl. The core will
automatically reject requests with new flags, and correctly reject requests
with PTP_STRICT_FLAGS, where the driver is supposed to strictly validate
the flags.
Update the various drivers, refactoring their validation logic into the
.supported_extts_flags field. For consistency and readability,
PTP_ENABLE_FEATURE is not set in the supported flags list, and
PTP_EXTTS_EDGES is expanded to PTP_RISING_EDGE | PTP_FALLING_EDGE in all
cases.
Note the following driver files set n_ext_ts to a non-zero value but did
not check flags at all:
• drivers/net/ethernet/freescale/dpaa2/dpaa2-ptp.c
• drivers/net/ethernet/freescale/enetc/enetc_ptp.c
• drivers/net/ethernet/intel/i40e/i40e_ptp.c
• drivers/net/ethernet/marvell/octeontx2/nic/otx2_ptp.c
• drivers/net/ethernet/renesas/ravb_ptp.c
• drivers/net/ethernet/renesas/rtsn.c
• drivers/net/ethernet/renesas/rtsn.h
• drivers/net/ethernet/ti/am65-cpts.c
• drivers/net/ethernet/ti/cpts.h
• drivers/net/ethernet/ti/icssg/icss_iep.c
• drivers/net/ethernet/xscale/ptp_ixp46x.c
• drivers/net/phy/bcm-phy-ptp.c
• drivers/ptp/ptp_ocp.c
• drivers/ptp/ptp_pch.c
• drivers/ptp/ptp_qoriq.c
These drivers behavior does change slightly: they will now reject the
PTP_EXTTS_REQUEST2 ioctl, because they do not strictly validate their
flags. This also makes them no longer incorrectly accept PTP_EXT_OFFSET.
Also note that the renesas ravb driver does not support PTP_STRICT_FLAGS.
We could leave the .supported_extts_flags as 0, but I added the
PTP_RISING_EDGE | PTP_FALLING_EDGE since the driver previously manually
validated these flags. This is equivalent to 0 because the core will allow
these flags regardless unless PTP_STRICT_FLAGS is also set.
Signed-off-by: Jacob Keller <jacob.e.keller@intel.com>
Reviewed-by: Kory Maincent <kory.maincent@bootlin.com>
Link: https://patch.msgid.link/20250414-jk-supported-perout-flags-v2-1-f6b17d15475c@intel.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2025-04-14 14:26:30 -07:00
|
|
|
chip->ptp_clock_info.supported_extts_flags = PTP_RISING_EDGE |
|
|
|
|
PTP_FALLING_EDGE |
|
|
|
|
PTP_STRICT_FLAGS;
|
|
|
|
|
2023-01-13 16:12:58 +01:00
|
|
|
if (ptp_ops->set_ptp_cpu_port) {
|
|
|
|
struct dsa_port *dp;
|
|
|
|
int upstream = 0;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
dsa_switch_for_each_user_port(dp, chip->ds) {
|
|
|
|
upstream = dsa_upstream_port(chip->ds, dp->index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ptp_ops->set_ptp_cpu_port(chip, upstream);
|
|
|
|
if (err) {
|
|
|
|
dev_err(chip->dev, "Failed to set PTP CPU destination port!\n");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-14 01:07:45 +01:00
|
|
|
chip->ptp_clock = ptp_clock_register(&chip->ptp_clock_info, chip->dev);
|
|
|
|
if (IS_ERR(chip->ptp_clock))
|
|
|
|
return PTR_ERR(chip->ptp_clock);
|
|
|
|
|
|
|
|
schedule_delayed_work(&chip->overflow_work,
|
|
|
|
MV88E6XXX_TAI_OVERFLOW_PERIOD);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mv88e6xxx_ptp_free(struct mv88e6xxx_chip *chip)
|
|
|
|
{
|
|
|
|
if (chip->ptp_clock) {
|
|
|
|
cancel_delayed_work_sync(&chip->overflow_work);
|
2018-07-18 22:38:20 +02:00
|
|
|
if (chip->info->ops->ptp_ops->event_work)
|
|
|
|
cancel_delayed_work_sync(&chip->tai_event_work);
|
2018-02-14 01:07:45 +01:00
|
|
|
|
|
|
|
ptp_clock_unregister(chip->ptp_clock);
|
|
|
|
chip->ptp_clock = NULL;
|
|
|
|
}
|
|
|
|
}
|