1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/init.h>
23#include <linux/interrupt.h>
24#include <linux/irq.h>
25#include <linux/kernel.h>
26#include <linux/sched.h>
27#include <linux/io.h>
28#include <linux/clockchips.h>
29
30#include <asm/mach/time.h>
31#include <asm/system_misc.h>
32
33#include <mach/regs-irq.h>
34
35#include "generic.h"
36
37#define KS8695_TMR_OFFSET (0xF0000 + 0xE400)
38#define KS8695_TMR_VA (KS8695_IO_VA + KS8695_TMR_OFFSET)
39#define KS8695_TMR_PA (KS8695_IO_PA + KS8695_TMR_OFFSET)
40
41
42
43
44#define KS8695_TMCON (0x00)
45#define KS8695_T1TC (0x04)
46#define KS8695_T0TC (0x08)
47#define KS8695_T1PD (0x0C)
48#define KS8695_T0PD (0x10)
49
50
51#define TMCON_T1EN (1 << 1)
52#define TMCON_T0EN (1 << 0)
53
54
55#define T0TC_WATCHDOG (0xff)
56
57static int ks8695_set_periodic(struct clock_event_device *evt)
58{
59 u32 rate = DIV_ROUND_CLOSEST(KS8695_CLOCK_RATE, HZ);
60 u32 half = DIV_ROUND_CLOSEST(rate, 2);
61 u32 tmcon;
62
63
64 tmcon = readl_relaxed(KS8695_TMR_VA + KS8695_TMCON);
65 tmcon &= ~TMCON_T1EN;
66 writel_relaxed(tmcon, KS8695_TMR_VA + KS8695_TMCON);
67
68
69 writel_relaxed(half, KS8695_TMR_VA + KS8695_T1TC);
70 writel_relaxed(half, KS8695_TMR_VA + KS8695_T1PD);
71
72
73 tmcon |= TMCON_T1EN;
74 writel_relaxed(tmcon, KS8695_TMR_VA + KS8695_TMCON);
75 return 0;
76}
77
78static int ks8695_set_next_event(unsigned long cycles,
79 struct clock_event_device *evt)
80
81{
82 u32 half = DIV_ROUND_CLOSEST(cycles, 2);
83 u32 tmcon;
84
85
86 tmcon = readl_relaxed(KS8695_TMR_VA + KS8695_TMCON);
87 tmcon &= ~TMCON_T1EN;
88 writel_relaxed(tmcon, KS8695_TMR_VA + KS8695_TMCON);
89
90
91 writel_relaxed(half, KS8695_TMR_VA + KS8695_T1TC);
92 writel_relaxed(half, KS8695_TMR_VA + KS8695_T1PD);
93
94
95 tmcon |= TMCON_T1EN;
96 writel_relaxed(tmcon, KS8695_TMR_VA + KS8695_TMCON);
97
98 return 0;
99}
100
101static struct clock_event_device clockevent_ks8695 = {
102 .name = "ks8695_t1tc",
103
104 .rating = 300,
105 .features = CLOCK_EVT_FEAT_ONESHOT |
106 CLOCK_EVT_FEAT_PERIODIC,
107 .set_next_event = ks8695_set_next_event,
108 .set_state_periodic = ks8695_set_periodic,
109};
110
111
112
113
114static irqreturn_t ks8695_timer_interrupt(int irq, void *dev_id)
115{
116 struct clock_event_device *evt = &clockevent_ks8695;
117
118 evt->event_handler(evt);
119 return IRQ_HANDLED;
120}
121
122static struct irqaction ks8695_timer_irq = {
123 .name = "ks8695_tick",
124 .flags = IRQF_TIMER,
125 .handler = ks8695_timer_interrupt,
126};
127
128static void ks8695_timer_setup(void)
129{
130 unsigned long tmcon;
131
132
133 tmcon = readl_relaxed(KS8695_TMR_VA + KS8695_TMCON);
134 tmcon &= ~TMCON_T0EN;
135 tmcon &= ~TMCON_T1EN;
136 writel_relaxed(tmcon, KS8695_TMR_VA + KS8695_TMCON);
137
138
139
140
141
142
143 clockevents_config_and_register(&clockevent_ks8695,
144 KS8695_CLOCK_RATE, 2,
145 0xFFFFFFFFU);
146}
147
148void __init ks8695_timer_init(void)
149{
150 ks8695_timer_setup();
151
152
153 setup_irq(KS8695_IRQ_TIMER1, &ks8695_timer_irq);
154}
155
156void ks8695_restart(enum reboot_mode reboot_mode, const char *cmd)
157{
158 unsigned int reg;
159
160 if (reboot_mode == REBOOT_SOFT)
161 soft_restart(0);
162
163
164 reg = readl_relaxed(KS8695_TMR_VA + KS8695_TMCON);
165 writel_relaxed(reg & ~TMCON_T0EN, KS8695_TMR_VA + KS8695_TMCON);
166
167
168 writel_relaxed((10 << 8) | T0TC_WATCHDOG, KS8695_TMR_VA + KS8695_T0TC);
169
170
171 writel_relaxed(reg | TMCON_T0EN, KS8695_TMR_VA + KS8695_TMCON);
172}
173