xref: /OK3568_Linux_fs/kernel/arch/arm/mach-iop32x/time.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * arch/arm/plat-iop/time.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Timer code for IOP32x and IOP33x based systems
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Author: Deepak Saxena <dsaxena@mvista.com>
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Copyright 2002-2003 MontaVista Software Inc.
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/kernel.h>
13*4882a593Smuzhiyun #include <linux/interrupt.h>
14*4882a593Smuzhiyun #include <linux/time.h>
15*4882a593Smuzhiyun #include <linux/init.h>
16*4882a593Smuzhiyun #include <linux/timex.h>
17*4882a593Smuzhiyun #include <linux/io.h>
18*4882a593Smuzhiyun #include <linux/clocksource.h>
19*4882a593Smuzhiyun #include <linux/clockchips.h>
20*4882a593Smuzhiyun #include <linux/export.h>
21*4882a593Smuzhiyun #include <linux/sched_clock.h>
22*4882a593Smuzhiyun #include <asm/irq.h>
23*4882a593Smuzhiyun #include <linux/uaccess.h>
24*4882a593Smuzhiyun #include <asm/mach/irq.h>
25*4882a593Smuzhiyun #include <asm/mach/time.h>
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #include "hardware.h"
28*4882a593Smuzhiyun #include "irqs.h"
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun /*
31*4882a593Smuzhiyun  * Minimum clocksource/clockevent timer range in seconds
32*4882a593Smuzhiyun  */
33*4882a593Smuzhiyun #define IOP_MIN_RANGE 4
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /*
36*4882a593Smuzhiyun  * IOP clocksource (free-running timer 1).
37*4882a593Smuzhiyun  */
iop_clocksource_read(struct clocksource * unused)38*4882a593Smuzhiyun static u64 notrace iop_clocksource_read(struct clocksource *unused)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	return 0xffffffffu - read_tcr1();
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun static struct clocksource iop_clocksource = {
44*4882a593Smuzhiyun 	.name 		= "iop_timer1",
45*4882a593Smuzhiyun 	.rating		= 300,
46*4882a593Smuzhiyun 	.read		= iop_clocksource_read,
47*4882a593Smuzhiyun 	.mask		= CLOCKSOURCE_MASK(32),
48*4882a593Smuzhiyun 	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
49*4882a593Smuzhiyun };
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun /*
52*4882a593Smuzhiyun  * IOP sched_clock() implementation via its clocksource.
53*4882a593Smuzhiyun  */
iop_read_sched_clock(void)54*4882a593Smuzhiyun static u64 notrace iop_read_sched_clock(void)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun 	return 0xffffffffu - read_tcr1();
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun /*
60*4882a593Smuzhiyun  * IOP clockevents (interrupting timer 0).
61*4882a593Smuzhiyun  */
iop_set_next_event(unsigned long delta,struct clock_event_device * unused)62*4882a593Smuzhiyun static int iop_set_next_event(unsigned long delta,
63*4882a593Smuzhiyun 			      struct clock_event_device *unused)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun 	u32 tmr = IOP_TMR_PRIVILEGED | IOP_TMR_RATIO_1_1;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	BUG_ON(delta == 0);
68*4882a593Smuzhiyun 	write_tmr0(tmr & ~(IOP_TMR_EN | IOP_TMR_RELOAD));
69*4882a593Smuzhiyun 	write_tcr0(delta);
70*4882a593Smuzhiyun 	write_tmr0((tmr & ~IOP_TMR_RELOAD) | IOP_TMR_EN);
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	return 0;
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun static unsigned long ticks_per_jiffy;
76*4882a593Smuzhiyun 
iop_set_periodic(struct clock_event_device * evt)77*4882a593Smuzhiyun static int iop_set_periodic(struct clock_event_device *evt)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun 	u32 tmr = read_tmr0();
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	write_tmr0(tmr & ~IOP_TMR_EN);
82*4882a593Smuzhiyun 	write_tcr0(ticks_per_jiffy - 1);
83*4882a593Smuzhiyun 	write_trr0(ticks_per_jiffy - 1);
84*4882a593Smuzhiyun 	tmr |= (IOP_TMR_RELOAD | IOP_TMR_EN);
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	write_tmr0(tmr);
87*4882a593Smuzhiyun 	return 0;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun 
iop_set_oneshot(struct clock_event_device * evt)90*4882a593Smuzhiyun static int iop_set_oneshot(struct clock_event_device *evt)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	u32 tmr = read_tmr0();
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	/* ->set_next_event sets period and enables timer */
95*4882a593Smuzhiyun 	tmr &= ~(IOP_TMR_RELOAD | IOP_TMR_EN);
96*4882a593Smuzhiyun 	write_tmr0(tmr);
97*4882a593Smuzhiyun 	return 0;
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun 
iop_shutdown(struct clock_event_device * evt)100*4882a593Smuzhiyun static int iop_shutdown(struct clock_event_device *evt)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun 	u32 tmr = read_tmr0();
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	tmr &= ~IOP_TMR_EN;
105*4882a593Smuzhiyun 	write_tmr0(tmr);
106*4882a593Smuzhiyun 	return 0;
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun 
iop_resume(struct clock_event_device * evt)109*4882a593Smuzhiyun static int iop_resume(struct clock_event_device *evt)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun 	u32 tmr = read_tmr0();
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	tmr |= IOP_TMR_EN;
114*4882a593Smuzhiyun 	write_tmr0(tmr);
115*4882a593Smuzhiyun 	return 0;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun static struct clock_event_device iop_clockevent = {
119*4882a593Smuzhiyun 	.name			= "iop_timer0",
120*4882a593Smuzhiyun 	.features		= CLOCK_EVT_FEAT_PERIODIC |
121*4882a593Smuzhiyun 				  CLOCK_EVT_FEAT_ONESHOT,
122*4882a593Smuzhiyun 	.rating			= 300,
123*4882a593Smuzhiyun 	.set_next_event		= iop_set_next_event,
124*4882a593Smuzhiyun 	.set_state_shutdown	= iop_shutdown,
125*4882a593Smuzhiyun 	.set_state_periodic	= iop_set_periodic,
126*4882a593Smuzhiyun 	.tick_resume		= iop_resume,
127*4882a593Smuzhiyun 	.set_state_oneshot	= iop_set_oneshot,
128*4882a593Smuzhiyun };
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun static irqreturn_t
iop_timer_interrupt(int irq,void * dev_id)131*4882a593Smuzhiyun iop_timer_interrupt(int irq, void *dev_id)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	struct clock_event_device *evt = dev_id;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	write_tisr(1);
136*4882a593Smuzhiyun 	evt->event_handler(evt);
137*4882a593Smuzhiyun 	return IRQ_HANDLED;
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun static unsigned long iop_tick_rate;
get_iop_tick_rate(void)141*4882a593Smuzhiyun unsigned long get_iop_tick_rate(void)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun 	return iop_tick_rate;
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun EXPORT_SYMBOL(get_iop_tick_rate);
146*4882a593Smuzhiyun 
iop_init_time(unsigned long tick_rate)147*4882a593Smuzhiyun void __init iop_init_time(unsigned long tick_rate)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun 	u32 timer_ctl;
150*4882a593Smuzhiyun 	int irq = IRQ_IOP32X_TIMER0;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	sched_clock_register(iop_read_sched_clock, 32, tick_rate);
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	ticks_per_jiffy = DIV_ROUND_CLOSEST(tick_rate, HZ);
155*4882a593Smuzhiyun 	iop_tick_rate = tick_rate;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	timer_ctl = IOP_TMR_EN | IOP_TMR_PRIVILEGED |
158*4882a593Smuzhiyun 			IOP_TMR_RELOAD | IOP_TMR_RATIO_1_1;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	/*
161*4882a593Smuzhiyun 	 * Set up interrupting clockevent timer 0.
162*4882a593Smuzhiyun 	 */
163*4882a593Smuzhiyun 	write_tmr0(timer_ctl & ~IOP_TMR_EN);
164*4882a593Smuzhiyun 	write_tisr(1);
165*4882a593Smuzhiyun 	if (request_irq(irq, iop_timer_interrupt, IRQF_TIMER | IRQF_IRQPOLL,
166*4882a593Smuzhiyun 			"IOP Timer Tick", &iop_clockevent))
167*4882a593Smuzhiyun 		pr_err("Failed to request irq() %d (IOP Timer Tick)\n", irq);
168*4882a593Smuzhiyun 	iop_clockevent.cpumask = cpumask_of(0);
169*4882a593Smuzhiyun 	clockevents_config_and_register(&iop_clockevent, tick_rate,
170*4882a593Smuzhiyun 					0xf, 0xfffffffe);
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	/*
173*4882a593Smuzhiyun 	 * Set up free-running clocksource timer 1.
174*4882a593Smuzhiyun 	 */
175*4882a593Smuzhiyun 	write_trr1(0xffffffff);
176*4882a593Smuzhiyun 	write_tcr1(0xffffffff);
177*4882a593Smuzhiyun 	write_tmr1(timer_ctl);
178*4882a593Smuzhiyun 	clocksource_register_hz(&iop_clocksource, tick_rate);
179*4882a593Smuzhiyun }
180