1
2
3
4
5#include <linux/clockchips.h>
6#include <linux/init.h>
7#include <linux/interrupt.h>
8#include <linux/jiffies.h>
9#include <linux/module.h>
10#include <linux/spinlock.h>
11
12#include <asm/delay.h>
13#include <asm/i8253.h>
14#include <asm/io.h>
15#include <asm/time.h>
16
17DEFINE_SPINLOCK(i8253_lock);
18EXPORT_SYMBOL(i8253_lock);
19
20
21
22
23
24
25static void init_pit_timer(enum clock_event_mode mode,
26 struct clock_event_device *evt)
27{
28 spin_lock(&i8253_lock);
29
30 switch(mode) {
31 case CLOCK_EVT_MODE_PERIODIC:
32
33 outb_p(0x34, PIT_MODE);
34 outb_p(LATCH & 0xff , PIT_CH0);
35 outb(LATCH >> 8 , PIT_CH0);
36 break;
37
38 case CLOCK_EVT_MODE_SHUTDOWN:
39 case CLOCK_EVT_MODE_UNUSED:
40 if (evt->mode == CLOCK_EVT_MODE_PERIODIC ||
41 evt->mode == CLOCK_EVT_MODE_ONESHOT) {
42 outb_p(0x30, PIT_MODE);
43 outb_p(0, PIT_CH0);
44 outb_p(0, PIT_CH0);
45 }
46 break;
47
48 case CLOCK_EVT_MODE_ONESHOT:
49
50 outb_p(0x38, PIT_MODE);
51 break;
52
53 case CLOCK_EVT_MODE_RESUME:
54
55 break;
56 }
57 spin_unlock(&i8253_lock);
58}
59
60
61
62
63
64
65static int pit_next_event(unsigned long delta, struct clock_event_device *evt)
66{
67 spin_lock(&i8253_lock);
68 outb_p(delta & 0xff , PIT_CH0);
69 outb(delta >> 8 , PIT_CH0);
70 spin_unlock(&i8253_lock);
71
72 return 0;
73}
74
75
76
77
78
79
80
81
82
83static struct clock_event_device pit_clockevent = {
84 .name = "pit",
85 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
86 .set_mode = init_pit_timer,
87 .set_next_event = pit_next_event,
88 .irq = 0,
89};
90
91static irqreturn_t timer_interrupt(int irq, void *dev_id)
92{
93 pit_clockevent.event_handler(&pit_clockevent);
94
95 return IRQ_HANDLED;
96}
97
98static struct irqaction irq0 = {
99 .handler = timer_interrupt,
100 .flags = IRQF_DISABLED | IRQF_NOBALANCING,
101 .name = "timer"
102};
103
104
105
106
107
108void __init setup_pit_timer(void)
109{
110 struct clock_event_device *cd = &pit_clockevent;
111 unsigned int cpu = smp_processor_id();
112
113
114
115
116
117 cd->cpumask = cpumask_of(cpu);
118 clockevent_set_clock(cd, CLOCK_TICK_RATE);
119 cd->max_delta_ns = clockevent_delta2ns(0x7FFF, cd);
120 cd->min_delta_ns = clockevent_delta2ns(0xF, cd);
121 clockevents_register_device(cd);
122
123 setup_irq(0, &irq0);
124}
125
126
127
128
129
130
131static cycle_t pit_read(struct clocksource *cs)
132{
133 unsigned long flags;
134 int count;
135 u32 jifs;
136 static int old_count;
137 static u32 old_jifs;
138
139 spin_lock_irqsave(&i8253_lock, flags);
140
141
142
143
144
145
146
147
148
149
150
151
152
153 jifs = jiffies;
154 outb_p(0x00, PIT_MODE);
155 count = inb_p(PIT_CH0);
156 count |= inb_p(PIT_CH0) << 8;
157
158
159 if (count > LATCH) {
160 outb_p(0x34, PIT_MODE);
161 outb_p(LATCH & 0xff, PIT_CH0);
162 outb(LATCH >> 8, PIT_CH0);
163 count = LATCH - 1;
164 }
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179 if (count > old_count && jifs == old_jifs) {
180 count = old_count;
181 }
182 old_count = count;
183 old_jifs = jifs;
184
185 spin_unlock_irqrestore(&i8253_lock, flags);
186
187 count = (LATCH - 1) - count;
188
189 return (cycle_t)(jifs * LATCH) + count;
190}
191
192static struct clocksource clocksource_pit = {
193 .name = "pit",
194 .rating = 110,
195 .read = pit_read,
196 .mask = CLOCKSOURCE_MASK(32),
197 .mult = 0,
198 .shift = 20,
199};
200
201static int __init init_pit_clocksource(void)
202{
203 if (num_possible_cpus() > 1)
204 return 0;
205
206 clocksource_pit.mult = clocksource_hz2mult(CLOCK_TICK_RATE, 20);
207 return clocksource_register(&clocksource_pit);
208}
209arch_initcall(init_pit_clocksource);
210