xref: /OK3568_Linux_fs/kernel/drivers/clocksource/renesas-ostm.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Renesas Timer Support - OSTM
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2017 Renesas Electronics America, Inc.
6*4882a593Smuzhiyun  * Copyright (C) 2017 Chris Brandt
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/clk.h>
10*4882a593Smuzhiyun #include <linux/clockchips.h>
11*4882a593Smuzhiyun #include <linux/interrupt.h>
12*4882a593Smuzhiyun #include <linux/sched_clock.h>
13*4882a593Smuzhiyun #include <linux/slab.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include "timer-of.h"
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /*
18*4882a593Smuzhiyun  * The OSTM contains independent channels.
19*4882a593Smuzhiyun  * The first OSTM channel probed will be set up as a free running
20*4882a593Smuzhiyun  * clocksource. Additionally we will use this clocksource for the system
21*4882a593Smuzhiyun  * schedule timer sched_clock().
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * The second (or more) channel probed will be set up as an interrupt
24*4882a593Smuzhiyun  * driven clock event.
25*4882a593Smuzhiyun  */
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun static void __iomem *system_clock;	/* For sched_clock() */
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /* OSTM REGISTERS */
30*4882a593Smuzhiyun #define	OSTM_CMP		0x000	/* RW,32 */
31*4882a593Smuzhiyun #define	OSTM_CNT		0x004	/* R,32 */
32*4882a593Smuzhiyun #define	OSTM_TE			0x010	/* R,8 */
33*4882a593Smuzhiyun #define	OSTM_TS			0x014	/* W,8 */
34*4882a593Smuzhiyun #define	OSTM_TT			0x018	/* W,8 */
35*4882a593Smuzhiyun #define	OSTM_CTL		0x020	/* RW,8 */
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #define	TE			0x01
38*4882a593Smuzhiyun #define	TS			0x01
39*4882a593Smuzhiyun #define	TT			0x01
40*4882a593Smuzhiyun #define	CTL_PERIODIC		0x00
41*4882a593Smuzhiyun #define	CTL_ONESHOT		0x02
42*4882a593Smuzhiyun #define	CTL_FREERUN		0x02
43*4882a593Smuzhiyun 
ostm_timer_stop(struct timer_of * to)44*4882a593Smuzhiyun static void ostm_timer_stop(struct timer_of *to)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun 	if (readb(timer_of_base(to) + OSTM_TE) & TE) {
47*4882a593Smuzhiyun 		writeb(TT, timer_of_base(to) + OSTM_TT);
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 		/*
50*4882a593Smuzhiyun 		 * Read back the register simply to confirm the write operation
51*4882a593Smuzhiyun 		 * has completed since I/O writes can sometimes get queued by
52*4882a593Smuzhiyun 		 * the bus architecture.
53*4882a593Smuzhiyun 		 */
54*4882a593Smuzhiyun 		while (readb(timer_of_base(to) + OSTM_TE) & TE)
55*4882a593Smuzhiyun 			;
56*4882a593Smuzhiyun 	}
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun 
ostm_init_clksrc(struct timer_of * to)59*4882a593Smuzhiyun static int __init ostm_init_clksrc(struct timer_of *to)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun 	ostm_timer_stop(to);
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	writel(0, timer_of_base(to) + OSTM_CMP);
64*4882a593Smuzhiyun 	writeb(CTL_FREERUN, timer_of_base(to) + OSTM_CTL);
65*4882a593Smuzhiyun 	writeb(TS, timer_of_base(to) + OSTM_TS);
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	return clocksource_mmio_init(timer_of_base(to) + OSTM_CNT,
68*4882a593Smuzhiyun 				     to->np->full_name, timer_of_rate(to), 300,
69*4882a593Smuzhiyun 				     32, clocksource_mmio_readl_up);
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun 
ostm_read_sched_clock(void)72*4882a593Smuzhiyun static u64 notrace ostm_read_sched_clock(void)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun 	return readl(system_clock);
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun 
ostm_init_sched_clock(struct timer_of * to)77*4882a593Smuzhiyun static void __init ostm_init_sched_clock(struct timer_of *to)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun 	system_clock = timer_of_base(to) + OSTM_CNT;
80*4882a593Smuzhiyun 	sched_clock_register(ostm_read_sched_clock, 32, timer_of_rate(to));
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun 
ostm_clock_event_next(unsigned long delta,struct clock_event_device * ced)83*4882a593Smuzhiyun static int ostm_clock_event_next(unsigned long delta,
84*4882a593Smuzhiyun 				 struct clock_event_device *ced)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	struct timer_of *to = to_timer_of(ced);
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	ostm_timer_stop(to);
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	writel(delta, timer_of_base(to) + OSTM_CMP);
91*4882a593Smuzhiyun 	writeb(CTL_ONESHOT, timer_of_base(to) + OSTM_CTL);
92*4882a593Smuzhiyun 	writeb(TS, timer_of_base(to) + OSTM_TS);
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	return 0;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun 
ostm_shutdown(struct clock_event_device * ced)97*4882a593Smuzhiyun static int ostm_shutdown(struct clock_event_device *ced)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun 	struct timer_of *to = to_timer_of(ced);
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	ostm_timer_stop(to);
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	return 0;
104*4882a593Smuzhiyun }
ostm_set_periodic(struct clock_event_device * ced)105*4882a593Smuzhiyun static int ostm_set_periodic(struct clock_event_device *ced)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun 	struct timer_of *to = to_timer_of(ced);
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	if (clockevent_state_oneshot(ced) || clockevent_state_periodic(ced))
110*4882a593Smuzhiyun 		ostm_timer_stop(to);
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	writel(timer_of_period(to) - 1, timer_of_base(to) + OSTM_CMP);
113*4882a593Smuzhiyun 	writeb(CTL_PERIODIC, timer_of_base(to) + OSTM_CTL);
114*4882a593Smuzhiyun 	writeb(TS, timer_of_base(to) + OSTM_TS);
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	return 0;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun 
ostm_set_oneshot(struct clock_event_device * ced)119*4882a593Smuzhiyun static int ostm_set_oneshot(struct clock_event_device *ced)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun 	struct timer_of *to = to_timer_of(ced);
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	ostm_timer_stop(to);
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	return 0;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun 
ostm_timer_interrupt(int irq,void * dev_id)128*4882a593Smuzhiyun static irqreturn_t ostm_timer_interrupt(int irq, void *dev_id)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	struct clock_event_device *ced = dev_id;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	if (clockevent_state_oneshot(ced))
133*4882a593Smuzhiyun 		ostm_timer_stop(to_timer_of(ced));
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	/* notify clockevent layer */
136*4882a593Smuzhiyun 	if (ced->event_handler)
137*4882a593Smuzhiyun 		ced->event_handler(ced);
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	return IRQ_HANDLED;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun 
ostm_init_clkevt(struct timer_of * to)142*4882a593Smuzhiyun static int __init ostm_init_clkevt(struct timer_of *to)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun 	struct clock_event_device *ced = &to->clkevt;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	ced->features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC;
147*4882a593Smuzhiyun 	ced->set_state_shutdown = ostm_shutdown;
148*4882a593Smuzhiyun 	ced->set_state_periodic = ostm_set_periodic;
149*4882a593Smuzhiyun 	ced->set_state_oneshot = ostm_set_oneshot;
150*4882a593Smuzhiyun 	ced->set_next_event = ostm_clock_event_next;
151*4882a593Smuzhiyun 	ced->shift = 32;
152*4882a593Smuzhiyun 	ced->rating = 300;
153*4882a593Smuzhiyun 	ced->cpumask = cpumask_of(0);
154*4882a593Smuzhiyun 	clockevents_config_and_register(ced, timer_of_rate(to), 0xf,
155*4882a593Smuzhiyun 					0xffffffff);
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	return 0;
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun 
ostm_init(struct device_node * np)160*4882a593Smuzhiyun static int __init ostm_init(struct device_node *np)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun 	struct timer_of *to;
163*4882a593Smuzhiyun 	int ret;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	to = kzalloc(sizeof(*to), GFP_KERNEL);
166*4882a593Smuzhiyun 	if (!to)
167*4882a593Smuzhiyun 		return -ENOMEM;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	to->flags = TIMER_OF_BASE | TIMER_OF_CLOCK;
170*4882a593Smuzhiyun 	if (system_clock) {
171*4882a593Smuzhiyun 		/*
172*4882a593Smuzhiyun 		 * clock sources don't use interrupts, clock events do
173*4882a593Smuzhiyun 		 */
174*4882a593Smuzhiyun 		to->flags |= TIMER_OF_IRQ;
175*4882a593Smuzhiyun 		to->of_irq.flags = IRQF_TIMER | IRQF_IRQPOLL;
176*4882a593Smuzhiyun 		to->of_irq.handler = ostm_timer_interrupt;
177*4882a593Smuzhiyun 	}
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	ret = timer_of_init(np, to);
180*4882a593Smuzhiyun 	if (ret)
181*4882a593Smuzhiyun 		goto err_free;
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	/*
184*4882a593Smuzhiyun 	 * First probed device will be used as system clocksource. Any
185*4882a593Smuzhiyun 	 * additional devices will be used as clock events.
186*4882a593Smuzhiyun 	 */
187*4882a593Smuzhiyun 	if (!system_clock) {
188*4882a593Smuzhiyun 		ret = ostm_init_clksrc(to);
189*4882a593Smuzhiyun 		if (ret)
190*4882a593Smuzhiyun 			goto err_cleanup;
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 		ostm_init_sched_clock(to);
193*4882a593Smuzhiyun 		pr_info("%pOF: used for clocksource\n", np);
194*4882a593Smuzhiyun 	} else {
195*4882a593Smuzhiyun 		ret = ostm_init_clkevt(to);
196*4882a593Smuzhiyun 		if (ret)
197*4882a593Smuzhiyun 			goto err_cleanup;
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 		pr_info("%pOF: used for clock events\n", np);
200*4882a593Smuzhiyun 	}
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	return 0;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun err_cleanup:
205*4882a593Smuzhiyun 	timer_of_cleanup(to);
206*4882a593Smuzhiyun err_free:
207*4882a593Smuzhiyun 	kfree(to);
208*4882a593Smuzhiyun 	return ret;
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun TIMER_OF_DECLARE(ostm, "renesas,ostm", ostm_init);
212