1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * PTP 1588 clock using the IXP46X
5 * Copyright (C) 2010 OMICRON electronics GmbH
7 #include <linux/device.h>
8 #include <linux/module.h>
9 #include <linux/mod_devicetable.h>
10 #include <linux/err.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
14 #include <linux/irq.h>
15 #include <linux/kernel.h>
16 #include <linux/ptp_clock_kernel.h>
17 #include <linux/platform_device.h>
18 #include <linux/soc/ixp4xx/cpu.h>
20 #include "ixp46x_ts.h"
22 #define DRIVER "ptp_ixp46x"
26 struct ixp46x_ts_regs *regs;
27 struct ptp_clock *ptp_clock;
28 struct ptp_clock_info caps;
35 static DEFINE_SPINLOCK(register_lock);
38 * Register access functions
41 static u64 ixp_systime_read(struct ixp46x_ts_regs *regs)
46 lo = __raw_readl(®s->systime_lo);
47 hi = __raw_readl(®s->systime_hi);
49 ns = ((u64) hi) << 32;
51 ns <<= TICKS_NS_SHIFT;
56 static void ixp_systime_write(struct ixp46x_ts_regs *regs, u64 ns)
60 ns >>= TICKS_NS_SHIFT;
64 __raw_writel(lo, ®s->systime_lo);
65 __raw_writel(hi, ®s->systime_hi);
69 * Interrupt service routine
72 static irqreturn_t isr(int irq, void *priv)
74 struct ixp_clock *ixp_clock = priv;
75 struct ixp46x_ts_regs *regs = ixp_clock->regs;
76 struct ptp_clock_event event;
77 u32 ack = 0, lo, hi, val;
79 val = __raw_readl(®s->event);
83 if (ixp_clock->exts0_enabled) {
84 hi = __raw_readl(®s->asms_hi);
85 lo = __raw_readl(®s->asms_lo);
86 event.type = PTP_CLOCK_EXTTS;
88 event.timestamp = ((u64) hi) << 32;
89 event.timestamp |= lo;
90 event.timestamp <<= TICKS_NS_SHIFT;
91 ptp_clock_event(ixp_clock->ptp_clock, &event);
97 if (ixp_clock->exts1_enabled) {
98 hi = __raw_readl(®s->amms_hi);
99 lo = __raw_readl(®s->amms_lo);
100 event.type = PTP_CLOCK_EXTTS;
102 event.timestamp = ((u64) hi) << 32;
103 event.timestamp |= lo;
104 event.timestamp <<= TICKS_NS_SHIFT;
105 ptp_clock_event(ixp_clock->ptp_clock, &event);
110 ack |= TTIPEND; /* this bit seems to be always set */
113 __raw_writel(ack, ®s->event);
120 * PTP clock operations
123 static int ptp_ixp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
126 struct ixp_clock *ixp_clock = container_of(ptp, struct ixp_clock, caps);
127 struct ixp46x_ts_regs *regs = ixp_clock->regs;
129 addend = adjust_by_scaled_ppm(DEFAULT_ADDEND, scaled_ppm);
131 __raw_writel(addend, ®s->addend);
136 static int ptp_ixp_adjtime(struct ptp_clock_info *ptp, s64 delta)
140 struct ixp_clock *ixp_clock = container_of(ptp, struct ixp_clock, caps);
141 struct ixp46x_ts_regs *regs = ixp_clock->regs;
143 spin_lock_irqsave(®ister_lock, flags);
145 now = ixp_systime_read(regs);
147 ixp_systime_write(regs, now);
149 spin_unlock_irqrestore(®ister_lock, flags);
154 static int ptp_ixp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
158 struct ixp_clock *ixp_clock = container_of(ptp, struct ixp_clock, caps);
159 struct ixp46x_ts_regs *regs = ixp_clock->regs;
161 spin_lock_irqsave(®ister_lock, flags);
163 ns = ixp_systime_read(regs);
165 spin_unlock_irqrestore(®ister_lock, flags);
167 *ts = ns_to_timespec64(ns);
171 static int ptp_ixp_settime(struct ptp_clock_info *ptp,
172 const struct timespec64 *ts)
176 struct ixp_clock *ixp_clock = container_of(ptp, struct ixp_clock, caps);
177 struct ixp46x_ts_regs *regs = ixp_clock->regs;
179 ns = timespec64_to_ns(ts);
181 spin_lock_irqsave(®ister_lock, flags);
183 ixp_systime_write(regs, ns);
185 spin_unlock_irqrestore(®ister_lock, flags);
190 static int ptp_ixp_enable(struct ptp_clock_info *ptp,
191 struct ptp_clock_request *rq, int on)
193 struct ixp_clock *ixp_clock = container_of(ptp, struct ixp_clock, caps);
196 case PTP_CLK_REQ_EXTTS:
197 switch (rq->extts.index) {
199 ixp_clock->exts0_enabled = on ? 1 : 0;
202 ixp_clock->exts1_enabled = on ? 1 : 0;
215 static const struct ptp_clock_info ptp_ixp_caps = {
216 .owner = THIS_MODULE,
217 .name = "IXP46X timer",
219 .n_ext_ts = N_EXT_TS,
222 .adjfine = ptp_ixp_adjfine,
223 .adjtime = ptp_ixp_adjtime,
224 .gettime64 = ptp_ixp_gettime,
225 .settime64 = ptp_ixp_settime,
226 .enable = ptp_ixp_enable,
229 /* module operations */
231 static struct ixp_clock ixp_clock;
233 int ixp46x_ptp_find(struct ixp46x_ts_regs *__iomem *regs, int *phc_index)
235 *regs = ixp_clock.regs;
236 *phc_index = ptp_clock_index(ixp_clock.ptp_clock);
238 if (!ixp_clock.ptp_clock)
239 return -EPROBE_DEFER;
243 EXPORT_SYMBOL_GPL(ixp46x_ptp_find);
245 /* Called from the registered devm action */
246 static void ptp_ixp_unregister_action(void *d)
248 struct ptp_clock *ptp_clock = d;
250 ptp_clock_unregister(ptp_clock);
251 ixp_clock.ptp_clock = NULL;
254 static int ptp_ixp_probe(struct platform_device *pdev)
256 struct device *dev = &pdev->dev;
259 ixp_clock.regs = devm_platform_ioremap_resource(pdev, 0);
260 ixp_clock.master_irq = platform_get_irq(pdev, 0);
261 ixp_clock.slave_irq = platform_get_irq(pdev, 1);
262 if (IS_ERR(ixp_clock.regs) ||
263 ixp_clock.master_irq < 0 || ixp_clock.slave_irq < 0)
266 ixp_clock.caps = ptp_ixp_caps;
268 ixp_clock.ptp_clock = ptp_clock_register(&ixp_clock.caps, NULL);
270 if (IS_ERR(ixp_clock.ptp_clock))
271 return PTR_ERR(ixp_clock.ptp_clock);
273 ret = devm_add_action_or_reset(dev, ptp_ixp_unregister_action,
274 ixp_clock.ptp_clock);
276 dev_err(dev, "failed to install clock removal handler\n");
280 __raw_writel(DEFAULT_ADDEND, &ixp_clock.regs->addend);
281 __raw_writel(1, &ixp_clock.regs->trgt_lo);
282 __raw_writel(0, &ixp_clock.regs->trgt_hi);
283 __raw_writel(TTIPEND, &ixp_clock.regs->event);
285 ret = devm_request_irq(dev, ixp_clock.master_irq, isr,
286 0, DRIVER, &ixp_clock);
288 return dev_err_probe(dev, ret,
289 "request_irq failed for irq %d\n",
290 ixp_clock.master_irq);
292 ret = devm_request_irq(dev, ixp_clock.slave_irq, isr,
293 0, DRIVER, &ixp_clock);
295 return dev_err_probe(dev, ret,
296 "request_irq failed for irq %d\n",
297 ixp_clock.slave_irq);
302 static const struct of_device_id ptp_ixp_match[] = {
304 .compatible = "intel,ixp46x-ptp-timer",
309 static struct platform_driver ptp_ixp_driver = {
311 .name = "ptp-ixp46x",
312 .of_match_table = ptp_ixp_match,
313 .suppress_bind_attrs = true,
315 .probe = ptp_ixp_probe,
317 module_platform_driver(ptp_ixp_driver);
319 MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
320 MODULE_DESCRIPTION("PTP clock using the IXP46X timer");
321 MODULE_LICENSE("GPL");