2019-05-29 22:18:05 +08:00
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2013-03-26 12:43:10 +08:00
|
|
|
|
/*******************************************************************************
|
|
|
|
|
Copyright (C) 2013 Vayavya Labs Pvt Ltd
|
|
|
|
|
|
|
|
|
|
This implements all the API for managing HW timestamp & PTP.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Author: Rayagond Kokatanur <rayagond@vayavyalabs.com>
|
|
|
|
|
Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <linux/io.h>
|
2020-03-16 10:32:53 +08:00
|
|
|
|
#include <linux/iopoll.h>
|
2013-03-26 12:43:10 +08:00
|
|
|
|
#include <linux/delay.h>
|
2021-04-14 08:16:17 +08:00
|
|
|
|
#include <linux/ptp_clock_kernel.h>
|
2013-03-26 12:43:10 +08:00
|
|
|
|
#include "common.h"
|
|
|
|
|
#include "stmmac_ptp.h"
|
2021-04-14 08:16:17 +08:00
|
|
|
|
#include "dwmac4.h"
|
|
|
|
|
#include "stmmac.h"
|
2013-03-26 12:43:10 +08:00
|
|
|
|
|
2018-04-16 23:08:15 +08:00
|
|
|
|
static void config_hw_tstamping(void __iomem *ioaddr, u32 data)
|
2013-03-26 12:43:10 +08:00
|
|
|
|
{
|
|
|
|
|
writel(data, ioaddr + PTP_TCR);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-16 23:08:15 +08:00
|
|
|
|
static void config_sub_second_increment(void __iomem *ioaddr,
|
|
|
|
|
u32 ptp_clock, int gmac4, u32 *ssinc)
|
2013-03-26 12:43:10 +08:00
|
|
|
|
{
|
|
|
|
|
u32 value = readl(ioaddr + PTP_TCR);
|
|
|
|
|
unsigned long data;
|
2017-12-19 06:33:59 +08:00
|
|
|
|
u32 reg_value;
|
2013-03-26 12:43:10 +08:00
|
|
|
|
|
net: stmmac: Fix sub-second increment
In fine adjustement mode, which is the current default, the sub-second
increment register is the number of nanoseconds that will be added to
the clock when the accumulator overflows. At each clock cycle, the
value of the addend register is added to the accumulator.
Currently, we use 20ns = 1e09ns / 50MHz as this value whatever the
frequency of the ptp clock actually is.
The adjustment is then done on the addend register, only incrementing
every X clock cycles X being the ratio between 50MHz and ptp_clock_rate
(addend = 2^32 * 50MHz/ptp_clock_rate).
This causes the following issues :
- In case the frequency of the ptp clock is inferior or equal to 50MHz,
the addend value calculation will overflow and the default
addend value will be set to 0, causing the clock to not work at
all. (For instance, for ptp_clock_rate = 50MHz, addend = 2^32).
- The resolution of the timestamping clock is limited to 20ns while it
is not needed, thus limiting the accuracy of the timestamping to
20ns.
Fix this by setting sub-second increment to 2e09ns / ptp_clock_rate.
It will allow to reach the minimum possible frequency for
ptp_clk_ref, which is 5MHz for GMII 1000Mps Full-Duplex by setting the
sub-second-increment to a higher value. For instance, for 25MHz, it
gives ssinc = 80ns and default_addend = 2^31.
It will also allow to use a lower value for sub-second-increment, thus
improving the timestamping accuracy with frequencies higher than
100MHz, for instance, for 200MHz, ssinc = 10ns and default_addend =
2^31.
v1->v2:
- Remove modifications to the calculation of default addend, which broke
compatibility with clock frequencies for which 2000000000 / ptp_clk_freq
is not an integer.
- Modify description according to discussions.
Signed-off-by: Julien Beraud <julien.beraud@orolia.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2020-04-15 20:24:32 +08:00
|
|
|
|
/* For GMAC3.x, 4.x versions, in "fine adjustement mode" set sub-second
|
|
|
|
|
* increment to twice the number of nanoseconds of a clock cycle.
|
|
|
|
|
* The calculation of the default_addend value by the caller will set it
|
|
|
|
|
* to mid-range = 2^31 when the remainder of this division is zero,
|
|
|
|
|
* which will make the accumulator overflow once every 2 ptp_clock
|
|
|
|
|
* cycles, adding twice the number of nanoseconds of a clock cycle :
|
|
|
|
|
* 2000000000ULL / ptp_clock.
|
2013-03-26 12:43:10 +08:00
|
|
|
|
*/
|
2016-11-14 16:27:29 +08:00
|
|
|
|
if (value & PTP_TCR_TSCFUPDT)
|
net: stmmac: Fix sub-second increment
In fine adjustement mode, which is the current default, the sub-second
increment register is the number of nanoseconds that will be added to
the clock when the accumulator overflows. At each clock cycle, the
value of the addend register is added to the accumulator.
Currently, we use 20ns = 1e09ns / 50MHz as this value whatever the
frequency of the ptp clock actually is.
The adjustment is then done on the addend register, only incrementing
every X clock cycles X being the ratio between 50MHz and ptp_clock_rate
(addend = 2^32 * 50MHz/ptp_clock_rate).
This causes the following issues :
- In case the frequency of the ptp clock is inferior or equal to 50MHz,
the addend value calculation will overflow and the default
addend value will be set to 0, causing the clock to not work at
all. (For instance, for ptp_clock_rate = 50MHz, addend = 2^32).
- The resolution of the timestamping clock is limited to 20ns while it
is not needed, thus limiting the accuracy of the timestamping to
20ns.
Fix this by setting sub-second increment to 2e09ns / ptp_clock_rate.
It will allow to reach the minimum possible frequency for
ptp_clk_ref, which is 5MHz for GMII 1000Mps Full-Duplex by setting the
sub-second-increment to a higher value. For instance, for 25MHz, it
gives ssinc = 80ns and default_addend = 2^31.
It will also allow to use a lower value for sub-second-increment, thus
improving the timestamping accuracy with frequencies higher than
100MHz, for instance, for 200MHz, ssinc = 10ns and default_addend =
2^31.
v1->v2:
- Remove modifications to the calculation of default addend, which broke
compatibility with clock frequencies for which 2000000000 / ptp_clk_freq
is not an integer.
- Modify description according to discussions.
Signed-off-by: Julien Beraud <julien.beraud@orolia.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2020-04-15 20:24:32 +08:00
|
|
|
|
data = (2000000000ULL / ptp_clock);
|
2016-11-14 16:27:29 +08:00
|
|
|
|
else
|
|
|
|
|
data = (1000000000ULL / ptp_clock);
|
2013-03-26 12:43:10 +08:00
|
|
|
|
|
|
|
|
|
/* 0.465ns accuracy */
|
2013-09-03 13:55:07 +08:00
|
|
|
|
if (!(value & PTP_TCR_TSCTRLSSR))
|
|
|
|
|
data = (data * 1000) / 465;
|
2013-03-26 12:43:10 +08:00
|
|
|
|
|
2016-11-14 16:27:29 +08:00
|
|
|
|
data &= PTP_SSIR_SSINC_MASK;
|
|
|
|
|
|
2017-12-19 06:33:59 +08:00
|
|
|
|
reg_value = data;
|
2016-11-14 16:27:29 +08:00
|
|
|
|
if (gmac4)
|
2017-12-19 06:33:59 +08:00
|
|
|
|
reg_value <<= GMAC4_PTP_SSIR_SSINC_SHIFT;
|
2016-11-14 16:27:29 +08:00
|
|
|
|
|
2017-12-19 06:33:59 +08:00
|
|
|
|
writel(reg_value, ioaddr + PTP_SSIR);
|
2015-12-14 11:32:01 +08:00
|
|
|
|
|
2018-04-16 23:08:15 +08:00
|
|
|
|
if (ssinc)
|
|
|
|
|
*ssinc = data;
|
2013-03-26 12:43:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-16 23:08:15 +08:00
|
|
|
|
static int init_systime(void __iomem *ioaddr, u32 sec, u32 nsec)
|
2013-03-26 12:43:10 +08:00
|
|
|
|
{
|
|
|
|
|
u32 value;
|
|
|
|
|
|
|
|
|
|
writel(sec, ioaddr + PTP_STSUR);
|
|
|
|
|
writel(nsec, ioaddr + PTP_STNSUR);
|
|
|
|
|
/* issue command to initialize the system time value */
|
|
|
|
|
value = readl(ioaddr + PTP_TCR);
|
|
|
|
|
value |= PTP_TCR_TSINIT;
|
|
|
|
|
writel(value, ioaddr + PTP_TCR);
|
|
|
|
|
|
|
|
|
|
/* wait for present system time initialize to complete */
|
2022-04-19 16:42:26 +08:00
|
|
|
|
return readl_poll_timeout_atomic(ioaddr + PTP_TCR, value,
|
2020-03-16 10:32:53 +08:00
|
|
|
|
!(value & PTP_TCR_TSINIT),
|
2022-04-19 16:42:26 +08:00
|
|
|
|
10, 100000);
|
2013-03-26 12:43:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-16 23:08:15 +08:00
|
|
|
|
static int config_addend(void __iomem *ioaddr, u32 addend)
|
2013-03-26 12:43:10 +08:00
|
|
|
|
{
|
|
|
|
|
u32 value;
|
|
|
|
|
int limit;
|
|
|
|
|
|
|
|
|
|
writel(addend, ioaddr + PTP_TAR);
|
|
|
|
|
/* issue command to update the addend value */
|
|
|
|
|
value = readl(ioaddr + PTP_TCR);
|
|
|
|
|
value |= PTP_TCR_TSADDREG;
|
|
|
|
|
writel(value, ioaddr + PTP_TCR);
|
|
|
|
|
|
|
|
|
|
/* wait for present addend update to complete */
|
|
|
|
|
limit = 10;
|
|
|
|
|
while (limit--) {
|
|
|
|
|
if (!(readl(ioaddr + PTP_TCR) & PTP_TCR_TSADDREG))
|
|
|
|
|
break;
|
|
|
|
|
mdelay(10);
|
|
|
|
|
}
|
|
|
|
|
if (limit < 0)
|
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-16 23:08:15 +08:00
|
|
|
|
static int adjust_systime(void __iomem *ioaddr, u32 sec, u32 nsec,
|
|
|
|
|
int add_sub, int gmac4)
|
2013-03-26 12:43:11 +08:00
|
|
|
|
{
|
|
|
|
|
u32 value;
|
|
|
|
|
int limit;
|
|
|
|
|
|
2016-11-14 16:27:29 +08:00
|
|
|
|
if (add_sub) {
|
|
|
|
|
/* If the new sec value needs to be subtracted with
|
|
|
|
|
* the system time, then MAC_STSUR reg should be
|
|
|
|
|
* programmed with (2^32 – <new_sec_value>)
|
|
|
|
|
*/
|
|
|
|
|
if (gmac4)
|
2019-06-19 22:13:48 +08:00
|
|
|
|
sec = -sec;
|
2016-11-14 16:27:29 +08:00
|
|
|
|
|
|
|
|
|
value = readl(ioaddr + PTP_TCR);
|
|
|
|
|
if (value & PTP_TCR_TSCTRLSSR)
|
|
|
|
|
nsec = (PTP_DIGITAL_ROLLOVER_MODE - nsec);
|
|
|
|
|
else
|
|
|
|
|
nsec = (PTP_BINARY_ROLLOVER_MODE - nsec);
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-26 12:43:11 +08:00
|
|
|
|
writel(sec, ioaddr + PTP_STSUR);
|
2016-11-14 16:27:29 +08:00
|
|
|
|
value = (add_sub << PTP_STNSUR_ADDSUB_SHIFT) | nsec;
|
|
|
|
|
writel(value, ioaddr + PTP_STNSUR);
|
|
|
|
|
|
2013-03-26 12:43:11 +08:00
|
|
|
|
/* issue command to initialize the system time value */
|
|
|
|
|
value = readl(ioaddr + PTP_TCR);
|
|
|
|
|
value |= PTP_TCR_TSUPDT;
|
|
|
|
|
writel(value, ioaddr + PTP_TCR);
|
|
|
|
|
|
|
|
|
|
/* wait for present system time adjust/update to complete */
|
|
|
|
|
limit = 10;
|
|
|
|
|
while (limit--) {
|
|
|
|
|
if (!(readl(ioaddr + PTP_TCR) & PTP_TCR_TSUPDT))
|
|
|
|
|
break;
|
|
|
|
|
mdelay(10);
|
|
|
|
|
}
|
|
|
|
|
if (limit < 0)
|
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-16 23:08:15 +08:00
|
|
|
|
static void get_systime(void __iomem *ioaddr, u64 *systime)
|
2013-03-26 12:43:11 +08:00
|
|
|
|
{
|
2022-02-04 00:00:25 +08:00
|
|
|
|
u64 ns, sec0, sec1;
|
|
|
|
|
|
|
|
|
|
/* Get the TSS value */
|
|
|
|
|
sec1 = readl_relaxed(ioaddr + PTP_STSR);
|
|
|
|
|
do {
|
|
|
|
|
sec0 = sec1;
|
|
|
|
|
/* Get the TSSS value */
|
|
|
|
|
ns = readl_relaxed(ioaddr + PTP_STNSR);
|
|
|
|
|
/* Get the TSS value */
|
|
|
|
|
sec1 = readl_relaxed(ioaddr + PTP_STSR);
|
|
|
|
|
} while (sec0 != sec1);
|
2013-03-26 12:43:11 +08:00
|
|
|
|
|
2018-04-16 23:08:15 +08:00
|
|
|
|
if (systime)
|
2022-02-04 00:00:25 +08:00
|
|
|
|
*systime = ns + (sec1 * 1000000000ULL);
|
2013-03-26 12:43:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 19:07:34 +08:00
|
|
|
|
static void get_ptptime(void __iomem *ptpaddr, u64 *ptp_time)
|
|
|
|
|
{
|
|
|
|
|
u64 ns;
|
|
|
|
|
|
|
|
|
|
ns = readl(ptpaddr + PTP_ATNR);
|
|
|
|
|
ns += readl(ptpaddr + PTP_ATSR) * NSEC_PER_SEC;
|
|
|
|
|
|
|
|
|
|
*ptp_time = ns;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 08:16:17 +08:00
|
|
|
|
static void timestamp_interrupt(struct stmmac_priv *priv)
|
|
|
|
|
{
|
|
|
|
|
u32 num_snapshot, ts_status, tsync_int;
|
|
|
|
|
struct ptp_clock_event event;
|
|
|
|
|
unsigned long flags;
|
|
|
|
|
u64 ptp_time;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
tsync_int = readl(priv->ioaddr + GMAC_INT_STATUS) & GMAC_INT_TSIE;
|
|
|
|
|
|
|
|
|
|
if (!tsync_int)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* Read timestamp status to clear interrupt from either external
|
|
|
|
|
* timestamp or start/end of PPS.
|
|
|
|
|
*/
|
|
|
|
|
ts_status = readl(priv->ioaddr + GMAC_TIMESTAMP_STATUS);
|
|
|
|
|
|
|
|
|
|
if (!priv->plat->ext_snapshot_en)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
num_snapshot = (ts_status & GMAC_TIMESTAMP_ATSNS_MASK) >>
|
|
|
|
|
GMAC_TIMESTAMP_ATSNS_SHIFT;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < num_snapshot; i++) {
|
2022-02-04 21:55:44 +08:00
|
|
|
|
read_lock_irqsave(&priv->ptp_lock, flags);
|
2021-04-14 08:16:17 +08:00
|
|
|
|
get_ptptime(priv->ptpaddr, &ptp_time);
|
2022-02-04 21:55:44 +08:00
|
|
|
|
read_unlock_irqrestore(&priv->ptp_lock, flags);
|
2021-04-14 08:16:17 +08:00
|
|
|
|
event.type = PTP_CLOCK_EXTTS;
|
|
|
|
|
event.index = 0;
|
|
|
|
|
event.timestamp = ptp_time;
|
|
|
|
|
ptp_clock_event(priv->ptp_clock, &event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-26 12:43:10 +08:00
|
|
|
|
const struct stmmac_hwtimestamp stmmac_ptp = {
|
2018-04-16 23:08:15 +08:00
|
|
|
|
.config_hw_tstamping = config_hw_tstamping,
|
|
|
|
|
.init_systime = init_systime,
|
|
|
|
|
.config_sub_second_increment = config_sub_second_increment,
|
|
|
|
|
.config_addend = config_addend,
|
|
|
|
|
.adjust_systime = adjust_systime,
|
|
|
|
|
.get_systime = get_systime,
|
2021-03-23 19:07:34 +08:00
|
|
|
|
.get_ptptime = get_ptptime,
|
2021-04-14 08:16:17 +08:00
|
|
|
|
.timestamp_interrupt = timestamp_interrupt,
|
2013-03-26 12:43:10 +08:00
|
|
|
|
};
|