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