blob: badfedb455a9f21793b80824f89ec34aa14e9535 [file] [log] [blame]
Paul Mundtaa016662006-01-16 22:14:18 -08001/*
2 * arch/sh/kernel/timers/timer-tmu.c - TMU Timer Support
3 *
4 * Copyright (C) 2005 Paul Mundt
5 *
6 * TMU handling code hacked out of arch/sh/kernel/time.c
7 *
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>
12 *
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
15 * for more details.
16 */
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>
23#include <asm/rtc.h>
24#include <asm/io.h>
25#include <asm/irq.h>
26#include <asm/clock.h>
27
28#define TMU_TOCR_INIT 0x00
29#define TMU0_TCR_INIT 0x0020
30#define TMU_TSTR_INIT 1
31
32#define TMU0_TCR_CALIB 0x0000
33
34static DEFINE_SPINLOCK(tmu0_lock);
35
36static unsigned long tmu_timer_get_offset(void)
37{
38 int count;
39 unsigned long flags;
40
41 static int count_p = 0x7fffffff; /* for the first call after boot */
42 static unsigned long jiffies_p = 0;
43
44 /*
45 * cache volatile jiffies temporarily; we have IRQs turned off.
46 */
47 unsigned long jiffies_t;
48
49 spin_lock_irqsave(&tmu0_lock, flags);
50 /* timer count may underflow right here */
51 count = ctrl_inl(TMU0_TCNT); /* read the latched count */
52
53 jiffies_t = jiffies;
54
55 /*
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
59 */
60
61 if (jiffies_t == jiffies_p) {
62 if (count > count_p) {
63 /* the nutcase */
64 if (ctrl_inw(TMU0_TCR) & 0x100) { /* Check UNF bit */
65 count -= LATCH;
66 } else {
67 printk("%s (): hardware timer problem?\n",
68 __FUNCTION__);
69 }
70 }
71 } else
72 jiffies_p = jiffies_t;
73
74 count_p = count;
75 spin_unlock_irqrestore(&tmu0_lock, flags);
76
77 count = ((LATCH-1) - count) * TICK_SIZE;
78 count = (count + LATCH/2) / LATCH;
79
80 return count;
81}
82
83static irqreturn_t tmu_timer_interrupt(int irq, void *dev_id,
84 struct pt_regs *regs)
85{
86 unsigned long timer_status;
87
88 /* Clear UNF bit */
89 timer_status = ctrl_inw(TMU0_TCR);
90 timer_status &= ~0x100;
91 ctrl_outw(timer_status, TMU0_TCR);
92
93 /*
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
99 */
100 write_seqlock(&xtime_lock);
101 handle_timer_tick(regs);
102 write_sequnlock(&xtime_lock);
103
104 return IRQ_HANDLED;
105}
106
107static struct irqaction tmu_irq = {
108 .name = "timer",
109 .handler = tmu_timer_interrupt,
Thomas Gleixner6d208192006-07-01 19:29:25 -0700110 .flags = IRQF_DISABLED,
Paul Mundtaa016662006-01-16 22:14:18 -0800111 .mask = CPU_MASK_NONE,
112};
113
Paul Mundtaa016662006-01-16 22:14:18 -0800114static void tmu_clk_init(struct clk *clk)
115{
116 u8 divisor = TMU0_TCR_INIT & 0x7;
117 ctrl_outw(TMU0_TCR_INIT, TMU0_TCR);
118 clk->rate = clk->parent->rate / (4 << (divisor << 1));
119}
120
121static void tmu_clk_recalc(struct clk *clk)
122{
123 u8 divisor = ctrl_inw(TMU0_TCR) & 0x7;
124 clk->rate = clk->parent->rate / (4 << (divisor << 1));
125}
126
127static struct clk_ops tmu_clk_ops = {
128 .init = tmu_clk_init,
129 .recalc = tmu_clk_recalc,
130};
131
132static struct clk tmu0_clk = {
133 .name = "tmu0_clk",
134 .ops = &tmu_clk_ops,
135};
136
Andriy Skulysh3aa770e2006-09-27 16:20:22 +0900137static int tmu_timer_start(void)
138{
139 ctrl_outb(TMU_TSTR_INIT, TMU_TSTR);
140 return 0;
141}
142
143static int tmu_timer_stop(void)
144{
145 ctrl_outb(0, TMU_TSTR);
146 return 0;
147}
148
Paul Mundtaa016662006-01-16 22:14:18 -0800149static int tmu_timer_init(void)
150{
151 unsigned long interval;
152
153 setup_irq(TIMER_IRQ, &tmu_irq);
154
155 tmu0_clk.parent = clk_get("module_clk");
156
157 /* Start TMU0 */
Andriy Skulysh3aa770e2006-09-27 16:20:22 +0900158 tmu_timer_stop();
Paul Mundtaa016662006-01-16 22:14:18 -0800159#if !defined(CONFIG_CPU_SUBTYPE_SH7300) && !defined(CONFIG_CPU_SUBTYPE_SH7760)
160 ctrl_outb(TMU_TOCR_INIT, TMU_TOCR);
161#endif
162
163 clk_register(&tmu0_clk);
164 clk_enable(&tmu0_clk);
165
166 interval = (clk_get_rate(&tmu0_clk) + HZ / 2) / HZ;
167 printk(KERN_INFO "Interval = %ld\n", interval);
168
169 ctrl_outl(interval, TMU0_TCOR);
170 ctrl_outl(interval, TMU0_TCNT);
171
Andriy Skulysh3aa770e2006-09-27 16:20:22 +0900172 tmu_timer_start();
Paul Mundtaa016662006-01-16 22:14:18 -0800173
174 return 0;
175}
176
177struct sys_timer_ops tmu_timer_ops = {
178 .init = tmu_timer_init,
Andriy Skulysh3aa770e2006-09-27 16:20:22 +0900179 .start = tmu_timer_start,
180 .stop = tmu_timer_stop,
Paul Mundta700f352006-10-04 13:27:32 +0900181#ifndef CONFIG_GENERIC_TIME
Paul Mundtaa016662006-01-16 22:14:18 -0800182 .get_offset = tmu_timer_get_offset,
Paul Mundta700f352006-10-04 13:27:32 +0900183#endif
Paul Mundtaa016662006-01-16 22:14:18 -0800184};
185
186struct sys_timer tmu_timer = {
187 .name = "tmu",
188 .ops = &tmu_timer_ops,
189};