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/module.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->min_delta_ns = clockevent_delta2ns(0xf, cd);
127
128
129 _wrmsr(DIVIL_MSR_REG(MFGPT_IRQ), 0, 0x100);
130
131
132 _wrmsr(DIVIL_MSR_REG(PIC_ZSEL_LOW), 0, 0x50000);
133
134
135 _rdmsr(DIVIL_MSR_REG(DIVIL_LBAR_MFGPT), &basehi, &mfgpt_base);
136
137 clockevents_register_device(cd);
138
139 setup_irq(CS5536_MFGPT_INTR, &irq5);
140}
141
142
143
144
145
146
147static cycle_t mfgpt_read(struct clocksource *cs)
148{
149 unsigned long flags;
150 int count;
151 u32 jifs;
152 static int old_count;
153 static u32 old_jifs;
154
155 raw_spin_lock_irqsave(&mfgpt_lock, flags);
156
157
158
159
160
161
162
163
164
165
166
167
168
169 jifs = jiffies;
170
171 count = inw(MFGPT0_CNT);
172
173
174
175
176
177
178
179
180
181
182
183 if (count < old_count && jifs == old_jifs)
184 count = old_count;
185
186 old_count = count;
187 old_jifs = jifs;
188
189 raw_spin_unlock_irqrestore(&mfgpt_lock, flags);
190
191 return (cycle_t) (jifs * COMPARE) + count;
192}
193
194static struct clocksource clocksource_mfgpt = {
195 .name = "mfgpt",
196 .rating = 120,
197 .read = mfgpt_read,
198 .mask = CLOCKSOURCE_MASK(32),
199};
200
201int __init init_mfgpt_clocksource(void)
202{
203 if (num_possible_cpus() > 1)
204 return 0;
205
206 return clocksource_register_hz(&clocksource_mfgpt, MFGPT_TICK_RATE);
207}
208
209arch_initcall(init_mfgpt_clocksource);
210