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>
33 #include <linux/fsl/ptp_qoriq.h>
36 * Register access functions
39 /* Caller must hold qoriq_ptp->lock. */
40 static u64 tmr_cnt_read(struct qoriq_ptp *qoriq_ptp)
45 lo = qoriq_read(&qoriq_ptp->regs->tmr_cnt_l);
46 hi = qoriq_read(&qoriq_ptp->regs->tmr_cnt_h);
47 ns = ((u64) hi) << 32;
52 /* Caller must hold qoriq_ptp->lock. */
53 static void tmr_cnt_write(struct qoriq_ptp *qoriq_ptp, u64 ns)
56 u32 lo = ns & 0xffffffff;
58 qoriq_write(&qoriq_ptp->regs->tmr_cnt_l, lo);
59 qoriq_write(&qoriq_ptp->regs->tmr_cnt_h, hi);
62 /* Caller must hold qoriq_ptp->lock. */
63 static void set_alarm(struct qoriq_ptp *qoriq_ptp)
68 ns = tmr_cnt_read(qoriq_ptp) + 1500000000ULL;
69 ns = div_u64(ns, 1000000000UL) * 1000000000ULL;
70 ns -= qoriq_ptp->tclk_period;
73 qoriq_write(&qoriq_ptp->regs->tmr_alarm1_l, lo);
74 qoriq_write(&qoriq_ptp->regs->tmr_alarm1_h, hi);
77 /* Caller must hold qoriq_ptp->lock. */
78 static void set_fipers(struct qoriq_ptp *qoriq_ptp)
81 qoriq_write(&qoriq_ptp->regs->tmr_fiper1, qoriq_ptp->tmr_fiper1);
82 qoriq_write(&qoriq_ptp->regs->tmr_fiper2, qoriq_ptp->tmr_fiper2);
86 * Interrupt service routine
89 static irqreturn_t isr(int irq, void *priv)
91 struct qoriq_ptp *qoriq_ptp = priv;
92 struct ptp_clock_event event;
94 u32 ack = 0, lo, hi, mask, val;
96 val = qoriq_read(&qoriq_ptp->regs->tmr_tevent);
100 hi = qoriq_read(&qoriq_ptp->regs->tmr_etts1_h);
101 lo = qoriq_read(&qoriq_ptp->regs->tmr_etts1_l);
102 event.type = PTP_CLOCK_EXTTS;
104 event.timestamp = ((u64) hi) << 32;
105 event.timestamp |= lo;
106 ptp_clock_event(qoriq_ptp->clock, &event);
111 hi = qoriq_read(&qoriq_ptp->regs->tmr_etts2_h);
112 lo = qoriq_read(&qoriq_ptp->regs->tmr_etts2_l);
113 event.type = PTP_CLOCK_EXTTS;
115 event.timestamp = ((u64) hi) << 32;
116 event.timestamp |= lo;
117 ptp_clock_event(qoriq_ptp->clock, &event);
122 if (qoriq_ptp->alarm_value) {
123 event.type = PTP_CLOCK_ALARM;
125 event.timestamp = qoriq_ptp->alarm_value;
126 ptp_clock_event(qoriq_ptp->clock, &event);
128 if (qoriq_ptp->alarm_interval) {
129 ns = qoriq_ptp->alarm_value + qoriq_ptp->alarm_interval;
131 lo = ns & 0xffffffff;
132 spin_lock(&qoriq_ptp->lock);
133 qoriq_write(&qoriq_ptp->regs->tmr_alarm2_l, lo);
134 qoriq_write(&qoriq_ptp->regs->tmr_alarm2_h, hi);
135 spin_unlock(&qoriq_ptp->lock);
136 qoriq_ptp->alarm_value = ns;
138 qoriq_write(&qoriq_ptp->regs->tmr_tevent, ALM2);
139 spin_lock(&qoriq_ptp->lock);
140 mask = qoriq_read(&qoriq_ptp->regs->tmr_temask);
142 qoriq_write(&qoriq_ptp->regs->tmr_temask, mask);
143 spin_unlock(&qoriq_ptp->lock);
144 qoriq_ptp->alarm_value = 0;
145 qoriq_ptp->alarm_interval = 0;
151 event.type = PTP_CLOCK_PPS;
152 ptp_clock_event(qoriq_ptp->clock, &event);
156 qoriq_write(&qoriq_ptp->regs->tmr_tevent, ack);
163 * PTP clock operations
166 static int ptp_qoriq_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
171 struct qoriq_ptp *qoriq_ptp = container_of(ptp, struct qoriq_ptp, caps);
173 if (scaled_ppm < 0) {
175 scaled_ppm = -scaled_ppm;
177 tmr_add = qoriq_ptp->tmr_add;
180 /* calculate diff as adj*(scaled_ppm/65536)/1000000
181 * and round() to the nearest integer
184 diff = div_u64(adj, 8000000);
185 diff = (diff >> 13) + ((diff >> 12) & 1);
187 tmr_add = neg_adj ? tmr_add - diff : tmr_add + diff;
189 qoriq_write(&qoriq_ptp->regs->tmr_add, tmr_add);
194 static int ptp_qoriq_adjtime(struct ptp_clock_info *ptp, s64 delta)
198 struct qoriq_ptp *qoriq_ptp = container_of(ptp, struct qoriq_ptp, caps);
200 spin_lock_irqsave(&qoriq_ptp->lock, flags);
202 now = tmr_cnt_read(qoriq_ptp);
204 tmr_cnt_write(qoriq_ptp, now);
205 set_fipers(qoriq_ptp);
207 spin_unlock_irqrestore(&qoriq_ptp->lock, flags);
212 static int ptp_qoriq_gettime(struct ptp_clock_info *ptp,
213 struct timespec64 *ts)
217 struct qoriq_ptp *qoriq_ptp = container_of(ptp, struct qoriq_ptp, caps);
219 spin_lock_irqsave(&qoriq_ptp->lock, flags);
221 ns = tmr_cnt_read(qoriq_ptp);
223 spin_unlock_irqrestore(&qoriq_ptp->lock, flags);
225 *ts = ns_to_timespec64(ns);
230 static int ptp_qoriq_settime(struct ptp_clock_info *ptp,
231 const struct timespec64 *ts)
235 struct qoriq_ptp *qoriq_ptp = container_of(ptp, struct qoriq_ptp, caps);
237 ns = timespec64_to_ns(ts);
239 spin_lock_irqsave(&qoriq_ptp->lock, flags);
241 tmr_cnt_write(qoriq_ptp, ns);
242 set_fipers(qoriq_ptp);
244 spin_unlock_irqrestore(&qoriq_ptp->lock, flags);
249 static int ptp_qoriq_enable(struct ptp_clock_info *ptp,
250 struct ptp_clock_request *rq, int on)
252 struct qoriq_ptp *qoriq_ptp = container_of(ptp, struct qoriq_ptp, caps);
257 case PTP_CLK_REQ_EXTTS:
258 switch (rq->extts.index) {
268 spin_lock_irqsave(&qoriq_ptp->lock, flags);
269 mask = qoriq_read(&qoriq_ptp->regs->tmr_temask);
274 qoriq_write(&qoriq_ptp->regs->tmr_temask, mask);
275 spin_unlock_irqrestore(&qoriq_ptp->lock, flags);
278 case PTP_CLK_REQ_PPS:
279 spin_lock_irqsave(&qoriq_ptp->lock, flags);
280 mask = qoriq_read(&qoriq_ptp->regs->tmr_temask);
285 qoriq_write(&qoriq_ptp->regs->tmr_temask, mask);
286 spin_unlock_irqrestore(&qoriq_ptp->lock, flags);
296 static const struct ptp_clock_info ptp_qoriq_caps = {
297 .owner = THIS_MODULE,
298 .name = "qoriq ptp clock",
301 .n_ext_ts = N_EXT_TS,
305 .adjfine = ptp_qoriq_adjfine,
306 .adjtime = ptp_qoriq_adjtime,
307 .gettime64 = ptp_qoriq_gettime,
308 .settime64 = ptp_qoriq_settime,
309 .enable = ptp_qoriq_enable,
312 static int qoriq_ptp_probe(struct platform_device *dev)
314 struct device_node *node = dev->dev.of_node;
315 struct qoriq_ptp *qoriq_ptp;
316 struct timespec64 now;
321 qoriq_ptp = kzalloc(sizeof(*qoriq_ptp), GFP_KERNEL);
327 qoriq_ptp->caps = ptp_qoriq_caps;
329 if (of_property_read_u32(node, "fsl,cksel", &qoriq_ptp->cksel))
330 qoriq_ptp->cksel = DEFAULT_CKSEL;
332 if (of_property_read_u32(node,
333 "fsl,tclk-period", &qoriq_ptp->tclk_period) ||
334 of_property_read_u32(node,
335 "fsl,tmr-prsc", &qoriq_ptp->tmr_prsc) ||
336 of_property_read_u32(node,
337 "fsl,tmr-add", &qoriq_ptp->tmr_add) ||
338 of_property_read_u32(node,
339 "fsl,tmr-fiper1", &qoriq_ptp->tmr_fiper1) ||
340 of_property_read_u32(node,
341 "fsl,tmr-fiper2", &qoriq_ptp->tmr_fiper2) ||
342 of_property_read_u32(node,
343 "fsl,max-adj", &qoriq_ptp->caps.max_adj)) {
344 pr_err("device tree node missing required elements\n");
348 qoriq_ptp->irq = platform_get_irq(dev, 0);
350 if (qoriq_ptp->irq < 0) {
351 pr_err("irq not in device tree\n");
354 if (request_irq(qoriq_ptp->irq, isr, 0, DRIVER, qoriq_ptp)) {
355 pr_err("request_irq failed\n");
359 qoriq_ptp->rsrc = platform_get_resource(dev, IORESOURCE_MEM, 0);
360 if (!qoriq_ptp->rsrc) {
361 pr_err("no resource\n");
364 if (request_resource(&iomem_resource, qoriq_ptp->rsrc)) {
365 pr_err("resource busy\n");
369 spin_lock_init(&qoriq_ptp->lock);
371 qoriq_ptp->regs = ioremap(qoriq_ptp->rsrc->start,
372 resource_size(qoriq_ptp->rsrc));
373 if (!qoriq_ptp->regs) {
374 pr_err("ioremap ptp registers failed\n");
377 ktime_get_real_ts64(&now);
378 ptp_qoriq_settime(&qoriq_ptp->caps, &now);
381 (qoriq_ptp->tclk_period & TCLK_PERIOD_MASK) << TCLK_PERIOD_SHIFT |
382 (qoriq_ptp->cksel & CKSEL_MASK) << CKSEL_SHIFT;
384 spin_lock_irqsave(&qoriq_ptp->lock, flags);
386 qoriq_write(&qoriq_ptp->regs->tmr_ctrl, tmr_ctrl);
387 qoriq_write(&qoriq_ptp->regs->tmr_add, qoriq_ptp->tmr_add);
388 qoriq_write(&qoriq_ptp->regs->tmr_prsc, qoriq_ptp->tmr_prsc);
389 qoriq_write(&qoriq_ptp->regs->tmr_fiper1, qoriq_ptp->tmr_fiper1);
390 qoriq_write(&qoriq_ptp->regs->tmr_fiper2, qoriq_ptp->tmr_fiper2);
391 set_alarm(qoriq_ptp);
392 qoriq_write(&qoriq_ptp->regs->tmr_ctrl, tmr_ctrl|FIPERST|RTPE|TE|FRD);
394 spin_unlock_irqrestore(&qoriq_ptp->lock, flags);
396 qoriq_ptp->clock = ptp_clock_register(&qoriq_ptp->caps, &dev->dev);
397 if (IS_ERR(qoriq_ptp->clock)) {
398 err = PTR_ERR(qoriq_ptp->clock);
401 qoriq_ptp->phc_index = ptp_clock_index(qoriq_ptp->clock);
403 platform_set_drvdata(dev, qoriq_ptp);
408 iounmap(qoriq_ptp->regs);
410 release_resource(qoriq_ptp->rsrc);
412 free_irq(qoriq_ptp->irq, qoriq_ptp);
419 static int qoriq_ptp_remove(struct platform_device *dev)
421 struct qoriq_ptp *qoriq_ptp = platform_get_drvdata(dev);
423 qoriq_write(&qoriq_ptp->regs->tmr_temask, 0);
424 qoriq_write(&qoriq_ptp->regs->tmr_ctrl, 0);
426 ptp_clock_unregister(qoriq_ptp->clock);
427 iounmap(qoriq_ptp->regs);
428 release_resource(qoriq_ptp->rsrc);
429 free_irq(qoriq_ptp->irq, qoriq_ptp);
435 static const struct of_device_id match_table[] = {
436 { .compatible = "fsl,etsec-ptp" },
439 MODULE_DEVICE_TABLE(of, match_table);
441 static struct platform_driver qoriq_ptp_driver = {
444 .of_match_table = match_table,
446 .probe = qoriq_ptp_probe,
447 .remove = qoriq_ptp_remove,
450 module_platform_driver(qoriq_ptp_driver);
452 MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
453 MODULE_DESCRIPTION("PTP clock for Freescale QorIQ 1588 timer");
454 MODULE_LICENSE("GPL");