1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * PTP 1588 clock for Freescale QorIQ 1588 timer
5 * Copyright (C) 2010 OMICRON electronics GmbH
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/device.h>
11 #include <linux/hrtimer.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
15 #include <linux/of_platform.h>
16 #include <linux/timex.h>
17 #include <linux/slab.h>
18 #include <linux/clk.h>
20 #include <linux/fsl/ptp_qoriq.h>
23 * Register access functions
26 /* Caller must hold ptp_qoriq->lock. */
27 static u64 tmr_cnt_read(struct ptp_qoriq *ptp_qoriq)
29 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
33 lo = ptp_qoriq->read(®s->ctrl_regs->tmr_cnt_l);
34 hi = ptp_qoriq->read(®s->ctrl_regs->tmr_cnt_h);
35 ns = ((u64) hi) << 32;
40 /* Caller must hold ptp_qoriq->lock. */
41 static void tmr_cnt_write(struct ptp_qoriq *ptp_qoriq, u64 ns)
43 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
45 u32 lo = ns & 0xffffffff;
47 ptp_qoriq->write(®s->ctrl_regs->tmr_cnt_l, lo);
48 ptp_qoriq->write(®s->ctrl_regs->tmr_cnt_h, hi);
51 /* Caller must hold ptp_qoriq->lock. */
52 static void set_alarm(struct ptp_qoriq *ptp_qoriq)
54 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
58 ns = tmr_cnt_read(ptp_qoriq) + 1500000000ULL;
59 ns = div_u64(ns, 1000000000UL) * 1000000000ULL;
60 ns -= ptp_qoriq->tclk_period;
63 ptp_qoriq->write(®s->alarm_regs->tmr_alarm1_l, lo);
64 ptp_qoriq->write(®s->alarm_regs->tmr_alarm1_h, hi);
67 /* Caller must hold ptp_qoriq->lock. */
68 static void set_fipers(struct ptp_qoriq *ptp_qoriq)
70 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
73 ptp_qoriq->write(®s->fiper_regs->tmr_fiper1, ptp_qoriq->tmr_fiper1);
74 ptp_qoriq->write(®s->fiper_regs->tmr_fiper2, ptp_qoriq->tmr_fiper2);
76 if (ptp_qoriq->fiper3_support)
77 ptp_qoriq->write(®s->fiper_regs->tmr_fiper3,
78 ptp_qoriq->tmr_fiper3);
81 int extts_clean_up(struct ptp_qoriq *ptp_qoriq, int index, bool update_event)
83 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
84 struct ptp_clock_event event;
85 void __iomem *reg_etts_l;
86 void __iomem *reg_etts_h;
92 reg_etts_l = ®s->etts_regs->tmr_etts1_l;
93 reg_etts_h = ®s->etts_regs->tmr_etts1_h;
97 reg_etts_l = ®s->etts_regs->tmr_etts2_l;
98 reg_etts_h = ®s->etts_regs->tmr_etts2_h;
104 event.type = PTP_CLOCK_EXTTS;
107 if (ptp_qoriq->extts_fifo_support)
108 if (!(ptp_qoriq->read(®s->ctrl_regs->tmr_stat) & valid))
112 lo = ptp_qoriq->read(reg_etts_l);
113 hi = ptp_qoriq->read(reg_etts_h);
116 event.timestamp = ((u64) hi) << 32;
117 event.timestamp |= lo;
118 ptp_clock_event(ptp_qoriq->clock, &event);
121 if (!ptp_qoriq->extts_fifo_support)
123 } while (ptp_qoriq->read(®s->ctrl_regs->tmr_stat) & valid);
127 EXPORT_SYMBOL_GPL(extts_clean_up);
130 * Interrupt service routine
133 irqreturn_t ptp_qoriq_isr(int irq, void *priv)
135 struct ptp_qoriq *ptp_qoriq = priv;
136 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
137 struct ptp_clock_event event;
138 u32 ack = 0, mask, val, irqs;
140 spin_lock(&ptp_qoriq->lock);
142 val = ptp_qoriq->read(®s->ctrl_regs->tmr_tevent);
143 mask = ptp_qoriq->read(®s->ctrl_regs->tmr_temask);
145 spin_unlock(&ptp_qoriq->lock);
151 extts_clean_up(ptp_qoriq, 0, true);
156 extts_clean_up(ptp_qoriq, 1, true);
161 event.type = PTP_CLOCK_PPS;
162 ptp_clock_event(ptp_qoriq->clock, &event);
166 ptp_qoriq->write(®s->ctrl_regs->tmr_tevent, ack);
171 EXPORT_SYMBOL_GPL(ptp_qoriq_isr);
174 * PTP clock operations
177 int ptp_qoriq_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
182 struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
183 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
185 if (scaled_ppm < 0) {
187 scaled_ppm = -scaled_ppm;
189 tmr_add = ptp_qoriq->tmr_add;
192 /* calculate diff as adj*(scaled_ppm/65536)/1000000
193 * and round() to the nearest integer
196 diff = div_u64(adj, 8000000);
197 diff = (diff >> 13) + ((diff >> 12) & 1);
199 tmr_add = neg_adj ? tmr_add - diff : tmr_add + diff;
201 ptp_qoriq->write(®s->ctrl_regs->tmr_add, tmr_add);
205 EXPORT_SYMBOL_GPL(ptp_qoriq_adjfine);
207 int ptp_qoriq_adjtime(struct ptp_clock_info *ptp, s64 delta)
211 struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
213 spin_lock_irqsave(&ptp_qoriq->lock, flags);
215 now = tmr_cnt_read(ptp_qoriq);
217 tmr_cnt_write(ptp_qoriq, now);
218 set_fipers(ptp_qoriq);
220 spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
224 EXPORT_SYMBOL_GPL(ptp_qoriq_adjtime);
226 int ptp_qoriq_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
230 struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
232 spin_lock_irqsave(&ptp_qoriq->lock, flags);
234 ns = tmr_cnt_read(ptp_qoriq);
236 spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
238 *ts = ns_to_timespec64(ns);
242 EXPORT_SYMBOL_GPL(ptp_qoriq_gettime);
244 int ptp_qoriq_settime(struct ptp_clock_info *ptp,
245 const struct timespec64 *ts)
249 struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
251 ns = timespec64_to_ns(ts);
253 spin_lock_irqsave(&ptp_qoriq->lock, flags);
255 tmr_cnt_write(ptp_qoriq, ns);
256 set_fipers(ptp_qoriq);
258 spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
262 EXPORT_SYMBOL_GPL(ptp_qoriq_settime);
264 int ptp_qoriq_enable(struct ptp_clock_info *ptp,
265 struct ptp_clock_request *rq, int on)
267 struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
268 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
273 case PTP_CLK_REQ_EXTTS:
274 switch (rq->extts.index) {
286 extts_clean_up(ptp_qoriq, rq->extts.index, false);
289 case PTP_CLK_REQ_PPS:
296 spin_lock_irqsave(&ptp_qoriq->lock, flags);
298 mask = ptp_qoriq->read(®s->ctrl_regs->tmr_temask);
301 ptp_qoriq->write(®s->ctrl_regs->tmr_tevent, bit);
306 ptp_qoriq->write(®s->ctrl_regs->tmr_temask, mask);
308 spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
311 EXPORT_SYMBOL_GPL(ptp_qoriq_enable);
313 static const struct ptp_clock_info ptp_qoriq_caps = {
314 .owner = THIS_MODULE,
315 .name = "qoriq ptp clock",
318 .n_ext_ts = N_EXT_TS,
322 .adjfine = ptp_qoriq_adjfine,
323 .adjtime = ptp_qoriq_adjtime,
324 .gettime64 = ptp_qoriq_gettime,
325 .settime64 = ptp_qoriq_settime,
326 .enable = ptp_qoriq_enable,
330 * ptp_qoriq_nominal_freq - calculate nominal frequency according to
331 * reference clock frequency
333 * @clk_src: reference clock frequency
335 * The nominal frequency is the desired clock frequency.
336 * It should be less than the reference clock frequency.
337 * It should be a factor of 1000MHz.
339 * Return the nominal frequency
341 static u32 ptp_qoriq_nominal_freq(u32 clk_src)
346 remainder = clk_src % 100;
348 clk_src -= remainder;
355 } while (1000 % clk_src);
357 return clk_src * 1000000;
361 * ptp_qoriq_auto_config - calculate a set of default configurations
363 * @ptp_qoriq: pointer to ptp_qoriq
364 * @node: pointer to device_node
366 * If below dts properties are not provided, this function will be
367 * called to calculate a set of default configurations for them.
373 * "fsl,tmr-fiper3" (required only for DPAA2 and ENETC hardware)
376 * Return 0 if success
378 static int ptp_qoriq_auto_config(struct ptp_qoriq *ptp_qoriq,
379 struct device_node *node)
388 ptp_qoriq->cksel = DEFAULT_CKSEL;
390 clk = of_clk_get(node, 0);
392 clk_src = clk_get_rate(clk);
396 if (clk_src <= 100000000UL) {
397 pr_err("error reference clock value, or lower than 100MHz\n");
401 nominal_freq = ptp_qoriq_nominal_freq(clk_src);
405 ptp_qoriq->tclk_period = 1000000000UL / nominal_freq;
406 ptp_qoriq->tmr_prsc = DEFAULT_TMR_PRSC;
408 /* Calculate initial frequency compensation value for TMR_ADD register.
409 * freq_comp = ceil(2^32 / freq_ratio)
410 * freq_ratio = reference_clock_freq / nominal_freq
412 freq_comp = ((u64)1 << 32) * nominal_freq;
413 freq_comp = div_u64_rem(freq_comp, clk_src, &remainder);
417 ptp_qoriq->tmr_add = freq_comp;
418 ptp_qoriq->tmr_fiper1 = DEFAULT_FIPER1_PERIOD - ptp_qoriq->tclk_period;
419 ptp_qoriq->tmr_fiper2 = DEFAULT_FIPER2_PERIOD - ptp_qoriq->tclk_period;
420 ptp_qoriq->tmr_fiper3 = DEFAULT_FIPER3_PERIOD - ptp_qoriq->tclk_period;
422 /* max_adj = 1000000000 * (freq_ratio - 1.0) - 1
423 * freq_ratio = reference_clock_freq / nominal_freq
425 max_adj = 1000000000ULL * (clk_src - nominal_freq);
426 max_adj = div_u64(max_adj, nominal_freq) - 1;
427 ptp_qoriq->caps.max_adj = max_adj;
432 int ptp_qoriq_init(struct ptp_qoriq *ptp_qoriq, void __iomem *base,
433 const struct ptp_clock_info *caps)
435 struct device_node *node = ptp_qoriq->dev->of_node;
436 struct ptp_qoriq_registers *regs;
437 struct timespec64 now;
444 ptp_qoriq->base = base;
445 ptp_qoriq->caps = *caps;
447 if (of_property_read_u32(node, "fsl,cksel", &ptp_qoriq->cksel))
448 ptp_qoriq->cksel = DEFAULT_CKSEL;
450 if (of_property_read_bool(node, "fsl,extts-fifo"))
451 ptp_qoriq->extts_fifo_support = true;
453 ptp_qoriq->extts_fifo_support = false;
455 if (of_device_is_compatible(node, "fsl,dpaa2-ptp") ||
456 of_device_is_compatible(node, "fsl,enetc-ptp"))
457 ptp_qoriq->fiper3_support = true;
459 if (of_property_read_u32(node,
460 "fsl,tclk-period", &ptp_qoriq->tclk_period) ||
461 of_property_read_u32(node,
462 "fsl,tmr-prsc", &ptp_qoriq->tmr_prsc) ||
463 of_property_read_u32(node,
464 "fsl,tmr-add", &ptp_qoriq->tmr_add) ||
465 of_property_read_u32(node,
466 "fsl,tmr-fiper1", &ptp_qoriq->tmr_fiper1) ||
467 of_property_read_u32(node,
468 "fsl,tmr-fiper2", &ptp_qoriq->tmr_fiper2) ||
469 of_property_read_u32(node,
470 "fsl,max-adj", &ptp_qoriq->caps.max_adj) ||
471 (ptp_qoriq->fiper3_support &&
472 of_property_read_u32(node, "fsl,tmr-fiper3",
473 &ptp_qoriq->tmr_fiper3))) {
474 pr_warn("device tree node missing required elements, try automatic configuration\n");
476 if (ptp_qoriq_auto_config(ptp_qoriq, node))
480 if (of_property_read_bool(node, "little-endian")) {
481 ptp_qoriq->read = qoriq_read_le;
482 ptp_qoriq->write = qoriq_write_le;
484 ptp_qoriq->read = qoriq_read_be;
485 ptp_qoriq->write = qoriq_write_be;
488 /* The eTSEC uses differnt memory map with DPAA/ENETC */
489 if (of_device_is_compatible(node, "fsl,etsec-ptp")) {
490 ptp_qoriq->regs.ctrl_regs = base + ETSEC_CTRL_REGS_OFFSET;
491 ptp_qoriq->regs.alarm_regs = base + ETSEC_ALARM_REGS_OFFSET;
492 ptp_qoriq->regs.fiper_regs = base + ETSEC_FIPER_REGS_OFFSET;
493 ptp_qoriq->regs.etts_regs = base + ETSEC_ETTS_REGS_OFFSET;
495 ptp_qoriq->regs.ctrl_regs = base + CTRL_REGS_OFFSET;
496 ptp_qoriq->regs.alarm_regs = base + ALARM_REGS_OFFSET;
497 ptp_qoriq->regs.fiper_regs = base + FIPER_REGS_OFFSET;
498 ptp_qoriq->regs.etts_regs = base + ETTS_REGS_OFFSET;
501 spin_lock_init(&ptp_qoriq->lock);
503 ktime_get_real_ts64(&now);
504 ptp_qoriq_settime(&ptp_qoriq->caps, &now);
507 (ptp_qoriq->tclk_period & TCLK_PERIOD_MASK) << TCLK_PERIOD_SHIFT |
508 (ptp_qoriq->cksel & CKSEL_MASK) << CKSEL_SHIFT;
510 spin_lock_irqsave(&ptp_qoriq->lock, flags);
512 regs = &ptp_qoriq->regs;
513 ptp_qoriq->write(®s->ctrl_regs->tmr_ctrl, tmr_ctrl);
514 ptp_qoriq->write(®s->ctrl_regs->tmr_add, ptp_qoriq->tmr_add);
515 ptp_qoriq->write(®s->ctrl_regs->tmr_prsc, ptp_qoriq->tmr_prsc);
516 ptp_qoriq->write(®s->fiper_regs->tmr_fiper1, ptp_qoriq->tmr_fiper1);
517 ptp_qoriq->write(®s->fiper_regs->tmr_fiper2, ptp_qoriq->tmr_fiper2);
519 if (ptp_qoriq->fiper3_support)
520 ptp_qoriq->write(®s->fiper_regs->tmr_fiper3,
521 ptp_qoriq->tmr_fiper3);
523 set_alarm(ptp_qoriq);
524 ptp_qoriq->write(®s->ctrl_regs->tmr_ctrl,
525 tmr_ctrl|FIPERST|RTPE|TE|FRD);
527 spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
529 ptp_qoriq->clock = ptp_clock_register(&ptp_qoriq->caps, ptp_qoriq->dev);
530 if (IS_ERR(ptp_qoriq->clock))
531 return PTR_ERR(ptp_qoriq->clock);
533 ptp_qoriq->phc_index = ptp_clock_index(ptp_qoriq->clock);
534 ptp_qoriq_create_debugfs(ptp_qoriq);
537 EXPORT_SYMBOL_GPL(ptp_qoriq_init);
539 void ptp_qoriq_free(struct ptp_qoriq *ptp_qoriq)
541 struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
543 ptp_qoriq->write(®s->ctrl_regs->tmr_temask, 0);
544 ptp_qoriq->write(®s->ctrl_regs->tmr_ctrl, 0);
546 ptp_qoriq_remove_debugfs(ptp_qoriq);
547 ptp_clock_unregister(ptp_qoriq->clock);
548 iounmap(ptp_qoriq->base);
549 free_irq(ptp_qoriq->irq, ptp_qoriq);
551 EXPORT_SYMBOL_GPL(ptp_qoriq_free);
553 static int ptp_qoriq_probe(struct platform_device *dev)
555 struct ptp_qoriq *ptp_qoriq;
559 ptp_qoriq = kzalloc(sizeof(*ptp_qoriq), GFP_KERNEL);
563 ptp_qoriq->dev = &dev->dev;
567 ptp_qoriq->irq = platform_get_irq(dev, 0);
568 if (ptp_qoriq->irq < 0) {
569 pr_err("irq not in device tree\n");
572 if (request_irq(ptp_qoriq->irq, ptp_qoriq_isr, IRQF_SHARED,
573 DRIVER, ptp_qoriq)) {
574 pr_err("request_irq failed\n");
578 ptp_qoriq->rsrc = platform_get_resource(dev, IORESOURCE_MEM, 0);
579 if (!ptp_qoriq->rsrc) {
580 pr_err("no resource\n");
583 if (request_resource(&iomem_resource, ptp_qoriq->rsrc)) {
584 pr_err("resource busy\n");
588 base = ioremap(ptp_qoriq->rsrc->start,
589 resource_size(ptp_qoriq->rsrc));
591 pr_err("ioremap ptp registers failed\n");
595 err = ptp_qoriq_init(ptp_qoriq, base, &ptp_qoriq_caps);
599 platform_set_drvdata(dev, ptp_qoriq);
603 iounmap(ptp_qoriq->base);
605 release_resource(ptp_qoriq->rsrc);
607 free_irq(ptp_qoriq->irq, ptp_qoriq);
614 static int ptp_qoriq_remove(struct platform_device *dev)
616 struct ptp_qoriq *ptp_qoriq = platform_get_drvdata(dev);
618 ptp_qoriq_free(ptp_qoriq);
619 release_resource(ptp_qoriq->rsrc);
624 static const struct of_device_id match_table[] = {
625 { .compatible = "fsl,etsec-ptp" },
626 { .compatible = "fsl,fman-ptp-timer" },
629 MODULE_DEVICE_TABLE(of, match_table);
631 static struct platform_driver ptp_qoriq_driver = {
634 .of_match_table = match_table,
636 .probe = ptp_qoriq_probe,
637 .remove = ptp_qoriq_remove,
640 module_platform_driver(ptp_qoriq_driver);
642 MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
643 MODULE_DESCRIPTION("PTP clock for Freescale QorIQ 1588 timer");
644 MODULE_LICENSE("GPL");