xref: /linux/drivers/clocksource/renesas-ostm.c (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
1274157a4SKuninori Morimoto // SPDX-License-Identifier: GPL-2.0
2fb6002a8SChris Brandt /*
3fb6002a8SChris Brandt  * Renesas Timer Support - OSTM
4fb6002a8SChris Brandt  *
5fb6002a8SChris Brandt  * Copyright (C) 2017 Renesas Electronics America, Inc.
6fb6002a8SChris Brandt  * Copyright (C) 2017 Chris Brandt
7fb6002a8SChris Brandt  */
8fb6002a8SChris Brandt 
9fb6002a8SChris Brandt #include <linux/clk.h>
10fb6002a8SChris Brandt #include <linux/clockchips.h>
11fb6002a8SChris Brandt #include <linux/interrupt.h>
123a3e9f23SBiju Das #include <linux/platform_device.h>
133a3e9f23SBiju Das #include <linux/reset.h>
14fb6002a8SChris Brandt #include <linux/sched_clock.h>
15fb6002a8SChris Brandt #include <linux/slab.h>
16fb6002a8SChris Brandt 
1722731423SGeert Uytterhoeven #include "timer-of.h"
1822731423SGeert Uytterhoeven 
19fb6002a8SChris Brandt /*
20fb6002a8SChris Brandt  * The OSTM contains independent channels.
21fb6002a8SChris Brandt  * The first OSTM channel probed will be set up as a free running
22fb6002a8SChris Brandt  * clocksource. Additionally we will use this clocksource for the system
23fb6002a8SChris Brandt  * schedule timer sched_clock().
24fb6002a8SChris Brandt  *
25fb6002a8SChris Brandt  * The second (or more) channel probed will be set up as an interrupt
26fb6002a8SChris Brandt  * driven clock event.
27fb6002a8SChris Brandt  */
28fb6002a8SChris Brandt 
29fb6002a8SChris Brandt static void __iomem *system_clock;	/* For sched_clock() */
30fb6002a8SChris Brandt 
31fb6002a8SChris Brandt /* OSTM REGISTERS */
32fb6002a8SChris Brandt #define	OSTM_CMP		0x000	/* RW,32 */
33fb6002a8SChris Brandt #define	OSTM_CNT		0x004	/* R,32 */
34fb6002a8SChris Brandt #define	OSTM_TE			0x010	/* R,8 */
35fb6002a8SChris Brandt #define	OSTM_TS			0x014	/* W,8 */
36fb6002a8SChris Brandt #define	OSTM_TT			0x018	/* W,8 */
37fb6002a8SChris Brandt #define	OSTM_CTL		0x020	/* RW,8 */
38fb6002a8SChris Brandt 
39fb6002a8SChris Brandt #define	TE			0x01
40fb6002a8SChris Brandt #define	TS			0x01
41fb6002a8SChris Brandt #define	TT			0x01
42fb6002a8SChris Brandt #define	CTL_PERIODIC		0x00
43fb6002a8SChris Brandt #define	CTL_ONESHOT		0x02
44fb6002a8SChris Brandt #define	CTL_FREERUN		0x02
45fb6002a8SChris Brandt 
ostm_timer_stop(struct timer_of * to)4622731423SGeert Uytterhoeven static void ostm_timer_stop(struct timer_of *to)
47fb6002a8SChris Brandt {
4822731423SGeert Uytterhoeven 	if (readb(timer_of_base(to) + OSTM_TE) & TE) {
4922731423SGeert Uytterhoeven 		writeb(TT, timer_of_base(to) + OSTM_TT);
50fb6002a8SChris Brandt 
51fb6002a8SChris Brandt 		/*
52fb6002a8SChris Brandt 		 * Read back the register simply to confirm the write operation
53fb6002a8SChris Brandt 		 * has completed since I/O writes can sometimes get queued by
54fb6002a8SChris Brandt 		 * the bus architecture.
55fb6002a8SChris Brandt 		 */
5622731423SGeert Uytterhoeven 		while (readb(timer_of_base(to) + OSTM_TE) & TE)
57fb6002a8SChris Brandt 			;
58fb6002a8SChris Brandt 	}
59fb6002a8SChris Brandt }
60fb6002a8SChris Brandt 
ostm_init_clksrc(struct timer_of * to)6122731423SGeert Uytterhoeven static int __init ostm_init_clksrc(struct timer_of *to)
62fb6002a8SChris Brandt {
6322731423SGeert Uytterhoeven 	ostm_timer_stop(to);
64fb6002a8SChris Brandt 
6522731423SGeert Uytterhoeven 	writel(0, timer_of_base(to) + OSTM_CMP);
6622731423SGeert Uytterhoeven 	writeb(CTL_FREERUN, timer_of_base(to) + OSTM_CTL);
6722731423SGeert Uytterhoeven 	writeb(TS, timer_of_base(to) + OSTM_TS);
68fb6002a8SChris Brandt 
69b35a5e59SGeert Uytterhoeven 	return clocksource_mmio_init(timer_of_base(to) + OSTM_CNT,
70b35a5e59SGeert Uytterhoeven 				     to->np->full_name, timer_of_rate(to), 300,
71b35a5e59SGeert Uytterhoeven 				     32, clocksource_mmio_readl_up);
72fb6002a8SChris Brandt }
73fb6002a8SChris Brandt 
ostm_read_sched_clock(void)74fb6002a8SChris Brandt static u64 notrace ostm_read_sched_clock(void)
75fb6002a8SChris Brandt {
76fb6002a8SChris Brandt 	return readl(system_clock);
77fb6002a8SChris Brandt }
78fb6002a8SChris Brandt 
ostm_init_sched_clock(struct timer_of * to)7922731423SGeert Uytterhoeven static void __init ostm_init_sched_clock(struct timer_of *to)
80fb6002a8SChris Brandt {
8122731423SGeert Uytterhoeven 	system_clock = timer_of_base(to) + OSTM_CNT;
8222731423SGeert Uytterhoeven 	sched_clock_register(ostm_read_sched_clock, 32, timer_of_rate(to));
83fb6002a8SChris Brandt }
84fb6002a8SChris Brandt 
ostm_clock_event_next(unsigned long delta,struct clock_event_device * ced)85fb6002a8SChris Brandt static int ostm_clock_event_next(unsigned long delta,
86fb6002a8SChris Brandt 				 struct clock_event_device *ced)
87fb6002a8SChris Brandt {
8822731423SGeert Uytterhoeven 	struct timer_of *to = to_timer_of(ced);
89fb6002a8SChris Brandt 
9022731423SGeert Uytterhoeven 	ostm_timer_stop(to);
91fb6002a8SChris Brandt 
9222731423SGeert Uytterhoeven 	writel(delta, timer_of_base(to) + OSTM_CMP);
9322731423SGeert Uytterhoeven 	writeb(CTL_ONESHOT, timer_of_base(to) + OSTM_CTL);
9422731423SGeert Uytterhoeven 	writeb(TS, timer_of_base(to) + OSTM_TS);
95fb6002a8SChris Brandt 
96fb6002a8SChris Brandt 	return 0;
97fb6002a8SChris Brandt }
98fb6002a8SChris Brandt 
ostm_shutdown(struct clock_event_device * ced)99fb6002a8SChris Brandt static int ostm_shutdown(struct clock_event_device *ced)
100fb6002a8SChris Brandt {
10122731423SGeert Uytterhoeven 	struct timer_of *to = to_timer_of(ced);
102fb6002a8SChris Brandt 
10322731423SGeert Uytterhoeven 	ostm_timer_stop(to);
104fb6002a8SChris Brandt 
105fb6002a8SChris Brandt 	return 0;
106fb6002a8SChris Brandt }
ostm_set_periodic(struct clock_event_device * ced)107fb6002a8SChris Brandt static int ostm_set_periodic(struct clock_event_device *ced)
108fb6002a8SChris Brandt {
10922731423SGeert Uytterhoeven 	struct timer_of *to = to_timer_of(ced);
110fb6002a8SChris Brandt 
111fb6002a8SChris Brandt 	if (clockevent_state_oneshot(ced) || clockevent_state_periodic(ced))
11222731423SGeert Uytterhoeven 		ostm_timer_stop(to);
113fb6002a8SChris Brandt 
11422731423SGeert Uytterhoeven 	writel(timer_of_period(to) - 1, timer_of_base(to) + OSTM_CMP);
11522731423SGeert Uytterhoeven 	writeb(CTL_PERIODIC, timer_of_base(to) + OSTM_CTL);
11622731423SGeert Uytterhoeven 	writeb(TS, timer_of_base(to) + OSTM_TS);
117fb6002a8SChris Brandt 
118fb6002a8SChris Brandt 	return 0;
119fb6002a8SChris Brandt }
120fb6002a8SChris Brandt 
ostm_set_oneshot(struct clock_event_device * ced)121fb6002a8SChris Brandt static int ostm_set_oneshot(struct clock_event_device *ced)
122fb6002a8SChris Brandt {
12322731423SGeert Uytterhoeven 	struct timer_of *to = to_timer_of(ced);
124fb6002a8SChris Brandt 
12522731423SGeert Uytterhoeven 	ostm_timer_stop(to);
126fb6002a8SChris Brandt 
127fb6002a8SChris Brandt 	return 0;
128fb6002a8SChris Brandt }
129fb6002a8SChris Brandt 
ostm_timer_interrupt(int irq,void * dev_id)130fb6002a8SChris Brandt static irqreturn_t ostm_timer_interrupt(int irq, void *dev_id)
131fb6002a8SChris Brandt {
13222731423SGeert Uytterhoeven 	struct clock_event_device *ced = dev_id;
133fb6002a8SChris Brandt 
13422731423SGeert Uytterhoeven 	if (clockevent_state_oneshot(ced))
13522731423SGeert Uytterhoeven 		ostm_timer_stop(to_timer_of(ced));
136fb6002a8SChris Brandt 
137fb6002a8SChris Brandt 	/* notify clockevent layer */
13822731423SGeert Uytterhoeven 	if (ced->event_handler)
13922731423SGeert Uytterhoeven 		ced->event_handler(ced);
140fb6002a8SChris Brandt 
141fb6002a8SChris Brandt 	return IRQ_HANDLED;
142fb6002a8SChris Brandt }
143fb6002a8SChris Brandt 
ostm_init_clkevt(struct timer_of * to)14422731423SGeert Uytterhoeven static int __init ostm_init_clkevt(struct timer_of *to)
145fb6002a8SChris Brandt {
14622731423SGeert Uytterhoeven 	struct clock_event_device *ced = &to->clkevt;
147fb6002a8SChris Brandt 
148fb6002a8SChris Brandt 	ced->features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC;
149fb6002a8SChris Brandt 	ced->set_state_shutdown = ostm_shutdown;
150fb6002a8SChris Brandt 	ced->set_state_periodic = ostm_set_periodic;
151fb6002a8SChris Brandt 	ced->set_state_oneshot = ostm_set_oneshot;
152fb6002a8SChris Brandt 	ced->set_next_event = ostm_clock_event_next;
153fb6002a8SChris Brandt 	ced->shift = 32;
154fb6002a8SChris Brandt 	ced->rating = 300;
155fb6002a8SChris Brandt 	ced->cpumask = cpumask_of(0);
15622731423SGeert Uytterhoeven 	clockevents_config_and_register(ced, timer_of_rate(to), 0xf,
15722731423SGeert Uytterhoeven 					0xffffffff);
158fb6002a8SChris Brandt 
159fb6002a8SChris Brandt 	return 0;
160fb6002a8SChris Brandt }
161fb6002a8SChris Brandt 
ostm_init(struct device_node * np)162fb6002a8SChris Brandt static int __init ostm_init(struct device_node *np)
163fb6002a8SChris Brandt {
1643a3e9f23SBiju Das 	struct reset_control *rstc;
16522731423SGeert Uytterhoeven 	struct timer_of *to;
16622731423SGeert Uytterhoeven 	int ret;
167fb6002a8SChris Brandt 
16822731423SGeert Uytterhoeven 	to = kzalloc(sizeof(*to), GFP_KERNEL);
16922731423SGeert Uytterhoeven 	if (!to)
170fb6002a8SChris Brandt 		return -ENOMEM;
171fb6002a8SChris Brandt 
1723a3e9f23SBiju Das 	rstc = of_reset_control_get_optional_exclusive(np, NULL);
1733a3e9f23SBiju Das 	if (IS_ERR(rstc)) {
1743a3e9f23SBiju Das 		ret = PTR_ERR(rstc);
1753a3e9f23SBiju Das 		goto err_free;
1763a3e9f23SBiju Das 	}
1773a3e9f23SBiju Das 
1783a3e9f23SBiju Das 	reset_control_deassert(rstc);
1793a3e9f23SBiju Das 
18022731423SGeert Uytterhoeven 	to->flags = TIMER_OF_BASE | TIMER_OF_CLOCK;
18122731423SGeert Uytterhoeven 	if (system_clock) {
18222731423SGeert Uytterhoeven 		/*
18322731423SGeert Uytterhoeven 		 * clock sources don't use interrupts, clock events do
18422731423SGeert Uytterhoeven 		 */
18522731423SGeert Uytterhoeven 		to->flags |= TIMER_OF_IRQ;
18622731423SGeert Uytterhoeven 		to->of_irq.flags = IRQF_TIMER | IRQF_IRQPOLL;
18722731423SGeert Uytterhoeven 		to->of_irq.handler = ostm_timer_interrupt;
188fb6002a8SChris Brandt 	}
189fb6002a8SChris Brandt 
19022731423SGeert Uytterhoeven 	ret = timer_of_init(np, to);
19122731423SGeert Uytterhoeven 	if (ret)
1923a3e9f23SBiju Das 		goto err_reset;
193fb6002a8SChris Brandt 
194fb6002a8SChris Brandt 	/*
195fb6002a8SChris Brandt 	 * First probed device will be used as system clocksource. Any
196fb6002a8SChris Brandt 	 * additional devices will be used as clock events.
197fb6002a8SChris Brandt 	 */
198fb6002a8SChris Brandt 	if (!system_clock) {
19922731423SGeert Uytterhoeven 		ret = ostm_init_clksrc(to);
20022731423SGeert Uytterhoeven 		if (ret)
20122731423SGeert Uytterhoeven 			goto err_cleanup;
202fb6002a8SChris Brandt 
20322731423SGeert Uytterhoeven 		ostm_init_sched_clock(to);
204b35a5e59SGeert Uytterhoeven 		pr_info("%pOF: used for clocksource\n", np);
205fb6002a8SChris Brandt 	} else {
20622731423SGeert Uytterhoeven 		ret = ostm_init_clkevt(to);
20722731423SGeert Uytterhoeven 		if (ret)
20822731423SGeert Uytterhoeven 			goto err_cleanup;
209fb6002a8SChris Brandt 
210b35a5e59SGeert Uytterhoeven 		pr_info("%pOF: used for clock events\n", np);
211fb6002a8SChris Brandt 	}
212fb6002a8SChris Brandt 
213*37385c07SGeert Uytterhoeven 	of_node_set_flag(np, OF_POPULATED);
214fb6002a8SChris Brandt 	return 0;
21522731423SGeert Uytterhoeven 
21622731423SGeert Uytterhoeven err_cleanup:
21722731423SGeert Uytterhoeven 	timer_of_cleanup(to);
2183a3e9f23SBiju Das err_reset:
2193a3e9f23SBiju Das 	reset_control_assert(rstc);
2203a3e9f23SBiju Das 	reset_control_put(rstc);
22122731423SGeert Uytterhoeven err_free:
22222731423SGeert Uytterhoeven 	kfree(to);
22322731423SGeert Uytterhoeven 	return ret;
224fb6002a8SChris Brandt }
225fb6002a8SChris Brandt 
22617273395SDaniel Lezcano TIMER_OF_DECLARE(ostm, "renesas,ostm", ostm_init);
2273a3e9f23SBiju Das 
2280f63c95aSLad Prabhakar #if defined(CONFIG_ARCH_RZG2L) || defined(CONFIG_ARCH_R9A09G057)
ostm_probe(struct platform_device * pdev)2293a3e9f23SBiju Das static int __init ostm_probe(struct platform_device *pdev)
2303a3e9f23SBiju Das {
2313a3e9f23SBiju Das 	struct device *dev = &pdev->dev;
2323a3e9f23SBiju Das 
2333a3e9f23SBiju Das 	return ostm_init(dev->of_node);
2343a3e9f23SBiju Das }
2353a3e9f23SBiju Das 
2363a3e9f23SBiju Das static const struct of_device_id ostm_of_table[] = {
2373a3e9f23SBiju Das 	{ .compatible = "renesas,ostm", },
2383a3e9f23SBiju Das 	{ /* sentinel */ }
2393a3e9f23SBiju Das };
2403a3e9f23SBiju Das 
2413a3e9f23SBiju Das static struct platform_driver ostm_device_driver = {
2423a3e9f23SBiju Das 	.driver = {
2433a3e9f23SBiju Das 		.name = "renesas_ostm",
2443a3e9f23SBiju Das 		.of_match_table = of_match_ptr(ostm_of_table),
2453a3e9f23SBiju Das 		.suppress_bind_attrs = true,
2463a3e9f23SBiju Das 	},
2473a3e9f23SBiju Das };
2483a3e9f23SBiju Das builtin_platform_driver_probe(ostm_device_driver, ostm_probe);
2493a3e9f23SBiju Das #endif
250