2 * arch/sh/kernel/timers/timer-tmu.c - TMU Timer Support
4 * Copyright (C) 2005 Paul Mundt
6 * TMU handling code hacked out of arch/sh/kernel/time.c
8 * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka
9 * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
10 * Copyright (C) 2002, 2003, 2004 Paul Mundt
11 * Copyright (C) 2002 M. R. Brown <mrbrown@linux-sh.org>
13 * This file is subject to the terms and conditions of the GNU General Public
14 * License. See the file "COPYING" in the main directory of this archive
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/interrupt.h>
20 #include <linux/spinlock.h>
21 #include <linux/seqlock.h>
22 #include <asm/timer.h>
26 #include <asm/clock.h>
28 #define TMU_TOCR_INIT 0x00
29 #define TMU0_TCR_INIT 0x0020
30 #define TMU_TSTR_INIT 1
32 #define TMU0_TCR_CALIB 0x0000
34 static DEFINE_SPINLOCK(tmu0_lock
);
36 static unsigned long tmu_timer_get_offset(void)
41 static int count_p
= 0x7fffffff; /* for the first call after boot */
42 static unsigned long jiffies_p
= 0;
45 * cache volatile jiffies temporarily; we have IRQs turned off.
47 unsigned long jiffies_t
;
49 spin_lock_irqsave(&tmu0_lock
, flags
);
50 /* timer count may underflow right here */
51 count
= ctrl_inl(TMU0_TCNT
); /* read the latched count */
56 * avoiding timer inconsistencies (they are rare, but they happen)...
57 * there is one kind of problem that must be avoided here:
58 * 1. the timer counter underflows
61 if (jiffies_t
== jiffies_p
) {
62 if (count
> count_p
) {
64 if (ctrl_inw(TMU0_TCR
) & 0x100) { /* Check UNF bit */
67 printk("%s (): hardware timer problem?\n",
72 jiffies_p
= jiffies_t
;
75 spin_unlock_irqrestore(&tmu0_lock
, flags
);
77 count
= ((LATCH
-1) - count
) * TICK_SIZE
;
78 count
= (count
+ LATCH
/2) / LATCH
;
83 static irqreturn_t
tmu_timer_interrupt(int irq
, void *dev_id
,
86 unsigned long timer_status
;
89 timer_status
= ctrl_inw(TMU0_TCR
);
90 timer_status
&= ~0x100;
91 ctrl_outw(timer_status
, TMU0_TCR
);
94 * Here we are in the timer irq handler. We just have irqs locally
95 * disabled but we don't know if the timer_bh is running on the other
96 * CPU. We need to avoid to SMP race with it. NOTE: we don' t need
97 * the irq version of write_lock because as just said we have irq
98 * locally disabled. -arca
100 write_seqlock(&xtime_lock
);
101 handle_timer_tick(regs
);
102 write_sequnlock(&xtime_lock
);
107 static struct irqaction tmu_irq
= {
109 .handler
= tmu_timer_interrupt
,
110 .flags
= IRQF_DISABLED
,
111 .mask
= CPU_MASK_NONE
,
115 * Hah! We'll see if this works (switching from usecs to nsecs).
117 static unsigned long tmu_timer_get_frequency(void)
120 struct timespec ts1
, ts2
;
121 unsigned long diff_nsec
;
122 unsigned long factor
;
124 /* Setup the timer: We don't want to generate interrupts, just
125 * have it count down at its natural rate.
127 ctrl_outb(0, TMU_TSTR
);
128 #if !defined(CONFIG_CPU_SUBTYPE_SH7300) && !defined(CONFIG_CPU_SUBTYPE_SH7760)
129 ctrl_outb(TMU_TOCR_INIT
, TMU_TOCR
);
131 ctrl_outw(TMU0_TCR_CALIB
, TMU0_TCR
);
132 ctrl_outl(0xffffffff, TMU0_TCOR
);
133 ctrl_outl(0xffffffff, TMU0_TCNT
);
139 } while (ts1
.tv_nsec
== ts2
.tv_nsec
&& ts1
.tv_sec
== ts2
.tv_sec
);
141 /* actually start the timer */
142 ctrl_outb(TMU_TSTR_INIT
, TMU_TSTR
);
146 } while (ts1
.tv_nsec
== ts2
.tv_nsec
&& ts1
.tv_sec
== ts2
.tv_sec
);
148 freq
= 0xffffffff - ctrl_inl(TMU0_TCNT
);
149 if (ts2
.tv_nsec
< ts1
.tv_nsec
) {
150 ts2
.tv_nsec
+= 1000000000;
154 diff_nsec
= (ts2
.tv_sec
- ts1
.tv_sec
) * 1000000000 + (ts2
.tv_nsec
- ts1
.tv_nsec
);
156 /* this should work well if the RTC has a precision of n Hz, where
157 * n is an integer. I don't think we have to worry about the other
159 factor
= (1000000000 + diff_nsec
/2) / diff_nsec
;
161 if (factor
* diff_nsec
> 1100000000 ||
162 factor
* diff_nsec
< 900000000)
163 panic("weird RTC (diff_nsec %ld)", diff_nsec
);
165 return freq
* factor
;
168 static void tmu_clk_init(struct clk
*clk
)
170 u8 divisor
= TMU0_TCR_INIT
& 0x7;
171 ctrl_outw(TMU0_TCR_INIT
, TMU0_TCR
);
172 clk
->rate
= clk
->parent
->rate
/ (4 << (divisor
<< 1));
175 static void tmu_clk_recalc(struct clk
*clk
)
177 u8 divisor
= ctrl_inw(TMU0_TCR
) & 0x7;
178 clk
->rate
= clk
->parent
->rate
/ (4 << (divisor
<< 1));
181 static struct clk_ops tmu_clk_ops
= {
182 .init
= tmu_clk_init
,
183 .recalc
= tmu_clk_recalc
,
186 static struct clk tmu0_clk
= {
191 static int tmu_timer_init(void)
193 unsigned long interval
;
195 setup_irq(TIMER_IRQ
, &tmu_irq
);
197 tmu0_clk
.parent
= clk_get("module_clk");
200 ctrl_outb(0, TMU_TSTR
);
201 #if !defined(CONFIG_CPU_SUBTYPE_SH7300) && !defined(CONFIG_CPU_SUBTYPE_SH7760)
202 ctrl_outb(TMU_TOCR_INIT
, TMU_TOCR
);
205 clk_register(&tmu0_clk
);
206 clk_enable(&tmu0_clk
);
208 interval
= (clk_get_rate(&tmu0_clk
) + HZ
/ 2) / HZ
;
209 printk(KERN_INFO
"Interval = %ld\n", interval
);
211 ctrl_outl(interval
, TMU0_TCOR
);
212 ctrl_outl(interval
, TMU0_TCNT
);
214 ctrl_outb(TMU_TSTR_INIT
, TMU_TSTR
);
219 struct sys_timer_ops tmu_timer_ops
= {
220 .init
= tmu_timer_init
,
221 .get_frequency
= tmu_timer_get_frequency
,
222 .get_offset
= tmu_timer_get_offset
,
225 struct sys_timer tmu_timer
= {
227 .ops
= &tmu_timer_ops
,