1 // SPDX-License-Identifier: GPL-2.0
3 * 64-bit Periodic Interval Timer driver
5 * Copyright (C) 2019 Microchip Technology Inc. and its subsidiaries
7 * Author: Claudiu Beznea <claudiu.beznea@microchip.com>
10 #include <linux/clk.h>
11 #include <linux/clockchips.h>
12 #include <linux/interrupt.h>
13 #include <linux/of_address.h>
14 #include <linux/of_irq.h>
15 #include <linux/sched_clock.h>
16 #include <linux/slab.h>
18 #define MCHP_PIT64B_CR 0x00 /* Control Register */
19 #define MCHP_PIT64B_CR_START BIT(0)
20 #define MCHP_PIT64B_CR_SWRST BIT(8)
22 #define MCHP_PIT64B_MR 0x04 /* Mode Register */
23 #define MCHP_PIT64B_MR_CONT BIT(0)
24 #define MCHP_PIT64B_MR_ONE_SHOT (0)
25 #define MCHP_PIT64B_MR_SGCLK BIT(3)
26 #define MCHP_PIT64B_MR_PRES GENMASK(11, 8)
28 #define MCHP_PIT64B_LSB_PR 0x08 /* LSB Period Register */
30 #define MCHP_PIT64B_MSB_PR 0x0C /* MSB Period Register */
32 #define MCHP_PIT64B_IER 0x10 /* Interrupt Enable Register */
33 #define MCHP_PIT64B_IER_PERIOD BIT(0)
35 #define MCHP_PIT64B_ISR 0x1C /* Interrupt Status Register */
37 #define MCHP_PIT64B_TLSBR 0x20 /* Timer LSB Register */
39 #define MCHP_PIT64B_TMSBR 0x24 /* Timer MSB Register */
41 #define MCHP_PIT64B_PRES_MAX 0x10
42 #define MCHP_PIT64B_LSBMASK GENMASK_ULL(31, 0)
43 #define MCHP_PIT64B_PRES_TO_MODE(p) (MCHP_PIT64B_MR_PRES & ((p) << 8))
44 #define MCHP_PIT64B_MODE_TO_PRES(m) ((MCHP_PIT64B_MR_PRES & (m)) >> 8)
45 #define MCHP_PIT64B_DEF_FREQ 5000000UL /* 5 MHz */
47 #define MCHP_PIT64B_NAME "pit64b"
50 * struct mchp_pit64b_timer - PIT64B timer data structure
51 * @base: base address of PIT64B hardware block
52 * @pclk: PIT64B's peripheral clock
53 * @gclk: PIT64B's generic clock
54 * @mode: precomputed value for mode register
56 struct mchp_pit64b_timer {
64 * struct mchp_pit64b_clkevt - PIT64B clockevent data structure
65 * @timer: PIT64B timer
68 struct mchp_pit64b_clkevt {
69 struct mchp_pit64b_timer timer;
70 struct clock_event_device clkevt;
73 #define clkevt_to_mchp_pit64b_timer(x) \
74 ((struct mchp_pit64b_timer *)container_of(x,\
75 struct mchp_pit64b_clkevt, clkevt))
78 * struct mchp_pit64b_clksrc - PIT64B clocksource data structure
79 * @timer: PIT64B timer
80 * @clksrc: clocksource
82 struct mchp_pit64b_clksrc {
83 struct mchp_pit64b_timer timer;
84 struct clocksource clksrc;
87 #define clksrc_to_mchp_pit64b_timer(x) \
88 ((struct mchp_pit64b_timer *)container_of(x,\
89 struct mchp_pit64b_clksrc, clksrc))
91 /* Base address for clocksource timer. */
92 static void __iomem *mchp_pit64b_cs_base;
93 /* Default cycles for clockevent timer. */
94 static u64 mchp_pit64b_ce_cycles;
96 static inline u64 mchp_pit64b_cnt_read(void __iomem *base)
101 raw_local_irq_save(flags);
104 * When using a 64 bit period TLSB must be read first, followed by the
105 * read of TMSB. This sequence generates an atomic read of the 64 bit
106 * timer value whatever the lapse of time between the accesses.
108 low = readl_relaxed(base + MCHP_PIT64B_TLSBR);
109 high = readl_relaxed(base + MCHP_PIT64B_TMSBR);
111 raw_local_irq_restore(flags);
113 return (((u64)high << 32) | low);
116 static inline void mchp_pit64b_reset(struct mchp_pit64b_timer *timer,
117 u64 cycles, u32 mode, u32 irqs)
121 low = cycles & MCHP_PIT64B_LSBMASK;
124 writel_relaxed(MCHP_PIT64B_CR_SWRST, timer->base + MCHP_PIT64B_CR);
125 writel_relaxed(mode | timer->mode, timer->base + MCHP_PIT64B_MR);
126 writel_relaxed(high, timer->base + MCHP_PIT64B_MSB_PR);
127 writel_relaxed(low, timer->base + MCHP_PIT64B_LSB_PR);
128 writel_relaxed(irqs, timer->base + MCHP_PIT64B_IER);
129 writel_relaxed(MCHP_PIT64B_CR_START, timer->base + MCHP_PIT64B_CR);
132 static void mchp_pit64b_suspend(struct mchp_pit64b_timer *timer)
134 writel_relaxed(MCHP_PIT64B_CR_SWRST, timer->base + MCHP_PIT64B_CR);
135 if (timer->mode & MCHP_PIT64B_MR_SGCLK)
136 clk_disable_unprepare(timer->gclk);
137 clk_disable_unprepare(timer->pclk);
140 static void mchp_pit64b_resume(struct mchp_pit64b_timer *timer)
142 clk_prepare_enable(timer->pclk);
143 if (timer->mode & MCHP_PIT64B_MR_SGCLK)
144 clk_prepare_enable(timer->gclk);
147 static void mchp_pit64b_clksrc_suspend(struct clocksource *cs)
149 struct mchp_pit64b_timer *timer = clksrc_to_mchp_pit64b_timer(cs);
151 mchp_pit64b_suspend(timer);
154 static void mchp_pit64b_clksrc_resume(struct clocksource *cs)
156 struct mchp_pit64b_timer *timer = clksrc_to_mchp_pit64b_timer(cs);
158 mchp_pit64b_resume(timer);
159 mchp_pit64b_reset(timer, ULLONG_MAX, MCHP_PIT64B_MR_CONT, 0);
162 static u64 mchp_pit64b_clksrc_read(struct clocksource *cs)
164 return mchp_pit64b_cnt_read(mchp_pit64b_cs_base);
167 static u64 notrace mchp_pit64b_sched_read_clk(void)
169 return mchp_pit64b_cnt_read(mchp_pit64b_cs_base);
172 static int mchp_pit64b_clkevt_shutdown(struct clock_event_device *cedev)
174 struct mchp_pit64b_timer *timer = clkevt_to_mchp_pit64b_timer(cedev);
176 if (!clockevent_state_detached(cedev))
177 mchp_pit64b_suspend(timer);
182 static int mchp_pit64b_clkevt_set_periodic(struct clock_event_device *cedev)
184 struct mchp_pit64b_timer *timer = clkevt_to_mchp_pit64b_timer(cedev);
186 if (clockevent_state_shutdown(cedev))
187 mchp_pit64b_resume(timer);
189 mchp_pit64b_reset(timer, mchp_pit64b_ce_cycles, MCHP_PIT64B_MR_CONT,
190 MCHP_PIT64B_IER_PERIOD);
195 static int mchp_pit64b_clkevt_set_oneshot(struct clock_event_device *cedev)
197 struct mchp_pit64b_timer *timer = clkevt_to_mchp_pit64b_timer(cedev);
199 if (clockevent_state_shutdown(cedev))
200 mchp_pit64b_resume(timer);
202 mchp_pit64b_reset(timer, mchp_pit64b_ce_cycles, MCHP_PIT64B_MR_ONE_SHOT,
203 MCHP_PIT64B_IER_PERIOD);
208 static int mchp_pit64b_clkevt_set_next_event(unsigned long evt,
209 struct clock_event_device *cedev)
211 struct mchp_pit64b_timer *timer = clkevt_to_mchp_pit64b_timer(cedev);
213 mchp_pit64b_reset(timer, evt, MCHP_PIT64B_MR_ONE_SHOT,
214 MCHP_PIT64B_IER_PERIOD);
219 static irqreturn_t mchp_pit64b_interrupt(int irq, void *dev_id)
221 struct mchp_pit64b_clkevt *irq_data = dev_id;
223 /* Need to clear the interrupt. */
224 readl_relaxed(irq_data->timer.base + MCHP_PIT64B_ISR);
226 irq_data->clkevt.event_handler(&irq_data->clkevt);
231 static void __init mchp_pit64b_pres_compute(u32 *pres, u32 clk_rate,
236 for (*pres = 0; *pres < MCHP_PIT64B_PRES_MAX; (*pres)++) {
237 tmp = clk_rate / (*pres + 1);
242 /* Use the biggest prescaler if we didn't match one. */
243 if (*pres == MCHP_PIT64B_PRES_MAX)
244 *pres = MCHP_PIT64B_PRES_MAX - 1;
248 * mchp_pit64b_init_mode() - prepare PIT64B mode register value to be used at
249 * runtime; this includes prescaler and SGCLK bit
250 * @timer: pointer to pit64b timer to init
251 * @max_rate: maximum rate that timer's clock could use
253 * PIT64B timer may be fed by gclk or pclk. When gclk is used its rate has to
254 * be at least 3 times lower that pclk's rate. pclk rate is fixed, gclk rate
255 * could be changed via clock APIs. The chosen clock (pclk or gclk) could be
256 * divided by the internal PIT64B's divider.
258 * This function, first tries to use GCLK by requesting the desired rate from
259 * PMC and then using the internal PIT64B prescaler, if any, to reach the
260 * requested rate. If PCLK/GCLK < 3 (condition requested by PIT64B hardware)
261 * then the function falls back on using PCLK as clock source for PIT64B timer
262 * choosing the highest prescaler in case it doesn't locate one to match the
263 * requested frequency.
265 * Below is presented the PIT64B block in relation with PMC:
268 * PMC +------------------------------------+
270 * | |-->gclk -->|-->| | +---------+ +-----+ |
271 * | | | | MUX |--->| Divider |->|timer| |
272 * | |-->pclk -->|-->| | +---------+ +-----+ |
276 * +------------------------------------+
279 * - gclk rate <= pclk rate/3
280 * - gclk rate could be requested from PMC
281 * - pclk rate is fixed (cannot be requested from PMC)
283 static int __init mchp_pit64b_init_mode(struct mchp_pit64b_timer *timer,
284 unsigned long max_rate)
286 unsigned long pclk_rate, diff = 0, best_diff = ULONG_MAX;
288 u32 pres, best_pres = 0;
290 pclk_rate = clk_get_rate(timer->pclk);
296 /* Try using GCLK. */
297 gclk_round = clk_round_rate(timer->gclk, max_rate);
301 if (pclk_rate / gclk_round < 3)
304 mchp_pit64b_pres_compute(&pres, gclk_round, max_rate);
305 best_diff = abs(gclk_round / (pres + 1) - max_rate);
309 timer->mode |= MCHP_PIT64B_MR_SGCLK;
310 clk_set_rate(timer->gclk, gclk_round);
315 /* Check if requested rate could be obtained using PCLK. */
316 mchp_pit64b_pres_compute(&pres, pclk_rate, max_rate);
317 diff = abs(pclk_rate / (pres + 1) - max_rate);
319 if (best_diff > diff) {
324 timer->mode |= MCHP_PIT64B_MR_SGCLK;
325 clk_set_rate(timer->gclk, gclk_round);
329 timer->mode |= MCHP_PIT64B_PRES_TO_MODE(best_pres);
331 pr_info("PIT64B: using clk=%s with prescaler %u, freq=%lu [Hz]\n",
332 timer->mode & MCHP_PIT64B_MR_SGCLK ? "gclk" : "pclk", best_pres,
333 timer->mode & MCHP_PIT64B_MR_SGCLK ?
334 gclk_round / (best_pres + 1) : pclk_rate / (best_pres + 1));
339 static int __init mchp_pit64b_init_clksrc(struct mchp_pit64b_timer *timer,
342 struct mchp_pit64b_clksrc *cs;
345 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
349 mchp_pit64b_resume(timer);
350 mchp_pit64b_reset(timer, ULLONG_MAX, MCHP_PIT64B_MR_CONT, 0);
352 mchp_pit64b_cs_base = timer->base;
354 cs->timer.base = timer->base;
355 cs->timer.pclk = timer->pclk;
356 cs->timer.gclk = timer->gclk;
357 cs->timer.mode = timer->mode;
358 cs->clksrc.name = MCHP_PIT64B_NAME;
359 cs->clksrc.mask = CLOCKSOURCE_MASK(64);
360 cs->clksrc.flags = CLOCK_SOURCE_IS_CONTINUOUS;
361 cs->clksrc.rating = 210;
362 cs->clksrc.read = mchp_pit64b_clksrc_read;
363 cs->clksrc.suspend = mchp_pit64b_clksrc_suspend;
364 cs->clksrc.resume = mchp_pit64b_clksrc_resume;
366 ret = clocksource_register_hz(&cs->clksrc, clk_rate);
368 pr_debug("clksrc: Failed to register PIT64B clocksource!\n");
371 mchp_pit64b_suspend(timer);
377 sched_clock_register(mchp_pit64b_sched_read_clk, 64, clk_rate);
382 static int __init mchp_pit64b_init_clkevt(struct mchp_pit64b_timer *timer,
383 u32 clk_rate, u32 irq)
385 struct mchp_pit64b_clkevt *ce;
388 ce = kzalloc(sizeof(*ce), GFP_KERNEL);
392 mchp_pit64b_ce_cycles = DIV_ROUND_CLOSEST(clk_rate, HZ);
394 ce->timer.base = timer->base;
395 ce->timer.pclk = timer->pclk;
396 ce->timer.gclk = timer->gclk;
397 ce->timer.mode = timer->mode;
398 ce->clkevt.name = MCHP_PIT64B_NAME;
399 ce->clkevt.features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC;
400 ce->clkevt.rating = 150;
401 ce->clkevt.set_state_shutdown = mchp_pit64b_clkevt_shutdown;
402 ce->clkevt.set_state_periodic = mchp_pit64b_clkevt_set_periodic;
403 ce->clkevt.set_state_oneshot = mchp_pit64b_clkevt_set_oneshot;
404 ce->clkevt.set_next_event = mchp_pit64b_clkevt_set_next_event;
405 ce->clkevt.cpumask = cpumask_of(0);
406 ce->clkevt.irq = irq;
408 ret = request_irq(irq, mchp_pit64b_interrupt, IRQF_TIMER,
411 pr_debug("clkevt: Failed to setup PIT64B IRQ\n");
416 clockevents_config_and_register(&ce->clkevt, clk_rate, 1, ULONG_MAX);
421 static int __init mchp_pit64b_dt_init_timer(struct device_node *node,
424 struct mchp_pit64b_timer timer;
425 unsigned long clk_rate;
430 timer.pclk = of_clk_get_by_name(node, "pclk");
431 if (IS_ERR(timer.pclk))
432 return PTR_ERR(timer.pclk);
434 timer.gclk = of_clk_get_by_name(node, "gclk");
435 if (IS_ERR(timer.gclk))
436 return PTR_ERR(timer.gclk);
438 timer.base = of_iomap(node, 0);
443 irq = irq_of_parse_and_map(node, 0);
450 /* Initialize mode (prescaler + SGCK bit). To be used at runtime. */
451 ret = mchp_pit64b_init_mode(&timer, MCHP_PIT64B_DEF_FREQ);
455 if (timer.mode & MCHP_PIT64B_MR_SGCLK)
456 clk_rate = clk_get_rate(timer.gclk);
458 clk_rate = clk_get_rate(timer.pclk);
459 clk_rate = clk_rate / (MCHP_PIT64B_MODE_TO_PRES(timer.mode) + 1);
462 ret = mchp_pit64b_init_clkevt(&timer, clk_rate, irq);
464 ret = mchp_pit64b_init_clksrc(&timer, clk_rate);
472 irq_dispose_mapping(irq);
479 static int __init mchp_pit64b_dt_init(struct device_node *node)
485 /* 1st request, register clockevent. */
486 return mchp_pit64b_dt_init_timer(node, true);
488 /* 2nd request, register clocksource. */
489 return mchp_pit64b_dt_init_timer(node, false);
492 /* The rest, don't care. */
496 TIMER_OF_DECLARE(mchp_pit64b, "microchip,sam9x60-pit64b", mchp_pit64b_dt_init);