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.
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-14 11:32:01 +08:00
|
|
|
|
static u32 stmmac_config_sub_second_increment(void __iomem *ioaddr,
|
2016-11-14 16:27:29 +08:00
|
|
|
|
u32 ptp_clock, int gmac4)
|
2013-03-26 12:43:10 +08:00
|
|
|
|
{
|
|
|
|
|
u32 value = readl(ioaddr + PTP_TCR);
|
|
|
|
|
unsigned long data;
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
if (gmac4)
|
|
|
|
|
data = data << GMAC4_PTP_SSIR_SSINC_SHIFT;
|
|
|
|
|
|
2013-03-26 12:43:10 +08:00
|
|
|
|
writel(data, ioaddr + PTP_SSIR);
|
2015-12-14 11:32:01 +08:00
|
|
|
|
|
|
|
|
|
return data;
|
2013-03-26 12:43:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-26 12:43:11 +08:00
|
|
|
|
static int stmmac_adjust_systime(void __iomem *ioaddr, u32 sec, u32 nsec,
|
2016-11-14 16:27:29 +08:00
|
|
|
|
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)
|
|
|
|
|
sec = (100000000ULL - sec);
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static u64 stmmac_get_systime(void __iomem *ioaddr)
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
return ns;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-26 12:43:10 +08:00
|
|
|
|
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,
|
2013-03-26 12:43:11 +08:00
|
|
|
|
.adjust_systime = stmmac_adjust_systime,
|
|
|
|
|
.get_systime = stmmac_get_systime,
|
2013-03-26 12:43:10 +08:00
|
|
|
|
};
|