1403f69bbSGerhard Engleder // SPDX-License-Identifier: GPL-2.0
2403f69bbSGerhard Engleder /* Copyright (C) 2021 Gerhard Engleder <gerhard@engleder-embedded.com> */
3403f69bbSGerhard Engleder
4403f69bbSGerhard Engleder #include "tsnep.h"
5403f69bbSGerhard Engleder
tsnep_get_system_time(struct tsnep_adapter * adapter,u64 * time)6403f69bbSGerhard Engleder void tsnep_get_system_time(struct tsnep_adapter *adapter, u64 *time)
7403f69bbSGerhard Engleder {
8403f69bbSGerhard Engleder u32 high_before;
9403f69bbSGerhard Engleder u32 low;
10403f69bbSGerhard Engleder u32 high;
11403f69bbSGerhard Engleder
12403f69bbSGerhard Engleder /* read high dword twice to detect overrun */
13403f69bbSGerhard Engleder high = ioread32(adapter->addr + ECM_SYSTEM_TIME_HIGH);
14403f69bbSGerhard Engleder do {
15403f69bbSGerhard Engleder low = ioread32(adapter->addr + ECM_SYSTEM_TIME_LOW);
16403f69bbSGerhard Engleder high_before = high;
17403f69bbSGerhard Engleder high = ioread32(adapter->addr + ECM_SYSTEM_TIME_HIGH);
18403f69bbSGerhard Engleder } while (high != high_before);
19403f69bbSGerhard Engleder *time = (((u64)high) << 32) | ((u64)low);
20403f69bbSGerhard Engleder }
21403f69bbSGerhard Engleder
tsnep_ptp_ioctl(struct net_device * netdev,struct ifreq * ifr,int cmd)22403f69bbSGerhard Engleder int tsnep_ptp_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
23403f69bbSGerhard Engleder {
24403f69bbSGerhard Engleder struct tsnep_adapter *adapter = netdev_priv(netdev);
25403f69bbSGerhard Engleder struct hwtstamp_config config;
26403f69bbSGerhard Engleder
27403f69bbSGerhard Engleder if (!ifr)
28403f69bbSGerhard Engleder return -EINVAL;
29403f69bbSGerhard Engleder
30403f69bbSGerhard Engleder if (cmd == SIOCSHWTSTAMP) {
31403f69bbSGerhard Engleder if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
32403f69bbSGerhard Engleder return -EFAULT;
33403f69bbSGerhard Engleder
34403f69bbSGerhard Engleder switch (config.tx_type) {
35403f69bbSGerhard Engleder case HWTSTAMP_TX_OFF:
36403f69bbSGerhard Engleder case HWTSTAMP_TX_ON:
37403f69bbSGerhard Engleder break;
38403f69bbSGerhard Engleder default:
39403f69bbSGerhard Engleder return -ERANGE;
40403f69bbSGerhard Engleder }
41403f69bbSGerhard Engleder
42403f69bbSGerhard Engleder switch (config.rx_filter) {
43403f69bbSGerhard Engleder case HWTSTAMP_FILTER_NONE:
44403f69bbSGerhard Engleder break;
45403f69bbSGerhard Engleder case HWTSTAMP_FILTER_ALL:
46403f69bbSGerhard Engleder case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
47403f69bbSGerhard Engleder case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
48403f69bbSGerhard Engleder case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
49403f69bbSGerhard Engleder case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
50403f69bbSGerhard Engleder case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
51403f69bbSGerhard Engleder case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
52403f69bbSGerhard Engleder case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
53403f69bbSGerhard Engleder case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
54403f69bbSGerhard Engleder case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
55403f69bbSGerhard Engleder case HWTSTAMP_FILTER_PTP_V2_EVENT:
56403f69bbSGerhard Engleder case HWTSTAMP_FILTER_PTP_V2_SYNC:
57403f69bbSGerhard Engleder case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
58403f69bbSGerhard Engleder case HWTSTAMP_FILTER_NTP_ALL:
59403f69bbSGerhard Engleder config.rx_filter = HWTSTAMP_FILTER_ALL;
60403f69bbSGerhard Engleder break;
61403f69bbSGerhard Engleder default:
62403f69bbSGerhard Engleder return -ERANGE;
63403f69bbSGerhard Engleder }
64403f69bbSGerhard Engleder
65403f69bbSGerhard Engleder memcpy(&adapter->hwtstamp_config, &config,
66403f69bbSGerhard Engleder sizeof(adapter->hwtstamp_config));
67403f69bbSGerhard Engleder }
68403f69bbSGerhard Engleder
69403f69bbSGerhard Engleder if (copy_to_user(ifr->ifr_data, &adapter->hwtstamp_config,
70403f69bbSGerhard Engleder sizeof(adapter->hwtstamp_config)))
71403f69bbSGerhard Engleder return -EFAULT;
72403f69bbSGerhard Engleder
73403f69bbSGerhard Engleder return 0;
74403f69bbSGerhard Engleder }
75403f69bbSGerhard Engleder
tsnep_ptp_adjfine(struct ptp_clock_info * ptp,long scaled_ppm)76403f69bbSGerhard Engleder static int tsnep_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
77403f69bbSGerhard Engleder {
78403f69bbSGerhard Engleder struct tsnep_adapter *adapter = container_of(ptp, struct tsnep_adapter,
79403f69bbSGerhard Engleder ptp_clock_info);
80403f69bbSGerhard Engleder bool negative = false;
81403f69bbSGerhard Engleder u64 rate_offset;
82403f69bbSGerhard Engleder
83403f69bbSGerhard Engleder if (scaled_ppm < 0) {
84403f69bbSGerhard Engleder scaled_ppm = -scaled_ppm;
85403f69bbSGerhard Engleder negative = true;
86403f69bbSGerhard Engleder }
87403f69bbSGerhard Engleder
88403f69bbSGerhard Engleder /* convert from 16 bit to 32 bit binary fractional, divide by 1000000 to
89403f69bbSGerhard Engleder * eliminate ppm, multiply with 8 to compensate 8ns clock cycle time,
90403f69bbSGerhard Engleder * simplify calculation because 15625 * 8 = 1000000 / 8
91403f69bbSGerhard Engleder */
92403f69bbSGerhard Engleder rate_offset = scaled_ppm;
93403f69bbSGerhard Engleder rate_offset <<= 16 - 3;
94403f69bbSGerhard Engleder rate_offset = div_u64(rate_offset, 15625);
95403f69bbSGerhard Engleder
96403f69bbSGerhard Engleder rate_offset &= ECM_CLOCK_RATE_OFFSET_MASK;
97403f69bbSGerhard Engleder if (negative)
98403f69bbSGerhard Engleder rate_offset |= ECM_CLOCK_RATE_OFFSET_SIGN;
99403f69bbSGerhard Engleder iowrite32(rate_offset & 0xFFFFFFFF, adapter->addr + ECM_CLOCK_RATE);
100403f69bbSGerhard Engleder
101403f69bbSGerhard Engleder return 0;
102403f69bbSGerhard Engleder }
103403f69bbSGerhard Engleder
tsnep_ptp_adjtime(struct ptp_clock_info * ptp,s64 delta)104403f69bbSGerhard Engleder static int tsnep_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
105403f69bbSGerhard Engleder {
106403f69bbSGerhard Engleder struct tsnep_adapter *adapter = container_of(ptp, struct tsnep_adapter,
107403f69bbSGerhard Engleder ptp_clock_info);
108403f69bbSGerhard Engleder u64 system_time;
109403f69bbSGerhard Engleder unsigned long flags;
110403f69bbSGerhard Engleder
111403f69bbSGerhard Engleder spin_lock_irqsave(&adapter->ptp_lock, flags);
112403f69bbSGerhard Engleder
113403f69bbSGerhard Engleder tsnep_get_system_time(adapter, &system_time);
114403f69bbSGerhard Engleder
115403f69bbSGerhard Engleder system_time += delta;
116403f69bbSGerhard Engleder
117403f69bbSGerhard Engleder /* high dword is buffered in hardware and synchronously written to
118403f69bbSGerhard Engleder * system time when low dword is written
119403f69bbSGerhard Engleder */
120403f69bbSGerhard Engleder iowrite32(system_time >> 32, adapter->addr + ECM_SYSTEM_TIME_HIGH);
121403f69bbSGerhard Engleder iowrite32(system_time & 0xFFFFFFFF,
122403f69bbSGerhard Engleder adapter->addr + ECM_SYSTEM_TIME_LOW);
123403f69bbSGerhard Engleder
124403f69bbSGerhard Engleder spin_unlock_irqrestore(&adapter->ptp_lock, flags);
125403f69bbSGerhard Engleder
126403f69bbSGerhard Engleder return 0;
127403f69bbSGerhard Engleder }
128403f69bbSGerhard Engleder
tsnep_ptp_gettimex64(struct ptp_clock_info * ptp,struct timespec64 * ts,struct ptp_system_timestamp * sts)129403f69bbSGerhard Engleder static int tsnep_ptp_gettimex64(struct ptp_clock_info *ptp,
130403f69bbSGerhard Engleder struct timespec64 *ts,
131403f69bbSGerhard Engleder struct ptp_system_timestamp *sts)
132403f69bbSGerhard Engleder {
133403f69bbSGerhard Engleder struct tsnep_adapter *adapter = container_of(ptp, struct tsnep_adapter,
134403f69bbSGerhard Engleder ptp_clock_info);
135403f69bbSGerhard Engleder u32 high_before;
136403f69bbSGerhard Engleder u32 low;
137403f69bbSGerhard Engleder u32 high;
138403f69bbSGerhard Engleder u64 system_time;
139403f69bbSGerhard Engleder
140403f69bbSGerhard Engleder /* read high dword twice to detect overrun */
141403f69bbSGerhard Engleder high = ioread32(adapter->addr + ECM_SYSTEM_TIME_HIGH);
142403f69bbSGerhard Engleder do {
143403f69bbSGerhard Engleder ptp_read_system_prets(sts);
144403f69bbSGerhard Engleder low = ioread32(adapter->addr + ECM_SYSTEM_TIME_LOW);
145403f69bbSGerhard Engleder ptp_read_system_postts(sts);
146403f69bbSGerhard Engleder high_before = high;
147403f69bbSGerhard Engleder high = ioread32(adapter->addr + ECM_SYSTEM_TIME_HIGH);
148403f69bbSGerhard Engleder } while (high != high_before);
149403f69bbSGerhard Engleder system_time = (((u64)high) << 32) | ((u64)low);
150403f69bbSGerhard Engleder
151403f69bbSGerhard Engleder *ts = ns_to_timespec64(system_time);
152403f69bbSGerhard Engleder
153403f69bbSGerhard Engleder return 0;
154403f69bbSGerhard Engleder }
155403f69bbSGerhard Engleder
tsnep_ptp_settime64(struct ptp_clock_info * ptp,const struct timespec64 * ts)156403f69bbSGerhard Engleder static int tsnep_ptp_settime64(struct ptp_clock_info *ptp,
157403f69bbSGerhard Engleder const struct timespec64 *ts)
158403f69bbSGerhard Engleder {
159403f69bbSGerhard Engleder struct tsnep_adapter *adapter = container_of(ptp, struct tsnep_adapter,
160403f69bbSGerhard Engleder ptp_clock_info);
161403f69bbSGerhard Engleder u64 system_time = timespec64_to_ns(ts);
162403f69bbSGerhard Engleder unsigned long flags;
163403f69bbSGerhard Engleder
164403f69bbSGerhard Engleder spin_lock_irqsave(&adapter->ptp_lock, flags);
165403f69bbSGerhard Engleder
166403f69bbSGerhard Engleder /* high dword is buffered in hardware and synchronously written to
167403f69bbSGerhard Engleder * system time when low dword is written
168403f69bbSGerhard Engleder */
169403f69bbSGerhard Engleder iowrite32(system_time >> 32, adapter->addr + ECM_SYSTEM_TIME_HIGH);
170403f69bbSGerhard Engleder iowrite32(system_time & 0xFFFFFFFF,
171403f69bbSGerhard Engleder adapter->addr + ECM_SYSTEM_TIME_LOW);
172403f69bbSGerhard Engleder
173403f69bbSGerhard Engleder spin_unlock_irqrestore(&adapter->ptp_lock, flags);
174403f69bbSGerhard Engleder
175403f69bbSGerhard Engleder return 0;
176403f69bbSGerhard Engleder }
177403f69bbSGerhard Engleder
tsnep_ptp_getcyclesx64(struct ptp_clock_info * ptp,struct timespec64 * ts,struct ptp_system_timestamp * sts)178*0abb62b6SGerhard Engleder static int tsnep_ptp_getcyclesx64(struct ptp_clock_info *ptp,
179*0abb62b6SGerhard Engleder struct timespec64 *ts,
180*0abb62b6SGerhard Engleder struct ptp_system_timestamp *sts)
181*0abb62b6SGerhard Engleder {
182*0abb62b6SGerhard Engleder struct tsnep_adapter *adapter = container_of(ptp, struct tsnep_adapter,
183*0abb62b6SGerhard Engleder ptp_clock_info);
184*0abb62b6SGerhard Engleder u32 high_before;
185*0abb62b6SGerhard Engleder u32 low;
186*0abb62b6SGerhard Engleder u32 high;
187*0abb62b6SGerhard Engleder u64 counter;
188*0abb62b6SGerhard Engleder
189*0abb62b6SGerhard Engleder /* read high dword twice to detect overrun */
190*0abb62b6SGerhard Engleder high = ioread32(adapter->addr + ECM_COUNTER_HIGH);
191*0abb62b6SGerhard Engleder do {
192*0abb62b6SGerhard Engleder ptp_read_system_prets(sts);
193*0abb62b6SGerhard Engleder low = ioread32(adapter->addr + ECM_COUNTER_LOW);
194*0abb62b6SGerhard Engleder ptp_read_system_postts(sts);
195*0abb62b6SGerhard Engleder high_before = high;
196*0abb62b6SGerhard Engleder high = ioread32(adapter->addr + ECM_COUNTER_HIGH);
197*0abb62b6SGerhard Engleder } while (high != high_before);
198*0abb62b6SGerhard Engleder counter = (((u64)high) << 32) | ((u64)low);
199*0abb62b6SGerhard Engleder
200*0abb62b6SGerhard Engleder *ts = ns_to_timespec64(counter);
201*0abb62b6SGerhard Engleder
202*0abb62b6SGerhard Engleder return 0;
203*0abb62b6SGerhard Engleder }
204*0abb62b6SGerhard Engleder
tsnep_ptp_init(struct tsnep_adapter * adapter)205403f69bbSGerhard Engleder int tsnep_ptp_init(struct tsnep_adapter *adapter)
206403f69bbSGerhard Engleder {
207403f69bbSGerhard Engleder int retval = 0;
208403f69bbSGerhard Engleder
209403f69bbSGerhard Engleder adapter->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
210403f69bbSGerhard Engleder adapter->hwtstamp_config.tx_type = HWTSTAMP_TX_OFF;
211403f69bbSGerhard Engleder
212403f69bbSGerhard Engleder snprintf(adapter->ptp_clock_info.name, 16, "%s", TSNEP);
213403f69bbSGerhard Engleder adapter->ptp_clock_info.owner = THIS_MODULE;
214403f69bbSGerhard Engleder /* at most 2^-1ns adjustment every clock cycle for 8ns clock cycle time,
215403f69bbSGerhard Engleder * stay slightly below because only bits below 2^-1ns are supported
216403f69bbSGerhard Engleder */
217403f69bbSGerhard Engleder adapter->ptp_clock_info.max_adj = (500000000 / 8 - 1);
218403f69bbSGerhard Engleder adapter->ptp_clock_info.adjfine = tsnep_ptp_adjfine;
219403f69bbSGerhard Engleder adapter->ptp_clock_info.adjtime = tsnep_ptp_adjtime;
220403f69bbSGerhard Engleder adapter->ptp_clock_info.gettimex64 = tsnep_ptp_gettimex64;
221403f69bbSGerhard Engleder adapter->ptp_clock_info.settime64 = tsnep_ptp_settime64;
222*0abb62b6SGerhard Engleder adapter->ptp_clock_info.getcyclesx64 = tsnep_ptp_getcyclesx64;
223403f69bbSGerhard Engleder
224403f69bbSGerhard Engleder spin_lock_init(&adapter->ptp_lock);
225403f69bbSGerhard Engleder
226403f69bbSGerhard Engleder adapter->ptp_clock = ptp_clock_register(&adapter->ptp_clock_info,
227403f69bbSGerhard Engleder &adapter->pdev->dev);
228403f69bbSGerhard Engleder if (IS_ERR(adapter->ptp_clock)) {
229403f69bbSGerhard Engleder netdev_err(adapter->netdev, "ptp_clock_register failed\n");
230403f69bbSGerhard Engleder
231403f69bbSGerhard Engleder retval = PTR_ERR(adapter->ptp_clock);
232403f69bbSGerhard Engleder adapter->ptp_clock = NULL;
233403f69bbSGerhard Engleder } else if (adapter->ptp_clock) {
234403f69bbSGerhard Engleder netdev_info(adapter->netdev, "PHC added\n");
235403f69bbSGerhard Engleder }
236403f69bbSGerhard Engleder
237403f69bbSGerhard Engleder return retval;
238403f69bbSGerhard Engleder }
239403f69bbSGerhard Engleder
tsnep_ptp_cleanup(struct tsnep_adapter * adapter)240403f69bbSGerhard Engleder void tsnep_ptp_cleanup(struct tsnep_adapter *adapter)
241403f69bbSGerhard Engleder {
242403f69bbSGerhard Engleder if (adapter->ptp_clock) {
243403f69bbSGerhard Engleder ptp_clock_unregister(adapter->ptp_clock);
244403f69bbSGerhard Engleder netdev_info(adapter->netdev, "PHC removed\n");
245403f69bbSGerhard Engleder }
246403f69bbSGerhard Engleder }
247