1
2
3
4#include <linux/clockchips.h>
5#include <linux/init.h>
6#include <linux/io.h>
7#include <linux/spinlock.h>
8#include <linux/timex.h>
9#include <linux/module.h>
10#include <linux/i8253.h>
11#include <linux/smp.h>
12
13
14
15
16
17
18
19DEFINE_RAW_SPINLOCK(i8253_lock);
20EXPORT_SYMBOL(i8253_lock);
21
22#ifdef CONFIG_CLKSRC_I8253
23
24
25
26
27
28static cycle_t i8253_read(struct clocksource *cs)
29{
30 static int old_count;
31 static u32 old_jifs;
32 unsigned long flags;
33 int count;
34 u32 jifs;
35
36 raw_spin_lock_irqsave(&i8253_lock, flags);
37
38
39
40
41
42
43
44
45
46
47
48
49
50 jifs = jiffies;
51 outb_p(0x00, PIT_MODE);
52 count = inb_p(PIT_CH0);
53 count |= inb_p(PIT_CH0) << 8;
54
55
56 if (count > PIT_LATCH) {
57 outb_p(0x34, PIT_MODE);
58 outb_p(PIT_LATCH & 0xff, PIT_CH0);
59 outb_p(PIT_LATCH >> 8, PIT_CH0);
60 count = PIT_LATCH - 1;
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 if (count > old_count && jifs == old_jifs)
77 count = old_count;
78
79 old_count = count;
80 old_jifs = jifs;
81
82 raw_spin_unlock_irqrestore(&i8253_lock, flags);
83
84 count = (PIT_LATCH - 1) - count;
85
86 return (cycle_t)(jifs * PIT_LATCH) + count;
87}
88
89static struct clocksource i8253_cs = {
90 .name = "pit",
91 .rating = 110,
92 .read = i8253_read,
93 .mask = CLOCKSOURCE_MASK(32),
94};
95
96int __init clocksource_i8253_init(void)
97{
98 return clocksource_register_hz(&i8253_cs, PIT_TICK_RATE);
99}
100#endif
101
102#ifdef CONFIG_CLKEVT_I8253
103
104
105
106
107
108static void init_pit_timer(enum clock_event_mode mode,
109 struct clock_event_device *evt)
110{
111 raw_spin_lock(&i8253_lock);
112
113 switch (mode) {
114 case CLOCK_EVT_MODE_PERIODIC:
115
116 outb_p(0x34, PIT_MODE);
117 outb_p(PIT_LATCH & 0xff , PIT_CH0);
118 outb_p(PIT_LATCH >> 8 , PIT_CH0);
119 break;
120
121 case CLOCK_EVT_MODE_SHUTDOWN:
122 case CLOCK_EVT_MODE_UNUSED:
123 if (evt->mode == CLOCK_EVT_MODE_PERIODIC ||
124 evt->mode == CLOCK_EVT_MODE_ONESHOT) {
125 outb_p(0x30, PIT_MODE);
126 outb_p(0, PIT_CH0);
127 outb_p(0, PIT_CH0);
128 }
129 break;
130
131 case CLOCK_EVT_MODE_ONESHOT:
132
133 outb_p(0x38, PIT_MODE);
134 break;
135
136 case CLOCK_EVT_MODE_RESUME:
137
138 break;
139 }
140 raw_spin_unlock(&i8253_lock);
141}
142
143
144
145
146
147
148static int pit_next_event(unsigned long delta, struct clock_event_device *evt)
149{
150 raw_spin_lock(&i8253_lock);
151 outb_p(delta & 0xff , PIT_CH0);
152 outb_p(delta >> 8 , PIT_CH0);
153 raw_spin_unlock(&i8253_lock);
154
155 return 0;
156}
157
158
159
160
161
162struct clock_event_device i8253_clockevent = {
163 .name = "pit",
164 .features = CLOCK_EVT_FEAT_PERIODIC,
165 .set_mode = init_pit_timer,
166 .set_next_event = pit_next_event,
167};
168
169
170
171
172
173void __init clockevent_i8253_init(bool oneshot)
174{
175 if (oneshot)
176 i8253_clockevent.features |= CLOCK_EVT_FEAT_ONESHOT;
177
178
179
180
181 i8253_clockevent.cpumask = cpumask_of(smp_processor_id());
182
183 clockevents_config_and_register(&i8253_clockevent, PIT_TICK_RATE,
184 0xF, 0x7FFF);
185}
186#endif
187