1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/io.h>
19#include <linux/init.h>
20#include <linux/export.h>
21#include <linux/jiffies.h>
22#include <linux/spinlock.h>
23#include <linux/interrupt.h>
24#include <linux/clockchips.h>
25
26#include <asm/time.h>
27
28#include <cs5536/cs5536_mfgpt.h>
29
30static DEFINE_RAW_SPINLOCK(mfgpt_lock);
31
32static u32 mfgpt_base;
33
34
35
36
37
38
39
40
41void disable_mfgpt0_counter(void)
42{
43 outw(inw(MFGPT0_SETUP) & 0x7fff, MFGPT0_SETUP);
44}
45EXPORT_SYMBOL(disable_mfgpt0_counter);
46
47
48void enable_mfgpt0_counter(void)
49{
50 outw(0xe310, MFGPT0_SETUP);
51}
52EXPORT_SYMBOL(enable_mfgpt0_counter);
53
54static int mfgpt_timer_set_periodic(struct clock_event_device *evt)
55{
56 raw_spin_lock(&mfgpt_lock);
57
58 outw(COMPARE, MFGPT0_CMP2);
59 outw(0, MFGPT0_CNT);
60 enable_mfgpt0_counter();
61
62 raw_spin_unlock(&mfgpt_lock);
63 return 0;
64}
65
66static int mfgpt_timer_shutdown(struct clock_event_device *evt)
67{
68 if (clockevent_state_periodic(evt) || clockevent_state_oneshot(evt)) {
69 raw_spin_lock(&mfgpt_lock);
70 disable_mfgpt0_counter();
71 raw_spin_unlock(&mfgpt_lock);
72 }
73
74 return 0;
75}
76
77static struct clock_event_device mfgpt_clockevent = {
78 .name = "mfgpt",
79 .features = CLOCK_EVT_FEAT_PERIODIC,
80
81
82 .set_state_shutdown = mfgpt_timer_shutdown,
83 .set_state_periodic = mfgpt_timer_set_periodic,
84 .irq = CS5536_MFGPT_INTR,
85};
86
87static irqreturn_t timer_interrupt(int irq, void *dev_id)
88{
89 u32 basehi;
90
91
92
93
94
95
96
97 _rdmsr(DIVIL_MSR_REG(DIVIL_LBAR_MFGPT), &basehi, &mfgpt_base);
98
99
100 outw(inw(MFGPT0_SETUP) | 0x4000, MFGPT0_SETUP);
101
102 mfgpt_clockevent.event_handler(&mfgpt_clockevent);
103
104 return IRQ_HANDLED;
105}
106
107static struct irqaction irq5 = {
108 .handler = timer_interrupt,
109 .flags = IRQF_NOBALANCING | IRQF_TIMER,
110 .name = "timer"
111};
112
113
114
115
116
117void __init setup_mfgpt0_timer(void)
118{
119 u32 basehi;
120 struct clock_event_device *cd = &mfgpt_clockevent;
121 unsigned int cpu = smp_processor_id();
122
123 cd->cpumask = cpumask_of(cpu);
124 clockevent_set_clock(cd, MFGPT_TICK_RATE);
125 cd->max_delta_ns = clockevent_delta2ns(0xffff, cd);
126 cd->max_delta_ticks = 0xffff;
127 cd->min_delta_ns = clockevent_delta2ns(0xf, cd);
128 cd->min_delta_ticks = 0xf;
129
130
131 _wrmsr(DIVIL_MSR_REG(MFGPT_IRQ), 0, 0x100);
132
133
134 _wrmsr(DIVIL_MSR_REG(PIC_ZSEL_LOW), 0, 0x50000);
135
136
137 _rdmsr(DIVIL_MSR_REG(DIVIL_LBAR_MFGPT), &basehi, &mfgpt_base);
138
139 clockevents_register_device(cd);
140
141 setup_irq(CS5536_MFGPT_INTR, &irq5);
142}
143
144
145
146
147
148
149static u64 mfgpt_read(struct clocksource *cs)
150{
151 unsigned long flags;
152 int count;
153 u32 jifs;
154 static int old_count;
155 static u32 old_jifs;
156
157 raw_spin_lock_irqsave(&mfgpt_lock, flags);
158
159
160
161
162
163
164
165
166
167
168
169
170
171 jifs = jiffies;
172
173 count = inw(MFGPT0_CNT);
174
175
176
177
178
179
180
181
182
183
184
185 if (count < old_count && jifs == old_jifs)
186 count = old_count;
187
188 old_count = count;
189 old_jifs = jifs;
190
191 raw_spin_unlock_irqrestore(&mfgpt_lock, flags);
192
193 return (u64) (jifs * COMPARE) + count;
194}
195
196static struct clocksource clocksource_mfgpt = {
197 .name = "mfgpt",
198 .rating = 120,
199 .read = mfgpt_read,
200 .mask = CLOCKSOURCE_MASK(32),
201};
202
203int __init init_mfgpt_clocksource(void)
204{
205 if (num_possible_cpus() > 1)
206 return 0;
207
208 return clocksource_register_hz(&clocksource_mfgpt, MFGPT_TICK_RATE);
209}
210
211arch_initcall(init_mfgpt_clocksource);
212