[ Upstream commit 91a2559c1dc5b0f7e1256d42b1508935e8eabfbf ]
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>
Signed-off-by: Sasha Levin <sashal@kernel.org>
179 lines
4.6 KiB
C
179 lines
4.6 KiB
C
/*******************************************************************************
|
||
Copyright (C) 2013 Vayavya Labs Pvt Ltd
|
||
|
||
This implements all the API for managing HW timestamp & PTP.
|
||
|
||
This program is free software; you can redistribute it and/or modify it
|
||
under the terms and conditions of the GNU General Public License,
|
||
version 2, as published by the Free Software Foundation.
|
||
|
||
This program is distributed in the hope it will be useful, but WITHOUT
|
||
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||
more details.
|
||
|
||
The full GNU General Public License is included in this distribution in
|
||
the file called "COPYING".
|
||
|
||
Author: Rayagond Kokatanur <rayagond@vayavyalabs.com>
|
||
Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
|
||
*******************************************************************************/
|
||
|
||
#include <linux/io.h>
|
||
#include <linux/delay.h>
|
||
#include "common.h"
|
||
#include "stmmac_ptp.h"
|
||
|
||
static void stmmac_config_hw_tstamping(void __iomem *ioaddr, u32 data)
|
||
{
|
||
writel(data, ioaddr + PTP_TCR);
|
||
}
|
||
|
||
static u32 stmmac_config_sub_second_increment(void __iomem *ioaddr,
|
||
u32 ptp_clock, int gmac4)
|
||
{
|
||
u32 value = readl(ioaddr + PTP_TCR);
|
||
unsigned long data;
|
||
u32 reg_value;
|
||
|
||
/* 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.
|
||
*/
|
||
if (value & PTP_TCR_TSCFUPDT)
|
||
data = (2000000000ULL / ptp_clock);
|
||
else
|
||
data = (1000000000ULL / ptp_clock);
|
||
|
||
/* 0.465ns accuracy */
|
||
if (!(value & PTP_TCR_TSCTRLSSR))
|
||
data = (data * 1000) / 465;
|
||
|
||
data &= PTP_SSIR_SSINC_MASK;
|
||
|
||
reg_value = data;
|
||
if (gmac4)
|
||
reg_value <<= GMAC4_PTP_SSIR_SSINC_SHIFT;
|
||
|
||
writel(reg_value, ioaddr + PTP_SSIR);
|
||
|
||
return data;
|
||
}
|
||
|
||
static int stmmac_init_systime(void __iomem *ioaddr, u32 sec, u32 nsec)
|
||
{
|
||
int limit;
|
||
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 */
|
||
limit = 10;
|
||
while (limit--) {
|
||
if (!(readl(ioaddr + PTP_TCR) & PTP_TCR_TSINIT))
|
||
break;
|
||
mdelay(10);
|
||
}
|
||
if (limit < 0)
|
||
return -EBUSY;
|
||
|
||
return 0;
|
||
}
|
||
|
||
static int stmmac_config_addend(void __iomem *ioaddr, u32 addend)
|
||
{
|
||
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;
|
||
}
|
||
|
||
static int stmmac_adjust_systime(void __iomem *ioaddr, u32 sec, u32 nsec,
|
||
int add_sub, int gmac4)
|
||
{
|
||
u32 value;
|
||
int limit;
|
||
|
||
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)
|
||
sec = -sec;
|
||
|
||
value = readl(ioaddr + PTP_TCR);
|
||
if (value & PTP_TCR_TSCTRLSSR)
|
||
nsec = (PTP_DIGITAL_ROLLOVER_MODE - nsec);
|
||
else
|
||
nsec = (PTP_BINARY_ROLLOVER_MODE - nsec);
|
||
}
|
||
|
||
writel(sec, ioaddr + PTP_STSUR);
|
||
value = (add_sub << PTP_STNSUR_ADDSUB_SHIFT) | nsec;
|
||
writel(value, ioaddr + PTP_STNSUR);
|
||
|
||
/* 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;
|
||
}
|
||
|
||
static u64 stmmac_get_systime(void __iomem *ioaddr)
|
||
{
|
||
u64 ns;
|
||
|
||
/* Get the TSSS value */
|
||
ns = readl(ioaddr + PTP_STNSR);
|
||
/* Get the TSS and convert sec time value to nanosecond */
|
||
ns += readl(ioaddr + PTP_STSR) * 1000000000ULL;
|
||
|
||
return ns;
|
||
}
|
||
|
||
const struct stmmac_hwtimestamp stmmac_ptp = {
|
||
.config_hw_tstamping = stmmac_config_hw_tstamping,
|
||
.init_systime = stmmac_init_systime,
|
||
.config_sub_second_increment = stmmac_config_sub_second_increment,
|
||
.config_addend = stmmac_config_addend,
|
||
.adjust_systime = stmmac_adjust_systime,
|
||
.get_systime = stmmac_get_systime,
|
||
};
|