xref: /OK3568_Linux_fs/kernel/arch/mips/kernel/cevt-gt641xx.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  GT641xx clockevent routines.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *  Copyright (C) 2007	Yoichi Yuasa <yuasa@linux-mips.org>
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun #include <linux/clockchips.h>
8*4882a593Smuzhiyun #include <linux/init.h>
9*4882a593Smuzhiyun #include <linux/interrupt.h>
10*4882a593Smuzhiyun #include <linux/spinlock.h>
11*4882a593Smuzhiyun #include <linux/irq.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <asm/gt64120.h>
14*4882a593Smuzhiyun #include <asm/time.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun static DEFINE_RAW_SPINLOCK(gt641xx_timer_lock);
17*4882a593Smuzhiyun static unsigned int gt641xx_base_clock;
18*4882a593Smuzhiyun 
gt641xx_set_base_clock(unsigned int clock)19*4882a593Smuzhiyun void gt641xx_set_base_clock(unsigned int clock)
20*4882a593Smuzhiyun {
21*4882a593Smuzhiyun 	gt641xx_base_clock = clock;
22*4882a593Smuzhiyun }
23*4882a593Smuzhiyun 
gt641xx_timer0_state(void)24*4882a593Smuzhiyun int gt641xx_timer0_state(void)
25*4882a593Smuzhiyun {
26*4882a593Smuzhiyun 	if (GT_READ(GT_TC0_OFS))
27*4882a593Smuzhiyun 		return 0;
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun 	GT_WRITE(GT_TC0_OFS, gt641xx_base_clock / HZ);
30*4882a593Smuzhiyun 	GT_WRITE(GT_TC_CONTROL_OFS, GT_TC_CONTROL_ENTC0_MSK);
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 	return 1;
33*4882a593Smuzhiyun }
34*4882a593Smuzhiyun 
gt641xx_timer0_set_next_event(unsigned long delta,struct clock_event_device * evt)35*4882a593Smuzhiyun static int gt641xx_timer0_set_next_event(unsigned long delta,
36*4882a593Smuzhiyun 					 struct clock_event_device *evt)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun 	u32 ctrl;
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 	raw_spin_lock(&gt641xx_timer_lock);
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	ctrl = GT_READ(GT_TC_CONTROL_OFS);
43*4882a593Smuzhiyun 	ctrl &= ~(GT_TC_CONTROL_ENTC0_MSK | GT_TC_CONTROL_SELTC0_MSK);
44*4882a593Smuzhiyun 	ctrl |= GT_TC_CONTROL_ENTC0_MSK;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	GT_WRITE(GT_TC0_OFS, delta);
47*4882a593Smuzhiyun 	GT_WRITE(GT_TC_CONTROL_OFS, ctrl);
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	raw_spin_unlock(&gt641xx_timer_lock);
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	return 0;
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun 
gt641xx_timer0_shutdown(struct clock_event_device * evt)54*4882a593Smuzhiyun static int gt641xx_timer0_shutdown(struct clock_event_device *evt)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun 	u32 ctrl;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	raw_spin_lock(&gt641xx_timer_lock);
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	ctrl = GT_READ(GT_TC_CONTROL_OFS);
61*4882a593Smuzhiyun 	ctrl &= ~(GT_TC_CONTROL_ENTC0_MSK | GT_TC_CONTROL_SELTC0_MSK);
62*4882a593Smuzhiyun 	GT_WRITE(GT_TC_CONTROL_OFS, ctrl);
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	raw_spin_unlock(&gt641xx_timer_lock);
65*4882a593Smuzhiyun 	return 0;
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun 
gt641xx_timer0_set_oneshot(struct clock_event_device * evt)68*4882a593Smuzhiyun static int gt641xx_timer0_set_oneshot(struct clock_event_device *evt)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	u32 ctrl;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	raw_spin_lock(&gt641xx_timer_lock);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	ctrl = GT_READ(GT_TC_CONTROL_OFS);
75*4882a593Smuzhiyun 	ctrl &= ~GT_TC_CONTROL_SELTC0_MSK;
76*4882a593Smuzhiyun 	ctrl |= GT_TC_CONTROL_ENTC0_MSK;
77*4882a593Smuzhiyun 	GT_WRITE(GT_TC_CONTROL_OFS, ctrl);
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	raw_spin_unlock(&gt641xx_timer_lock);
80*4882a593Smuzhiyun 	return 0;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun 
gt641xx_timer0_set_periodic(struct clock_event_device * evt)83*4882a593Smuzhiyun static int gt641xx_timer0_set_periodic(struct clock_event_device *evt)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun 	u32 ctrl;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	raw_spin_lock(&gt641xx_timer_lock);
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	ctrl = GT_READ(GT_TC_CONTROL_OFS);
90*4882a593Smuzhiyun 	ctrl |= GT_TC_CONTROL_ENTC0_MSK | GT_TC_CONTROL_SELTC0_MSK;
91*4882a593Smuzhiyun 	GT_WRITE(GT_TC_CONTROL_OFS, ctrl);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	raw_spin_unlock(&gt641xx_timer_lock);
94*4882a593Smuzhiyun 	return 0;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun 
gt641xx_timer0_event_handler(struct clock_event_device * dev)97*4882a593Smuzhiyun static void gt641xx_timer0_event_handler(struct clock_event_device *dev)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun static struct clock_event_device gt641xx_timer0_clockevent = {
102*4882a593Smuzhiyun 	.name			= "gt641xx-timer0",
103*4882a593Smuzhiyun 	.features		= CLOCK_EVT_FEAT_PERIODIC |
104*4882a593Smuzhiyun 				  CLOCK_EVT_FEAT_ONESHOT,
105*4882a593Smuzhiyun 	.irq			= GT641XX_TIMER0_IRQ,
106*4882a593Smuzhiyun 	.set_next_event		= gt641xx_timer0_set_next_event,
107*4882a593Smuzhiyun 	.set_state_shutdown	= gt641xx_timer0_shutdown,
108*4882a593Smuzhiyun 	.set_state_periodic	= gt641xx_timer0_set_periodic,
109*4882a593Smuzhiyun 	.set_state_oneshot	= gt641xx_timer0_set_oneshot,
110*4882a593Smuzhiyun 	.tick_resume		= gt641xx_timer0_shutdown,
111*4882a593Smuzhiyun 	.event_handler		= gt641xx_timer0_event_handler,
112*4882a593Smuzhiyun };
113*4882a593Smuzhiyun 
gt641xx_timer0_interrupt(int irq,void * dev_id)114*4882a593Smuzhiyun static irqreturn_t gt641xx_timer0_interrupt(int irq, void *dev_id)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun 	struct clock_event_device *cd = &gt641xx_timer0_clockevent;
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	cd->event_handler(cd);
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	return IRQ_HANDLED;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun 
gt641xx_timer0_clockevent_init(void)123*4882a593Smuzhiyun static int __init gt641xx_timer0_clockevent_init(void)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun 	struct clock_event_device *cd;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	if (!gt641xx_base_clock)
128*4882a593Smuzhiyun 		return 0;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	GT_WRITE(GT_TC0_OFS, gt641xx_base_clock / HZ);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	cd = &gt641xx_timer0_clockevent;
133*4882a593Smuzhiyun 	cd->rating = 200 + gt641xx_base_clock / 10000000;
134*4882a593Smuzhiyun 	clockevent_set_clock(cd, gt641xx_base_clock);
135*4882a593Smuzhiyun 	cd->max_delta_ns = clockevent_delta2ns(0x7fffffff, cd);
136*4882a593Smuzhiyun 	cd->max_delta_ticks = 0x7fffffff;
137*4882a593Smuzhiyun 	cd->min_delta_ns = clockevent_delta2ns(0x300, cd);
138*4882a593Smuzhiyun 	cd->min_delta_ticks = 0x300;
139*4882a593Smuzhiyun 	cd->cpumask = cpumask_of(0);
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	clockevents_register_device(&gt641xx_timer0_clockevent);
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	return request_irq(GT641XX_TIMER0_IRQ, gt641xx_timer0_interrupt,
144*4882a593Smuzhiyun 			   IRQF_PERCPU | IRQF_TIMER, "gt641xx_timer0", NULL);
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun arch_initcall(gt641xx_timer0_clockevent_init);
147