GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / net / ethernet / stmicro / stmmac / stmmac_hwtstamp.c
1 /*******************************************************************************
2   Copyright (C) 2013  Vayavya Labs Pvt Ltd
3
4   This implements all the API for managing HW timestamp & PTP.
5
6   This program is free software; you can redistribute it and/or modify it
7   under the terms and conditions of the GNU General Public License,
8   version 2, as published by the Free Software Foundation.
9
10   This program is distributed in the hope it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13   more details.
14
15   The full GNU General Public License is included in this distribution in
16   the file called "COPYING".
17
18   Author: Rayagond Kokatanur <rayagond@vayavyalabs.com>
19   Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
20 *******************************************************************************/
21
22 #include <linux/io.h>
23 #include <linux/delay.h>
24 #include "common.h"
25 #include "stmmac_ptp.h"
26
27 static void stmmac_config_hw_tstamping(void __iomem *ioaddr, u32 data)
28 {
29         writel(data, ioaddr + PTP_TCR);
30 }
31
32 static u32 stmmac_config_sub_second_increment(void __iomem *ioaddr,
33                                               u32 ptp_clock, int gmac4)
34 {
35         u32 value = readl(ioaddr + PTP_TCR);
36         unsigned long data;
37         u32 reg_value;
38
39         /* For GMAC3.x, 4.x versions, in "fine adjustement mode" set sub-second
40          * increment to twice the number of nanoseconds of a clock cycle.
41          * The calculation of the default_addend value by the caller will set it
42          * to mid-range = 2^31 when the remainder of this division is zero,
43          * which will make the accumulator overflow once every 2 ptp_clock
44          * cycles, adding twice the number of nanoseconds of a clock cycle :
45          * 2000000000ULL / ptp_clock.
46          */
47         if (value & PTP_TCR_TSCFUPDT)
48                 data = (2000000000ULL / ptp_clock);
49         else
50                 data = (1000000000ULL / ptp_clock);
51
52         /* 0.465ns accuracy */
53         if (!(value & PTP_TCR_TSCTRLSSR))
54                 data = (data * 1000) / 465;
55
56         data &= PTP_SSIR_SSINC_MASK;
57
58         reg_value = data;
59         if (gmac4)
60                 reg_value <<= GMAC4_PTP_SSIR_SSINC_SHIFT;
61
62         writel(reg_value, ioaddr + PTP_SSIR);
63
64         return data;
65 }
66
67 static int stmmac_init_systime(void __iomem *ioaddr, u32 sec, u32 nsec)
68 {
69         int limit;
70         u32 value;
71
72         writel(sec, ioaddr + PTP_STSUR);
73         writel(nsec, ioaddr + PTP_STNSUR);
74         /* issue command to initialize the system time value */
75         value = readl(ioaddr + PTP_TCR);
76         value |= PTP_TCR_TSINIT;
77         writel(value, ioaddr + PTP_TCR);
78
79         /* wait for present system time initialize to complete */
80         limit = 10;
81         while (limit--) {
82                 if (!(readl(ioaddr + PTP_TCR) & PTP_TCR_TSINIT))
83                         break;
84                 mdelay(10);
85         }
86         if (limit < 0)
87                 return -EBUSY;
88
89         return 0;
90 }
91
92 static int stmmac_config_addend(void __iomem *ioaddr, u32 addend)
93 {
94         u32 value;
95         int limit;
96
97         writel(addend, ioaddr + PTP_TAR);
98         /* issue command to update the addend value */
99         value = readl(ioaddr + PTP_TCR);
100         value |= PTP_TCR_TSADDREG;
101         writel(value, ioaddr + PTP_TCR);
102
103         /* wait for present addend update to complete */
104         limit = 10;
105         while (limit--) {
106                 if (!(readl(ioaddr + PTP_TCR) & PTP_TCR_TSADDREG))
107                         break;
108                 mdelay(10);
109         }
110         if (limit < 0)
111                 return -EBUSY;
112
113         return 0;
114 }
115
116 static int stmmac_adjust_systime(void __iomem *ioaddr, u32 sec, u32 nsec,
117                                  int add_sub, int gmac4)
118 {
119         u32 value;
120         int limit;
121
122         if (add_sub) {
123                 /* If the new sec value needs to be subtracted with
124                  * the system time, then MAC_STSUR reg should be
125                  * programmed with (2^32 – <new_sec_value>)
126                  */
127                 if (gmac4)
128                         sec = -sec;
129
130                 value = readl(ioaddr + PTP_TCR);
131                 if (value & PTP_TCR_TSCTRLSSR)
132                         nsec = (PTP_DIGITAL_ROLLOVER_MODE - nsec);
133                 else
134                         nsec = (PTP_BINARY_ROLLOVER_MODE - nsec);
135         }
136
137         writel(sec, ioaddr + PTP_STSUR);
138         value = (add_sub << PTP_STNSUR_ADDSUB_SHIFT) | nsec;
139         writel(value, ioaddr + PTP_STNSUR);
140
141         /* issue command to initialize the system time value */
142         value = readl(ioaddr + PTP_TCR);
143         value |= PTP_TCR_TSUPDT;
144         writel(value, ioaddr + PTP_TCR);
145
146         /* wait for present system time adjust/update to complete */
147         limit = 10;
148         while (limit--) {
149                 if (!(readl(ioaddr + PTP_TCR) & PTP_TCR_TSUPDT))
150                         break;
151                 mdelay(10);
152         }
153         if (limit < 0)
154                 return -EBUSY;
155
156         return 0;
157 }
158
159 static u64 stmmac_get_systime(void __iomem *ioaddr)
160 {
161         u64 ns;
162
163         /* Get the TSSS value */
164         ns = readl(ioaddr + PTP_STNSR);
165         /* Get the TSS and convert sec time value to nanosecond */
166         ns += readl(ioaddr + PTP_STSR) * 1000000000ULL;
167
168         return ns;
169 }
170
171 const struct stmmac_hwtimestamp stmmac_ptp = {
172         .config_hw_tstamping = stmmac_config_hw_tstamping,
173         .init_systime = stmmac_init_systime,
174         .config_sub_second_increment = stmmac_config_sub_second_increment,
175         .config_addend = stmmac_config_addend,
176         .adjust_systime = stmmac_adjust_systime,
177         .get_systime = stmmac_get_systime,
178 };