xref: /linux/drivers/clocksource/timer-microchip-pit64b.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1625022a5SClaudiu Beznea // SPDX-License-Identifier: GPL-2.0
2625022a5SClaudiu Beznea /*
3625022a5SClaudiu Beznea  * 64-bit Periodic Interval Timer driver
4625022a5SClaudiu Beznea  *
5625022a5SClaudiu Beznea  * Copyright (C) 2019 Microchip Technology Inc. and its subsidiaries
6625022a5SClaudiu Beznea  *
7625022a5SClaudiu Beznea  * Author: Claudiu Beznea <claudiu.beznea@microchip.com>
8625022a5SClaudiu Beznea  */
9625022a5SClaudiu Beznea 
10625022a5SClaudiu Beznea #include <linux/clk.h>
11625022a5SClaudiu Beznea #include <linux/clockchips.h>
12*f3af3dc7SClaudiu Beznea #include <linux/delay.h>
13625022a5SClaudiu Beznea #include <linux/interrupt.h>
14625022a5SClaudiu Beznea #include <linux/of_address.h>
15625022a5SClaudiu Beznea #include <linux/of_irq.h>
16625022a5SClaudiu Beznea #include <linux/sched_clock.h>
17625022a5SClaudiu Beznea #include <linux/slab.h>
18625022a5SClaudiu Beznea 
19625022a5SClaudiu Beznea #define MCHP_PIT64B_CR			0x00	/* Control Register */
20625022a5SClaudiu Beznea #define MCHP_PIT64B_CR_START		BIT(0)
21625022a5SClaudiu Beznea #define MCHP_PIT64B_CR_SWRST		BIT(8)
22625022a5SClaudiu Beznea 
23625022a5SClaudiu Beznea #define MCHP_PIT64B_MR			0x04	/* Mode Register */
24625022a5SClaudiu Beznea #define MCHP_PIT64B_MR_CONT		BIT(0)
25625022a5SClaudiu Beznea #define MCHP_PIT64B_MR_ONE_SHOT		(0)
26625022a5SClaudiu Beznea #define MCHP_PIT64B_MR_SGCLK		BIT(3)
27625022a5SClaudiu Beznea #define MCHP_PIT64B_MR_PRES		GENMASK(11, 8)
28625022a5SClaudiu Beznea 
29625022a5SClaudiu Beznea #define MCHP_PIT64B_LSB_PR		0x08	/* LSB Period Register */
30625022a5SClaudiu Beznea 
31625022a5SClaudiu Beznea #define MCHP_PIT64B_MSB_PR		0x0C	/* MSB Period Register */
32625022a5SClaudiu Beznea 
33625022a5SClaudiu Beznea #define MCHP_PIT64B_IER			0x10	/* Interrupt Enable Register */
34625022a5SClaudiu Beznea #define MCHP_PIT64B_IER_PERIOD		BIT(0)
35625022a5SClaudiu Beznea 
36625022a5SClaudiu Beznea #define MCHP_PIT64B_ISR			0x1C	/* Interrupt Status Register */
37625022a5SClaudiu Beznea 
38625022a5SClaudiu Beznea #define MCHP_PIT64B_TLSBR		0x20	/* Timer LSB Register */
39625022a5SClaudiu Beznea 
40625022a5SClaudiu Beznea #define MCHP_PIT64B_TMSBR		0x24	/* Timer MSB Register */
41625022a5SClaudiu Beznea 
42625022a5SClaudiu Beznea #define MCHP_PIT64B_PRES_MAX		0x10
43625022a5SClaudiu Beznea #define MCHP_PIT64B_LSBMASK		GENMASK_ULL(31, 0)
44625022a5SClaudiu Beznea #define MCHP_PIT64B_PRES_TO_MODE(p)	(MCHP_PIT64B_MR_PRES & ((p) << 8))
45625022a5SClaudiu Beznea #define MCHP_PIT64B_MODE_TO_PRES(m)	((MCHP_PIT64B_MR_PRES & (m)) >> 8)
46389e3bffSClaudiu Beznea #define MCHP_PIT64B_DEF_FREQ		5000000UL	/* 5 MHz */
47625022a5SClaudiu Beznea 
48625022a5SClaudiu Beznea #define MCHP_PIT64B_NAME		"pit64b"
49625022a5SClaudiu Beznea 
50625022a5SClaudiu Beznea /**
51625022a5SClaudiu Beznea  * struct mchp_pit64b_timer - PIT64B timer data structure
52625022a5SClaudiu Beznea  * @base: base address of PIT64B hardware block
53625022a5SClaudiu Beznea  * @pclk: PIT64B's peripheral clock
54625022a5SClaudiu Beznea  * @gclk: PIT64B's generic clock
55625022a5SClaudiu Beznea  * @mode: precomputed value for mode register
56625022a5SClaudiu Beznea  */
57625022a5SClaudiu Beznea struct mchp_pit64b_timer {
58625022a5SClaudiu Beznea 	void __iomem	*base;
59625022a5SClaudiu Beznea 	struct clk	*pclk;
60625022a5SClaudiu Beznea 	struct clk	*gclk;
61625022a5SClaudiu Beznea 	u32		mode;
62625022a5SClaudiu Beznea };
63625022a5SClaudiu Beznea 
64625022a5SClaudiu Beznea /**
65278150b2SClaudiu Beznea  * struct mchp_pit64b_clkevt - PIT64B clockevent data structure
66625022a5SClaudiu Beznea  * @timer: PIT64B timer
67625022a5SClaudiu Beznea  * @clkevt: clockevent
68625022a5SClaudiu Beznea  */
69625022a5SClaudiu Beznea struct mchp_pit64b_clkevt {
70625022a5SClaudiu Beznea 	struct mchp_pit64b_timer	timer;
71625022a5SClaudiu Beznea 	struct clock_event_device	clkevt;
72625022a5SClaudiu Beznea };
73625022a5SClaudiu Beznea 
74e85c1d21SClaudiu Beznea #define clkevt_to_mchp_pit64b_timer(x) \
75625022a5SClaudiu Beznea 	((struct mchp_pit64b_timer *)container_of(x,\
76625022a5SClaudiu Beznea 		struct mchp_pit64b_clkevt, clkevt))
77625022a5SClaudiu Beznea 
78e85c1d21SClaudiu Beznea /**
79278150b2SClaudiu Beznea  * struct mchp_pit64b_clksrc - PIT64B clocksource data structure
80e85c1d21SClaudiu Beznea  * @timer: PIT64B timer
81e85c1d21SClaudiu Beznea  * @clksrc: clocksource
82e85c1d21SClaudiu Beznea  */
83e85c1d21SClaudiu Beznea struct mchp_pit64b_clksrc {
84e85c1d21SClaudiu Beznea 	struct mchp_pit64b_timer	timer;
85e85c1d21SClaudiu Beznea 	struct clocksource		clksrc;
86e85c1d21SClaudiu Beznea };
87e85c1d21SClaudiu Beznea 
88e85c1d21SClaudiu Beznea #define clksrc_to_mchp_pit64b_timer(x) \
89e85c1d21SClaudiu Beznea 	((struct mchp_pit64b_timer *)container_of(x,\
90e85c1d21SClaudiu Beznea 		struct mchp_pit64b_clksrc, clksrc))
91e85c1d21SClaudiu Beznea 
92625022a5SClaudiu Beznea /* Base address for clocksource timer. */
93625022a5SClaudiu Beznea static void __iomem *mchp_pit64b_cs_base;
94625022a5SClaudiu Beznea /* Default cycles for clockevent timer. */
95625022a5SClaudiu Beznea static u64 mchp_pit64b_ce_cycles;
96*f3af3dc7SClaudiu Beznea /* Delay timer. */
97*f3af3dc7SClaudiu Beznea static struct delay_timer mchp_pit64b_dt;
98625022a5SClaudiu Beznea 
mchp_pit64b_cnt_read(void __iomem * base)99625022a5SClaudiu Beznea static inline u64 mchp_pit64b_cnt_read(void __iomem *base)
100625022a5SClaudiu Beznea {
101625022a5SClaudiu Beznea 	unsigned long	flags;
102625022a5SClaudiu Beznea 	u32		low, high;
103625022a5SClaudiu Beznea 
104625022a5SClaudiu Beznea 	raw_local_irq_save(flags);
105625022a5SClaudiu Beznea 
106625022a5SClaudiu Beznea 	/*
107625022a5SClaudiu Beznea 	 * When using a 64 bit period TLSB must be read first, followed by the
108625022a5SClaudiu Beznea 	 * read of TMSB. This sequence generates an atomic read of the 64 bit
109625022a5SClaudiu Beznea 	 * timer value whatever the lapse of time between the accesses.
110625022a5SClaudiu Beznea 	 */
111625022a5SClaudiu Beznea 	low = readl_relaxed(base + MCHP_PIT64B_TLSBR);
112625022a5SClaudiu Beznea 	high = readl_relaxed(base + MCHP_PIT64B_TMSBR);
113625022a5SClaudiu Beznea 
114625022a5SClaudiu Beznea 	raw_local_irq_restore(flags);
115625022a5SClaudiu Beznea 
116625022a5SClaudiu Beznea 	return (((u64)high << 32) | low);
117625022a5SClaudiu Beznea }
118625022a5SClaudiu Beznea 
mchp_pit64b_reset(struct mchp_pit64b_timer * timer,u64 cycles,u32 mode,u32 irqs)119625022a5SClaudiu Beznea static inline void mchp_pit64b_reset(struct mchp_pit64b_timer *timer,
120625022a5SClaudiu Beznea 				     u64 cycles, u32 mode, u32 irqs)
121625022a5SClaudiu Beznea {
122625022a5SClaudiu Beznea 	u32 low, high;
123625022a5SClaudiu Beznea 
124625022a5SClaudiu Beznea 	low = cycles & MCHP_PIT64B_LSBMASK;
125625022a5SClaudiu Beznea 	high = cycles >> 32;
126625022a5SClaudiu Beznea 
127625022a5SClaudiu Beznea 	writel_relaxed(MCHP_PIT64B_CR_SWRST, timer->base + MCHP_PIT64B_CR);
128625022a5SClaudiu Beznea 	writel_relaxed(mode | timer->mode, timer->base + MCHP_PIT64B_MR);
129625022a5SClaudiu Beznea 	writel_relaxed(high, timer->base + MCHP_PIT64B_MSB_PR);
130625022a5SClaudiu Beznea 	writel_relaxed(low, timer->base + MCHP_PIT64B_LSB_PR);
131625022a5SClaudiu Beznea 	writel_relaxed(irqs, timer->base + MCHP_PIT64B_IER);
132625022a5SClaudiu Beznea 	writel_relaxed(MCHP_PIT64B_CR_START, timer->base + MCHP_PIT64B_CR);
133625022a5SClaudiu Beznea }
134625022a5SClaudiu Beznea 
mchp_pit64b_suspend(struct mchp_pit64b_timer * timer)135e85c1d21SClaudiu Beznea static void mchp_pit64b_suspend(struct mchp_pit64b_timer *timer)
136e85c1d21SClaudiu Beznea {
137e85c1d21SClaudiu Beznea 	writel_relaxed(MCHP_PIT64B_CR_SWRST, timer->base + MCHP_PIT64B_CR);
138e85c1d21SClaudiu Beznea 	if (timer->mode & MCHP_PIT64B_MR_SGCLK)
139e85c1d21SClaudiu Beznea 		clk_disable_unprepare(timer->gclk);
140e85c1d21SClaudiu Beznea 	clk_disable_unprepare(timer->pclk);
141e85c1d21SClaudiu Beznea }
142e85c1d21SClaudiu Beznea 
mchp_pit64b_resume(struct mchp_pit64b_timer * timer)143e85c1d21SClaudiu Beznea static void mchp_pit64b_resume(struct mchp_pit64b_timer *timer)
144e85c1d21SClaudiu Beznea {
145e85c1d21SClaudiu Beznea 	clk_prepare_enable(timer->pclk);
146e85c1d21SClaudiu Beznea 	if (timer->mode & MCHP_PIT64B_MR_SGCLK)
147e85c1d21SClaudiu Beznea 		clk_prepare_enable(timer->gclk);
148e85c1d21SClaudiu Beznea }
149e85c1d21SClaudiu Beznea 
mchp_pit64b_clksrc_suspend(struct clocksource * cs)150e85c1d21SClaudiu Beznea static void mchp_pit64b_clksrc_suspend(struct clocksource *cs)
151e85c1d21SClaudiu Beznea {
152e85c1d21SClaudiu Beznea 	struct mchp_pit64b_timer *timer = clksrc_to_mchp_pit64b_timer(cs);
153e85c1d21SClaudiu Beznea 
154e85c1d21SClaudiu Beznea 	mchp_pit64b_suspend(timer);
155e85c1d21SClaudiu Beznea }
156e85c1d21SClaudiu Beznea 
mchp_pit64b_clksrc_resume(struct clocksource * cs)157e85c1d21SClaudiu Beznea static void mchp_pit64b_clksrc_resume(struct clocksource *cs)
158e85c1d21SClaudiu Beznea {
159e85c1d21SClaudiu Beznea 	struct mchp_pit64b_timer *timer = clksrc_to_mchp_pit64b_timer(cs);
160e85c1d21SClaudiu Beznea 
161e85c1d21SClaudiu Beznea 	mchp_pit64b_resume(timer);
162e85c1d21SClaudiu Beznea 	mchp_pit64b_reset(timer, ULLONG_MAX, MCHP_PIT64B_MR_CONT, 0);
163e85c1d21SClaudiu Beznea }
164e85c1d21SClaudiu Beznea 
mchp_pit64b_clksrc_read(struct clocksource * cs)165625022a5SClaudiu Beznea static u64 mchp_pit64b_clksrc_read(struct clocksource *cs)
166625022a5SClaudiu Beznea {
167625022a5SClaudiu Beznea 	return mchp_pit64b_cnt_read(mchp_pit64b_cs_base);
168625022a5SClaudiu Beznea }
169625022a5SClaudiu Beznea 
mchp_pit64b_sched_read_clk(void)170ff10ee97SClaudiu Beznea static u64 notrace mchp_pit64b_sched_read_clk(void)
171625022a5SClaudiu Beznea {
172625022a5SClaudiu Beznea 	return mchp_pit64b_cnt_read(mchp_pit64b_cs_base);
173625022a5SClaudiu Beznea }
174625022a5SClaudiu Beznea 
mchp_pit64b_dt_read(void)175*f3af3dc7SClaudiu Beznea static unsigned long notrace mchp_pit64b_dt_read(void)
176*f3af3dc7SClaudiu Beznea {
177*f3af3dc7SClaudiu Beznea 	return mchp_pit64b_cnt_read(mchp_pit64b_cs_base);
178*f3af3dc7SClaudiu Beznea }
179*f3af3dc7SClaudiu Beznea 
mchp_pit64b_clkevt_shutdown(struct clock_event_device * cedev)180625022a5SClaudiu Beznea static int mchp_pit64b_clkevt_shutdown(struct clock_event_device *cedev)
181625022a5SClaudiu Beznea {
182e85c1d21SClaudiu Beznea 	struct mchp_pit64b_timer *timer = clkevt_to_mchp_pit64b_timer(cedev);
183625022a5SClaudiu Beznea 
1842c9c4c9eSClaudiu Beznea 	if (!clockevent_state_detached(cedev))
1852c9c4c9eSClaudiu Beznea 		mchp_pit64b_suspend(timer);
186625022a5SClaudiu Beznea 
187625022a5SClaudiu Beznea 	return 0;
188625022a5SClaudiu Beznea }
189625022a5SClaudiu Beznea 
mchp_pit64b_clkevt_set_periodic(struct clock_event_device * cedev)190625022a5SClaudiu Beznea static int mchp_pit64b_clkevt_set_periodic(struct clock_event_device *cedev)
191625022a5SClaudiu Beznea {
192e85c1d21SClaudiu Beznea 	struct mchp_pit64b_timer *timer = clkevt_to_mchp_pit64b_timer(cedev);
193625022a5SClaudiu Beznea 
1942c9c4c9eSClaudiu Beznea 	if (clockevent_state_shutdown(cedev))
1952c9c4c9eSClaudiu Beznea 		mchp_pit64b_resume(timer);
1962c9c4c9eSClaudiu Beznea 
197625022a5SClaudiu Beznea 	mchp_pit64b_reset(timer, mchp_pit64b_ce_cycles, MCHP_PIT64B_MR_CONT,
198625022a5SClaudiu Beznea 			  MCHP_PIT64B_IER_PERIOD);
199625022a5SClaudiu Beznea 
200625022a5SClaudiu Beznea 	return 0;
201625022a5SClaudiu Beznea }
202625022a5SClaudiu Beznea 
mchp_pit64b_clkevt_set_oneshot(struct clock_event_device * cedev)2032c9c4c9eSClaudiu Beznea static int mchp_pit64b_clkevt_set_oneshot(struct clock_event_device *cedev)
2042c9c4c9eSClaudiu Beznea {
2052c9c4c9eSClaudiu Beznea 	struct mchp_pit64b_timer *timer = clkevt_to_mchp_pit64b_timer(cedev);
2062c9c4c9eSClaudiu Beznea 
2072c9c4c9eSClaudiu Beznea 	if (clockevent_state_shutdown(cedev))
2082c9c4c9eSClaudiu Beznea 		mchp_pit64b_resume(timer);
2092c9c4c9eSClaudiu Beznea 
2102c9c4c9eSClaudiu Beznea 	mchp_pit64b_reset(timer, mchp_pit64b_ce_cycles, MCHP_PIT64B_MR_ONE_SHOT,
2112c9c4c9eSClaudiu Beznea 			  MCHP_PIT64B_IER_PERIOD);
2122c9c4c9eSClaudiu Beznea 
2132c9c4c9eSClaudiu Beznea 	return 0;
2142c9c4c9eSClaudiu Beznea }
2152c9c4c9eSClaudiu Beznea 
mchp_pit64b_clkevt_set_next_event(unsigned long evt,struct clock_event_device * cedev)216625022a5SClaudiu Beznea static int mchp_pit64b_clkevt_set_next_event(unsigned long evt,
217625022a5SClaudiu Beznea 					     struct clock_event_device *cedev)
218625022a5SClaudiu Beznea {
219e85c1d21SClaudiu Beznea 	struct mchp_pit64b_timer *timer = clkevt_to_mchp_pit64b_timer(cedev);
220625022a5SClaudiu Beznea 
221625022a5SClaudiu Beznea 	mchp_pit64b_reset(timer, evt, MCHP_PIT64B_MR_ONE_SHOT,
222625022a5SClaudiu Beznea 			  MCHP_PIT64B_IER_PERIOD);
223625022a5SClaudiu Beznea 
224625022a5SClaudiu Beznea 	return 0;
225625022a5SClaudiu Beznea }
226625022a5SClaudiu Beznea 
mchp_pit64b_interrupt(int irq,void * dev_id)227625022a5SClaudiu Beznea static irqreturn_t mchp_pit64b_interrupt(int irq, void *dev_id)
228625022a5SClaudiu Beznea {
229625022a5SClaudiu Beznea 	struct mchp_pit64b_clkevt *irq_data = dev_id;
230625022a5SClaudiu Beznea 
231625022a5SClaudiu Beznea 	/* Need to clear the interrupt. */
232625022a5SClaudiu Beznea 	readl_relaxed(irq_data->timer.base + MCHP_PIT64B_ISR);
233625022a5SClaudiu Beznea 
234625022a5SClaudiu Beznea 	irq_data->clkevt.event_handler(&irq_data->clkevt);
235625022a5SClaudiu Beznea 
236625022a5SClaudiu Beznea 	return IRQ_HANDLED;
237625022a5SClaudiu Beznea }
238625022a5SClaudiu Beznea 
mchp_pit64b_pres_compute(u32 * pres,u32 clk_rate,u32 max_rate)239625022a5SClaudiu Beznea static void __init mchp_pit64b_pres_compute(u32 *pres, u32 clk_rate,
240625022a5SClaudiu Beznea 					    u32 max_rate)
241625022a5SClaudiu Beznea {
242625022a5SClaudiu Beznea 	u32 tmp;
243625022a5SClaudiu Beznea 
244625022a5SClaudiu Beznea 	for (*pres = 0; *pres < MCHP_PIT64B_PRES_MAX; (*pres)++) {
245625022a5SClaudiu Beznea 		tmp = clk_rate / (*pres + 1);
246625022a5SClaudiu Beznea 		if (tmp <= max_rate)
247625022a5SClaudiu Beznea 			break;
248625022a5SClaudiu Beznea 	}
249625022a5SClaudiu Beznea 
2504bf07f65SIngo Molnar 	/* Use the biggest prescaler if we didn't match one. */
251625022a5SClaudiu Beznea 	if (*pres == MCHP_PIT64B_PRES_MAX)
252625022a5SClaudiu Beznea 		*pres = MCHP_PIT64B_PRES_MAX - 1;
253625022a5SClaudiu Beznea }
254625022a5SClaudiu Beznea 
255625022a5SClaudiu Beznea /**
256278150b2SClaudiu Beznea  * mchp_pit64b_init_mode() - prepare PIT64B mode register value to be used at
257625022a5SClaudiu Beznea  *			     runtime; this includes prescaler and SGCLK bit
258278150b2SClaudiu Beznea  * @timer: pointer to pit64b timer to init
259278150b2SClaudiu Beznea  * @max_rate: maximum rate that timer's clock could use
260625022a5SClaudiu Beznea  *
261625022a5SClaudiu Beznea  * PIT64B timer may be fed by gclk or pclk. When gclk is used its rate has to
262625022a5SClaudiu Beznea  * be at least 3 times lower that pclk's rate. pclk rate is fixed, gclk rate
263625022a5SClaudiu Beznea  * could be changed via clock APIs. The chosen clock (pclk or gclk) could be
264625022a5SClaudiu Beznea  * divided by the internal PIT64B's divider.
265625022a5SClaudiu Beznea  *
266625022a5SClaudiu Beznea  * This function, first tries to use GCLK by requesting the desired rate from
267625022a5SClaudiu Beznea  * PMC and then using the internal PIT64B prescaler, if any, to reach the
268625022a5SClaudiu Beznea  * requested rate. If PCLK/GCLK < 3 (condition requested by PIT64B hardware)
269625022a5SClaudiu Beznea  * then the function falls back on using PCLK as clock source for PIT64B timer
270625022a5SClaudiu Beznea  * choosing the highest prescaler in case it doesn't locate one to match the
271625022a5SClaudiu Beznea  * requested frequency.
272625022a5SClaudiu Beznea  *
273625022a5SClaudiu Beznea  * Below is presented the PIT64B block in relation with PMC:
274625022a5SClaudiu Beznea  *
275625022a5SClaudiu Beznea  *                                PIT64B
276625022a5SClaudiu Beznea  *  PMC             +------------------------------------+
277625022a5SClaudiu Beznea  * +----+           |   +-----+                          |
278625022a5SClaudiu Beznea  * |    |-->gclk -->|-->|     |    +---------+  +-----+  |
279625022a5SClaudiu Beznea  * |    |           |   | MUX |--->| Divider |->|timer|  |
280625022a5SClaudiu Beznea  * |    |-->pclk -->|-->|     |    +---------+  +-----+  |
281625022a5SClaudiu Beznea  * +----+           |   +-----+                          |
282625022a5SClaudiu Beznea  *                  |      ^                             |
283625022a5SClaudiu Beznea  *                  |     sel                            |
284625022a5SClaudiu Beznea  *                  +------------------------------------+
285625022a5SClaudiu Beznea  *
286625022a5SClaudiu Beznea  * Where:
287625022a5SClaudiu Beznea  *	- gclk rate <= pclk rate/3
288625022a5SClaudiu Beznea  *	- gclk rate could be requested from PMC
289625022a5SClaudiu Beznea  *	- pclk rate is fixed (cannot be requested from PMC)
290625022a5SClaudiu Beznea  */
mchp_pit64b_init_mode(struct mchp_pit64b_timer * timer,unsigned long max_rate)291625022a5SClaudiu Beznea static int __init mchp_pit64b_init_mode(struct mchp_pit64b_timer *timer,
292625022a5SClaudiu Beznea 					unsigned long max_rate)
293625022a5SClaudiu Beznea {
294625022a5SClaudiu Beznea 	unsigned long pclk_rate, diff = 0, best_diff = ULONG_MAX;
295625022a5SClaudiu Beznea 	long gclk_round = 0;
296625022a5SClaudiu Beznea 	u32 pres, best_pres = 0;
297625022a5SClaudiu Beznea 
298625022a5SClaudiu Beznea 	pclk_rate = clk_get_rate(timer->pclk);
299625022a5SClaudiu Beznea 	if (!pclk_rate)
300625022a5SClaudiu Beznea 		return -EINVAL;
301625022a5SClaudiu Beznea 
302b9c60a74SClaudiu Beznea 	timer->mode = 0;
303b9c60a74SClaudiu Beznea 
304625022a5SClaudiu Beznea 	/* Try using GCLK. */
305625022a5SClaudiu Beznea 	gclk_round = clk_round_rate(timer->gclk, max_rate);
306625022a5SClaudiu Beznea 	if (gclk_round < 0)
307625022a5SClaudiu Beznea 		goto pclk;
308625022a5SClaudiu Beznea 
309625022a5SClaudiu Beznea 	if (pclk_rate / gclk_round < 3)
310625022a5SClaudiu Beznea 		goto pclk;
311625022a5SClaudiu Beznea 
312625022a5SClaudiu Beznea 	mchp_pit64b_pres_compute(&pres, gclk_round, max_rate);
313625022a5SClaudiu Beznea 	best_diff = abs(gclk_round / (pres + 1) - max_rate);
314625022a5SClaudiu Beznea 	best_pres = pres;
315625022a5SClaudiu Beznea 
316625022a5SClaudiu Beznea 	if (!best_diff) {
317625022a5SClaudiu Beznea 		timer->mode |= MCHP_PIT64B_MR_SGCLK;
31805852445SClaudiu Beznea 		clk_set_rate(timer->gclk, gclk_round);
319625022a5SClaudiu Beznea 		goto done;
320625022a5SClaudiu Beznea 	}
321625022a5SClaudiu Beznea 
322625022a5SClaudiu Beznea pclk:
323625022a5SClaudiu Beznea 	/* Check if requested rate could be obtained using PCLK. */
324625022a5SClaudiu Beznea 	mchp_pit64b_pres_compute(&pres, pclk_rate, max_rate);
325625022a5SClaudiu Beznea 	diff = abs(pclk_rate / (pres + 1) - max_rate);
326625022a5SClaudiu Beznea 
327625022a5SClaudiu Beznea 	if (best_diff > diff) {
328625022a5SClaudiu Beznea 		/* Use PCLK. */
329625022a5SClaudiu Beznea 		best_pres = pres;
330625022a5SClaudiu Beznea 	} else {
331625022a5SClaudiu Beznea 		/* Use GCLK. */
332625022a5SClaudiu Beznea 		timer->mode |= MCHP_PIT64B_MR_SGCLK;
333625022a5SClaudiu Beznea 		clk_set_rate(timer->gclk, gclk_round);
334625022a5SClaudiu Beznea 	}
335625022a5SClaudiu Beznea 
336625022a5SClaudiu Beznea done:
337625022a5SClaudiu Beznea 	timer->mode |= MCHP_PIT64B_PRES_TO_MODE(best_pres);
338625022a5SClaudiu Beznea 
339625022a5SClaudiu Beznea 	pr_info("PIT64B: using clk=%s with prescaler %u, freq=%lu [Hz]\n",
340625022a5SClaudiu Beznea 		timer->mode & MCHP_PIT64B_MR_SGCLK ? "gclk" : "pclk", best_pres,
341625022a5SClaudiu Beznea 		timer->mode & MCHP_PIT64B_MR_SGCLK ?
342625022a5SClaudiu Beznea 		gclk_round / (best_pres + 1) : pclk_rate / (best_pres + 1));
343625022a5SClaudiu Beznea 
344625022a5SClaudiu Beznea 	return 0;
345625022a5SClaudiu Beznea }
346625022a5SClaudiu Beznea 
mchp_pit64b_init_clksrc(struct mchp_pit64b_timer * timer,u32 clk_rate)347625022a5SClaudiu Beznea static int __init mchp_pit64b_init_clksrc(struct mchp_pit64b_timer *timer,
348625022a5SClaudiu Beznea 					  u32 clk_rate)
349625022a5SClaudiu Beznea {
350e85c1d21SClaudiu Beznea 	struct mchp_pit64b_clksrc *cs;
351625022a5SClaudiu Beznea 	int ret;
352625022a5SClaudiu Beznea 
353e85c1d21SClaudiu Beznea 	cs = kzalloc(sizeof(*cs), GFP_KERNEL);
354e85c1d21SClaudiu Beznea 	if (!cs)
355e85c1d21SClaudiu Beznea 		return -ENOMEM;
356e85c1d21SClaudiu Beznea 
357b02180e8SClaudiu Beznea 	mchp_pit64b_resume(timer);
358625022a5SClaudiu Beznea 	mchp_pit64b_reset(timer, ULLONG_MAX, MCHP_PIT64B_MR_CONT, 0);
359625022a5SClaudiu Beznea 
360625022a5SClaudiu Beznea 	mchp_pit64b_cs_base = timer->base;
361625022a5SClaudiu Beznea 
362e85c1d21SClaudiu Beznea 	cs->timer.base = timer->base;
363e85c1d21SClaudiu Beznea 	cs->timer.pclk = timer->pclk;
364e85c1d21SClaudiu Beznea 	cs->timer.gclk = timer->gclk;
365e85c1d21SClaudiu Beznea 	cs->timer.mode = timer->mode;
366e85c1d21SClaudiu Beznea 	cs->clksrc.name = MCHP_PIT64B_NAME;
367e85c1d21SClaudiu Beznea 	cs->clksrc.mask = CLOCKSOURCE_MASK(64);
368e85c1d21SClaudiu Beznea 	cs->clksrc.flags = CLOCK_SOURCE_IS_CONTINUOUS;
369e85c1d21SClaudiu Beznea 	cs->clksrc.rating = 210;
370e85c1d21SClaudiu Beznea 	cs->clksrc.read = mchp_pit64b_clksrc_read;
371e85c1d21SClaudiu Beznea 	cs->clksrc.suspend = mchp_pit64b_clksrc_suspend;
372e85c1d21SClaudiu Beznea 	cs->clksrc.resume = mchp_pit64b_clksrc_resume;
373e85c1d21SClaudiu Beznea 
374e85c1d21SClaudiu Beznea 	ret = clocksource_register_hz(&cs->clksrc, clk_rate);
375625022a5SClaudiu Beznea 	if (ret) {
376625022a5SClaudiu Beznea 		pr_debug("clksrc: Failed to register PIT64B clocksource!\n");
377625022a5SClaudiu Beznea 
378625022a5SClaudiu Beznea 		/* Stop timer. */
379b02180e8SClaudiu Beznea 		mchp_pit64b_suspend(timer);
380e85c1d21SClaudiu Beznea 		kfree(cs);
381625022a5SClaudiu Beznea 
382625022a5SClaudiu Beznea 		return ret;
383625022a5SClaudiu Beznea 	}
384625022a5SClaudiu Beznea 
385625022a5SClaudiu Beznea 	sched_clock_register(mchp_pit64b_sched_read_clk, 64, clk_rate);
386625022a5SClaudiu Beznea 
387*f3af3dc7SClaudiu Beznea 	mchp_pit64b_dt.read_current_timer = mchp_pit64b_dt_read;
388*f3af3dc7SClaudiu Beznea 	mchp_pit64b_dt.freq = clk_rate;
389*f3af3dc7SClaudiu Beznea 	register_current_timer_delay(&mchp_pit64b_dt);
390*f3af3dc7SClaudiu Beznea 
391625022a5SClaudiu Beznea 	return 0;
392625022a5SClaudiu Beznea }
393625022a5SClaudiu Beznea 
mchp_pit64b_init_clkevt(struct mchp_pit64b_timer * timer,u32 clk_rate,u32 irq)394625022a5SClaudiu Beznea static int __init mchp_pit64b_init_clkevt(struct mchp_pit64b_timer *timer,
395625022a5SClaudiu Beznea 					  u32 clk_rate, u32 irq)
396625022a5SClaudiu Beznea {
397625022a5SClaudiu Beznea 	struct mchp_pit64b_clkevt *ce;
398625022a5SClaudiu Beznea 	int ret;
399625022a5SClaudiu Beznea 
400625022a5SClaudiu Beznea 	ce = kzalloc(sizeof(*ce), GFP_KERNEL);
401625022a5SClaudiu Beznea 	if (!ce)
402625022a5SClaudiu Beznea 		return -ENOMEM;
403625022a5SClaudiu Beznea 
404625022a5SClaudiu Beznea 	mchp_pit64b_ce_cycles = DIV_ROUND_CLOSEST(clk_rate, HZ);
405625022a5SClaudiu Beznea 
406625022a5SClaudiu Beznea 	ce->timer.base = timer->base;
407625022a5SClaudiu Beznea 	ce->timer.pclk = timer->pclk;
408625022a5SClaudiu Beznea 	ce->timer.gclk = timer->gclk;
409625022a5SClaudiu Beznea 	ce->timer.mode = timer->mode;
410625022a5SClaudiu Beznea 	ce->clkevt.name = MCHP_PIT64B_NAME;
411625022a5SClaudiu Beznea 	ce->clkevt.features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC;
412625022a5SClaudiu Beznea 	ce->clkevt.rating = 150;
413625022a5SClaudiu Beznea 	ce->clkevt.set_state_shutdown = mchp_pit64b_clkevt_shutdown;
414625022a5SClaudiu Beznea 	ce->clkevt.set_state_periodic = mchp_pit64b_clkevt_set_periodic;
4152c9c4c9eSClaudiu Beznea 	ce->clkevt.set_state_oneshot = mchp_pit64b_clkevt_set_oneshot;
416625022a5SClaudiu Beznea 	ce->clkevt.set_next_event = mchp_pit64b_clkevt_set_next_event;
417625022a5SClaudiu Beznea 	ce->clkevt.cpumask = cpumask_of(0);
418625022a5SClaudiu Beznea 	ce->clkevt.irq = irq;
419625022a5SClaudiu Beznea 
420625022a5SClaudiu Beznea 	ret = request_irq(irq, mchp_pit64b_interrupt, IRQF_TIMER,
421625022a5SClaudiu Beznea 			  "pit64b_tick", ce);
422625022a5SClaudiu Beznea 	if (ret) {
423625022a5SClaudiu Beznea 		pr_debug("clkevt: Failed to setup PIT64B IRQ\n");
424625022a5SClaudiu Beznea 		kfree(ce);
425625022a5SClaudiu Beznea 		return ret;
426625022a5SClaudiu Beznea 	}
427625022a5SClaudiu Beznea 
428625022a5SClaudiu Beznea 	clockevents_config_and_register(&ce->clkevt, clk_rate, 1, ULONG_MAX);
429625022a5SClaudiu Beznea 
430625022a5SClaudiu Beznea 	return 0;
431625022a5SClaudiu Beznea }
432625022a5SClaudiu Beznea 
mchp_pit64b_dt_init_timer(struct device_node * node,bool clkevt)433625022a5SClaudiu Beznea static int __init mchp_pit64b_dt_init_timer(struct device_node *node,
434625022a5SClaudiu Beznea 					    bool clkevt)
435625022a5SClaudiu Beznea {
436b9c60a74SClaudiu Beznea 	struct mchp_pit64b_timer timer;
437625022a5SClaudiu Beznea 	unsigned long clk_rate;
438625022a5SClaudiu Beznea 	u32 irq = 0;
439625022a5SClaudiu Beznea 	int ret;
440625022a5SClaudiu Beznea 
441625022a5SClaudiu Beznea 	/* Parse DT node. */
442625022a5SClaudiu Beznea 	timer.pclk = of_clk_get_by_name(node, "pclk");
443625022a5SClaudiu Beznea 	if (IS_ERR(timer.pclk))
444625022a5SClaudiu Beznea 		return PTR_ERR(timer.pclk);
445625022a5SClaudiu Beznea 
446625022a5SClaudiu Beznea 	timer.gclk = of_clk_get_by_name(node, "gclk");
447625022a5SClaudiu Beznea 	if (IS_ERR(timer.gclk))
448625022a5SClaudiu Beznea 		return PTR_ERR(timer.gclk);
449625022a5SClaudiu Beznea 
450625022a5SClaudiu Beznea 	timer.base = of_iomap(node, 0);
451625022a5SClaudiu Beznea 	if (!timer.base)
452625022a5SClaudiu Beznea 		return -ENXIO;
453625022a5SClaudiu Beznea 
454625022a5SClaudiu Beznea 	if (clkevt) {
455625022a5SClaudiu Beznea 		irq = irq_of_parse_and_map(node, 0);
456625022a5SClaudiu Beznea 		if (!irq) {
457625022a5SClaudiu Beznea 			ret = -ENODEV;
458625022a5SClaudiu Beznea 			goto io_unmap;
459625022a5SClaudiu Beznea 		}
460625022a5SClaudiu Beznea 	}
461625022a5SClaudiu Beznea 
462625022a5SClaudiu Beznea 	/* Initialize mode (prescaler + SGCK bit). To be used at runtime. */
463389e3bffSClaudiu Beznea 	ret = mchp_pit64b_init_mode(&timer, MCHP_PIT64B_DEF_FREQ);
464625022a5SClaudiu Beznea 	if (ret)
465625022a5SClaudiu Beznea 		goto irq_unmap;
466625022a5SClaudiu Beznea 
467b02180e8SClaudiu Beznea 	if (timer.mode & MCHP_PIT64B_MR_SGCLK)
468625022a5SClaudiu Beznea 		clk_rate = clk_get_rate(timer.gclk);
469b02180e8SClaudiu Beznea 	else
470625022a5SClaudiu Beznea 		clk_rate = clk_get_rate(timer.pclk);
471625022a5SClaudiu Beznea 	clk_rate = clk_rate / (MCHP_PIT64B_MODE_TO_PRES(timer.mode) + 1);
472625022a5SClaudiu Beznea 
473625022a5SClaudiu Beznea 	if (clkevt)
474625022a5SClaudiu Beznea 		ret = mchp_pit64b_init_clkevt(&timer, clk_rate, irq);
475625022a5SClaudiu Beznea 	else
476625022a5SClaudiu Beznea 		ret = mchp_pit64b_init_clksrc(&timer, clk_rate);
477625022a5SClaudiu Beznea 
478625022a5SClaudiu Beznea 	if (ret)
479b02180e8SClaudiu Beznea 		goto irq_unmap;
480625022a5SClaudiu Beznea 
481625022a5SClaudiu Beznea 	return 0;
482625022a5SClaudiu Beznea 
483625022a5SClaudiu Beznea irq_unmap:
484625022a5SClaudiu Beznea 	irq_dispose_mapping(irq);
485625022a5SClaudiu Beznea io_unmap:
486625022a5SClaudiu Beznea 	iounmap(timer.base);
487625022a5SClaudiu Beznea 
488625022a5SClaudiu Beznea 	return ret;
489625022a5SClaudiu Beznea }
490625022a5SClaudiu Beznea 
mchp_pit64b_dt_init(struct device_node * node)491625022a5SClaudiu Beznea static int __init mchp_pit64b_dt_init(struct device_node *node)
492625022a5SClaudiu Beznea {
493625022a5SClaudiu Beznea 	static int inits;
494625022a5SClaudiu Beznea 
495625022a5SClaudiu Beznea 	switch (inits++) {
496625022a5SClaudiu Beznea 	case 0:
497625022a5SClaudiu Beznea 		/* 1st request, register clockevent. */
498625022a5SClaudiu Beznea 		return mchp_pit64b_dt_init_timer(node, true);
499625022a5SClaudiu Beznea 	case 1:
500625022a5SClaudiu Beznea 		/* 2nd request, register clocksource. */
501625022a5SClaudiu Beznea 		return mchp_pit64b_dt_init_timer(node, false);
502625022a5SClaudiu Beznea 	}
503625022a5SClaudiu Beznea 
504625022a5SClaudiu Beznea 	/* The rest, don't care. */
505625022a5SClaudiu Beznea 	return -EINVAL;
506625022a5SClaudiu Beznea }
507625022a5SClaudiu Beznea 
508625022a5SClaudiu Beznea TIMER_OF_DECLARE(mchp_pit64b, "microchip,sam9x60-pit64b", mchp_pit64b_dt_init);
509