1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/string.h>
25#include <linux/ptrace.h>
26#include <linux/errno.h>
27#include <linux/ioport.h>
28#include <linux/slab.h>
29#include <linux/interrupt.h>
30#include <linux/pci.h>
31#include <linux/init.h>
32#include <linux/delay.h>
33#include <linux/netdevice.h>
34#include <linux/etherdevice.h>
35#include <linux/skbuff.h>
36#include <linux/spinlock.h>
37#include <linux/workqueue.h>
38#include <linux/bitops.h>
39#include <linux/io.h>
40#include <linux/irq.h>
41#include <linux/clk.h>
42#include <linux/platform_device.h>
43#include <linux/phy.h>
44#include <linux/fec.h>
45#include <linux/of.h>
46#include <linux/of_device.h>
47#include <linux/of_gpio.h>
48#include <linux/of_net.h>
49
50#include "fec.h"
51
52
53#define FEC_T_CTRL_SLAVE 0x00002000
54#define FEC_T_CTRL_CAPTURE 0x00000800
55#define FEC_T_CTRL_RESTART 0x00000200
56#define FEC_T_CTRL_PERIOD_RST 0x00000030
57#define FEC_T_CTRL_PERIOD_EN 0x00000010
58#define FEC_T_CTRL_ENABLE 0x00000001
59
60#define FEC_T_INC_MASK 0x0000007f
61#define FEC_T_INC_OFFSET 0
62#define FEC_T_INC_CORR_MASK 0x00007f00
63#define FEC_T_INC_CORR_OFFSET 8
64
65#define FEC_ATIME_CTRL 0x400
66#define FEC_ATIME 0x404
67#define FEC_ATIME_EVT_OFFSET 0x408
68#define FEC_ATIME_EVT_PERIOD 0x40c
69#define FEC_ATIME_CORR 0x410
70#define FEC_ATIME_INC 0x414
71#define FEC_TS_TIMESTAMP 0x418
72
73#define FEC_CC_MULT (1 << 31)
74
75
76
77
78
79
80
81
82static u64 fec_ptp_read(const struct cyclecounter *cc)
83{
84 struct fec_enet_private *fep =
85 container_of(cc, struct fec_enet_private, cc);
86 u32 tempval;
87
88 tempval = readl(fep->hwp + FEC_ATIME_CTRL);
89 tempval |= FEC_T_CTRL_CAPTURE;
90 writel(tempval, fep->hwp + FEC_ATIME_CTRL);
91
92 return readl(fep->hwp + FEC_ATIME);
93}
94
95
96
97
98
99
100
101
102
103void fec_ptp_start_cyclecounter(struct net_device *ndev)
104{
105 struct fec_enet_private *fep = netdev_priv(ndev);
106 unsigned long flags;
107 int inc;
108
109 inc = 1000000000 / fep->cycle_speed;
110
111
112 spin_lock_irqsave(&fep->tmreg_lock, flags);
113
114
115 writel(inc << FEC_T_INC_OFFSET, fep->hwp + FEC_ATIME_INC);
116
117
118 writel(0, fep->hwp + FEC_ATIME_EVT_PERIOD);
119
120 writel(FEC_T_CTRL_ENABLE, fep->hwp + FEC_ATIME_CTRL);
121
122 memset(&fep->cc, 0, sizeof(fep->cc));
123 fep->cc.read = fec_ptp_read;
124 fep->cc.mask = CLOCKSOURCE_MASK(32);
125 fep->cc.shift = 31;
126 fep->cc.mult = FEC_CC_MULT;
127
128
129 timecounter_init(&fep->tc, &fep->cc, ktime_to_ns(ktime_get_real()));
130
131 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
132}
133
134
135
136
137
138
139
140
141
142
143
144
145static int fec_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
146{
147 u64 diff;
148 unsigned long flags;
149 int neg_adj = 0;
150 u32 mult = FEC_CC_MULT;
151
152 struct fec_enet_private *fep =
153 container_of(ptp, struct fec_enet_private, ptp_caps);
154
155 if (ppb < 0) {
156 ppb = -ppb;
157 neg_adj = 1;
158 }
159
160 diff = mult;
161 diff *= ppb;
162 diff = div_u64(diff, 1000000000ULL);
163
164 spin_lock_irqsave(&fep->tmreg_lock, flags);
165
166
167
168
169
170 timecounter_read(&fep->tc);
171
172 fep->cc.mult = neg_adj ? mult - diff : mult + diff;
173
174 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
175
176 return 0;
177}
178
179
180
181
182
183
184
185
186static int fec_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
187{
188 struct fec_enet_private *fep =
189 container_of(ptp, struct fec_enet_private, ptp_caps);
190 unsigned long flags;
191 u64 now;
192
193 spin_lock_irqsave(&fep->tmreg_lock, flags);
194
195 now = timecounter_read(&fep->tc);
196 now += delta;
197
198
199 timecounter_init(&fep->tc, &fep->cc, now);
200
201 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
202
203 return 0;
204}
205
206
207
208
209
210
211
212
213
214static int fec_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
215{
216 struct fec_enet_private *adapter =
217 container_of(ptp, struct fec_enet_private, ptp_caps);
218 u64 ns;
219 u32 remainder;
220 unsigned long flags;
221
222 spin_lock_irqsave(&adapter->tmreg_lock, flags);
223 ns = timecounter_read(&adapter->tc);
224 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
225
226 ts->tv_sec = div_u64_rem(ns, 1000000000ULL, &remainder);
227 ts->tv_nsec = remainder;
228
229 return 0;
230}
231
232
233
234
235
236
237
238
239
240static int fec_ptp_settime(struct ptp_clock_info *ptp,
241 const struct timespec *ts)
242{
243 struct fec_enet_private *fep =
244 container_of(ptp, struct fec_enet_private, ptp_caps);
245
246 u64 ns;
247 unsigned long flags;
248
249 ns = ts->tv_sec * 1000000000ULL;
250 ns += ts->tv_nsec;
251
252 spin_lock_irqsave(&fep->tmreg_lock, flags);
253 timecounter_init(&fep->tc, &fep->cc, ns);
254 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
255 return 0;
256}
257
258
259
260
261
262
263
264
265static int fec_ptp_enable(struct ptp_clock_info *ptp,
266 struct ptp_clock_request *rq, int on)
267{
268 return -EOPNOTSUPP;
269}
270
271
272
273
274
275
276
277int fec_ptp_ioctl(struct net_device *ndev, struct ifreq *ifr, int cmd)
278{
279 struct fec_enet_private *fep = netdev_priv(ndev);
280
281 struct hwtstamp_config config;
282
283 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
284 return -EFAULT;
285
286
287 if (config.flags)
288 return -EINVAL;
289
290 switch (config.tx_type) {
291 case HWTSTAMP_TX_OFF:
292 fep->hwts_tx_en = 0;
293 break;
294 case HWTSTAMP_TX_ON:
295 fep->hwts_tx_en = 1;
296 break;
297 default:
298 return -ERANGE;
299 }
300
301 switch (config.rx_filter) {
302 case HWTSTAMP_FILTER_NONE:
303 if (fep->hwts_rx_en)
304 fep->hwts_rx_en = 0;
305 config.rx_filter = HWTSTAMP_FILTER_NONE;
306 break;
307
308 default:
309
310
311
312
313
314
315 fep->hwts_rx_en = 1;
316 config.rx_filter = HWTSTAMP_FILTER_ALL;
317 break;
318 }
319
320 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
321 -EFAULT : 0;
322}
323
324
325
326
327
328static void fec_time_keep(unsigned long _data)
329{
330 struct fec_enet_private *fep = (struct fec_enet_private *)_data;
331 u64 ns;
332 unsigned long flags;
333
334 spin_lock_irqsave(&fep->tmreg_lock, flags);
335 ns = timecounter_read(&fep->tc);
336 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
337
338 mod_timer(&fep->time_keep, jiffies + HZ);
339}
340
341
342
343
344
345
346
347
348
349
350void fec_ptp_init(struct net_device *ndev, struct platform_device *pdev)
351{
352 struct fec_enet_private *fep = netdev_priv(ndev);
353
354 fep->ptp_caps.owner = THIS_MODULE;
355 snprintf(fep->ptp_caps.name, 16, "fec ptp");
356
357 fep->ptp_caps.max_adj = 250000000;
358 fep->ptp_caps.n_alarm = 0;
359 fep->ptp_caps.n_ext_ts = 0;
360 fep->ptp_caps.n_per_out = 0;
361 fep->ptp_caps.pps = 0;
362 fep->ptp_caps.adjfreq = fec_ptp_adjfreq;
363 fep->ptp_caps.adjtime = fec_ptp_adjtime;
364 fep->ptp_caps.gettime = fec_ptp_gettime;
365 fep->ptp_caps.settime = fec_ptp_settime;
366 fep->ptp_caps.enable = fec_ptp_enable;
367
368 fep->cycle_speed = clk_get_rate(fep->clk_ptp);
369
370 spin_lock_init(&fep->tmreg_lock);
371
372 fec_ptp_start_cyclecounter(ndev);
373
374 init_timer(&fep->time_keep);
375 fep->time_keep.data = (unsigned long)fep;
376 fep->time_keep.function = fec_time_keep;
377 fep->time_keep.expires = jiffies + HZ;
378 add_timer(&fep->time_keep);
379
380 fep->ptp_clock = ptp_clock_register(&fep->ptp_caps, &pdev->dev);
381 if (IS_ERR(fep->ptp_clock)) {
382 fep->ptp_clock = NULL;
383 pr_err("ptp_clock_register failed\n");
384 }
385}
386