1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * arch/arm/plat-iop/time.c
5 * Timer code for IOP32x and IOP33x based systems
7 * Author: Deepak Saxena <dsaxena@mvista.com>
9 * Copyright 2002-2003 MontaVista Software Inc.
12 #include <linux/kernel.h>
13 #include <linux/interrupt.h>
14 #include <linux/time.h>
15 #include <linux/init.h>
16 #include <linux/timex.h>
18 #include <linux/clocksource.h>
19 #include <linux/clockchips.h>
20 #include <linux/export.h>
21 #include <linux/sched_clock.h>
23 #include <linux/uaccess.h>
24 #include <asm/mach/irq.h>
25 #include <asm/mach/time.h>
31 * Minimum clocksource/clockevent timer range in seconds
33 #define IOP_MIN_RANGE 4
36 * IOP clocksource (free-running timer 1).
38 static u64 notrace
iop_clocksource_read(struct clocksource
*unused
)
40 return 0xffffffffu
- read_tcr1();
43 static struct clocksource iop_clocksource
= {
46 .read
= iop_clocksource_read
,
47 .mask
= CLOCKSOURCE_MASK(32),
48 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
52 * IOP sched_clock() implementation via its clocksource.
54 static u64 notrace
iop_read_sched_clock(void)
56 return 0xffffffffu
- read_tcr1();
60 * IOP clockevents (interrupting timer 0).
62 static int iop_set_next_event(unsigned long delta
,
63 struct clock_event_device
*unused
)
65 u32 tmr
= IOP_TMR_PRIVILEGED
| IOP_TMR_RATIO_1_1
;
68 write_tmr0(tmr
& ~(IOP_TMR_EN
| IOP_TMR_RELOAD
));
70 write_tmr0((tmr
& ~IOP_TMR_RELOAD
) | IOP_TMR_EN
);
75 static unsigned long ticks_per_jiffy
;
77 static int iop_set_periodic(struct clock_event_device
*evt
)
79 u32 tmr
= read_tmr0();
81 write_tmr0(tmr
& ~IOP_TMR_EN
);
82 write_tcr0(ticks_per_jiffy
- 1);
83 write_trr0(ticks_per_jiffy
- 1);
84 tmr
|= (IOP_TMR_RELOAD
| IOP_TMR_EN
);
90 static int iop_set_oneshot(struct clock_event_device
*evt
)
92 u32 tmr
= read_tmr0();
94 /* ->set_next_event sets period and enables timer */
95 tmr
&= ~(IOP_TMR_RELOAD
| IOP_TMR_EN
);
100 static int iop_shutdown(struct clock_event_device
*evt
)
102 u32 tmr
= read_tmr0();
109 static int iop_resume(struct clock_event_device
*evt
)
111 u32 tmr
= read_tmr0();
118 static struct clock_event_device iop_clockevent
= {
119 .name
= "iop_timer0",
120 .features
= CLOCK_EVT_FEAT_PERIODIC
|
121 CLOCK_EVT_FEAT_ONESHOT
,
123 .set_next_event
= iop_set_next_event
,
124 .set_state_shutdown
= iop_shutdown
,
125 .set_state_periodic
= iop_set_periodic
,
126 .tick_resume
= iop_resume
,
127 .set_state_oneshot
= iop_set_oneshot
,
131 iop_timer_interrupt(int irq
, void *dev_id
)
133 struct clock_event_device
*evt
= dev_id
;
136 evt
->event_handler(evt
);
140 static unsigned long iop_tick_rate
;
141 unsigned long get_iop_tick_rate(void)
143 return iop_tick_rate
;
145 EXPORT_SYMBOL(get_iop_tick_rate
);
147 void __init
iop_init_time(unsigned long tick_rate
)
150 int irq
= IRQ_IOP32X_TIMER0
;
152 sched_clock_register(iop_read_sched_clock
, 32, tick_rate
);
154 ticks_per_jiffy
= DIV_ROUND_CLOSEST(tick_rate
, HZ
);
155 iop_tick_rate
= tick_rate
;
157 timer_ctl
= IOP_TMR_EN
| IOP_TMR_PRIVILEGED
|
158 IOP_TMR_RELOAD
| IOP_TMR_RATIO_1_1
;
161 * Set up interrupting clockevent timer 0.
163 write_tmr0(timer_ctl
& ~IOP_TMR_EN
);
165 if (request_irq(irq
, iop_timer_interrupt
, IRQF_TIMER
| IRQF_IRQPOLL
,
166 "IOP Timer Tick", &iop_clockevent
))
167 pr_err("Failed to request irq() %d (IOP Timer Tick)\n", irq
);
168 iop_clockevent
.cpumask
= cpumask_of(0);
169 clockevents_config_and_register(&iop_clockevent
, tick_rate
,
173 * Set up free-running clocksource timer 1.
175 write_trr1(0xffffffff);
176 write_tcr1(0xffffffff);
177 write_tmr1(timer_ctl
);
178 clocksource_register_hz(&iop_clocksource
, tick_rate
);