blob: cca3617a2321fa22198658e83b1ee5d65176f298 [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)
73/**
74 * fec_ptp_read - read raw cycle counter (to be used by time counter)
75 * @cc: the cyclecounter structure
76 *
77 * this function reads the cyclecounter registers and is called by the
78 * cyclecounter structure used to construct a ns counter from the
79 * arbitrary fixed point registers
80 */
81static cycle_t fec_ptp_read(const struct cyclecounter *cc)
82{
83 struct fec_enet_private *fep =
84 container_of(cc, struct fec_enet_private, cc);
85 u32 tempval;
86
87 tempval = readl(fep->hwp + FEC_ATIME_CTRL);
88 tempval |= FEC_T_CTRL_CAPTURE;
89 writel(tempval, fep->hwp + FEC_ATIME_CTRL);
90
91 return readl(fep->hwp + FEC_ATIME);
92}
93
94/**
95 * fec_ptp_start_cyclecounter - create the cycle counter from hw
96 * @ndev: network device
97 *
98 * this function initializes the timecounter and cyclecounter
99 * structures for use in generated a ns counter from the arbitrary
100 * fixed point cycles registers in the hardware.
101 */
102void fec_ptp_start_cyclecounter(struct net_device *ndev)
103{
104 struct fec_enet_private *fep = netdev_priv(ndev);
105 unsigned long flags;
106 int inc;
107
Frank Li85bd1792013-02-06 14:59:59 +0000108 inc = 1000000000 / fep->cycle_speed;
Frank Li6605b732012-10-30 18:25:31 +0000109
110 /* grab the ptp lock */
111 spin_lock_irqsave(&fep->tmreg_lock, flags);
112
113 /* 1ns counter */
114 writel(inc << FEC_T_INC_OFFSET, fep->hwp + FEC_ATIME_INC);
115
116 /* use free running count */
117 writel(0, fep->hwp + FEC_ATIME_EVT_PERIOD);
118
119 writel(FEC_T_CTRL_ENABLE, fep->hwp + FEC_ATIME_CTRL);
120
121 memset(&fep->cc, 0, sizeof(fep->cc));
122 fep->cc.read = fec_ptp_read;
123 fep->cc.mask = CLOCKSOURCE_MASK(32);
124 fep->cc.shift = 31;
125 fep->cc.mult = FEC_CC_MULT;
126
127 /* reset the ns time counter */
128 timecounter_init(&fep->tc, &fep->cc, ktime_to_ns(ktime_get_real()));
129
130 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
131}
132
133/**
134 * fec_ptp_adjfreq - adjust ptp cycle frequency
135 * @ptp: the ptp clock structure
136 * @ppb: parts per billion adjustment from base
137 *
138 * Adjust the frequency of the ptp cycle counter by the
139 * indicated ppb from the base frequency.
140 *
141 * Because ENET hardware frequency adjust is complex,
142 * using software method to do that.
143 */
144static int fec_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
145{
146 u64 diff;
147 unsigned long flags;
148 int neg_adj = 0;
Frank Li7da716a2012-11-06 20:14:49 +0000149 u32 mult = FEC_CC_MULT;
Frank Li6605b732012-10-30 18:25:31 +0000150
151 struct fec_enet_private *fep =
152 container_of(ptp, struct fec_enet_private, ptp_caps);
153
154 if (ppb < 0) {
155 ppb = -ppb;
156 neg_adj = 1;
157 }
158
Frank Li7da716a2012-11-06 20:14:49 +0000159 diff = mult;
160 diff *= ppb;
161 diff = div_u64(diff, 1000000000ULL);
162
Frank Li6605b732012-10-30 18:25:31 +0000163 spin_lock_irqsave(&fep->tmreg_lock, flags);
164 /*
165 * dummy read to set cycle_last in tc to now.
166 * So use adjusted mult to calculate when next call
167 * timercounter_read.
168 */
169 timecounter_read(&fep->tc);
Frank Li6605b732012-10-30 18:25:31 +0000170
Frank Li7da716a2012-11-06 20:14:49 +0000171 fep->cc.mult = neg_adj ? mult - diff : mult + diff;
Frank Li6605b732012-10-30 18:25:31 +0000172
173 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
174
175 return 0;
176}
177
178/**
179 * fec_ptp_adjtime
180 * @ptp: the ptp clock structure
181 * @delta: offset to adjust the cycle counter by
182 *
183 * adjust the timer by resetting the timecounter structure.
184 */
185static int fec_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
186{
187 struct fec_enet_private *fep =
188 container_of(ptp, struct fec_enet_private, ptp_caps);
189 unsigned long flags;
190 u64 now;
191
192 spin_lock_irqsave(&fep->tmreg_lock, flags);
193
194 now = timecounter_read(&fep->tc);
195 now += delta;
196
197 /* reset the timecounter */
198 timecounter_init(&fep->tc, &fep->cc, now);
199
200 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
201
202 return 0;
203}
204
205/**
206 * fec_ptp_gettime
207 * @ptp: the ptp clock structure
208 * @ts: timespec structure to hold the current time value
209 *
210 * read the timecounter and return the correct value on ns,
211 * after converting it into a struct timespec.
212 */
213static int fec_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
214{
215 struct fec_enet_private *adapter =
216 container_of(ptp, struct fec_enet_private, ptp_caps);
217 u64 ns;
218 u32 remainder;
219 unsigned long flags;
220
221 spin_lock_irqsave(&adapter->tmreg_lock, flags);
222 ns = timecounter_read(&adapter->tc);
223 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
224
225 ts->tv_sec = div_u64_rem(ns, 1000000000ULL, &remainder);
226 ts->tv_nsec = remainder;
227
228 return 0;
229}
230
231/**
232 * fec_ptp_settime
233 * @ptp: the ptp clock structure
234 * @ts: the timespec containing the new time for the cycle counter
235 *
236 * reset the timecounter to use a new base value instead of the kernel
237 * wall timer value.
238 */
239static int fec_ptp_settime(struct ptp_clock_info *ptp,
240 const struct timespec *ts)
241{
242 struct fec_enet_private *fep =
243 container_of(ptp, struct fec_enet_private, ptp_caps);
244
245 u64 ns;
246 unsigned long flags;
247
Nimrod Andy91c0d982014-08-21 17:09:38 +0800248 mutex_lock(&fep->ptp_clk_mutex);
249 /* Check the ptp clock */
250 if (!fep->ptp_clk_on) {
251 mutex_unlock(&fep->ptp_clk_mutex);
252 return -EINVAL;
253 }
254
Frank Li6605b732012-10-30 18:25:31 +0000255 ns = ts->tv_sec * 1000000000ULL;
256 ns += ts->tv_nsec;
257
258 spin_lock_irqsave(&fep->tmreg_lock, flags);
259 timecounter_init(&fep->tc, &fep->cc, ns);
260 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
Nimrod Andy91c0d982014-08-21 17:09:38 +0800261 mutex_unlock(&fep->ptp_clk_mutex);
Frank Li6605b732012-10-30 18:25:31 +0000262 return 0;
263}
264
265/**
266 * fec_ptp_enable
267 * @ptp: the ptp clock structure
268 * @rq: the requested feature to change
269 * @on: whether to enable or disable the feature
270 *
271 */
272static int fec_ptp_enable(struct ptp_clock_info *ptp,
273 struct ptp_clock_request *rq, int on)
274{
275 return -EOPNOTSUPP;
276}
277
278/**
279 * fec_ptp_hwtstamp_ioctl - control hardware time stamping
280 * @ndev: pointer to net_device
281 * @ifreq: ioctl data
282 * @cmd: particular ioctl requested
283 */
Ben Hutchings1d5244d2013-11-18 23:02:44 +0000284int fec_ptp_set(struct net_device *ndev, struct ifreq *ifr)
Frank Li6605b732012-10-30 18:25:31 +0000285{
286 struct fec_enet_private *fep = netdev_priv(ndev);
287
288 struct hwtstamp_config config;
289
290 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
291 return -EFAULT;
292
293 /* reserved for future extensions */
294 if (config.flags)
295 return -EINVAL;
296
297 switch (config.tx_type) {
298 case HWTSTAMP_TX_OFF:
299 fep->hwts_tx_en = 0;
300 break;
301 case HWTSTAMP_TX_ON:
302 fep->hwts_tx_en = 1;
303 break;
304 default:
305 return -ERANGE;
306 }
307
308 switch (config.rx_filter) {
309 case HWTSTAMP_FILTER_NONE:
310 if (fep->hwts_rx_en)
311 fep->hwts_rx_en = 0;
312 config.rx_filter = HWTSTAMP_FILTER_NONE;
313 break;
314
315 default:
316 /*
317 * register RXMTRL must be set in order to do V1 packets,
318 * therefore it is not possible to time stamp both V1 Sync and
319 * Delay_Req messages and hardware does not support
320 * timestamping all packets => return error
321 */
322 fep->hwts_rx_en = 1;
323 config.rx_filter = HWTSTAMP_FILTER_ALL;
324 break;
325 }
326
327 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
328 -EFAULT : 0;
329}
330
Ben Hutchings1d5244d2013-11-18 23:02:44 +0000331int fec_ptp_get(struct net_device *ndev, struct ifreq *ifr)
332{
333 struct fec_enet_private *fep = netdev_priv(ndev);
334 struct hwtstamp_config config;
335
336 config.flags = 0;
337 config.tx_type = fep->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
338 config.rx_filter = (fep->hwts_rx_en ?
339 HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE);
340
341 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
342 -EFAULT : 0;
343}
344
Frank Li6605b732012-10-30 18:25:31 +0000345/**
346 * fec_time_keep - call timecounter_read every second to avoid timer overrun
347 * because ENET just support 32bit counter, will timeout in 4s
348 */
Nimrod Andy91c0d982014-08-21 17:09:38 +0800349static void fec_time_keep(struct work_struct *work)
Frank Li6605b732012-10-30 18:25:31 +0000350{
Nimrod Andy91c0d982014-08-21 17:09:38 +0800351 struct delayed_work *dwork = to_delayed_work(work);
352 struct fec_enet_private *fep = container_of(dwork, struct fec_enet_private, time_keep);
Frank Li6605b732012-10-30 18:25:31 +0000353 u64 ns;
354 unsigned long flags;
355
Nimrod Andy91c0d982014-08-21 17:09:38 +0800356 mutex_lock(&fep->ptp_clk_mutex);
357 if (fep->ptp_clk_on) {
358 spin_lock_irqsave(&fep->tmreg_lock, flags);
359 ns = timecounter_read(&fep->tc);
360 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
361 }
362 mutex_unlock(&fep->ptp_clk_mutex);
Frank Li6605b732012-10-30 18:25:31 +0000363
Nimrod Andy91c0d982014-08-21 17:09:38 +0800364 schedule_delayed_work(&fep->time_keep, HZ);
Frank Li6605b732012-10-30 18:25:31 +0000365}
366
367/**
368 * fec_ptp_init
369 * @ndev: The FEC network adapter
370 *
371 * This function performs the required steps for enabling ptp
372 * support. If ptp support has already been loaded it simply calls the
373 * cyclecounter init routine and exits.
374 */
375
Fabio Estevamca162a82013-06-07 10:48:00 +0000376void fec_ptp_init(struct platform_device *pdev)
Frank Li6605b732012-10-30 18:25:31 +0000377{
Fabio Estevamca162a82013-06-07 10:48:00 +0000378 struct net_device *ndev = platform_get_drvdata(pdev);
Frank Li6605b732012-10-30 18:25:31 +0000379 struct fec_enet_private *fep = netdev_priv(ndev);
380
381 fep->ptp_caps.owner = THIS_MODULE;
382 snprintf(fep->ptp_caps.name, 16, "fec ptp");
383
384 fep->ptp_caps.max_adj = 250000000;
385 fep->ptp_caps.n_alarm = 0;
386 fep->ptp_caps.n_ext_ts = 0;
387 fep->ptp_caps.n_per_out = 0;
Richard Cochran4986b4f02014-03-20 22:21:55 +0100388 fep->ptp_caps.n_pins = 0;
Frank Li6605b732012-10-30 18:25:31 +0000389 fep->ptp_caps.pps = 0;
390 fep->ptp_caps.adjfreq = fec_ptp_adjfreq;
391 fep->ptp_caps.adjtime = fec_ptp_adjtime;
392 fep->ptp_caps.gettime = fec_ptp_gettime;
393 fep->ptp_caps.settime = fec_ptp_settime;
394 fep->ptp_caps.enable = fec_ptp_enable;
395
Frank Li85bd1792013-02-06 14:59:59 +0000396 fep->cycle_speed = clk_get_rate(fep->clk_ptp);
397
Frank Li6605b732012-10-30 18:25:31 +0000398 spin_lock_init(&fep->tmreg_lock);
399
400 fec_ptp_start_cyclecounter(ndev);
401
Nimrod Andy91c0d982014-08-21 17:09:38 +0800402 INIT_DELAYED_WORK(&fep->time_keep, fec_time_keep);
Frank Li6605b732012-10-30 18:25:31 +0000403
404 fep->ptp_clock = ptp_clock_register(&fep->ptp_caps, &pdev->dev);
405 if (IS_ERR(fep->ptp_clock)) {
406 fep->ptp_clock = NULL;
407 pr_err("ptp_clock_register failed\n");
Frank Li6605b732012-10-30 18:25:31 +0000408 }
Nimrod Andy91c0d982014-08-21 17:09:38 +0800409
410 schedule_delayed_work(&fep->time_keep, HZ);
Frank Li6605b732012-10-30 18:25:31 +0000411}