blob: fe8e9e5cfb2b720728bf40faf39d56978e01c85b [file] [log] [blame]
Richard Cochranc78275f2011-04-22 12:03:54 +02001/*
2 * PTP 1588 clock using the eTSEC
3 *
4 * Copyright (C) 2010 OMICRON electronics GmbH
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
Joe Perches375d6a12013-04-13 19:03:18 +000020
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
Richard Cochranc78275f2011-04-22 12:03:54 +020023#include <linux/device.h>
24#include <linux/hrtimer.h>
25#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/of.h>
30#include <linux/of_platform.h>
31#include <linux/timex.h>
32#include <linux/io.h>
33
34#include <linux/ptp_clock_kernel.h>
35
36#include "gianfar.h"
37
38/*
39 * gianfar ptp registers
40 * Generated by regen.tcl on Thu May 13 01:38:57 PM CEST 2010
41 */
42struct gianfar_ptp_registers {
43 u32 tmr_ctrl; /* Timer control register */
44 u32 tmr_tevent; /* Timestamp event register */
45 u32 tmr_temask; /* Timer event mask register */
46 u32 tmr_pevent; /* Timestamp event register */
47 u32 tmr_pemask; /* Timer event mask register */
48 u32 tmr_stat; /* Timestamp status register */
49 u32 tmr_cnt_h; /* Timer counter high register */
50 u32 tmr_cnt_l; /* Timer counter low register */
51 u32 tmr_add; /* Timer drift compensation addend register */
52 u32 tmr_acc; /* Timer accumulator register */
53 u32 tmr_prsc; /* Timer prescale */
54 u8 res1[4];
55 u32 tmroff_h; /* Timer offset high */
56 u32 tmroff_l; /* Timer offset low */
57 u8 res2[8];
58 u32 tmr_alarm1_h; /* Timer alarm 1 high register */
59 u32 tmr_alarm1_l; /* Timer alarm 1 high register */
60 u32 tmr_alarm2_h; /* Timer alarm 2 high register */
61 u32 tmr_alarm2_l; /* Timer alarm 2 high register */
62 u8 res3[48];
63 u32 tmr_fiper1; /* Timer fixed period interval */
64 u32 tmr_fiper2; /* Timer fixed period interval */
65 u32 tmr_fiper3; /* Timer fixed period interval */
66 u8 res4[20];
67 u32 tmr_etts1_h; /* Timestamp of general purpose external trigger */
68 u32 tmr_etts1_l; /* Timestamp of general purpose external trigger */
69 u32 tmr_etts2_h; /* Timestamp of general purpose external trigger */
70 u32 tmr_etts2_l; /* Timestamp of general purpose external trigger */
71};
72
73/* Bit definitions for the TMR_CTRL register */
74#define ALM1P (1<<31) /* Alarm1 output polarity */
75#define ALM2P (1<<30) /* Alarm2 output polarity */
76#define FS (1<<28) /* FIPER start indication */
77#define PP1L (1<<27) /* Fiper1 pulse loopback mode enabled. */
78#define PP2L (1<<26) /* Fiper2 pulse loopback mode enabled. */
79#define TCLK_PERIOD_SHIFT (16) /* 1588 timer reference clock period. */
80#define TCLK_PERIOD_MASK (0x3ff)
81#define RTPE (1<<15) /* Record Tx Timestamp to PAL Enable. */
82#define FRD (1<<14) /* FIPER Realignment Disable */
83#define ESFDP (1<<11) /* External Tx/Rx SFD Polarity. */
84#define ESFDE (1<<10) /* External Tx/Rx SFD Enable. */
85#define ETEP2 (1<<9) /* External trigger 2 edge polarity */
86#define ETEP1 (1<<8) /* External trigger 1 edge polarity */
87#define COPH (1<<7) /* Generated clock output phase. */
88#define CIPH (1<<6) /* External oscillator input clock phase */
89#define TMSR (1<<5) /* Timer soft reset. */
90#define BYP (1<<3) /* Bypass drift compensated clock */
91#define TE (1<<2) /* 1588 timer enable. */
92#define CKSEL_SHIFT (0) /* 1588 Timer reference clock source */
93#define CKSEL_MASK (0x3)
94
95/* Bit definitions for the TMR_TEVENT register */
96#define ETS2 (1<<25) /* External trigger 2 timestamp sampled */
97#define ETS1 (1<<24) /* External trigger 1 timestamp sampled */
98#define ALM2 (1<<17) /* Current time = alarm time register 2 */
99#define ALM1 (1<<16) /* Current time = alarm time register 1 */
100#define PP1 (1<<7) /* periodic pulse generated on FIPER1 */
101#define PP2 (1<<6) /* periodic pulse generated on FIPER2 */
102#define PP3 (1<<5) /* periodic pulse generated on FIPER3 */
103
104/* Bit definitions for the TMR_TEMASK register */
105#define ETS2EN (1<<25) /* External trigger 2 timestamp enable */
106#define ETS1EN (1<<24) /* External trigger 1 timestamp enable */
107#define ALM2EN (1<<17) /* Timer ALM2 event enable */
108#define ALM1EN (1<<16) /* Timer ALM1 event enable */
109#define PP1EN (1<<7) /* Periodic pulse event 1 enable */
110#define PP2EN (1<<6) /* Periodic pulse event 2 enable */
111
112/* Bit definitions for the TMR_PEVENT register */
113#define TXP2 (1<<9) /* PTP transmitted timestamp im TXTS2 */
114#define TXP1 (1<<8) /* PTP transmitted timestamp in TXTS1 */
115#define RXP (1<<0) /* PTP frame has been received */
116
117/* Bit definitions for the TMR_PEMASK register */
118#define TXP2EN (1<<9) /* Transmit PTP packet event 2 enable */
119#define TXP1EN (1<<8) /* Transmit PTP packet event 1 enable */
120#define RXPEN (1<<0) /* Receive PTP packet event enable */
121
122/* Bit definitions for the TMR_STAT register */
123#define STAT_VEC_SHIFT (0) /* Timer general purpose status vector */
124#define STAT_VEC_MASK (0x3f)
125
126/* Bit definitions for the TMR_PRSC register */
127#define PRSC_OCK_SHIFT (0) /* Output clock division/prescale factor. */
128#define PRSC_OCK_MASK (0xffff)
129
130
131#define DRIVER "gianfar_ptp"
132#define DEFAULT_CKSEL 1
133#define N_ALARM 1 /* first alarm is used internally to reset fipers */
134#define N_EXT_TS 2
135#define REG_SIZE sizeof(struct gianfar_ptp_registers)
136
137struct etsects {
138 struct gianfar_ptp_registers *regs;
139 spinlock_t lock; /* protects regs */
140 struct ptp_clock *clock;
141 struct ptp_clock_info caps;
142 struct resource *rsrc;
143 int irq;
144 u64 alarm_interval; /* for periodic alarm */
145 u64 alarm_value;
146 u32 tclk_period; /* nanoseconds */
147 u32 tmr_prsc;
148 u32 tmr_add;
149 u32 cksel;
150 u32 tmr_fiper1;
151 u32 tmr_fiper2;
152};
153
154/*
155 * Register access functions
156 */
157
158/* Caller must hold etsects->lock. */
159static u64 tmr_cnt_read(struct etsects *etsects)
160{
161 u64 ns;
162 u32 lo, hi;
163
164 lo = gfar_read(&etsects->regs->tmr_cnt_l);
165 hi = gfar_read(&etsects->regs->tmr_cnt_h);
166 ns = ((u64) hi) << 32;
167 ns |= lo;
168 return ns;
169}
170
171/* Caller must hold etsects->lock. */
172static void tmr_cnt_write(struct etsects *etsects, u64 ns)
173{
174 u32 hi = ns >> 32;
175 u32 lo = ns & 0xffffffff;
176
177 gfar_write(&etsects->regs->tmr_cnt_l, lo);
178 gfar_write(&etsects->regs->tmr_cnt_h, hi);
179}
180
181/* Caller must hold etsects->lock. */
182static void set_alarm(struct etsects *etsects)
183{
184 u64 ns;
185 u32 lo, hi;
186
187 ns = tmr_cnt_read(etsects) + 1500000000ULL;
188 ns = div_u64(ns, 1000000000UL) * 1000000000ULL;
189 ns -= etsects->tclk_period;
190 hi = ns >> 32;
191 lo = ns & 0xffffffff;
192 gfar_write(&etsects->regs->tmr_alarm1_l, lo);
193 gfar_write(&etsects->regs->tmr_alarm1_h, hi);
194}
195
196/* Caller must hold etsects->lock. */
197static void set_fipers(struct etsects *etsects)
198{
Richard Cochrancbc05662011-08-06 21:03:03 +0000199 set_alarm(etsects);
Richard Cochranc78275f2011-04-22 12:03:54 +0200200 gfar_write(&etsects->regs->tmr_fiper1, etsects->tmr_fiper1);
201 gfar_write(&etsects->regs->tmr_fiper2, etsects->tmr_fiper2);
Richard Cochranc78275f2011-04-22 12:03:54 +0200202}
203
204/*
205 * Interrupt service routine
206 */
207
208static irqreturn_t isr(int irq, void *priv)
209{
210 struct etsects *etsects = priv;
211 struct ptp_clock_event event;
212 u64 ns;
213 u32 ack = 0, lo, hi, mask, val;
214
215 val = gfar_read(&etsects->regs->tmr_tevent);
216
217 if (val & ETS1) {
218 ack |= ETS1;
219 hi = gfar_read(&etsects->regs->tmr_etts1_h);
220 lo = gfar_read(&etsects->regs->tmr_etts1_l);
221 event.type = PTP_CLOCK_EXTTS;
222 event.index = 0;
223 event.timestamp = ((u64) hi) << 32;
224 event.timestamp |= lo;
225 ptp_clock_event(etsects->clock, &event);
226 }
227
228 if (val & ETS2) {
229 ack |= ETS2;
230 hi = gfar_read(&etsects->regs->tmr_etts2_h);
231 lo = gfar_read(&etsects->regs->tmr_etts2_l);
232 event.type = PTP_CLOCK_EXTTS;
233 event.index = 1;
234 event.timestamp = ((u64) hi) << 32;
235 event.timestamp |= lo;
236 ptp_clock_event(etsects->clock, &event);
237 }
238
239 if (val & ALM2) {
240 ack |= ALM2;
241 if (etsects->alarm_value) {
242 event.type = PTP_CLOCK_ALARM;
243 event.index = 0;
244 event.timestamp = etsects->alarm_value;
245 ptp_clock_event(etsects->clock, &event);
246 }
247 if (etsects->alarm_interval) {
248 ns = etsects->alarm_value + etsects->alarm_interval;
249 hi = ns >> 32;
250 lo = ns & 0xffffffff;
251 spin_lock(&etsects->lock);
252 gfar_write(&etsects->regs->tmr_alarm2_l, lo);
253 gfar_write(&etsects->regs->tmr_alarm2_h, hi);
254 spin_unlock(&etsects->lock);
255 etsects->alarm_value = ns;
256 } else {
257 gfar_write(&etsects->regs->tmr_tevent, ALM2);
258 spin_lock(&etsects->lock);
259 mask = gfar_read(&etsects->regs->tmr_temask);
260 mask &= ~ALM2EN;
261 gfar_write(&etsects->regs->tmr_temask, mask);
262 spin_unlock(&etsects->lock);
263 etsects->alarm_value = 0;
264 etsects->alarm_interval = 0;
265 }
266 }
267
268 if (val & PP1) {
269 ack |= PP1;
270 event.type = PTP_CLOCK_PPS;
271 ptp_clock_event(etsects->clock, &event);
272 }
273
274 if (ack) {
275 gfar_write(&etsects->regs->tmr_tevent, ack);
276 return IRQ_HANDLED;
277 } else
278 return IRQ_NONE;
279}
280
281/*
282 * PTP clock operations
283 */
284
285static int ptp_gianfar_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
286{
287 u64 adj;
288 u32 diff, tmr_add;
289 int neg_adj = 0;
290 struct etsects *etsects = container_of(ptp, struct etsects, caps);
291
292 if (ppb < 0) {
293 neg_adj = 1;
294 ppb = -ppb;
295 }
296 tmr_add = etsects->tmr_add;
297 adj = tmr_add;
298 adj *= ppb;
299 diff = div_u64(adj, 1000000000ULL);
300
301 tmr_add = neg_adj ? tmr_add - diff : tmr_add + diff;
302
303 gfar_write(&etsects->regs->tmr_add, tmr_add);
304
305 return 0;
306}
307
308static int ptp_gianfar_adjtime(struct ptp_clock_info *ptp, s64 delta)
309{
310 s64 now;
311 unsigned long flags;
312 struct etsects *etsects = container_of(ptp, struct etsects, caps);
313
314 spin_lock_irqsave(&etsects->lock, flags);
315
316 now = tmr_cnt_read(etsects);
317 now += delta;
318 tmr_cnt_write(etsects, now);
319
320 spin_unlock_irqrestore(&etsects->lock, flags);
321
322 set_fipers(etsects);
323
324 return 0;
325}
326
327static int ptp_gianfar_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
328{
329 u64 ns;
330 u32 remainder;
331 unsigned long flags;
332 struct etsects *etsects = container_of(ptp, struct etsects, caps);
333
334 spin_lock_irqsave(&etsects->lock, flags);
335
336 ns = tmr_cnt_read(etsects);
337
338 spin_unlock_irqrestore(&etsects->lock, flags);
339
340 ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
341 ts->tv_nsec = remainder;
342 return 0;
343}
344
345static int ptp_gianfar_settime(struct ptp_clock_info *ptp,
346 const struct timespec *ts)
347{
348 u64 ns;
349 unsigned long flags;
350 struct etsects *etsects = container_of(ptp, struct etsects, caps);
351
352 ns = ts->tv_sec * 1000000000ULL;
353 ns += ts->tv_nsec;
354
355 spin_lock_irqsave(&etsects->lock, flags);
356
357 tmr_cnt_write(etsects, ns);
358 set_fipers(etsects);
359
360 spin_unlock_irqrestore(&etsects->lock, flags);
361
362 return 0;
363}
364
365static int ptp_gianfar_enable(struct ptp_clock_info *ptp,
366 struct ptp_clock_request *rq, int on)
367{
368 struct etsects *etsects = container_of(ptp, struct etsects, caps);
369 unsigned long flags;
370 u32 bit, mask;
371
372 switch (rq->type) {
373 case PTP_CLK_REQ_EXTTS:
374 switch (rq->extts.index) {
375 case 0:
376 bit = ETS1EN;
377 break;
378 case 1:
379 bit = ETS2EN;
380 break;
381 default:
382 return -EINVAL;
383 }
384 spin_lock_irqsave(&etsects->lock, flags);
385 mask = gfar_read(&etsects->regs->tmr_temask);
386 if (on)
387 mask |= bit;
388 else
389 mask &= ~bit;
390 gfar_write(&etsects->regs->tmr_temask, mask);
391 spin_unlock_irqrestore(&etsects->lock, flags);
392 return 0;
393
394 case PTP_CLK_REQ_PPS:
395 spin_lock_irqsave(&etsects->lock, flags);
396 mask = gfar_read(&etsects->regs->tmr_temask);
397 if (on)
398 mask |= PP1EN;
399 else
400 mask &= ~PP1EN;
401 gfar_write(&etsects->regs->tmr_temask, mask);
402 spin_unlock_irqrestore(&etsects->lock, flags);
403 return 0;
404
405 default:
406 break;
407 }
408
409 return -EOPNOTSUPP;
410}
411
412static struct ptp_clock_info ptp_gianfar_caps = {
413 .owner = THIS_MODULE,
414 .name = "gianfar clock",
415 .max_adj = 512000,
416 .n_alarm = N_ALARM,
417 .n_ext_ts = N_EXT_TS,
418 .n_per_out = 0,
419 .pps = 1,
420 .adjfreq = ptp_gianfar_adjfreq,
421 .adjtime = ptp_gianfar_adjtime,
422 .gettime = ptp_gianfar_gettime,
423 .settime = ptp_gianfar_settime,
424 .enable = ptp_gianfar_enable,
425};
426
427/* OF device tree */
428
429static int get_of_u32(struct device_node *node, char *str, u32 *val)
430{
431 int plen;
432 const u32 *prop = of_get_property(node, str, &plen);
433
434 if (!prop || plen != sizeof(*prop))
435 return -1;
436 *val = *prop;
437 return 0;
438}
439
440static int gianfar_ptp_probe(struct platform_device *dev)
441{
442 struct device_node *node = dev->dev.of_node;
443 struct etsects *etsects;
444 struct timespec now;
445 int err = -ENOMEM;
446 u32 tmr_ctrl;
447 unsigned long flags;
448
449 etsects = kzalloc(sizeof(*etsects), GFP_KERNEL);
450 if (!etsects)
451 goto no_memory;
452
453 err = -ENODEV;
454
455 etsects->caps = ptp_gianfar_caps;
456 etsects->cksel = DEFAULT_CKSEL;
457
458 if (get_of_u32(node, "fsl,tclk-period", &etsects->tclk_period) ||
459 get_of_u32(node, "fsl,tmr-prsc", &etsects->tmr_prsc) ||
460 get_of_u32(node, "fsl,tmr-add", &etsects->tmr_add) ||
461 get_of_u32(node, "fsl,tmr-fiper1", &etsects->tmr_fiper1) ||
462 get_of_u32(node, "fsl,tmr-fiper2", &etsects->tmr_fiper2) ||
463 get_of_u32(node, "fsl,max-adj", &etsects->caps.max_adj)) {
464 pr_err("device tree node missing required elements\n");
465 goto no_node;
466 }
467
468 etsects->irq = platform_get_irq(dev, 0);
469
470 if (etsects->irq == NO_IRQ) {
471 pr_err("irq not in device tree\n");
472 goto no_node;
473 }
474 if (request_irq(etsects->irq, isr, 0, DRIVER, etsects)) {
475 pr_err("request_irq failed\n");
476 goto no_node;
477 }
478
479 etsects->rsrc = platform_get_resource(dev, IORESOURCE_MEM, 0);
480 if (!etsects->rsrc) {
481 pr_err("no resource\n");
482 goto no_resource;
483 }
Wei Yang2b9c1282012-10-24 05:21:36 +0000484 if (request_resource(&iomem_resource, etsects->rsrc)) {
Richard Cochranc78275f2011-04-22 12:03:54 +0200485 pr_err("resource busy\n");
486 goto no_resource;
487 }
488
489 spin_lock_init(&etsects->lock);
490
491 etsects->regs = ioremap(etsects->rsrc->start,
Joe Perches28f65c112011-06-09 09:13:32 -0700492 resource_size(etsects->rsrc));
Richard Cochranc78275f2011-04-22 12:03:54 +0200493 if (!etsects->regs) {
494 pr_err("ioremap ptp registers failed\n");
495 goto no_ioremap;
496 }
497 getnstimeofday(&now);
498 ptp_gianfar_settime(&etsects->caps, &now);
499
500 tmr_ctrl =
501 (etsects->tclk_period & TCLK_PERIOD_MASK) << TCLK_PERIOD_SHIFT |
502 (etsects->cksel & CKSEL_MASK) << CKSEL_SHIFT;
503
504 spin_lock_irqsave(&etsects->lock, flags);
505
506 gfar_write(&etsects->regs->tmr_ctrl, tmr_ctrl);
507 gfar_write(&etsects->regs->tmr_add, etsects->tmr_add);
508 gfar_write(&etsects->regs->tmr_prsc, etsects->tmr_prsc);
509 gfar_write(&etsects->regs->tmr_fiper1, etsects->tmr_fiper1);
510 gfar_write(&etsects->regs->tmr_fiper2, etsects->tmr_fiper2);
511 set_alarm(etsects);
Richard Cochrancbc05662011-08-06 21:03:03 +0000512 gfar_write(&etsects->regs->tmr_ctrl, tmr_ctrl|FS|RTPE|TE|FRD);
Richard Cochranc78275f2011-04-22 12:03:54 +0200513
514 spin_unlock_irqrestore(&etsects->lock, flags);
515
Richard Cochran1ef76152012-09-22 07:02:03 +0000516 etsects->clock = ptp_clock_register(&etsects->caps, &dev->dev);
Richard Cochranc78275f2011-04-22 12:03:54 +0200517 if (IS_ERR(etsects->clock)) {
518 err = PTR_ERR(etsects->clock);
519 goto no_clock;
520 }
Richard Cochran28889b72012-09-20 19:11:12 +0000521 gfar_phc_index = ptp_clock_index(etsects->clock);
Richard Cochranc78275f2011-04-22 12:03:54 +0200522
523 dev_set_drvdata(&dev->dev, etsects);
524
525 return 0;
526
527no_clock:
528no_ioremap:
529 release_resource(etsects->rsrc);
530no_resource:
531 free_irq(etsects->irq, etsects);
532no_node:
533 kfree(etsects);
534no_memory:
535 return err;
536}
537
538static int gianfar_ptp_remove(struct platform_device *dev)
539{
540 struct etsects *etsects = dev_get_drvdata(&dev->dev);
541
542 gfar_write(&etsects->regs->tmr_temask, 0);
543 gfar_write(&etsects->regs->tmr_ctrl, 0);
544
Richard Cochran28889b72012-09-20 19:11:12 +0000545 gfar_phc_index = -1;
Richard Cochranc78275f2011-04-22 12:03:54 +0200546 ptp_clock_unregister(etsects->clock);
547 iounmap(etsects->regs);
548 release_resource(etsects->rsrc);
549 free_irq(etsects->irq, etsects);
550 kfree(etsects);
551
552 return 0;
553}
554
555static struct of_device_id match_table[] = {
556 { .compatible = "fsl,etsec-ptp" },
557 {},
558};
559
560static struct platform_driver gianfar_ptp_driver = {
561 .driver = {
562 .name = "gianfar_ptp",
563 .of_match_table = match_table,
564 .owner = THIS_MODULE,
565 },
566 .probe = gianfar_ptp_probe,
567 .remove = gianfar_ptp_remove,
568};
569
Axel Lindb62f682011-11-27 16:44:17 +0000570module_platform_driver(gianfar_ptp_driver);
Richard Cochranc78275f2011-04-22 12:03:54 +0200571
Richard Cochranc2ec3ff2012-03-16 22:39:29 +0000572MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
Richard Cochranc78275f2011-04-22 12:03:54 +0200573MODULE_DESCRIPTION("PTP clock using the eTSEC");
574MODULE_LICENSE("GPL");