2 * PTP 1588 clock for Freescale QorIQ 1588 timer
4 * Copyright (C) 2010 OMICRON electronics GmbH
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.
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.
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.
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/device.h>
24 #include <linux/hrtimer.h>
25 #include <linux/interrupt.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
29 #include <linux/of_platform.h>
30 #include <linux/timex.h>
31 #include <linux/slab.h>
32 #include <linux/clk.h>
34 #include <linux/fsl/ptp_qoriq.h>
37 * Register access functions
40 /* Caller must hold qoriq_ptp->lock. */
41 static u64
tmr_cnt_read(struct qoriq_ptp
*qoriq_ptp
)
43 struct qoriq_ptp_registers
*regs
= &qoriq_ptp
->regs
;
47 lo
= qoriq_read(®s
->ctrl_regs
->tmr_cnt_l
);
48 hi
= qoriq_read(®s
->ctrl_regs
->tmr_cnt_h
);
49 ns
= ((u64
) hi
) << 32;
54 /* Caller must hold qoriq_ptp->lock. */
55 static void tmr_cnt_write(struct qoriq_ptp
*qoriq_ptp
, u64 ns
)
57 struct qoriq_ptp_registers
*regs
= &qoriq_ptp
->regs
;
59 u32 lo
= ns
& 0xffffffff;
61 qoriq_write(®s
->ctrl_regs
->tmr_cnt_l
, lo
);
62 qoriq_write(®s
->ctrl_regs
->tmr_cnt_h
, hi
);
65 /* Caller must hold qoriq_ptp->lock. */
66 static void set_alarm(struct qoriq_ptp
*qoriq_ptp
)
68 struct qoriq_ptp_registers
*regs
= &qoriq_ptp
->regs
;
72 ns
= tmr_cnt_read(qoriq_ptp
) + 1500000000ULL;
73 ns
= div_u64(ns
, 1000000000UL) * 1000000000ULL;
74 ns
-= qoriq_ptp
->tclk_period
;
77 qoriq_write(®s
->alarm_regs
->tmr_alarm1_l
, lo
);
78 qoriq_write(®s
->alarm_regs
->tmr_alarm1_h
, hi
);
81 /* Caller must hold qoriq_ptp->lock. */
82 static void set_fipers(struct qoriq_ptp
*qoriq_ptp
)
84 struct qoriq_ptp_registers
*regs
= &qoriq_ptp
->regs
;
87 qoriq_write(®s
->fiper_regs
->tmr_fiper1
, qoriq_ptp
->tmr_fiper1
);
88 qoriq_write(®s
->fiper_regs
->tmr_fiper2
, qoriq_ptp
->tmr_fiper2
);
92 * Interrupt service routine
95 static irqreturn_t
isr(int irq
, void *priv
)
97 struct qoriq_ptp
*qoriq_ptp
= priv
;
98 struct qoriq_ptp_registers
*regs
= &qoriq_ptp
->regs
;
99 struct ptp_clock_event event
;
101 u32 ack
= 0, lo
, hi
, mask
, val
;
103 val
= qoriq_read(®s
->ctrl_regs
->tmr_tevent
);
107 hi
= qoriq_read(®s
->etts_regs
->tmr_etts1_h
);
108 lo
= qoriq_read(®s
->etts_regs
->tmr_etts1_l
);
109 event
.type
= PTP_CLOCK_EXTTS
;
111 event
.timestamp
= ((u64
) hi
) << 32;
112 event
.timestamp
|= lo
;
113 ptp_clock_event(qoriq_ptp
->clock
, &event
);
118 hi
= qoriq_read(®s
->etts_regs
->tmr_etts2_h
);
119 lo
= qoriq_read(®s
->etts_regs
->tmr_etts2_l
);
120 event
.type
= PTP_CLOCK_EXTTS
;
122 event
.timestamp
= ((u64
) hi
) << 32;
123 event
.timestamp
|= lo
;
124 ptp_clock_event(qoriq_ptp
->clock
, &event
);
129 if (qoriq_ptp
->alarm_value
) {
130 event
.type
= PTP_CLOCK_ALARM
;
132 event
.timestamp
= qoriq_ptp
->alarm_value
;
133 ptp_clock_event(qoriq_ptp
->clock
, &event
);
135 if (qoriq_ptp
->alarm_interval
) {
136 ns
= qoriq_ptp
->alarm_value
+ qoriq_ptp
->alarm_interval
;
138 lo
= ns
& 0xffffffff;
139 spin_lock(&qoriq_ptp
->lock
);
140 qoriq_write(®s
->alarm_regs
->tmr_alarm2_l
, lo
);
141 qoriq_write(®s
->alarm_regs
->tmr_alarm2_h
, hi
);
142 spin_unlock(&qoriq_ptp
->lock
);
143 qoriq_ptp
->alarm_value
= ns
;
145 qoriq_write(®s
->ctrl_regs
->tmr_tevent
, ALM2
);
146 spin_lock(&qoriq_ptp
->lock
);
147 mask
= qoriq_read(®s
->ctrl_regs
->tmr_temask
);
149 qoriq_write(®s
->ctrl_regs
->tmr_temask
, mask
);
150 spin_unlock(&qoriq_ptp
->lock
);
151 qoriq_ptp
->alarm_value
= 0;
152 qoriq_ptp
->alarm_interval
= 0;
158 event
.type
= PTP_CLOCK_PPS
;
159 ptp_clock_event(qoriq_ptp
->clock
, &event
);
163 qoriq_write(®s
->ctrl_regs
->tmr_tevent
, ack
);
170 * PTP clock operations
173 static int ptp_qoriq_adjfine(struct ptp_clock_info
*ptp
, long scaled_ppm
)
178 struct qoriq_ptp
*qoriq_ptp
= container_of(ptp
, struct qoriq_ptp
, caps
);
179 struct qoriq_ptp_registers
*regs
= &qoriq_ptp
->regs
;
181 if (scaled_ppm
< 0) {
183 scaled_ppm
= -scaled_ppm
;
185 tmr_add
= qoriq_ptp
->tmr_add
;
188 /* calculate diff as adj*(scaled_ppm/65536)/1000000
189 * and round() to the nearest integer
192 diff
= div_u64(adj
, 8000000);
193 diff
= (diff
>> 13) + ((diff
>> 12) & 1);
195 tmr_add
= neg_adj
? tmr_add
- diff
: tmr_add
+ diff
;
197 qoriq_write(®s
->ctrl_regs
->tmr_add
, tmr_add
);
202 static int ptp_qoriq_adjtime(struct ptp_clock_info
*ptp
, s64 delta
)
206 struct qoriq_ptp
*qoriq_ptp
= container_of(ptp
, struct qoriq_ptp
, caps
);
208 spin_lock_irqsave(&qoriq_ptp
->lock
, flags
);
210 now
= tmr_cnt_read(qoriq_ptp
);
212 tmr_cnt_write(qoriq_ptp
, now
);
213 set_fipers(qoriq_ptp
);
215 spin_unlock_irqrestore(&qoriq_ptp
->lock
, flags
);
220 static int ptp_qoriq_gettime(struct ptp_clock_info
*ptp
,
221 struct timespec64
*ts
)
225 struct qoriq_ptp
*qoriq_ptp
= container_of(ptp
, struct qoriq_ptp
, caps
);
227 spin_lock_irqsave(&qoriq_ptp
->lock
, flags
);
229 ns
= tmr_cnt_read(qoriq_ptp
);
231 spin_unlock_irqrestore(&qoriq_ptp
->lock
, flags
);
233 *ts
= ns_to_timespec64(ns
);
238 static int ptp_qoriq_settime(struct ptp_clock_info
*ptp
,
239 const struct timespec64
*ts
)
243 struct qoriq_ptp
*qoriq_ptp
= container_of(ptp
, struct qoriq_ptp
, caps
);
245 ns
= timespec64_to_ns(ts
);
247 spin_lock_irqsave(&qoriq_ptp
->lock
, flags
);
249 tmr_cnt_write(qoriq_ptp
, ns
);
250 set_fipers(qoriq_ptp
);
252 spin_unlock_irqrestore(&qoriq_ptp
->lock
, flags
);
257 static int ptp_qoriq_enable(struct ptp_clock_info
*ptp
,
258 struct ptp_clock_request
*rq
, int on
)
260 struct qoriq_ptp
*qoriq_ptp
= container_of(ptp
, struct qoriq_ptp
, caps
);
261 struct qoriq_ptp_registers
*regs
= &qoriq_ptp
->regs
;
266 case PTP_CLK_REQ_EXTTS
:
267 switch (rq
->extts
.index
) {
277 spin_lock_irqsave(&qoriq_ptp
->lock
, flags
);
278 mask
= qoriq_read(®s
->ctrl_regs
->tmr_temask
);
283 qoriq_write(®s
->ctrl_regs
->tmr_temask
, mask
);
284 spin_unlock_irqrestore(&qoriq_ptp
->lock
, flags
);
287 case PTP_CLK_REQ_PPS
:
288 spin_lock_irqsave(&qoriq_ptp
->lock
, flags
);
289 mask
= qoriq_read(®s
->ctrl_regs
->tmr_temask
);
294 qoriq_write(®s
->ctrl_regs
->tmr_temask
, mask
);
295 spin_unlock_irqrestore(&qoriq_ptp
->lock
, flags
);
305 static const struct ptp_clock_info ptp_qoriq_caps
= {
306 .owner
= THIS_MODULE
,
307 .name
= "qoriq ptp clock",
310 .n_ext_ts
= N_EXT_TS
,
314 .adjfine
= ptp_qoriq_adjfine
,
315 .adjtime
= ptp_qoriq_adjtime
,
316 .gettime64
= ptp_qoriq_gettime
,
317 .settime64
= ptp_qoriq_settime
,
318 .enable
= ptp_qoriq_enable
,
322 * qoriq_ptp_nominal_freq - calculate nominal frequency according to
323 * reference clock frequency
325 * @clk_src: reference clock frequency
327 * The nominal frequency is the desired clock frequency.
328 * It should be less than the reference clock frequency.
329 * It should be a factor of 1000MHz.
331 * Return the nominal frequency
333 static u32
qoriq_ptp_nominal_freq(u32 clk_src
)
338 remainder
= clk_src
% 100;
340 clk_src
-= remainder
;
347 } while (1000 % clk_src
);
349 return clk_src
* 1000000;
353 * qoriq_ptp_auto_config - calculate a set of default configurations
355 * @qoriq_ptp: pointer to qoriq_ptp
356 * @node: pointer to device_node
358 * If below dts properties are not provided, this function will be
359 * called to calculate a set of default configurations for them.
367 * Return 0 if success
369 static int qoriq_ptp_auto_config(struct qoriq_ptp
*qoriq_ptp
,
370 struct device_node
*node
)
379 qoriq_ptp
->cksel
= DEFAULT_CKSEL
;
381 clk
= of_clk_get(node
, 0);
383 clk_src
= clk_get_rate(clk
);
387 if (clk_src
<= 100000000UL) {
388 pr_err("error reference clock value, or lower than 100MHz\n");
392 nominal_freq
= qoriq_ptp_nominal_freq(clk_src
);
396 qoriq_ptp
->tclk_period
= 1000000000UL / nominal_freq
;
397 qoriq_ptp
->tmr_prsc
= DEFAULT_TMR_PRSC
;
399 /* Calculate initial frequency compensation value for TMR_ADD register.
400 * freq_comp = ceil(2^32 / freq_ratio)
401 * freq_ratio = reference_clock_freq / nominal_freq
403 freq_comp
= ((u64
)1 << 32) * nominal_freq
;
404 freq_comp
= div_u64_rem(freq_comp
, clk_src
, &remainder
);
408 qoriq_ptp
->tmr_add
= freq_comp
;
409 qoriq_ptp
->tmr_fiper1
= DEFAULT_FIPER1_PERIOD
- qoriq_ptp
->tclk_period
;
410 qoriq_ptp
->tmr_fiper2
= DEFAULT_FIPER2_PERIOD
- qoriq_ptp
->tclk_period
;
412 /* max_adj = 1000000000 * (freq_ratio - 1.0) - 1
413 * freq_ratio = reference_clock_freq / nominal_freq
415 max_adj
= 1000000000ULL * (clk_src
- nominal_freq
);
416 max_adj
= div_u64(max_adj
, nominal_freq
) - 1;
417 qoriq_ptp
->caps
.max_adj
= max_adj
;
422 static int qoriq_ptp_probe(struct platform_device
*dev
)
424 struct device_node
*node
= dev
->dev
.of_node
;
425 struct qoriq_ptp
*qoriq_ptp
;
426 struct qoriq_ptp_registers
*regs
;
427 struct timespec64 now
;
433 qoriq_ptp
= kzalloc(sizeof(*qoriq_ptp
), GFP_KERNEL
);
439 qoriq_ptp
->caps
= ptp_qoriq_caps
;
441 if (of_property_read_u32(node
, "fsl,cksel", &qoriq_ptp
->cksel
))
442 qoriq_ptp
->cksel
= DEFAULT_CKSEL
;
444 if (of_property_read_u32(node
,
445 "fsl,tclk-period", &qoriq_ptp
->tclk_period
) ||
446 of_property_read_u32(node
,
447 "fsl,tmr-prsc", &qoriq_ptp
->tmr_prsc
) ||
448 of_property_read_u32(node
,
449 "fsl,tmr-add", &qoriq_ptp
->tmr_add
) ||
450 of_property_read_u32(node
,
451 "fsl,tmr-fiper1", &qoriq_ptp
->tmr_fiper1
) ||
452 of_property_read_u32(node
,
453 "fsl,tmr-fiper2", &qoriq_ptp
->tmr_fiper2
) ||
454 of_property_read_u32(node
,
455 "fsl,max-adj", &qoriq_ptp
->caps
.max_adj
)) {
456 pr_warn("device tree node missing required elements, try automatic configuration\n");
458 if (qoriq_ptp_auto_config(qoriq_ptp
, node
))
464 qoriq_ptp
->irq
= platform_get_irq(dev
, 0);
466 if (qoriq_ptp
->irq
< 0) {
467 pr_err("irq not in device tree\n");
470 if (request_irq(qoriq_ptp
->irq
, isr
, IRQF_SHARED
, DRIVER
, qoriq_ptp
)) {
471 pr_err("request_irq failed\n");
475 qoriq_ptp
->rsrc
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
476 if (!qoriq_ptp
->rsrc
) {
477 pr_err("no resource\n");
480 if (request_resource(&iomem_resource
, qoriq_ptp
->rsrc
)) {
481 pr_err("resource busy\n");
485 spin_lock_init(&qoriq_ptp
->lock
);
487 base
= ioremap(qoriq_ptp
->rsrc
->start
,
488 resource_size(qoriq_ptp
->rsrc
));
490 pr_err("ioremap ptp registers failed\n");
494 qoriq_ptp
->base
= base
;
496 if (of_device_is_compatible(node
, "fsl,fman-ptp-timer")) {
497 qoriq_ptp
->regs
.ctrl_regs
= base
+ FMAN_CTRL_REGS_OFFSET
;
498 qoriq_ptp
->regs
.alarm_regs
= base
+ FMAN_ALARM_REGS_OFFSET
;
499 qoriq_ptp
->regs
.fiper_regs
= base
+ FMAN_FIPER_REGS_OFFSET
;
500 qoriq_ptp
->regs
.etts_regs
= base
+ FMAN_ETTS_REGS_OFFSET
;
502 qoriq_ptp
->regs
.ctrl_regs
= base
+ CTRL_REGS_OFFSET
;
503 qoriq_ptp
->regs
.alarm_regs
= base
+ ALARM_REGS_OFFSET
;
504 qoriq_ptp
->regs
.fiper_regs
= base
+ FIPER_REGS_OFFSET
;
505 qoriq_ptp
->regs
.etts_regs
= base
+ ETTS_REGS_OFFSET
;
508 ktime_get_real_ts64(&now
);
509 ptp_qoriq_settime(&qoriq_ptp
->caps
, &now
);
512 (qoriq_ptp
->tclk_period
& TCLK_PERIOD_MASK
) << TCLK_PERIOD_SHIFT
|
513 (qoriq_ptp
->cksel
& CKSEL_MASK
) << CKSEL_SHIFT
;
515 spin_lock_irqsave(&qoriq_ptp
->lock
, flags
);
517 regs
= &qoriq_ptp
->regs
;
518 qoriq_write(®s
->ctrl_regs
->tmr_ctrl
, tmr_ctrl
);
519 qoriq_write(®s
->ctrl_regs
->tmr_add
, qoriq_ptp
->tmr_add
);
520 qoriq_write(®s
->ctrl_regs
->tmr_prsc
, qoriq_ptp
->tmr_prsc
);
521 qoriq_write(®s
->fiper_regs
->tmr_fiper1
, qoriq_ptp
->tmr_fiper1
);
522 qoriq_write(®s
->fiper_regs
->tmr_fiper2
, qoriq_ptp
->tmr_fiper2
);
523 set_alarm(qoriq_ptp
);
524 qoriq_write(®s
->ctrl_regs
->tmr_ctrl
, tmr_ctrl
|FIPERST
|RTPE
|TE
|FRD
);
526 spin_unlock_irqrestore(&qoriq_ptp
->lock
, flags
);
528 qoriq_ptp
->clock
= ptp_clock_register(&qoriq_ptp
->caps
, &dev
->dev
);
529 if (IS_ERR(qoriq_ptp
->clock
)) {
530 err
= PTR_ERR(qoriq_ptp
->clock
);
533 qoriq_ptp
->phc_index
= ptp_clock_index(qoriq_ptp
->clock
);
535 platform_set_drvdata(dev
, qoriq_ptp
);
540 iounmap(qoriq_ptp
->base
);
542 release_resource(qoriq_ptp
->rsrc
);
544 free_irq(qoriq_ptp
->irq
, qoriq_ptp
);
552 static int qoriq_ptp_remove(struct platform_device
*dev
)
554 struct qoriq_ptp
*qoriq_ptp
= platform_get_drvdata(dev
);
555 struct qoriq_ptp_registers
*regs
= &qoriq_ptp
->regs
;
557 qoriq_write(®s
->ctrl_regs
->tmr_temask
, 0);
558 qoriq_write(®s
->ctrl_regs
->tmr_ctrl
, 0);
560 ptp_clock_unregister(qoriq_ptp
->clock
);
561 iounmap(qoriq_ptp
->base
);
562 release_resource(qoriq_ptp
->rsrc
);
563 free_irq(qoriq_ptp
->irq
, qoriq_ptp
);
569 static const struct of_device_id match_table
[] = {
570 { .compatible
= "fsl,etsec-ptp" },
571 { .compatible
= "fsl,fman-ptp-timer" },
574 MODULE_DEVICE_TABLE(of
, match_table
);
576 static struct platform_driver qoriq_ptp_driver
= {
579 .of_match_table
= match_table
,
581 .probe
= qoriq_ptp_probe
,
582 .remove
= qoriq_ptp_remove
,
585 module_platform_driver(qoriq_ptp_driver
);
587 MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
588 MODULE_DESCRIPTION("PTP clock for Freescale QorIQ 1588 timer");
589 MODULE_LICENSE("GPL");