blob: f5ee460d5be93fd4fa8bbdf64742c886943067aa [file] [log] [blame]
Frank Li6605b732012-10-30 18:25:31 +00001/*
2 * Fast Ethernet Controller (ENET) PTP driver for MX6x.
3 *
4 * Copyright (C) 2012 Freescale Semiconductor, Inc.
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 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
Joe Perches31b77202013-04-13 19:03:17 +000020#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
Frank Li6605b732012-10-30 18:25:31 +000022#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>
Frank Li6605b732012-10-30 18:25:31 +000031#include <linux/delay.h>
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/skbuff.h>
35#include <linux/spinlock.h>
36#include <linux/workqueue.h>
37#include <linux/bitops.h>
38#include <linux/io.h>
39#include <linux/irq.h>
40#include <linux/clk.h>
41#include <linux/platform_device.h>
42#include <linux/phy.h>
43#include <linux/fec.h>
44#include <linux/of.h>
45#include <linux/of_device.h>
46#include <linux/of_gpio.h>
47#include <linux/of_net.h>
48
49#include "fec.h"
50
51/* FEC 1588 register bits */
52#define FEC_T_CTRL_SLAVE 0x00002000
53#define FEC_T_CTRL_CAPTURE 0x00000800
54#define FEC_T_CTRL_RESTART 0x00000200
55#define FEC_T_CTRL_PERIOD_RST 0x00000030
56#define FEC_T_CTRL_PERIOD_EN 0x00000010
57#define FEC_T_CTRL_ENABLE 0x00000001
58
59#define FEC_T_INC_MASK 0x0000007f
60#define FEC_T_INC_OFFSET 0
61#define FEC_T_INC_CORR_MASK 0x00007f00
62#define FEC_T_INC_CORR_OFFSET 8
63
64#define FEC_ATIME_CTRL 0x400
65#define FEC_ATIME 0x404
66#define FEC_ATIME_EVT_OFFSET 0x408
67#define FEC_ATIME_EVT_PERIOD 0x40c
68#define FEC_ATIME_CORR 0x410
69#define FEC_ATIME_INC 0x414
70#define FEC_TS_TIMESTAMP 0x418
71
72#define FEC_CC_MULT (1 << 31)
Luwei Zhouf28460b22014-10-10 13:15:28 +080073#define FEC_COUNTER_PERIOD (1 << 31)
Frank Li6605b732012-10-30 18:25:31 +000074/**
75 * fec_ptp_read - read raw cycle counter (to be used by time counter)
76 * @cc: the cyclecounter structure
77 *
78 * this function reads the cyclecounter registers and is called by the
79 * cyclecounter structure used to construct a ns counter from the
80 * arbitrary fixed point registers
81 */
82static cycle_t 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 * fec_ptp_start_cyclecounter - create the cycle counter from hw
97 * @ndev: network device
98 *
99 * this function initializes the timecounter and cyclecounter
100 * structures for use in generated a ns counter from the arbitrary
101 * fixed point cycles registers in the hardware.
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
Frank Li85bd1792013-02-06 14:59:59 +0000109 inc = 1000000000 / fep->cycle_speed;
Frank Li6605b732012-10-30 18:25:31 +0000110
111 /* grab the ptp lock */
112 spin_lock_irqsave(&fep->tmreg_lock, flags);
113
114 /* 1ns counter */
115 writel(inc << FEC_T_INC_OFFSET, fep->hwp + FEC_ATIME_INC);
116
Luwei Zhouf28460b22014-10-10 13:15:28 +0800117 /* use 31-bit timer counter */
118 writel(FEC_COUNTER_PERIOD, fep->hwp + FEC_ATIME_EVT_PERIOD);
Frank Li6605b732012-10-30 18:25:31 +0000119
Luwei Zhouf28460b22014-10-10 13:15:28 +0800120 writel(FEC_T_CTRL_ENABLE | FEC_T_CTRL_PERIOD_RST,
121 fep->hwp + FEC_ATIME_CTRL);
Frank Li6605b732012-10-30 18:25:31 +0000122
123 memset(&fep->cc, 0, sizeof(fep->cc));
124 fep->cc.read = fec_ptp_read;
Luwei Zhouf28460b22014-10-10 13:15:28 +0800125 fep->cc.mask = CLOCKSOURCE_MASK(31);
Frank Li6605b732012-10-30 18:25:31 +0000126 fep->cc.shift = 31;
127 fep->cc.mult = FEC_CC_MULT;
128
129 /* reset the ns time counter */
130 timecounter_init(&fep->tc, &fep->cc, ktime_to_ns(ktime_get_real()));
131
132 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
133}
134
135/**
136 * fec_ptp_adjfreq - adjust ptp cycle frequency
137 * @ptp: the ptp clock structure
138 * @ppb: parts per billion adjustment from base
139 *
140 * Adjust the frequency of the ptp cycle counter by the
141 * indicated ppb from the base frequency.
142 *
143 * Because ENET hardware frequency adjust is complex,
144 * using software method to do that.
145 */
146static int fec_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
147{
Frank Li6605b732012-10-30 18:25:31 +0000148 unsigned long flags;
149 int neg_adj = 0;
Luwei Zhou89bddcd2014-10-10 13:15:29 +0800150 u32 i, tmp;
151 u32 corr_inc, corr_period;
152 u32 corr_ns;
153 u64 lhs, rhs;
Frank Li6605b732012-10-30 18:25:31 +0000154
155 struct fec_enet_private *fep =
156 container_of(ptp, struct fec_enet_private, ptp_caps);
157
Luwei Zhou89bddcd2014-10-10 13:15:29 +0800158 if (ppb == 0)
159 return 0;
160
Frank Li6605b732012-10-30 18:25:31 +0000161 if (ppb < 0) {
162 ppb = -ppb;
163 neg_adj = 1;
164 }
165
Luwei Zhou89bddcd2014-10-10 13:15:29 +0800166 /* In theory, corr_inc/corr_period = ppb/NSEC_PER_SEC;
167 * Try to find the corr_inc between 1 to fep->ptp_inc to
168 * meet adjustment requirement.
169 */
170 lhs = NSEC_PER_SEC;
171 rhs = (u64)ppb * (u64)fep->ptp_inc;
172 for (i = 1; i <= fep->ptp_inc; i++) {
173 if (lhs >= rhs) {
174 corr_inc = i;
175 corr_period = div_u64(lhs, rhs);
176 break;
177 }
178 lhs += NSEC_PER_SEC;
179 }
180 /* Not found? Set it to high value - double speed
181 * correct in every clock step.
182 */
183 if (i > fep->ptp_inc) {
184 corr_inc = fep->ptp_inc;
185 corr_period = 1;
186 }
187
188 if (neg_adj)
189 corr_ns = fep->ptp_inc - corr_inc;
190 else
191 corr_ns = fep->ptp_inc + corr_inc;
Frank Li7da716a2012-11-06 20:14:49 +0000192
Frank Li6605b732012-10-30 18:25:31 +0000193 spin_lock_irqsave(&fep->tmreg_lock, flags);
Frank Li6605b732012-10-30 18:25:31 +0000194
Luwei Zhou89bddcd2014-10-10 13:15:29 +0800195 tmp = readl(fep->hwp + FEC_ATIME_INC) & FEC_T_INC_MASK;
196 tmp |= corr_ns << FEC_T_INC_CORR_OFFSET;
197 writel(tmp, fep->hwp + FEC_ATIME_INC);
198 writel(corr_period, fep->hwp + FEC_ATIME_CORR);
199 /* dummy read to update the timer. */
200 timecounter_read(&fep->tc);
Frank Li6605b732012-10-30 18:25:31 +0000201
202 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
203
204 return 0;
205}
206
207/**
208 * fec_ptp_adjtime
209 * @ptp: the ptp clock structure
210 * @delta: offset to adjust the cycle counter by
211 *
212 * adjust the timer by resetting the timecounter structure.
213 */
214static int fec_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
215{
216 struct fec_enet_private *fep =
217 container_of(ptp, struct fec_enet_private, ptp_caps);
218 unsigned long flags;
219 u64 now;
Luwei Zhou89bddcd2014-10-10 13:15:29 +0800220 u32 counter;
Frank Li6605b732012-10-30 18:25:31 +0000221
222 spin_lock_irqsave(&fep->tmreg_lock, flags);
223
224 now = timecounter_read(&fep->tc);
225 now += delta;
226
Luwei Zhou89bddcd2014-10-10 13:15:29 +0800227 /* Get the timer value based on adjusted timestamp.
228 * Update the counter with the masked value.
229 */
230 counter = now & fep->cc.mask;
231 writel(counter, fep->hwp + FEC_ATIME);
232
Frank Li6605b732012-10-30 18:25:31 +0000233 /* reset the timecounter */
234 timecounter_init(&fep->tc, &fep->cc, now);
235
236 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
237
238 return 0;
239}
240
241/**
242 * fec_ptp_gettime
243 * @ptp: the ptp clock structure
244 * @ts: timespec structure to hold the current time value
245 *
246 * read the timecounter and return the correct value on ns,
247 * after converting it into a struct timespec.
248 */
249static int fec_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
250{
251 struct fec_enet_private *adapter =
252 container_of(ptp, struct fec_enet_private, ptp_caps);
253 u64 ns;
254 u32 remainder;
255 unsigned long flags;
256
257 spin_lock_irqsave(&adapter->tmreg_lock, flags);
258 ns = timecounter_read(&adapter->tc);
259 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
260
261 ts->tv_sec = div_u64_rem(ns, 1000000000ULL, &remainder);
262 ts->tv_nsec = remainder;
263
264 return 0;
265}
266
267/**
268 * fec_ptp_settime
269 * @ptp: the ptp clock structure
270 * @ts: the timespec containing the new time for the cycle counter
271 *
272 * reset the timecounter to use a new base value instead of the kernel
273 * wall timer value.
274 */
275static int fec_ptp_settime(struct ptp_clock_info *ptp,
276 const struct timespec *ts)
277{
278 struct fec_enet_private *fep =
279 container_of(ptp, struct fec_enet_private, ptp_caps);
280
281 u64 ns;
282 unsigned long flags;
Luwei Zhou89bddcd2014-10-10 13:15:29 +0800283 u32 counter;
Frank Li6605b732012-10-30 18:25:31 +0000284
Nimrod Andy91c0d982014-08-21 17:09:38 +0800285 mutex_lock(&fep->ptp_clk_mutex);
286 /* Check the ptp clock */
287 if (!fep->ptp_clk_on) {
288 mutex_unlock(&fep->ptp_clk_mutex);
289 return -EINVAL;
290 }
291
Frank Li6605b732012-10-30 18:25:31 +0000292 ns = ts->tv_sec * 1000000000ULL;
293 ns += ts->tv_nsec;
Luwei Zhou89bddcd2014-10-10 13:15:29 +0800294 /* Get the timer value based on timestamp.
295 * Update the counter with the masked value.
296 */
297 counter = ns & fep->cc.mask;
Frank Li6605b732012-10-30 18:25:31 +0000298
299 spin_lock_irqsave(&fep->tmreg_lock, flags);
Luwei Zhou89bddcd2014-10-10 13:15:29 +0800300 writel(counter, fep->hwp + FEC_ATIME);
Frank Li6605b732012-10-30 18:25:31 +0000301 timecounter_init(&fep->tc, &fep->cc, ns);
302 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
Nimrod Andy91c0d982014-08-21 17:09:38 +0800303 mutex_unlock(&fep->ptp_clk_mutex);
Frank Li6605b732012-10-30 18:25:31 +0000304 return 0;
305}
306
307/**
308 * fec_ptp_enable
309 * @ptp: the ptp clock structure
310 * @rq: the requested feature to change
311 * @on: whether to enable or disable the feature
312 *
313 */
314static int fec_ptp_enable(struct ptp_clock_info *ptp,
315 struct ptp_clock_request *rq, int on)
316{
317 return -EOPNOTSUPP;
318}
319
320/**
321 * fec_ptp_hwtstamp_ioctl - control hardware time stamping
322 * @ndev: pointer to net_device
323 * @ifreq: ioctl data
324 * @cmd: particular ioctl requested
325 */
Ben Hutchings1d5244d2013-11-18 23:02:44 +0000326int fec_ptp_set(struct net_device *ndev, struct ifreq *ifr)
Frank Li6605b732012-10-30 18:25:31 +0000327{
328 struct fec_enet_private *fep = netdev_priv(ndev);
329
330 struct hwtstamp_config config;
331
332 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
333 return -EFAULT;
334
335 /* reserved for future extensions */
336 if (config.flags)
337 return -EINVAL;
338
339 switch (config.tx_type) {
340 case HWTSTAMP_TX_OFF:
341 fep->hwts_tx_en = 0;
342 break;
343 case HWTSTAMP_TX_ON:
344 fep->hwts_tx_en = 1;
345 break;
346 default:
347 return -ERANGE;
348 }
349
350 switch (config.rx_filter) {
351 case HWTSTAMP_FILTER_NONE:
352 if (fep->hwts_rx_en)
353 fep->hwts_rx_en = 0;
354 config.rx_filter = HWTSTAMP_FILTER_NONE;
355 break;
356
357 default:
358 /*
359 * register RXMTRL must be set in order to do V1 packets,
360 * therefore it is not possible to time stamp both V1 Sync and
361 * Delay_Req messages and hardware does not support
362 * timestamping all packets => return error
363 */
364 fep->hwts_rx_en = 1;
365 config.rx_filter = HWTSTAMP_FILTER_ALL;
366 break;
367 }
368
369 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
370 -EFAULT : 0;
371}
372
Ben Hutchings1d5244d2013-11-18 23:02:44 +0000373int fec_ptp_get(struct net_device *ndev, struct ifreq *ifr)
374{
375 struct fec_enet_private *fep = netdev_priv(ndev);
376 struct hwtstamp_config config;
377
378 config.flags = 0;
379 config.tx_type = fep->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
380 config.rx_filter = (fep->hwts_rx_en ?
381 HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE);
382
383 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
384 -EFAULT : 0;
385}
386
Frank Li6605b732012-10-30 18:25:31 +0000387/**
388 * fec_time_keep - call timecounter_read every second to avoid timer overrun
389 * because ENET just support 32bit counter, will timeout in 4s
390 */
Nimrod Andy91c0d982014-08-21 17:09:38 +0800391static void fec_time_keep(struct work_struct *work)
Frank Li6605b732012-10-30 18:25:31 +0000392{
Nimrod Andy91c0d982014-08-21 17:09:38 +0800393 struct delayed_work *dwork = to_delayed_work(work);
394 struct fec_enet_private *fep = container_of(dwork, struct fec_enet_private, time_keep);
Frank Li6605b732012-10-30 18:25:31 +0000395 u64 ns;
396 unsigned long flags;
397
Nimrod Andy91c0d982014-08-21 17:09:38 +0800398 mutex_lock(&fep->ptp_clk_mutex);
399 if (fep->ptp_clk_on) {
400 spin_lock_irqsave(&fep->tmreg_lock, flags);
401 ns = timecounter_read(&fep->tc);
402 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
403 }
404 mutex_unlock(&fep->ptp_clk_mutex);
Frank Li6605b732012-10-30 18:25:31 +0000405
Nimrod Andy91c0d982014-08-21 17:09:38 +0800406 schedule_delayed_work(&fep->time_keep, HZ);
Frank Li6605b732012-10-30 18:25:31 +0000407}
408
409/**
410 * fec_ptp_init
411 * @ndev: The FEC network adapter
412 *
413 * This function performs the required steps for enabling ptp
414 * support. If ptp support has already been loaded it simply calls the
415 * cyclecounter init routine and exits.
416 */
417
Fabio Estevamca162a82013-06-07 10:48:00 +0000418void fec_ptp_init(struct platform_device *pdev)
Frank Li6605b732012-10-30 18:25:31 +0000419{
Fabio Estevamca162a82013-06-07 10:48:00 +0000420 struct net_device *ndev = platform_get_drvdata(pdev);
Frank Li6605b732012-10-30 18:25:31 +0000421 struct fec_enet_private *fep = netdev_priv(ndev);
422
423 fep->ptp_caps.owner = THIS_MODULE;
424 snprintf(fep->ptp_caps.name, 16, "fec ptp");
425
426 fep->ptp_caps.max_adj = 250000000;
427 fep->ptp_caps.n_alarm = 0;
428 fep->ptp_caps.n_ext_ts = 0;
429 fep->ptp_caps.n_per_out = 0;
Richard Cochran4986b4f02014-03-20 22:21:55 +0100430 fep->ptp_caps.n_pins = 0;
Frank Li6605b732012-10-30 18:25:31 +0000431 fep->ptp_caps.pps = 0;
432 fep->ptp_caps.adjfreq = fec_ptp_adjfreq;
433 fep->ptp_caps.adjtime = fec_ptp_adjtime;
434 fep->ptp_caps.gettime = fec_ptp_gettime;
435 fep->ptp_caps.settime = fec_ptp_settime;
436 fep->ptp_caps.enable = fec_ptp_enable;
437
Frank Li85bd1792013-02-06 14:59:59 +0000438 fep->cycle_speed = clk_get_rate(fep->clk_ptp);
Luwei Zhou89bddcd2014-10-10 13:15:29 +0800439 fep->ptp_inc = NSEC_PER_SEC / fep->cycle_speed;
Frank Li85bd1792013-02-06 14:59:59 +0000440
Frank Li6605b732012-10-30 18:25:31 +0000441 spin_lock_init(&fep->tmreg_lock);
442
443 fec_ptp_start_cyclecounter(ndev);
444
Nimrod Andy91c0d982014-08-21 17:09:38 +0800445 INIT_DELAYED_WORK(&fep->time_keep, fec_time_keep);
Frank Li6605b732012-10-30 18:25:31 +0000446
447 fep->ptp_clock = ptp_clock_register(&fep->ptp_caps, &pdev->dev);
448 if (IS_ERR(fep->ptp_clock)) {
449 fep->ptp_clock = NULL;
450 pr_err("ptp_clock_register failed\n");
Frank Li6605b732012-10-30 18:25:31 +0000451 }
Nimrod Andy91c0d982014-08-21 17:09:38 +0800452
453 schedule_delayed_work(&fep->time_keep, HZ);
Frank Li6605b732012-10-30 18:25:31 +0000454}