1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * PTP 1588 clock using the IXP46X
4  *
5  * Copyright (C) 2010 OMICRON electronics GmbH
6  */
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>
13 #include <linux/io.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>
19 #include <mach/ixp4xx-regs.h>
20 
21 #include "ixp46x_ts.h"
22 
23 #define DRIVER		"ptp_ixp46x"
24 #define N_EXT_TS	2
25 
26 struct ixp_clock {
27 	struct ixp46x_ts_regs *regs;
28 	struct ptp_clock *ptp_clock;
29 	struct ptp_clock_info caps;
30 	int exts0_enabled;
31 	int exts1_enabled;
32 	int slave_irq;
33 	int master_irq;
34 };
35 
36 static DEFINE_SPINLOCK(register_lock);
37 
38 /*
39  * Register access functions
40  */
41 
42 static u64 ixp_systime_read(struct ixp46x_ts_regs *regs)
43 {
44 	u64 ns;
45 	u32 lo, hi;
46 
47 	lo = __raw_readl(&regs->systime_lo);
48 	hi = __raw_readl(&regs->systime_hi);
49 
50 	ns = ((u64) hi) << 32;
51 	ns |= lo;
52 	ns <<= TICKS_NS_SHIFT;
53 
54 	return ns;
55 }
56 
57 static void ixp_systime_write(struct ixp46x_ts_regs *regs, u64 ns)
58 {
59 	u32 hi, lo;
60 
61 	ns >>= TICKS_NS_SHIFT;
62 	hi = ns >> 32;
63 	lo = ns & 0xffffffff;
64 
65 	__raw_writel(lo, &regs->systime_lo);
66 	__raw_writel(hi, &regs->systime_hi);
67 }
68 
69 /*
70  * Interrupt service routine
71  */
72 
73 static irqreturn_t isr(int irq, void *priv)
74 {
75 	struct ixp_clock *ixp_clock = priv;
76 	struct ixp46x_ts_regs *regs = ixp_clock->regs;
77 	struct ptp_clock_event event;
78 	u32 ack = 0, lo, hi, val;
79 
80 	val = __raw_readl(&regs->event);
81 
82 	if (val & TSER_SNS) {
83 		ack |= TSER_SNS;
84 		if (ixp_clock->exts0_enabled) {
85 			hi = __raw_readl(&regs->asms_hi);
86 			lo = __raw_readl(&regs->asms_lo);
87 			event.type = PTP_CLOCK_EXTTS;
88 			event.index = 0;
89 			event.timestamp = ((u64) hi) << 32;
90 			event.timestamp |= lo;
91 			event.timestamp <<= TICKS_NS_SHIFT;
92 			ptp_clock_event(ixp_clock->ptp_clock, &event);
93 		}
94 	}
95 
96 	if (val & TSER_SNM) {
97 		ack |= TSER_SNM;
98 		if (ixp_clock->exts1_enabled) {
99 			hi = __raw_readl(&regs->amms_hi);
100 			lo = __raw_readl(&regs->amms_lo);
101 			event.type = PTP_CLOCK_EXTTS;
102 			event.index = 1;
103 			event.timestamp = ((u64) hi) << 32;
104 			event.timestamp |= lo;
105 			event.timestamp <<= TICKS_NS_SHIFT;
106 			ptp_clock_event(ixp_clock->ptp_clock, &event);
107 		}
108 	}
109 
110 	if (val & TTIPEND)
111 		ack |= TTIPEND; /* this bit seems to be always set */
112 
113 	if (ack) {
114 		__raw_writel(ack, &regs->event);
115 		return IRQ_HANDLED;
116 	} else
117 		return IRQ_NONE;
118 }
119 
120 /*
121  * PTP clock operations
122  */
123 
124 static int ptp_ixp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
125 {
126 	u64 adj;
127 	u32 diff, addend;
128 	int neg_adj = 0;
129 	struct ixp_clock *ixp_clock = container_of(ptp, struct ixp_clock, caps);
130 	struct ixp46x_ts_regs *regs = ixp_clock->regs;
131 
132 	if (ppb < 0) {
133 		neg_adj = 1;
134 		ppb = -ppb;
135 	}
136 	addend = DEFAULT_ADDEND;
137 	adj = addend;
138 	adj *= ppb;
139 	diff = div_u64(adj, 1000000000ULL);
140 
141 	addend = neg_adj ? addend - diff : addend + diff;
142 
143 	__raw_writel(addend, &regs->addend);
144 
145 	return 0;
146 }
147 
148 static int ptp_ixp_adjtime(struct ptp_clock_info *ptp, s64 delta)
149 {
150 	s64 now;
151 	unsigned long flags;
152 	struct ixp_clock *ixp_clock = container_of(ptp, struct ixp_clock, caps);
153 	struct ixp46x_ts_regs *regs = ixp_clock->regs;
154 
155 	spin_lock_irqsave(&register_lock, flags);
156 
157 	now = ixp_systime_read(regs);
158 	now += delta;
159 	ixp_systime_write(regs, now);
160 
161 	spin_unlock_irqrestore(&register_lock, flags);
162 
163 	return 0;
164 }
165 
166 static int ptp_ixp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
167 {
168 	u64 ns;
169 	unsigned long flags;
170 	struct ixp_clock *ixp_clock = container_of(ptp, struct ixp_clock, caps);
171 	struct ixp46x_ts_regs *regs = ixp_clock->regs;
172 
173 	spin_lock_irqsave(&register_lock, flags);
174 
175 	ns = ixp_systime_read(regs);
176 
177 	spin_unlock_irqrestore(&register_lock, flags);
178 
179 	*ts = ns_to_timespec64(ns);
180 	return 0;
181 }
182 
183 static int ptp_ixp_settime(struct ptp_clock_info *ptp,
184 			   const struct timespec64 *ts)
185 {
186 	u64 ns;
187 	unsigned long flags;
188 	struct ixp_clock *ixp_clock = container_of(ptp, struct ixp_clock, caps);
189 	struct ixp46x_ts_regs *regs = ixp_clock->regs;
190 
191 	ns = timespec64_to_ns(ts);
192 
193 	spin_lock_irqsave(&register_lock, flags);
194 
195 	ixp_systime_write(regs, ns);
196 
197 	spin_unlock_irqrestore(&register_lock, flags);
198 
199 	return 0;
200 }
201 
202 static int ptp_ixp_enable(struct ptp_clock_info *ptp,
203 			  struct ptp_clock_request *rq, int on)
204 {
205 	struct ixp_clock *ixp_clock = container_of(ptp, struct ixp_clock, caps);
206 
207 	switch (rq->type) {
208 	case PTP_CLK_REQ_EXTTS:
209 		switch (rq->extts.index) {
210 		case 0:
211 			ixp_clock->exts0_enabled = on ? 1 : 0;
212 			break;
213 		case 1:
214 			ixp_clock->exts1_enabled = on ? 1 : 0;
215 			break;
216 		default:
217 			return -EINVAL;
218 		}
219 		return 0;
220 	default:
221 		break;
222 	}
223 
224 	return -EOPNOTSUPP;
225 }
226 
227 static const struct ptp_clock_info ptp_ixp_caps = {
228 	.owner		= THIS_MODULE,
229 	.name		= "IXP46X timer",
230 	.max_adj	= 66666655,
231 	.n_ext_ts	= N_EXT_TS,
232 	.n_pins		= 0,
233 	.pps		= 0,
234 	.adjfreq	= ptp_ixp_adjfreq,
235 	.adjtime	= ptp_ixp_adjtime,
236 	.gettime64	= ptp_ixp_gettime,
237 	.settime64	= ptp_ixp_settime,
238 	.enable		= ptp_ixp_enable,
239 };
240 
241 /* module operations */
242 
243 static struct ixp_clock ixp_clock;
244 
245 int ixp46x_ptp_find(struct ixp46x_ts_regs *__iomem *regs, int *phc_index)
246 {
247 	*regs = ixp_clock.regs;
248 	*phc_index = ptp_clock_index(ixp_clock.ptp_clock);
249 
250 	if (!ixp_clock.ptp_clock)
251 		return -EPROBE_DEFER;
252 
253 	return 0;
254 }
255 EXPORT_SYMBOL_GPL(ixp46x_ptp_find);
256 
257 /* Called from the registered devm action */
258 static void ptp_ixp_unregister_action(void *d)
259 {
260 	struct ptp_clock *ptp_clock = d;
261 
262 	ptp_clock_unregister(ptp_clock);
263 	ixp_clock.ptp_clock = NULL;
264 }
265 
266 static int ptp_ixp_probe(struct platform_device *pdev)
267 {
268 	struct device *dev = &pdev->dev;
269 	int ret;
270 
271 	ixp_clock.regs = devm_platform_ioremap_resource(pdev, 0);
272 	ixp_clock.master_irq = platform_get_irq(pdev, 0);
273 	ixp_clock.slave_irq = platform_get_irq(pdev, 1);
274 	if (IS_ERR(ixp_clock.regs) ||
275 	    !ixp_clock.master_irq || !ixp_clock.slave_irq)
276 		return -ENXIO;
277 
278 	ixp_clock.caps = ptp_ixp_caps;
279 
280 	ixp_clock.ptp_clock = ptp_clock_register(&ixp_clock.caps, NULL);
281 
282 	if (IS_ERR(ixp_clock.ptp_clock))
283 		return PTR_ERR(ixp_clock.ptp_clock);
284 
285 	ret = devm_add_action_or_reset(dev, ptp_ixp_unregister_action,
286 				       ixp_clock.ptp_clock);
287 	if (ret) {
288 		dev_err(dev, "failed to install clock removal handler\n");
289 		return ret;
290 	}
291 
292 	__raw_writel(DEFAULT_ADDEND, &ixp_clock.regs->addend);
293 	__raw_writel(1, &ixp_clock.regs->trgt_lo);
294 	__raw_writel(0, &ixp_clock.regs->trgt_hi);
295 	__raw_writel(TTIPEND, &ixp_clock.regs->event);
296 
297 	ret = devm_request_irq(dev, ixp_clock.master_irq, isr,
298 			       0, DRIVER, &ixp_clock);
299 	if (ret)
300 		return dev_err_probe(dev, ret,
301 				     "request_irq failed for irq %d\n",
302 				     ixp_clock.master_irq);
303 
304 	ret = devm_request_irq(dev, ixp_clock.slave_irq, isr,
305 			       0, DRIVER, &ixp_clock);
306 	if (ret)
307 		return dev_err_probe(dev, ret,
308 				     "request_irq failed for irq %d\n",
309 				     ixp_clock.slave_irq);
310 
311 	return 0;
312 }
313 
314 static const struct of_device_id ptp_ixp_match[] = {
315 	{
316 		.compatible = "intel,ixp46x-ptp-timer",
317 	},
318 	{ },
319 };
320 
321 static struct platform_driver ptp_ixp_driver = {
322 	.driver = {
323 		.name = "ptp-ixp46x",
324 		.of_match_table = ptp_ixp_match,
325 		.suppress_bind_attrs = true,
326 	},
327 	.probe = ptp_ixp_probe,
328 };
329 module_platform_driver(ptp_ixp_driver);
330 
331 MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
332 MODULE_DESCRIPTION("PTP clock using the IXP46X timer");
333 MODULE_LICENSE("GPL");
334