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>
|
|
|
|
|
#include "common.h"
|
|
|
|
|
#include "stmmac_ptp.h"
|
|
|
|
|
|
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
|
|
|
|
|
2016-11-14 16:27:29 +08:00
|
|
|
|
/* For GMAC3.x, 4.x versions, convert the ptp_clock to nano second
|
|
|
|
|
* formula = (1/ptp_clock) * 1000000000
|
|
|
|
|
* where ptp_clock is 50MHz if fine method is used to update system
|
2013-03-26 12:43:10 +08:00
|
|
|
|
*/
|
2016-11-14 16:27:29 +08:00
|
|
|
|
if (value & PTP_TCR_TSCFUPDT)
|
|
|
|
|
data = (1000000000ULL / 50000000);
|
|
|
|
|
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 */
|
2020-03-16 10:32:53 +08:00
|
|
|
|
return readl_poll_timeout(ioaddr + PTP_TCR, value,
|
|
|
|
|
!(value & PTP_TCR_TSINIT),
|
|
|
|
|
10000, 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
|
|
|
|
{
|
|
|
|
|
u64 ns;
|
|
|
|
|
|
2016-11-14 16:27:29 +08:00
|
|
|
|
/* Get the TSSS value */
|
2013-03-26 12:43:11 +08:00
|
|
|
|
ns = readl(ioaddr + PTP_STNSR);
|
2016-11-14 16:27:29 +08:00
|
|
|
|
/* Get the TSS and convert sec time value to nanosecond */
|
2013-03-26 12:43:11 +08:00
|
|
|
|
ns += readl(ioaddr + PTP_STSR) * 1000000000ULL;
|
|
|
|
|
|
2018-04-16 23:08:15 +08:00
|
|
|
|
if (systime)
|
|
|
|
|
*systime = ns;
|
2013-03-26 12:43:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
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,
|
2013-03-26 12:43:10 +08:00
|
|
|
|
};
|