blob: 8016bdd1995f7ca80bfe14daa823754ac775e678 [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{
148 u64 diff;
149 unsigned long flags;
150 int neg_adj = 0;
Frank Li7da716a2012-11-06 20:14:49 +0000151 u32 mult = FEC_CC_MULT;
Frank Li6605b732012-10-30 18:25:31 +0000152
153 struct fec_enet_private *fep =
154 container_of(ptp, struct fec_enet_private, ptp_caps);
155
156 if (ppb < 0) {
157 ppb = -ppb;
158 neg_adj = 1;
159 }
160
Frank Li7da716a2012-11-06 20:14:49 +0000161 diff = mult;
162 diff *= ppb;
163 diff = div_u64(diff, 1000000000ULL);
164
Frank Li6605b732012-10-30 18:25:31 +0000165 spin_lock_irqsave(&fep->tmreg_lock, flags);
166 /*
167 * dummy read to set cycle_last in tc to now.
168 * So use adjusted mult to calculate when next call
169 * timercounter_read.
170 */
171 timecounter_read(&fep->tc);
Frank Li6605b732012-10-30 18:25:31 +0000172
Frank Li7da716a2012-11-06 20:14:49 +0000173 fep->cc.mult = neg_adj ? mult - diff : mult + diff;
Frank Li6605b732012-10-30 18:25:31 +0000174
175 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
176
177 return 0;
178}
179
180/**
181 * fec_ptp_adjtime
182 * @ptp: the ptp clock structure
183 * @delta: offset to adjust the cycle counter by
184 *
185 * adjust the timer by resetting the timecounter structure.
186 */
187static int fec_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
188{
189 struct fec_enet_private *fep =
190 container_of(ptp, struct fec_enet_private, ptp_caps);
191 unsigned long flags;
192 u64 now;
193
194 spin_lock_irqsave(&fep->tmreg_lock, flags);
195
196 now = timecounter_read(&fep->tc);
197 now += delta;
198
199 /* reset the timecounter */
200 timecounter_init(&fep->tc, &fep->cc, now);
201
202 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
203
204 return 0;
205}
206
207/**
208 * fec_ptp_gettime
209 * @ptp: the ptp clock structure
210 * @ts: timespec structure to hold the current time value
211 *
212 * read the timecounter and return the correct value on ns,
213 * after converting it into a struct timespec.
214 */
215static int fec_ptp_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
216{
217 struct fec_enet_private *adapter =
218 container_of(ptp, struct fec_enet_private, ptp_caps);
219 u64 ns;
220 u32 remainder;
221 unsigned long flags;
222
223 spin_lock_irqsave(&adapter->tmreg_lock, flags);
224 ns = timecounter_read(&adapter->tc);
225 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
226
227 ts->tv_sec = div_u64_rem(ns, 1000000000ULL, &remainder);
228 ts->tv_nsec = remainder;
229
230 return 0;
231}
232
233/**
234 * fec_ptp_settime
235 * @ptp: the ptp clock structure
236 * @ts: the timespec containing the new time for the cycle counter
237 *
238 * reset the timecounter to use a new base value instead of the kernel
239 * wall timer value.
240 */
241static int fec_ptp_settime(struct ptp_clock_info *ptp,
242 const struct timespec *ts)
243{
244 struct fec_enet_private *fep =
245 container_of(ptp, struct fec_enet_private, ptp_caps);
246
247 u64 ns;
248 unsigned long flags;
249
Nimrod Andy91c0d982014-08-21 17:09:38 +0800250 mutex_lock(&fep->ptp_clk_mutex);
251 /* Check the ptp clock */
252 if (!fep->ptp_clk_on) {
253 mutex_unlock(&fep->ptp_clk_mutex);
254 return -EINVAL;
255 }
256
Frank Li6605b732012-10-30 18:25:31 +0000257 ns = ts->tv_sec * 1000000000ULL;
258 ns += ts->tv_nsec;
259
260 spin_lock_irqsave(&fep->tmreg_lock, flags);
261 timecounter_init(&fep->tc, &fep->cc, ns);
262 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
Nimrod Andy91c0d982014-08-21 17:09:38 +0800263 mutex_unlock(&fep->ptp_clk_mutex);
Frank Li6605b732012-10-30 18:25:31 +0000264 return 0;
265}
266
267/**
268 * fec_ptp_enable
269 * @ptp: the ptp clock structure
270 * @rq: the requested feature to change
271 * @on: whether to enable or disable the feature
272 *
273 */
274static int fec_ptp_enable(struct ptp_clock_info *ptp,
275 struct ptp_clock_request *rq, int on)
276{
277 return -EOPNOTSUPP;
278}
279
280/**
281 * fec_ptp_hwtstamp_ioctl - control hardware time stamping
282 * @ndev: pointer to net_device
283 * @ifreq: ioctl data
284 * @cmd: particular ioctl requested
285 */
Ben Hutchings1d5244d2013-11-18 23:02:44 +0000286int fec_ptp_set(struct net_device *ndev, struct ifreq *ifr)
Frank Li6605b732012-10-30 18:25:31 +0000287{
288 struct fec_enet_private *fep = netdev_priv(ndev);
289
290 struct hwtstamp_config config;
291
292 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
293 return -EFAULT;
294
295 /* reserved for future extensions */
296 if (config.flags)
297 return -EINVAL;
298
299 switch (config.tx_type) {
300 case HWTSTAMP_TX_OFF:
301 fep->hwts_tx_en = 0;
302 break;
303 case HWTSTAMP_TX_ON:
304 fep->hwts_tx_en = 1;
305 break;
306 default:
307 return -ERANGE;
308 }
309
310 switch (config.rx_filter) {
311 case HWTSTAMP_FILTER_NONE:
312 if (fep->hwts_rx_en)
313 fep->hwts_rx_en = 0;
314 config.rx_filter = HWTSTAMP_FILTER_NONE;
315 break;
316
317 default:
318 /*
319 * register RXMTRL must be set in order to do V1 packets,
320 * therefore it is not possible to time stamp both V1 Sync and
321 * Delay_Req messages and hardware does not support
322 * timestamping all packets => return error
323 */
324 fep->hwts_rx_en = 1;
325 config.rx_filter = HWTSTAMP_FILTER_ALL;
326 break;
327 }
328
329 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
330 -EFAULT : 0;
331}
332
Ben Hutchings1d5244d2013-11-18 23:02:44 +0000333int fec_ptp_get(struct net_device *ndev, struct ifreq *ifr)
334{
335 struct fec_enet_private *fep = netdev_priv(ndev);
336 struct hwtstamp_config config;
337
338 config.flags = 0;
339 config.tx_type = fep->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
340 config.rx_filter = (fep->hwts_rx_en ?
341 HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE);
342
343 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
344 -EFAULT : 0;
345}
346
Frank Li6605b732012-10-30 18:25:31 +0000347/**
348 * fec_time_keep - call timecounter_read every second to avoid timer overrun
349 * because ENET just support 32bit counter, will timeout in 4s
350 */
Nimrod Andy91c0d982014-08-21 17:09:38 +0800351static void fec_time_keep(struct work_struct *work)
Frank Li6605b732012-10-30 18:25:31 +0000352{
Nimrod Andy91c0d982014-08-21 17:09:38 +0800353 struct delayed_work *dwork = to_delayed_work(work);
354 struct fec_enet_private *fep = container_of(dwork, struct fec_enet_private, time_keep);
Frank Li6605b732012-10-30 18:25:31 +0000355 u64 ns;
356 unsigned long flags;
357
Nimrod Andy91c0d982014-08-21 17:09:38 +0800358 mutex_lock(&fep->ptp_clk_mutex);
359 if (fep->ptp_clk_on) {
360 spin_lock_irqsave(&fep->tmreg_lock, flags);
361 ns = timecounter_read(&fep->tc);
362 spin_unlock_irqrestore(&fep->tmreg_lock, flags);
363 }
364 mutex_unlock(&fep->ptp_clk_mutex);
Frank Li6605b732012-10-30 18:25:31 +0000365
Nimrod Andy91c0d982014-08-21 17:09:38 +0800366 schedule_delayed_work(&fep->time_keep, HZ);
Frank Li6605b732012-10-30 18:25:31 +0000367}
368
369/**
370 * fec_ptp_init
371 * @ndev: The FEC network adapter
372 *
373 * This function performs the required steps for enabling ptp
374 * support. If ptp support has already been loaded it simply calls the
375 * cyclecounter init routine and exits.
376 */
377
Fabio Estevamca162a82013-06-07 10:48:00 +0000378void fec_ptp_init(struct platform_device *pdev)
Frank Li6605b732012-10-30 18:25:31 +0000379{
Fabio Estevamca162a82013-06-07 10:48:00 +0000380 struct net_device *ndev = platform_get_drvdata(pdev);
Frank Li6605b732012-10-30 18:25:31 +0000381 struct fec_enet_private *fep = netdev_priv(ndev);
382
383 fep->ptp_caps.owner = THIS_MODULE;
384 snprintf(fep->ptp_caps.name, 16, "fec ptp");
385
386 fep->ptp_caps.max_adj = 250000000;
387 fep->ptp_caps.n_alarm = 0;
388 fep->ptp_caps.n_ext_ts = 0;
389 fep->ptp_caps.n_per_out = 0;
Richard Cochran4986b4f02014-03-20 22:21:55 +0100390 fep->ptp_caps.n_pins = 0;
Frank Li6605b732012-10-30 18:25:31 +0000391 fep->ptp_caps.pps = 0;
392 fep->ptp_caps.adjfreq = fec_ptp_adjfreq;
393 fep->ptp_caps.adjtime = fec_ptp_adjtime;
394 fep->ptp_caps.gettime = fec_ptp_gettime;
395 fep->ptp_caps.settime = fec_ptp_settime;
396 fep->ptp_caps.enable = fec_ptp_enable;
397
Frank Li85bd1792013-02-06 14:59:59 +0000398 fep->cycle_speed = clk_get_rate(fep->clk_ptp);
399
Frank Li6605b732012-10-30 18:25:31 +0000400 spin_lock_init(&fep->tmreg_lock);
401
402 fec_ptp_start_cyclecounter(ndev);
403
Nimrod Andy91c0d982014-08-21 17:09:38 +0800404 INIT_DELAYED_WORK(&fep->time_keep, fec_time_keep);
Frank Li6605b732012-10-30 18:25:31 +0000405
406 fep->ptp_clock = ptp_clock_register(&fep->ptp_caps, &pdev->dev);
407 if (IS_ERR(fep->ptp_clock)) {
408 fep->ptp_clock = NULL;
409 pr_err("ptp_clock_register failed\n");
Frank Li6605b732012-10-30 18:25:31 +0000410 }
Nimrod Andy91c0d982014-08-21 17:09:38 +0800411
412 schedule_delayed_work(&fep->time_keep, HZ);
Frank Li6605b732012-10-30 18:25:31 +0000413}