xref: /openbmc/linux/drivers/ptp/ptp_qoriq.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ceefc71dSYangbo Lu /*
3ceefc71dSYangbo Lu  * PTP 1588 clock for Freescale QorIQ 1588 timer
4ceefc71dSYangbo Lu  *
5ceefc71dSYangbo Lu  * Copyright (C) 2010 OMICRON electronics GmbH
6ceefc71dSYangbo Lu  */
7ceefc71dSYangbo Lu 
8ceefc71dSYangbo Lu #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9ceefc71dSYangbo Lu 
10ceefc71dSYangbo Lu #include <linux/device.h>
11ceefc71dSYangbo Lu #include <linux/hrtimer.h>
12ceefc71dSYangbo Lu #include <linux/kernel.h>
13ceefc71dSYangbo Lu #include <linux/module.h>
14ceefc71dSYangbo Lu #include <linux/of.h>
15*9ffc4de5SRob Herring #include <linux/platform_device.h>
16ceefc71dSYangbo Lu #include <linux/timex.h>
17ceefc71dSYangbo Lu #include <linux/slab.h>
1891305f28SYangbo Lu #include <linux/clk.h>
19ceefc71dSYangbo Lu 
206c50c1edSYangbo Lu #include <linux/fsl/ptp_qoriq.h>
21ceefc71dSYangbo Lu 
22ceefc71dSYangbo Lu /*
23ceefc71dSYangbo Lu  * Register access functions
24ceefc71dSYangbo Lu  */
25ceefc71dSYangbo Lu 
261e562c81SYangbo Lu /* Caller must hold ptp_qoriq->lock. */
tmr_cnt_read(struct ptp_qoriq * ptp_qoriq)271e562c81SYangbo Lu static u64 tmr_cnt_read(struct ptp_qoriq *ptp_qoriq)
28ceefc71dSYangbo Lu {
291e562c81SYangbo Lu 	struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
30ceefc71dSYangbo Lu 	u64 ns;
31ceefc71dSYangbo Lu 	u32 lo, hi;
32ceefc71dSYangbo Lu 
33f038ddf2SYangbo Lu 	lo = ptp_qoriq->read(&regs->ctrl_regs->tmr_cnt_l);
34f038ddf2SYangbo Lu 	hi = ptp_qoriq->read(&regs->ctrl_regs->tmr_cnt_h);
35ceefc71dSYangbo Lu 	ns = ((u64) hi) << 32;
36ceefc71dSYangbo Lu 	ns |= lo;
37ceefc71dSYangbo Lu 	return ns;
38ceefc71dSYangbo Lu }
39ceefc71dSYangbo Lu 
401e562c81SYangbo Lu /* Caller must hold ptp_qoriq->lock. */
tmr_cnt_write(struct ptp_qoriq * ptp_qoriq,u64 ns)411e562c81SYangbo Lu static void tmr_cnt_write(struct ptp_qoriq *ptp_qoriq, u64 ns)
42ceefc71dSYangbo Lu {
431e562c81SYangbo Lu 	struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
44ceefc71dSYangbo Lu 	u32 hi = ns >> 32;
45ceefc71dSYangbo Lu 	u32 lo = ns & 0xffffffff;
46ceefc71dSYangbo Lu 
47f038ddf2SYangbo Lu 	ptp_qoriq->write(&regs->ctrl_regs->tmr_cnt_l, lo);
48f038ddf2SYangbo Lu 	ptp_qoriq->write(&regs->ctrl_regs->tmr_cnt_h, hi);
49ceefc71dSYangbo Lu }
50ceefc71dSYangbo Lu 
tmr_offset_read(struct ptp_qoriq * ptp_qoriq)5124a7fffbSNikhil Gupta static u64 tmr_offset_read(struct ptp_qoriq *ptp_qoriq)
5224a7fffbSNikhil Gupta {
5324a7fffbSNikhil Gupta 	struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
5424a7fffbSNikhil Gupta 	u32 lo, hi;
5524a7fffbSNikhil Gupta 	u64 ns;
5624a7fffbSNikhil Gupta 
5724a7fffbSNikhil Gupta 	lo = ptp_qoriq->read(&regs->ctrl_regs->tmroff_l);
5824a7fffbSNikhil Gupta 	hi = ptp_qoriq->read(&regs->ctrl_regs->tmroff_h);
5924a7fffbSNikhil Gupta 	ns = ((u64) hi) << 32;
6024a7fffbSNikhil Gupta 	ns |= lo;
6124a7fffbSNikhil Gupta 	return ns;
6224a7fffbSNikhil Gupta }
6324a7fffbSNikhil Gupta 
tmr_offset_write(struct ptp_qoriq * ptp_qoriq,u64 delta_ns)6424a7fffbSNikhil Gupta static void tmr_offset_write(struct ptp_qoriq *ptp_qoriq, u64 delta_ns)
6524a7fffbSNikhil Gupta {
6624a7fffbSNikhil Gupta 	struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
6724a7fffbSNikhil Gupta 	u32 lo = delta_ns & 0xffffffff;
6824a7fffbSNikhil Gupta 	u32 hi = delta_ns >> 32;
6924a7fffbSNikhil Gupta 
7024a7fffbSNikhil Gupta 	ptp_qoriq->write(&regs->ctrl_regs->tmroff_l, lo);
7124a7fffbSNikhil Gupta 	ptp_qoriq->write(&regs->ctrl_regs->tmroff_h, hi);
7224a7fffbSNikhil Gupta }
7324a7fffbSNikhil Gupta 
741e562c81SYangbo Lu /* Caller must hold ptp_qoriq->lock. */
set_alarm(struct ptp_qoriq * ptp_qoriq)751e562c81SYangbo Lu static void set_alarm(struct ptp_qoriq *ptp_qoriq)
76ceefc71dSYangbo Lu {
771e562c81SYangbo Lu 	struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
78ceefc71dSYangbo Lu 	u64 ns;
79ceefc71dSYangbo Lu 	u32 lo, hi;
80ceefc71dSYangbo Lu 
8124a7fffbSNikhil Gupta 	ns = tmr_cnt_read(ptp_qoriq) + tmr_offset_read(ptp_qoriq)
8224a7fffbSNikhil Gupta 				     + 1500000000ULL;
8324a7fffbSNikhil Gupta 
84ceefc71dSYangbo Lu 	ns = div_u64(ns, 1000000000UL) * 1000000000ULL;
851e562c81SYangbo Lu 	ns -= ptp_qoriq->tclk_period;
86ceefc71dSYangbo Lu 	hi = ns >> 32;
87ceefc71dSYangbo Lu 	lo = ns & 0xffffffff;
88f038ddf2SYangbo Lu 	ptp_qoriq->write(&regs->alarm_regs->tmr_alarm1_l, lo);
89f038ddf2SYangbo Lu 	ptp_qoriq->write(&regs->alarm_regs->tmr_alarm1_h, hi);
90ceefc71dSYangbo Lu }
91ceefc71dSYangbo Lu 
921e562c81SYangbo Lu /* Caller must hold ptp_qoriq->lock. */
set_fipers(struct ptp_qoriq * ptp_qoriq)931e562c81SYangbo Lu static void set_fipers(struct ptp_qoriq *ptp_qoriq)
94ceefc71dSYangbo Lu {
951e562c81SYangbo Lu 	struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
96a8f62d0cSYangbo Lu 
971e562c81SYangbo Lu 	set_alarm(ptp_qoriq);
98f038ddf2SYangbo Lu 	ptp_qoriq->write(&regs->fiper_regs->tmr_fiper1, ptp_qoriq->tmr_fiper1);
99f038ddf2SYangbo Lu 	ptp_qoriq->write(&regs->fiper_regs->tmr_fiper2, ptp_qoriq->tmr_fiper2);
1006d23d831SYangbo Lu 
1016d23d831SYangbo Lu 	if (ptp_qoriq->fiper3_support)
1026d23d831SYangbo Lu 		ptp_qoriq->write(&regs->fiper_regs->tmr_fiper3,
1036d23d831SYangbo Lu 				 ptp_qoriq->tmr_fiper3);
104ceefc71dSYangbo Lu }
105ceefc71dSYangbo Lu 
extts_clean_up(struct ptp_qoriq * ptp_qoriq,int index,bool update_event)1069429439fSYangbo Lu int extts_clean_up(struct ptp_qoriq *ptp_qoriq, int index, bool update_event)
1076815d8b0SYangbo Lu {
1081e562c81SYangbo Lu 	struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
1096815d8b0SYangbo Lu 	struct ptp_clock_event event;
1106815d8b0SYangbo Lu 	void __iomem *reg_etts_l;
1116815d8b0SYangbo Lu 	void __iomem *reg_etts_h;
11210bc877cSYangbo Lu 	u32 valid, lo, hi;
1136815d8b0SYangbo Lu 
1146815d8b0SYangbo Lu 	switch (index) {
1156815d8b0SYangbo Lu 	case 0:
1166815d8b0SYangbo Lu 		valid = ETS1_VLD;
1176815d8b0SYangbo Lu 		reg_etts_l = &regs->etts_regs->tmr_etts1_l;
1186815d8b0SYangbo Lu 		reg_etts_h = &regs->etts_regs->tmr_etts1_h;
1196815d8b0SYangbo Lu 		break;
1206815d8b0SYangbo Lu 	case 1:
1216815d8b0SYangbo Lu 		valid = ETS2_VLD;
1226815d8b0SYangbo Lu 		reg_etts_l = &regs->etts_regs->tmr_etts2_l;
1236815d8b0SYangbo Lu 		reg_etts_h = &regs->etts_regs->tmr_etts2_h;
1246815d8b0SYangbo Lu 		break;
1256815d8b0SYangbo Lu 	default:
1266815d8b0SYangbo Lu 		return -EINVAL;
1276815d8b0SYangbo Lu 	}
1286815d8b0SYangbo Lu 
1296815d8b0SYangbo Lu 	event.type = PTP_CLOCK_EXTTS;
1306815d8b0SYangbo Lu 	event.index = index;
1316815d8b0SYangbo Lu 
13210bc877cSYangbo Lu 	if (ptp_qoriq->extts_fifo_support)
13310bc877cSYangbo Lu 		if (!(ptp_qoriq->read(&regs->ctrl_regs->tmr_stat) & valid))
13410bc877cSYangbo Lu 			return 0;
13510bc877cSYangbo Lu 
1366815d8b0SYangbo Lu 	do {
137f038ddf2SYangbo Lu 		lo = ptp_qoriq->read(reg_etts_l);
138f038ddf2SYangbo Lu 		hi = ptp_qoriq->read(reg_etts_h);
1396815d8b0SYangbo Lu 
1406815d8b0SYangbo Lu 		if (update_event) {
1416815d8b0SYangbo Lu 			event.timestamp = ((u64) hi) << 32;
1426815d8b0SYangbo Lu 			event.timestamp |= lo;
1431e562c81SYangbo Lu 			ptp_clock_event(ptp_qoriq->clock, &event);
1446815d8b0SYangbo Lu 		}
1456815d8b0SYangbo Lu 
14610bc877cSYangbo Lu 		if (!ptp_qoriq->extts_fifo_support)
14710bc877cSYangbo Lu 			break;
14810bc877cSYangbo Lu 	} while (ptp_qoriq->read(&regs->ctrl_regs->tmr_stat) & valid);
1496815d8b0SYangbo Lu 
1506815d8b0SYangbo Lu 	return 0;
1516815d8b0SYangbo Lu }
1529429439fSYangbo Lu EXPORT_SYMBOL_GPL(extts_clean_up);
1536815d8b0SYangbo Lu 
154ceefc71dSYangbo Lu /*
155ceefc71dSYangbo Lu  * Interrupt service routine
156ceefc71dSYangbo Lu  */
157ceefc71dSYangbo Lu 
ptp_qoriq_isr(int irq,void * priv)15873356e4eSYangbo Lu irqreturn_t ptp_qoriq_isr(int irq, void *priv)
159ceefc71dSYangbo Lu {
1601e562c81SYangbo Lu 	struct ptp_qoriq *ptp_qoriq = priv;
1611e562c81SYangbo Lu 	struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
162ceefc71dSYangbo Lu 	struct ptp_clock_event event;
163d71151a3SYangbo Lu 	u32 ack = 0, mask, val, irqs;
164b0bc10ccSYangbo Lu 
1651e562c81SYangbo Lu 	spin_lock(&ptp_qoriq->lock);
166ceefc71dSYangbo Lu 
167f038ddf2SYangbo Lu 	val = ptp_qoriq->read(&regs->ctrl_regs->tmr_tevent);
168f038ddf2SYangbo Lu 	mask = ptp_qoriq->read(&regs->ctrl_regs->tmr_temask);
169ceefc71dSYangbo Lu 
1701e562c81SYangbo Lu 	spin_unlock(&ptp_qoriq->lock);
171b0bc10ccSYangbo Lu 
172b0bc10ccSYangbo Lu 	irqs = val & mask;
173b0bc10ccSYangbo Lu 
174b0bc10ccSYangbo Lu 	if (irqs & ETS1) {
175ceefc71dSYangbo Lu 		ack |= ETS1;
1761e562c81SYangbo Lu 		extts_clean_up(ptp_qoriq, 0, true);
177ceefc71dSYangbo Lu 	}
178ceefc71dSYangbo Lu 
179b0bc10ccSYangbo Lu 	if (irqs & ETS2) {
180ceefc71dSYangbo Lu 		ack |= ETS2;
1811e562c81SYangbo Lu 		extts_clean_up(ptp_qoriq, 1, true);
182ceefc71dSYangbo Lu 	}
183ceefc71dSYangbo Lu 
184b0bc10ccSYangbo Lu 	if (irqs & PP1) {
185ceefc71dSYangbo Lu 		ack |= PP1;
186ceefc71dSYangbo Lu 		event.type = PTP_CLOCK_PPS;
1871e562c81SYangbo Lu 		ptp_clock_event(ptp_qoriq->clock, &event);
188ceefc71dSYangbo Lu 	}
189ceefc71dSYangbo Lu 
190ceefc71dSYangbo Lu 	if (ack) {
191f038ddf2SYangbo Lu 		ptp_qoriq->write(&regs->ctrl_regs->tmr_tevent, ack);
192ceefc71dSYangbo Lu 		return IRQ_HANDLED;
193ceefc71dSYangbo Lu 	} else
194ceefc71dSYangbo Lu 		return IRQ_NONE;
195ceefc71dSYangbo Lu }
19673356e4eSYangbo Lu EXPORT_SYMBOL_GPL(ptp_qoriq_isr);
197ceefc71dSYangbo Lu 
198ceefc71dSYangbo Lu /*
199ceefc71dSYangbo Lu  * PTP clock operations
200ceefc71dSYangbo Lu  */
201ceefc71dSYangbo Lu 
ptp_qoriq_adjfine(struct ptp_clock_info * ptp,long scaled_ppm)20273356e4eSYangbo Lu int ptp_qoriq_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
203ceefc71dSYangbo Lu {
204ceefc71dSYangbo Lu 	u64 adj, diff;
205ceefc71dSYangbo Lu 	u32 tmr_add;
206ceefc71dSYangbo Lu 	int neg_adj = 0;
2071e562c81SYangbo Lu 	struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
2081e562c81SYangbo Lu 	struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
209ceefc71dSYangbo Lu 
210ceefc71dSYangbo Lu 	if (scaled_ppm < 0) {
211ceefc71dSYangbo Lu 		neg_adj = 1;
212ceefc71dSYangbo Lu 		scaled_ppm = -scaled_ppm;
213ceefc71dSYangbo Lu 	}
2141e562c81SYangbo Lu 	tmr_add = ptp_qoriq->tmr_add;
215ceefc71dSYangbo Lu 	adj = tmr_add;
216ceefc71dSYangbo Lu 
217f51d7bf1SYangbo Lu 	/*
218f51d7bf1SYangbo Lu 	 * Calculate diff and round() to the nearest integer
219f51d7bf1SYangbo Lu 	 *
220f51d7bf1SYangbo Lu 	 * diff = adj * (ppb / 1000000000)
221f51d7bf1SYangbo Lu 	 *      = adj * scaled_ppm / 65536000000
222ceefc71dSYangbo Lu 	 */
223f51d7bf1SYangbo Lu 	diff = mul_u64_u64_div_u64(adj, scaled_ppm, 32768000000);
224f51d7bf1SYangbo Lu 	diff = DIV64_U64_ROUND_UP(diff, 2);
225ceefc71dSYangbo Lu 
226ceefc71dSYangbo Lu 	tmr_add = neg_adj ? tmr_add - diff : tmr_add + diff;
227f038ddf2SYangbo Lu 	ptp_qoriq->write(&regs->ctrl_regs->tmr_add, tmr_add);
228ceefc71dSYangbo Lu 
229ceefc71dSYangbo Lu 	return 0;
230ceefc71dSYangbo Lu }
23173356e4eSYangbo Lu EXPORT_SYMBOL_GPL(ptp_qoriq_adjfine);
232ceefc71dSYangbo Lu 
ptp_qoriq_adjtime(struct ptp_clock_info * ptp,s64 delta)23373356e4eSYangbo Lu int ptp_qoriq_adjtime(struct ptp_clock_info *ptp, s64 delta)
234ceefc71dSYangbo Lu {
2351e562c81SYangbo Lu 	struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
23624a7fffbSNikhil Gupta 	s64 now, curr_delta;
23724a7fffbSNikhil Gupta 	unsigned long flags;
238ceefc71dSYangbo Lu 
2391e562c81SYangbo Lu 	spin_lock_irqsave(&ptp_qoriq->lock, flags);
240ceefc71dSYangbo Lu 
24124a7fffbSNikhil Gupta 	/* On LS1021A, eTSEC2 and eTSEC3 do not take into account the TMR_OFF
24224a7fffbSNikhil Gupta 	 * adjustment
24324a7fffbSNikhil Gupta 	 */
24424a7fffbSNikhil Gupta 	if (ptp_qoriq->etsec) {
2451e562c81SYangbo Lu 		now = tmr_cnt_read(ptp_qoriq);
246ceefc71dSYangbo Lu 		now += delta;
2471e562c81SYangbo Lu 		tmr_cnt_write(ptp_qoriq, now);
24824a7fffbSNikhil Gupta 	} else {
24924a7fffbSNikhil Gupta 		curr_delta = tmr_offset_read(ptp_qoriq);
25024a7fffbSNikhil Gupta 		curr_delta += delta;
25124a7fffbSNikhil Gupta 		tmr_offset_write(ptp_qoriq, curr_delta);
25224a7fffbSNikhil Gupta 	}
2531e562c81SYangbo Lu 	set_fipers(ptp_qoriq);
254ceefc71dSYangbo Lu 
2551e562c81SYangbo Lu 	spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
256ceefc71dSYangbo Lu 
257ceefc71dSYangbo Lu 	return 0;
258ceefc71dSYangbo Lu }
25973356e4eSYangbo Lu EXPORT_SYMBOL_GPL(ptp_qoriq_adjtime);
260ceefc71dSYangbo Lu 
ptp_qoriq_gettime(struct ptp_clock_info * ptp,struct timespec64 * ts)26173356e4eSYangbo Lu int ptp_qoriq_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
262ceefc71dSYangbo Lu {
263ceefc71dSYangbo Lu 	u64 ns;
264ceefc71dSYangbo Lu 	unsigned long flags;
2651e562c81SYangbo Lu 	struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
266ceefc71dSYangbo Lu 
2671e562c81SYangbo Lu 	spin_lock_irqsave(&ptp_qoriq->lock, flags);
268ceefc71dSYangbo Lu 
26924a7fffbSNikhil Gupta 	ns = tmr_cnt_read(ptp_qoriq) + tmr_offset_read(ptp_qoriq);
270ceefc71dSYangbo Lu 
2711e562c81SYangbo Lu 	spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
272ceefc71dSYangbo Lu 
273ceefc71dSYangbo Lu 	*ts = ns_to_timespec64(ns);
274ceefc71dSYangbo Lu 
275ceefc71dSYangbo Lu 	return 0;
276ceefc71dSYangbo Lu }
27773356e4eSYangbo Lu EXPORT_SYMBOL_GPL(ptp_qoriq_gettime);
278ceefc71dSYangbo Lu 
ptp_qoriq_settime(struct ptp_clock_info * ptp,const struct timespec64 * ts)27973356e4eSYangbo Lu int ptp_qoriq_settime(struct ptp_clock_info *ptp,
280ceefc71dSYangbo Lu 		      const struct timespec64 *ts)
281ceefc71dSYangbo Lu {
282ceefc71dSYangbo Lu 	u64 ns;
283ceefc71dSYangbo Lu 	unsigned long flags;
2841e562c81SYangbo Lu 	struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
285ceefc71dSYangbo Lu 
286ceefc71dSYangbo Lu 	ns = timespec64_to_ns(ts);
287ceefc71dSYangbo Lu 
2881e562c81SYangbo Lu 	spin_lock_irqsave(&ptp_qoriq->lock, flags);
289ceefc71dSYangbo Lu 
29024a7fffbSNikhil Gupta 	tmr_offset_write(ptp_qoriq, 0);
2911e562c81SYangbo Lu 	tmr_cnt_write(ptp_qoriq, ns);
2921e562c81SYangbo Lu 	set_fipers(ptp_qoriq);
293ceefc71dSYangbo Lu 
2941e562c81SYangbo Lu 	spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
295ceefc71dSYangbo Lu 
296ceefc71dSYangbo Lu 	return 0;
297ceefc71dSYangbo Lu }
29873356e4eSYangbo Lu EXPORT_SYMBOL_GPL(ptp_qoriq_settime);
299ceefc71dSYangbo Lu 
ptp_qoriq_enable(struct ptp_clock_info * ptp,struct ptp_clock_request * rq,int on)30073356e4eSYangbo Lu int ptp_qoriq_enable(struct ptp_clock_info *ptp,
301ceefc71dSYangbo Lu 		     struct ptp_clock_request *rq, int on)
302ceefc71dSYangbo Lu {
3031e562c81SYangbo Lu 	struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
3041e562c81SYangbo Lu 	struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
305ceefc71dSYangbo Lu 	unsigned long flags;
306b0bc10ccSYangbo Lu 	u32 bit, mask = 0;
307ceefc71dSYangbo Lu 
308ceefc71dSYangbo Lu 	switch (rq->type) {
309ceefc71dSYangbo Lu 	case PTP_CLK_REQ_EXTTS:
310ceefc71dSYangbo Lu 		switch (rq->extts.index) {
311ceefc71dSYangbo Lu 		case 0:
312ceefc71dSYangbo Lu 			bit = ETS1EN;
313ceefc71dSYangbo Lu 			break;
314ceefc71dSYangbo Lu 		case 1:
315ceefc71dSYangbo Lu 			bit = ETS2EN;
316ceefc71dSYangbo Lu 			break;
317ceefc71dSYangbo Lu 		default:
318ceefc71dSYangbo Lu 			return -EINVAL;
319ceefc71dSYangbo Lu 		}
3206815d8b0SYangbo Lu 
3216815d8b0SYangbo Lu 		if (on)
3221e562c81SYangbo Lu 			extts_clean_up(ptp_qoriq, rq->extts.index, false);
3236815d8b0SYangbo Lu 
324ceefc71dSYangbo Lu 		break;
325b0bc10ccSYangbo Lu 	case PTP_CLK_REQ_PPS:
326b0bc10ccSYangbo Lu 		bit = PP1EN;
327b0bc10ccSYangbo Lu 		break;
328b0bc10ccSYangbo Lu 	default:
329b0bc10ccSYangbo Lu 		return -EOPNOTSUPP;
330ceefc71dSYangbo Lu 	}
331ceefc71dSYangbo Lu 
3321e562c81SYangbo Lu 	spin_lock_irqsave(&ptp_qoriq->lock, flags);
333b0bc10ccSYangbo Lu 
334f038ddf2SYangbo Lu 	mask = ptp_qoriq->read(&regs->ctrl_regs->tmr_temask);
335b0bc10ccSYangbo Lu 	if (on) {
336b0bc10ccSYangbo Lu 		mask |= bit;
337f038ddf2SYangbo Lu 		ptp_qoriq->write(&regs->ctrl_regs->tmr_tevent, bit);
338b0bc10ccSYangbo Lu 	} else {
339b0bc10ccSYangbo Lu 		mask &= ~bit;
340b0bc10ccSYangbo Lu 	}
341b0bc10ccSYangbo Lu 
342f038ddf2SYangbo Lu 	ptp_qoriq->write(&regs->ctrl_regs->tmr_temask, mask);
343b0bc10ccSYangbo Lu 
3441e562c81SYangbo Lu 	spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
345b0bc10ccSYangbo Lu 	return 0;
346ceefc71dSYangbo Lu }
34773356e4eSYangbo Lu EXPORT_SYMBOL_GPL(ptp_qoriq_enable);
348ceefc71dSYangbo Lu 
349ceefc71dSYangbo Lu static const struct ptp_clock_info ptp_qoriq_caps = {
350ceefc71dSYangbo Lu 	.owner		= THIS_MODULE,
351ceefc71dSYangbo Lu 	.name		= "qoriq ptp clock",
352ceefc71dSYangbo Lu 	.max_adj	= 512000,
353ceefc71dSYangbo Lu 	.n_alarm	= 0,
354ceefc71dSYangbo Lu 	.n_ext_ts	= N_EXT_TS,
355ceefc71dSYangbo Lu 	.n_per_out	= 0,
356ceefc71dSYangbo Lu 	.n_pins		= 0,
357ceefc71dSYangbo Lu 	.pps		= 1,
358ceefc71dSYangbo Lu 	.adjfine	= ptp_qoriq_adjfine,
359ceefc71dSYangbo Lu 	.adjtime	= ptp_qoriq_adjtime,
360ceefc71dSYangbo Lu 	.gettime64	= ptp_qoriq_gettime,
361ceefc71dSYangbo Lu 	.settime64	= ptp_qoriq_settime,
362ceefc71dSYangbo Lu 	.enable		= ptp_qoriq_enable,
363ceefc71dSYangbo Lu };
364ceefc71dSYangbo Lu 
36591305f28SYangbo Lu /**
3661e562c81SYangbo Lu  * ptp_qoriq_nominal_freq - calculate nominal frequency according to
36791305f28SYangbo Lu  *			    reference clock frequency
36891305f28SYangbo Lu  *
36991305f28SYangbo Lu  * @clk_src: reference clock frequency
37091305f28SYangbo Lu  *
37191305f28SYangbo Lu  * The nominal frequency is the desired clock frequency.
37291305f28SYangbo Lu  * It should be less than the reference clock frequency.
37391305f28SYangbo Lu  * It should be a factor of 1000MHz.
37491305f28SYangbo Lu  *
37591305f28SYangbo Lu  * Return the nominal frequency
37691305f28SYangbo Lu  */
ptp_qoriq_nominal_freq(u32 clk_src)3771e562c81SYangbo Lu static u32 ptp_qoriq_nominal_freq(u32 clk_src)
37891305f28SYangbo Lu {
37991305f28SYangbo Lu 	u32 remainder = 0;
38091305f28SYangbo Lu 
38191305f28SYangbo Lu 	clk_src /= 1000000;
38291305f28SYangbo Lu 	remainder = clk_src % 100;
38391305f28SYangbo Lu 	if (remainder) {
38491305f28SYangbo Lu 		clk_src -= remainder;
38591305f28SYangbo Lu 		clk_src += 100;
38691305f28SYangbo Lu 	}
38791305f28SYangbo Lu 
38891305f28SYangbo Lu 	do {
38991305f28SYangbo Lu 		clk_src -= 100;
39091305f28SYangbo Lu 
39191305f28SYangbo Lu 	} while (1000 % clk_src);
39291305f28SYangbo Lu 
39391305f28SYangbo Lu 	return clk_src * 1000000;
39491305f28SYangbo Lu }
39591305f28SYangbo Lu 
39691305f28SYangbo Lu /**
3971e562c81SYangbo Lu  * ptp_qoriq_auto_config - calculate a set of default configurations
39891305f28SYangbo Lu  *
3991e562c81SYangbo Lu  * @ptp_qoriq: pointer to ptp_qoriq
40091305f28SYangbo Lu  * @node: pointer to device_node
40191305f28SYangbo Lu  *
40291305f28SYangbo Lu  * If below dts properties are not provided, this function will be
40391305f28SYangbo Lu  * called to calculate a set of default configurations for them.
40491305f28SYangbo Lu  *   "fsl,tclk-period"
40591305f28SYangbo Lu  *   "fsl,tmr-prsc"
40691305f28SYangbo Lu  *   "fsl,tmr-add"
40791305f28SYangbo Lu  *   "fsl,tmr-fiper1"
40891305f28SYangbo Lu  *   "fsl,tmr-fiper2"
4096d23d831SYangbo Lu  *   "fsl,tmr-fiper3" (required only for DPAA2 and ENETC hardware)
41091305f28SYangbo Lu  *   "fsl,max-adj"
41191305f28SYangbo Lu  *
41291305f28SYangbo Lu  * Return 0 if success
41391305f28SYangbo Lu  */
ptp_qoriq_auto_config(struct ptp_qoriq * ptp_qoriq,struct device_node * node)4141e562c81SYangbo Lu static int ptp_qoriq_auto_config(struct ptp_qoriq *ptp_qoriq,
41591305f28SYangbo Lu 				 struct device_node *node)
41691305f28SYangbo Lu {
41791305f28SYangbo Lu 	struct clk *clk;
41891305f28SYangbo Lu 	u64 freq_comp;
41991305f28SYangbo Lu 	u64 max_adj;
42091305f28SYangbo Lu 	u32 nominal_freq;
42174c05a33SYangbo Lu 	u32 remainder = 0;
42291305f28SYangbo Lu 	u32 clk_src = 0;
42391305f28SYangbo Lu 
4241e562c81SYangbo Lu 	ptp_qoriq->cksel = DEFAULT_CKSEL;
42591305f28SYangbo Lu 
42691305f28SYangbo Lu 	clk = of_clk_get(node, 0);
42791305f28SYangbo Lu 	if (!IS_ERR(clk)) {
42891305f28SYangbo Lu 		clk_src = clk_get_rate(clk);
42991305f28SYangbo Lu 		clk_put(clk);
43091305f28SYangbo Lu 	}
43191305f28SYangbo Lu 
43291305f28SYangbo Lu 	if (clk_src <= 100000000UL) {
43391305f28SYangbo Lu 		pr_err("error reference clock value, or lower than 100MHz\n");
43491305f28SYangbo Lu 		return -EINVAL;
43591305f28SYangbo Lu 	}
43691305f28SYangbo Lu 
4371e562c81SYangbo Lu 	nominal_freq = ptp_qoriq_nominal_freq(clk_src);
43891305f28SYangbo Lu 	if (!nominal_freq)
43991305f28SYangbo Lu 		return -EINVAL;
44091305f28SYangbo Lu 
4411e562c81SYangbo Lu 	ptp_qoriq->tclk_period = 1000000000UL / nominal_freq;
4421e562c81SYangbo Lu 	ptp_qoriq->tmr_prsc = DEFAULT_TMR_PRSC;
44391305f28SYangbo Lu 
44491305f28SYangbo Lu 	/* Calculate initial frequency compensation value for TMR_ADD register.
44591305f28SYangbo Lu 	 * freq_comp = ceil(2^32 / freq_ratio)
44691305f28SYangbo Lu 	 * freq_ratio = reference_clock_freq / nominal_freq
44791305f28SYangbo Lu 	 */
44891305f28SYangbo Lu 	freq_comp = ((u64)1 << 32) * nominal_freq;
44974c05a33SYangbo Lu 	freq_comp = div_u64_rem(freq_comp, clk_src, &remainder);
45074c05a33SYangbo Lu 	if (remainder)
45191305f28SYangbo Lu 		freq_comp++;
45291305f28SYangbo Lu 
4531e562c81SYangbo Lu 	ptp_qoriq->tmr_add = freq_comp;
4541e562c81SYangbo Lu 	ptp_qoriq->tmr_fiper1 = DEFAULT_FIPER1_PERIOD - ptp_qoriq->tclk_period;
4551e562c81SYangbo Lu 	ptp_qoriq->tmr_fiper2 = DEFAULT_FIPER2_PERIOD - ptp_qoriq->tclk_period;
4566d23d831SYangbo Lu 	ptp_qoriq->tmr_fiper3 = DEFAULT_FIPER3_PERIOD - ptp_qoriq->tclk_period;
45791305f28SYangbo Lu 
45891305f28SYangbo Lu 	/* max_adj = 1000000000 * (freq_ratio - 1.0) - 1
45991305f28SYangbo Lu 	 * freq_ratio = reference_clock_freq / nominal_freq
46091305f28SYangbo Lu 	 */
46191305f28SYangbo Lu 	max_adj = 1000000000ULL * (clk_src - nominal_freq);
46274c05a33SYangbo Lu 	max_adj = div_u64(max_adj, nominal_freq) - 1;
4631e562c81SYangbo Lu 	ptp_qoriq->caps.max_adj = max_adj;
46491305f28SYangbo Lu 
46591305f28SYangbo Lu 	return 0;
46691305f28SYangbo Lu }
46791305f28SYangbo Lu 
ptp_qoriq_init(struct ptp_qoriq * ptp_qoriq,void __iomem * base,const struct ptp_clock_info * caps)468ff54571aSYangbo Lu int ptp_qoriq_init(struct ptp_qoriq *ptp_qoriq, void __iomem *base,
46958066ac9SColin Ian King 		   const struct ptp_clock_info *caps)
470ceefc71dSYangbo Lu {
471ff54571aSYangbo Lu 	struct device_node *node = ptp_qoriq->dev->of_node;
4721e562c81SYangbo Lu 	struct ptp_qoriq_registers *regs;
473ceefc71dSYangbo Lu 	struct timespec64 now;
474ceefc71dSYangbo Lu 	unsigned long flags;
475ff54571aSYangbo Lu 	u32 tmr_ctrl;
476ceefc71dSYangbo Lu 
4777f4399baSClaudiu Manoil 	if (!node)
4787f4399baSClaudiu Manoil 		return -ENODEV;
4797f4399baSClaudiu Manoil 
480ff54571aSYangbo Lu 	ptp_qoriq->base = base;
48158066ac9SColin Ian King 	ptp_qoriq->caps = *caps;
482ceefc71dSYangbo Lu 
4831e562c81SYangbo Lu 	if (of_property_read_u32(node, "fsl,cksel", &ptp_qoriq->cksel))
4841e562c81SYangbo Lu 		ptp_qoriq->cksel = DEFAULT_CKSEL;
485ceefc71dSYangbo Lu 
4866815d8b0SYangbo Lu 	if (of_property_read_bool(node, "fsl,extts-fifo"))
4871e562c81SYangbo Lu 		ptp_qoriq->extts_fifo_support = true;
4886815d8b0SYangbo Lu 	else
4891e562c81SYangbo Lu 		ptp_qoriq->extts_fifo_support = false;
4906815d8b0SYangbo Lu 
4916d23d831SYangbo Lu 	if (of_device_is_compatible(node, "fsl,dpaa2-ptp") ||
4926d23d831SYangbo Lu 	    of_device_is_compatible(node, "fsl,enetc-ptp"))
4936d23d831SYangbo Lu 		ptp_qoriq->fiper3_support = true;
4946d23d831SYangbo Lu 
495ceefc71dSYangbo Lu 	if (of_property_read_u32(node,
4961e562c81SYangbo Lu 				 "fsl,tclk-period", &ptp_qoriq->tclk_period) ||
497ceefc71dSYangbo Lu 	    of_property_read_u32(node,
4981e562c81SYangbo Lu 				 "fsl,tmr-prsc", &ptp_qoriq->tmr_prsc) ||
499ceefc71dSYangbo Lu 	    of_property_read_u32(node,
5001e562c81SYangbo Lu 				 "fsl,tmr-add", &ptp_qoriq->tmr_add) ||
501ceefc71dSYangbo Lu 	    of_property_read_u32(node,
5021e562c81SYangbo Lu 				 "fsl,tmr-fiper1", &ptp_qoriq->tmr_fiper1) ||
503ceefc71dSYangbo Lu 	    of_property_read_u32(node,
5041e562c81SYangbo Lu 				 "fsl,tmr-fiper2", &ptp_qoriq->tmr_fiper2) ||
505ceefc71dSYangbo Lu 	    of_property_read_u32(node,
5066d23d831SYangbo Lu 				 "fsl,max-adj", &ptp_qoriq->caps.max_adj) ||
5076d23d831SYangbo Lu 	    (ptp_qoriq->fiper3_support &&
5086d23d831SYangbo Lu 	     of_property_read_u32(node, "fsl,tmr-fiper3",
5096d23d831SYangbo Lu 				  &ptp_qoriq->tmr_fiper3))) {
51091305f28SYangbo Lu 		pr_warn("device tree node missing required elements, try automatic configuration\n");
51191305f28SYangbo Lu 
5121e562c81SYangbo Lu 		if (ptp_qoriq_auto_config(ptp_qoriq, node))
513ff54571aSYangbo Lu 			return -ENODEV;
514ceefc71dSYangbo Lu 	}
515ceefc71dSYangbo Lu 
516f038ddf2SYangbo Lu 	if (of_property_read_bool(node, "little-endian")) {
517f038ddf2SYangbo Lu 		ptp_qoriq->read = qoriq_read_le;
518f038ddf2SYangbo Lu 		ptp_qoriq->write = qoriq_write_le;
519f038ddf2SYangbo Lu 	} else {
520f038ddf2SYangbo Lu 		ptp_qoriq->read = qoriq_read_be;
521f038ddf2SYangbo Lu 		ptp_qoriq->write = qoriq_write_be;
522f038ddf2SYangbo Lu 	}
523f038ddf2SYangbo Lu 
524d4e17687SYangbo Lu 	/* The eTSEC uses differnt memory map with DPAA/ENETC */
525d4e17687SYangbo Lu 	if (of_device_is_compatible(node, "fsl,etsec-ptp")) {
52624a7fffbSNikhil Gupta 		ptp_qoriq->etsec = true;
527d4e17687SYangbo Lu 		ptp_qoriq->regs.ctrl_regs = base + ETSEC_CTRL_REGS_OFFSET;
528d4e17687SYangbo Lu 		ptp_qoriq->regs.alarm_regs = base + ETSEC_ALARM_REGS_OFFSET;
529d4e17687SYangbo Lu 		ptp_qoriq->regs.fiper_regs = base + ETSEC_FIPER_REGS_OFFSET;
530d4e17687SYangbo Lu 		ptp_qoriq->regs.etts_regs = base + ETSEC_ETTS_REGS_OFFSET;
531a8f62d0cSYangbo Lu 	} else {
5321e562c81SYangbo Lu 		ptp_qoriq->regs.ctrl_regs = base + CTRL_REGS_OFFSET;
5331e562c81SYangbo Lu 		ptp_qoriq->regs.alarm_regs = base + ALARM_REGS_OFFSET;
5341e562c81SYangbo Lu 		ptp_qoriq->regs.fiper_regs = base + FIPER_REGS_OFFSET;
5351e562c81SYangbo Lu 		ptp_qoriq->regs.etts_regs = base + ETTS_REGS_OFFSET;
536a8f62d0cSYangbo Lu 	}
537a8f62d0cSYangbo Lu 
538db34a471SVladimir Oltean 	spin_lock_init(&ptp_qoriq->lock);
539db34a471SVladimir Oltean 
540f696a21cSArnd Bergmann 	ktime_get_real_ts64(&now);
5411e562c81SYangbo Lu 	ptp_qoriq_settime(&ptp_qoriq->caps, &now);
542ceefc71dSYangbo Lu 
543ceefc71dSYangbo Lu 	tmr_ctrl =
5441e562c81SYangbo Lu 	  (ptp_qoriq->tclk_period & TCLK_PERIOD_MASK) << TCLK_PERIOD_SHIFT |
5451e562c81SYangbo Lu 	  (ptp_qoriq->cksel & CKSEL_MASK) << CKSEL_SHIFT;
546ceefc71dSYangbo Lu 
5471e562c81SYangbo Lu 	spin_lock_irqsave(&ptp_qoriq->lock, flags);
548ceefc71dSYangbo Lu 
5491e562c81SYangbo Lu 	regs = &ptp_qoriq->regs;
550f038ddf2SYangbo Lu 	ptp_qoriq->write(&regs->ctrl_regs->tmr_ctrl, tmr_ctrl);
551f038ddf2SYangbo Lu 	ptp_qoriq->write(&regs->ctrl_regs->tmr_add, ptp_qoriq->tmr_add);
552f038ddf2SYangbo Lu 	ptp_qoriq->write(&regs->ctrl_regs->tmr_prsc, ptp_qoriq->tmr_prsc);
553f038ddf2SYangbo Lu 	ptp_qoriq->write(&regs->fiper_regs->tmr_fiper1, ptp_qoriq->tmr_fiper1);
554f038ddf2SYangbo Lu 	ptp_qoriq->write(&regs->fiper_regs->tmr_fiper2, ptp_qoriq->tmr_fiper2);
5556d23d831SYangbo Lu 
5566d23d831SYangbo Lu 	if (ptp_qoriq->fiper3_support)
5576d23d831SYangbo Lu 		ptp_qoriq->write(&regs->fiper_regs->tmr_fiper3,
5586d23d831SYangbo Lu 				 ptp_qoriq->tmr_fiper3);
5596d23d831SYangbo Lu 
5601e562c81SYangbo Lu 	set_alarm(ptp_qoriq);
561f038ddf2SYangbo Lu 	ptp_qoriq->write(&regs->ctrl_regs->tmr_ctrl,
562f038ddf2SYangbo Lu 			 tmr_ctrl|FIPERST|RTPE|TE|FRD);
563ceefc71dSYangbo Lu 
5641e562c81SYangbo Lu 	spin_unlock_irqrestore(&ptp_qoriq->lock, flags);
565ceefc71dSYangbo Lu 
566ff54571aSYangbo Lu 	ptp_qoriq->clock = ptp_clock_register(&ptp_qoriq->caps, ptp_qoriq->dev);
567ff54571aSYangbo Lu 	if (IS_ERR(ptp_qoriq->clock))
568ff54571aSYangbo Lu 		return PTR_ERR(ptp_qoriq->clock);
569ff54571aSYangbo Lu 
5701e562c81SYangbo Lu 	ptp_qoriq->phc_index = ptp_clock_index(ptp_qoriq->clock);
5711e562c81SYangbo Lu 	ptp_qoriq_create_debugfs(ptp_qoriq);
572ff54571aSYangbo Lu 	return 0;
573ff54571aSYangbo Lu }
574ff54571aSYangbo Lu EXPORT_SYMBOL_GPL(ptp_qoriq_init);
575ceefc71dSYangbo Lu 
ptp_qoriq_free(struct ptp_qoriq * ptp_qoriq)576ff54571aSYangbo Lu void ptp_qoriq_free(struct ptp_qoriq *ptp_qoriq)
577ff54571aSYangbo Lu {
578ff54571aSYangbo Lu 	struct ptp_qoriq_registers *regs = &ptp_qoriq->regs;
579ff54571aSYangbo Lu 
580f038ddf2SYangbo Lu 	ptp_qoriq->write(&regs->ctrl_regs->tmr_temask, 0);
581f038ddf2SYangbo Lu 	ptp_qoriq->write(&regs->ctrl_regs->tmr_ctrl,   0);
582ff54571aSYangbo Lu 
583ff54571aSYangbo Lu 	ptp_qoriq_remove_debugfs(ptp_qoriq);
584ff54571aSYangbo Lu 	ptp_clock_unregister(ptp_qoriq->clock);
585ff54571aSYangbo Lu 	iounmap(ptp_qoriq->base);
586ff54571aSYangbo Lu 	free_irq(ptp_qoriq->irq, ptp_qoriq);
587ff54571aSYangbo Lu }
588ff54571aSYangbo Lu EXPORT_SYMBOL_GPL(ptp_qoriq_free);
589ff54571aSYangbo Lu 
ptp_qoriq_probe(struct platform_device * dev)590ff54571aSYangbo Lu static int ptp_qoriq_probe(struct platform_device *dev)
591ff54571aSYangbo Lu {
592ff54571aSYangbo Lu 	struct ptp_qoriq *ptp_qoriq;
593ff54571aSYangbo Lu 	int err = -ENOMEM;
594ff54571aSYangbo Lu 	void __iomem *base;
595ff54571aSYangbo Lu 
596ff54571aSYangbo Lu 	ptp_qoriq = kzalloc(sizeof(*ptp_qoriq), GFP_KERNEL);
597ff54571aSYangbo Lu 	if (!ptp_qoriq)
598ff54571aSYangbo Lu 		goto no_memory;
599ff54571aSYangbo Lu 
600ff54571aSYangbo Lu 	ptp_qoriq->dev = &dev->dev;
601ff54571aSYangbo Lu 
602ff54571aSYangbo Lu 	err = -ENODEV;
603ff54571aSYangbo Lu 
604ff54571aSYangbo Lu 	ptp_qoriq->irq = platform_get_irq(dev, 0);
605ff54571aSYangbo Lu 	if (ptp_qoriq->irq < 0) {
606ff54571aSYangbo Lu 		pr_err("irq not in device tree\n");
607ff54571aSYangbo Lu 		goto no_node;
608ff54571aSYangbo Lu 	}
609ff54571aSYangbo Lu 	if (request_irq(ptp_qoriq->irq, ptp_qoriq_isr, IRQF_SHARED,
610ff54571aSYangbo Lu 			DRIVER, ptp_qoriq)) {
611ff54571aSYangbo Lu 		pr_err("request_irq failed\n");
612ff54571aSYangbo Lu 		goto no_node;
613ff54571aSYangbo Lu 	}
614ff54571aSYangbo Lu 
615ff54571aSYangbo Lu 	ptp_qoriq->rsrc = platform_get_resource(dev, IORESOURCE_MEM, 0);
616ff54571aSYangbo Lu 	if (!ptp_qoriq->rsrc) {
617ff54571aSYangbo Lu 		pr_err("no resource\n");
618ff54571aSYangbo Lu 		goto no_resource;
619ff54571aSYangbo Lu 	}
620ff54571aSYangbo Lu 	if (request_resource(&iomem_resource, ptp_qoriq->rsrc)) {
621ff54571aSYangbo Lu 		pr_err("resource busy\n");
622ff54571aSYangbo Lu 		goto no_resource;
623ff54571aSYangbo Lu 	}
624ff54571aSYangbo Lu 
625ff54571aSYangbo Lu 	base = ioremap(ptp_qoriq->rsrc->start,
626ff54571aSYangbo Lu 		       resource_size(ptp_qoriq->rsrc));
627ff54571aSYangbo Lu 	if (!base) {
628ff54571aSYangbo Lu 		pr_err("ioremap ptp registers failed\n");
629ff54571aSYangbo Lu 		goto no_ioremap;
630ff54571aSYangbo Lu 	}
631ff54571aSYangbo Lu 
63258066ac9SColin Ian King 	err = ptp_qoriq_init(ptp_qoriq, base, &ptp_qoriq_caps);
633ff54571aSYangbo Lu 	if (err)
634ff54571aSYangbo Lu 		goto no_clock;
635ff54571aSYangbo Lu 
636ff54571aSYangbo Lu 	platform_set_drvdata(dev, ptp_qoriq);
637ceefc71dSYangbo Lu 	return 0;
638ceefc71dSYangbo Lu 
639ceefc71dSYangbo Lu no_clock:
640f3364222SSongJingyi 	iounmap(base);
641ceefc71dSYangbo Lu no_ioremap:
6421e562c81SYangbo Lu 	release_resource(ptp_qoriq->rsrc);
643ceefc71dSYangbo Lu no_resource:
6441e562c81SYangbo Lu 	free_irq(ptp_qoriq->irq, ptp_qoriq);
645ceefc71dSYangbo Lu no_node:
6461e562c81SYangbo Lu 	kfree(ptp_qoriq);
647ceefc71dSYangbo Lu no_memory:
648ceefc71dSYangbo Lu 	return err;
649ceefc71dSYangbo Lu }
650ceefc71dSYangbo Lu 
ptp_qoriq_remove(struct platform_device * dev)6511e562c81SYangbo Lu static int ptp_qoriq_remove(struct platform_device *dev)
652ceefc71dSYangbo Lu {
6531e562c81SYangbo Lu 	struct ptp_qoriq *ptp_qoriq = platform_get_drvdata(dev);
654ceefc71dSYangbo Lu 
655ff54571aSYangbo Lu 	ptp_qoriq_free(ptp_qoriq);
6561e562c81SYangbo Lu 	release_resource(ptp_qoriq->rsrc);
6571e562c81SYangbo Lu 	kfree(ptp_qoriq);
658ceefc71dSYangbo Lu 	return 0;
659ceefc71dSYangbo Lu }
660ceefc71dSYangbo Lu 
661ceefc71dSYangbo Lu static const struct of_device_id match_table[] = {
662ceefc71dSYangbo Lu 	{ .compatible = "fsl,etsec-ptp" },
663a8f62d0cSYangbo Lu 	{ .compatible = "fsl,fman-ptp-timer" },
664ceefc71dSYangbo Lu 	{},
665ceefc71dSYangbo Lu };
666ceefc71dSYangbo Lu MODULE_DEVICE_TABLE(of, match_table);
667ceefc71dSYangbo Lu 
6681e562c81SYangbo Lu static struct platform_driver ptp_qoriq_driver = {
669ceefc71dSYangbo Lu 	.driver = {
670ceefc71dSYangbo Lu 		.name		= "ptp_qoriq",
671ceefc71dSYangbo Lu 		.of_match_table	= match_table,
672ceefc71dSYangbo Lu 	},
6731e562c81SYangbo Lu 	.probe       = ptp_qoriq_probe,
6741e562c81SYangbo Lu 	.remove      = ptp_qoriq_remove,
675ceefc71dSYangbo Lu };
676ceefc71dSYangbo Lu 
6771e562c81SYangbo Lu module_platform_driver(ptp_qoriq_driver);
678ceefc71dSYangbo Lu 
679ceefc71dSYangbo Lu MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
680ceefc71dSYangbo Lu MODULE_DESCRIPTION("PTP clock for Freescale QorIQ 1588 timer");
681ceefc71dSYangbo Lu MODULE_LICENSE("GPL");
682