Commit | Line | Data |
---|---|---|
5d0cf410 | 1 | #include <linux/clocksource.h> |
e9e2cdb4 | 2 | #include <linux/clockchips.h> |
28769149 | 3 | #include <linux/delay.h> |
5d0cf410 | 4 | #include <linux/errno.h> |
5 | #include <linux/hpet.h> | |
6 | #include <linux/init.h> | |
399afa4f ML |
7 | #include <linux/sysdev.h> |
8 | #include <linux/pm.h> | |
5d0cf410 | 9 | |
28769149 | 10 | #include <asm/fixmap.h> |
5d0cf410 | 11 | #include <asm/hpet.h> |
06a24dec | 12 | #include <asm/i8253.h> |
5d0cf410 | 13 | #include <asm/io.h> |
14 | ||
7f9f303a | 15 | #define HPET_MASK CLOCKSOURCE_MASK(32) |
5d0cf410 | 16 | #define HPET_SHIFT 22 |
17 | ||
b10db7f0 PM |
18 | /* FSEC = 10^-15 |
19 | NSEC = 10^-9 */ | |
6fd592da | 20 | #define FSEC_PER_NSEC 1000000L |
5d0cf410 | 21 | |
e9e2cdb4 TG |
22 | /* |
23 | * HPET address is set in acpi/boot.c, when an ACPI entry exists | |
24 | */ | |
25 | unsigned long hpet_address; | |
06a24dec | 26 | static void __iomem *hpet_virt_address; |
e9e2cdb4 | 27 | |
31c435d7 | 28 | unsigned long hpet_readl(unsigned long a) |
e9e2cdb4 TG |
29 | { |
30 | return readl(hpet_virt_address + a); | |
31 | } | |
32 | ||
33 | static inline void hpet_writel(unsigned long d, unsigned long a) | |
34 | { | |
35 | writel(d, hpet_virt_address + a); | |
36 | } | |
37 | ||
28769149 | 38 | #ifdef CONFIG_X86_64 |
28769149 | 39 | #include <asm/pgtable.h> |
2387ce57 | 40 | #endif |
28769149 | 41 | |
06a24dec TG |
42 | static inline void hpet_set_mapping(void) |
43 | { | |
44 | hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE); | |
2387ce57 YL |
45 | #ifdef CONFIG_X86_64 |
46 | __set_fixmap(VSYSCALL_HPET, hpet_address, PAGE_KERNEL_VSYSCALL_NOCACHE); | |
47 | #endif | |
06a24dec TG |
48 | } |
49 | ||
50 | static inline void hpet_clear_mapping(void) | |
51 | { | |
52 | iounmap(hpet_virt_address); | |
53 | hpet_virt_address = NULL; | |
54 | } | |
55 | ||
e9e2cdb4 TG |
56 | /* |
57 | * HPET command line enable / disable | |
58 | */ | |
59 | static int boot_hpet_disable; | |
b17530bd | 60 | int hpet_force_user; |
e9e2cdb4 TG |
61 | |
62 | static int __init hpet_setup(char* str) | |
63 | { | |
64 | if (str) { | |
65 | if (!strncmp("disable", str, 7)) | |
66 | boot_hpet_disable = 1; | |
b17530bd TG |
67 | if (!strncmp("force", str, 5)) |
68 | hpet_force_user = 1; | |
e9e2cdb4 TG |
69 | } |
70 | return 1; | |
71 | } | |
72 | __setup("hpet=", hpet_setup); | |
73 | ||
28769149 TG |
74 | static int __init disable_hpet(char *str) |
75 | { | |
76 | boot_hpet_disable = 1; | |
77 | return 1; | |
78 | } | |
79 | __setup("nohpet", disable_hpet); | |
80 | ||
e9e2cdb4 TG |
81 | static inline int is_hpet_capable(void) |
82 | { | |
83 | return (!boot_hpet_disable && hpet_address); | |
84 | } | |
85 | ||
86 | /* | |
87 | * HPET timer interrupt enable / disable | |
88 | */ | |
89 | static int hpet_legacy_int_enabled; | |
90 | ||
91 | /** | |
92 | * is_hpet_enabled - check whether the hpet timer interrupt is enabled | |
93 | */ | |
94 | int is_hpet_enabled(void) | |
95 | { | |
96 | return is_hpet_capable() && hpet_legacy_int_enabled; | |
97 | } | |
1bdbdaac | 98 | EXPORT_SYMBOL_GPL(is_hpet_enabled); |
e9e2cdb4 TG |
99 | |
100 | /* | |
101 | * When the hpet driver (/dev/hpet) is enabled, we need to reserve | |
102 | * timer 0 and timer 1 in case of RTC emulation. | |
103 | */ | |
104 | #ifdef CONFIG_HPET | |
105 | static void hpet_reserve_platform_timers(unsigned long id) | |
106 | { | |
107 | struct hpet __iomem *hpet = hpet_virt_address; | |
37a47db8 BR |
108 | struct hpet_timer __iomem *timer = &hpet->hpet_timers[2]; |
109 | unsigned int nrtimers, i; | |
e9e2cdb4 TG |
110 | struct hpet_data hd; |
111 | ||
112 | nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1; | |
113 | ||
114 | memset(&hd, 0, sizeof (hd)); | |
115 | hd.hd_phys_address = hpet_address; | |
06a24dec | 116 | hd.hd_address = hpet; |
e9e2cdb4 | 117 | hd.hd_nirqs = nrtimers; |
e9e2cdb4 TG |
118 | hpet_reserve_timer(&hd, 0); |
119 | ||
120 | #ifdef CONFIG_HPET_EMULATE_RTC | |
121 | hpet_reserve_timer(&hd, 1); | |
122 | #endif | |
5761d64b | 123 | |
64a76f66 DB |
124 | /* |
125 | * NOTE that hd_irq[] reflects IOAPIC input pins (LEGACY_8254 | |
126 | * is wrong for i8259!) not the output IRQ. Many BIOS writers | |
127 | * don't bother configuring *any* comparator interrupts. | |
128 | */ | |
e9e2cdb4 TG |
129 | hd.hd_irq[0] = HPET_LEGACY_8254; |
130 | hd.hd_irq[1] = HPET_LEGACY_RTC; | |
131 | ||
fc3fbc45 IM |
132 | for (i = 2; i < nrtimers; timer++, i++) { |
133 | hd.hd_irq[i] = (readl(&timer->hpet_config) & Tn_INT_ROUTE_CNF_MASK) >> | |
5761d64b | 134 | Tn_INT_ROUTE_CNF_SHIFT; |
fc3fbc45 | 135 | } |
5761d64b | 136 | |
e9e2cdb4 | 137 | hpet_alloc(&hd); |
5761d64b | 138 | |
e9e2cdb4 TG |
139 | } |
140 | #else | |
141 | static void hpet_reserve_platform_timers(unsigned long id) { } | |
142 | #endif | |
143 | ||
144 | /* | |
145 | * Common hpet info | |
146 | */ | |
147 | static unsigned long hpet_period; | |
148 | ||
610bf2f1 | 149 | static void hpet_legacy_set_mode(enum clock_event_mode mode, |
e9e2cdb4 | 150 | struct clock_event_device *evt); |
610bf2f1 | 151 | static int hpet_legacy_next_event(unsigned long delta, |
e9e2cdb4 TG |
152 | struct clock_event_device *evt); |
153 | ||
154 | /* | |
155 | * The hpet clock event device | |
156 | */ | |
157 | static struct clock_event_device hpet_clockevent = { | |
158 | .name = "hpet", | |
159 | .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT, | |
610bf2f1 VP |
160 | .set_mode = hpet_legacy_set_mode, |
161 | .set_next_event = hpet_legacy_next_event, | |
e9e2cdb4 TG |
162 | .shift = 32, |
163 | .irq = 0, | |
59c69f2a | 164 | .rating = 50, |
e9e2cdb4 TG |
165 | }; |
166 | ||
167 | static void hpet_start_counter(void) | |
168 | { | |
169 | unsigned long cfg = hpet_readl(HPET_CFG); | |
170 | ||
171 | cfg &= ~HPET_CFG_ENABLE; | |
172 | hpet_writel(cfg, HPET_CFG); | |
173 | hpet_writel(0, HPET_COUNTER); | |
174 | hpet_writel(0, HPET_COUNTER + 4); | |
175 | cfg |= HPET_CFG_ENABLE; | |
176 | hpet_writel(cfg, HPET_CFG); | |
177 | } | |
178 | ||
59c69f2a VP |
179 | static void hpet_resume_device(void) |
180 | { | |
bfe0c1cc | 181 | force_hpet_resume(); |
59c69f2a VP |
182 | } |
183 | ||
184 | static void hpet_restart_counter(void) | |
185 | { | |
186 | hpet_resume_device(); | |
187 | hpet_start_counter(); | |
188 | } | |
189 | ||
610bf2f1 | 190 | static void hpet_enable_legacy_int(void) |
e9e2cdb4 TG |
191 | { |
192 | unsigned long cfg = hpet_readl(HPET_CFG); | |
193 | ||
194 | cfg |= HPET_CFG_LEGACY; | |
195 | hpet_writel(cfg, HPET_CFG); | |
196 | hpet_legacy_int_enabled = 1; | |
197 | } | |
198 | ||
610bf2f1 VP |
199 | static void hpet_legacy_clockevent_register(void) |
200 | { | |
610bf2f1 VP |
201 | /* Start HPET legacy interrupts */ |
202 | hpet_enable_legacy_int(); | |
203 | ||
204 | /* | |
6fd592da CM |
205 | * The mult factor is defined as (include/linux/clockchips.h) |
206 | * mult/2^shift = cyc/ns (in contrast to ns/cyc in clocksource.h) | |
207 | * hpet_period is in units of femtoseconds (per cycle), so | |
208 | * mult/2^shift = cyc/ns = 10^6/hpet_period | |
209 | * mult = (10^6 * 2^shift)/hpet_period | |
210 | * mult = (FSEC_PER_NSEC << hpet_clockevent.shift)/hpet_period | |
610bf2f1 | 211 | */ |
6fd592da CM |
212 | hpet_clockevent.mult = div_sc((unsigned long) FSEC_PER_NSEC, |
213 | hpet_period, hpet_clockevent.shift); | |
610bf2f1 VP |
214 | /* Calculate the min / max delta */ |
215 | hpet_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF, | |
216 | &hpet_clockevent); | |
7cfb0435 TG |
217 | /* 5 usec minimum reprogramming delta. */ |
218 | hpet_clockevent.min_delta_ns = 5000; | |
610bf2f1 VP |
219 | |
220 | /* | |
221 | * Start hpet with the boot cpu mask and make it | |
222 | * global after the IO_APIC has been initialized. | |
223 | */ | |
224 | hpet_clockevent.cpumask = cpumask_of_cpu(smp_processor_id()); | |
225 | clockevents_register_device(&hpet_clockevent); | |
226 | global_clock_event = &hpet_clockevent; | |
227 | printk(KERN_DEBUG "hpet clockevent registered\n"); | |
228 | } | |
229 | ||
b40d575b | 230 | static void hpet_set_mode(enum clock_event_mode mode, |
231 | struct clock_event_device *evt, int timer) | |
e9e2cdb4 TG |
232 | { |
233 | unsigned long cfg, cmp, now; | |
234 | uint64_t delta; | |
235 | ||
236 | switch(mode) { | |
237 | case CLOCK_EVT_MODE_PERIODIC: | |
b40d575b | 238 | delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * evt->mult; |
239 | delta >>= evt->shift; | |
e9e2cdb4 TG |
240 | now = hpet_readl(HPET_COUNTER); |
241 | cmp = now + (unsigned long) delta; | |
b40d575b | 242 | cfg = hpet_readl(HPET_Tn_CFG(timer)); |
e9e2cdb4 TG |
243 | cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC | |
244 | HPET_TN_SETVAL | HPET_TN_32BIT; | |
b40d575b | 245 | hpet_writel(cfg, HPET_Tn_CFG(timer)); |
e9e2cdb4 TG |
246 | /* |
247 | * The first write after writing TN_SETVAL to the | |
248 | * config register sets the counter value, the second | |
249 | * write sets the period. | |
250 | */ | |
b40d575b | 251 | hpet_writel(cmp, HPET_Tn_CMP(timer)); |
e9e2cdb4 | 252 | udelay(1); |
b40d575b | 253 | hpet_writel((unsigned long) delta, HPET_Tn_CMP(timer)); |
e9e2cdb4 TG |
254 | break; |
255 | ||
256 | case CLOCK_EVT_MODE_ONESHOT: | |
b40d575b | 257 | cfg = hpet_readl(HPET_Tn_CFG(timer)); |
e9e2cdb4 TG |
258 | cfg &= ~HPET_TN_PERIODIC; |
259 | cfg |= HPET_TN_ENABLE | HPET_TN_32BIT; | |
b40d575b | 260 | hpet_writel(cfg, HPET_Tn_CFG(timer)); |
e9e2cdb4 TG |
261 | break; |
262 | ||
263 | case CLOCK_EVT_MODE_UNUSED: | |
264 | case CLOCK_EVT_MODE_SHUTDOWN: | |
b40d575b | 265 | cfg = hpet_readl(HPET_Tn_CFG(timer)); |
e9e2cdb4 | 266 | cfg &= ~HPET_TN_ENABLE; |
b40d575b | 267 | hpet_writel(cfg, HPET_Tn_CFG(timer)); |
e9e2cdb4 | 268 | break; |
18de5bc4 TG |
269 | |
270 | case CLOCK_EVT_MODE_RESUME: | |
610bf2f1 | 271 | hpet_enable_legacy_int(); |
18de5bc4 | 272 | break; |
e9e2cdb4 TG |
273 | } |
274 | } | |
275 | ||
b40d575b | 276 | static int hpet_next_event(unsigned long delta, |
277 | struct clock_event_device *evt, int timer) | |
e9e2cdb4 | 278 | { |
f7676254 | 279 | u32 cnt; |
e9e2cdb4 TG |
280 | |
281 | cnt = hpet_readl(HPET_COUNTER); | |
f7676254 | 282 | cnt += (u32) delta; |
b40d575b | 283 | hpet_writel(cnt, HPET_Tn_CMP(timer)); |
e9e2cdb4 | 284 | |
72d43d9b TG |
285 | /* |
286 | * We need to read back the CMP register to make sure that | |
287 | * what we wrote hit the chip before we compare it to the | |
288 | * counter. | |
289 | */ | |
290 | WARN_ON((u32)hpet_readl(HPET_T0_CMP) != cnt); | |
291 | ||
f7676254 | 292 | return (s32)((u32)hpet_readl(HPET_COUNTER) - cnt) >= 0 ? -ETIME : 0; |
e9e2cdb4 TG |
293 | } |
294 | ||
b40d575b | 295 | static void hpet_legacy_set_mode(enum clock_event_mode mode, |
296 | struct clock_event_device *evt) | |
297 | { | |
298 | hpet_set_mode(mode, evt, 0); | |
299 | } | |
300 | ||
301 | static int hpet_legacy_next_event(unsigned long delta, | |
302 | struct clock_event_device *evt) | |
303 | { | |
304 | return hpet_next_event(delta, evt, 0); | |
305 | } | |
306 | ||
6bb74df4 | 307 | /* |
308 | * Clock source related code | |
309 | */ | |
310 | static cycle_t read_hpet(void) | |
311 | { | |
312 | return (cycle_t)hpet_readl(HPET_COUNTER); | |
313 | } | |
314 | ||
28769149 TG |
315 | #ifdef CONFIG_X86_64 |
316 | static cycle_t __vsyscall_fn vread_hpet(void) | |
317 | { | |
318 | return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0); | |
319 | } | |
320 | #endif | |
321 | ||
6bb74df4 | 322 | static struct clocksource clocksource_hpet = { |
323 | .name = "hpet", | |
324 | .rating = 250, | |
325 | .read = read_hpet, | |
326 | .mask = HPET_MASK, | |
327 | .shift = HPET_SHIFT, | |
328 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, | |
59c69f2a | 329 | .resume = hpet_restart_counter, |
28769149 TG |
330 | #ifdef CONFIG_X86_64 |
331 | .vread = vread_hpet, | |
332 | #endif | |
6bb74df4 | 333 | }; |
334 | ||
610bf2f1 | 335 | static int hpet_clocksource_register(void) |
e9e2cdb4 | 336 | { |
6fd592da | 337 | u64 start, now; |
075bcd1f | 338 | cycle_t t1; |
e9e2cdb4 | 339 | |
e9e2cdb4 TG |
340 | /* Start the counter */ |
341 | hpet_start_counter(); | |
342 | ||
075bcd1f TG |
343 | /* Verify whether hpet counter works */ |
344 | t1 = read_hpet(); | |
345 | rdtscll(start); | |
346 | ||
347 | /* | |
348 | * We don't know the TSC frequency yet, but waiting for | |
349 | * 200000 TSC cycles is safe: | |
350 | * 4 GHz == 50us | |
351 | * 1 GHz == 200us | |
352 | */ | |
353 | do { | |
354 | rep_nop(); | |
355 | rdtscll(now); | |
356 | } while ((now - start) < 200000UL); | |
357 | ||
358 | if (t1 == read_hpet()) { | |
359 | printk(KERN_WARNING | |
360 | "HPET counter not counting. HPET disabled\n"); | |
610bf2f1 | 361 | return -ENODEV; |
075bcd1f TG |
362 | } |
363 | ||
6fd592da CM |
364 | /* |
365 | * The definition of mult is (include/linux/clocksource.h) | |
366 | * mult/2^shift = ns/cyc and hpet_period is in units of fsec/cyc | |
367 | * so we first need to convert hpet_period to ns/cyc units: | |
368 | * mult/2^shift = ns/cyc = hpet_period/10^6 | |
369 | * mult = (hpet_period * 2^shift)/10^6 | |
370 | * mult = (hpet_period << shift)/FSEC_PER_NSEC | |
6bb74df4 | 371 | */ |
6fd592da | 372 | clocksource_hpet.mult = div_sc(hpet_period, FSEC_PER_NSEC, HPET_SHIFT); |
6bb74df4 | 373 | |
374 | clocksource_register(&clocksource_hpet); | |
375 | ||
610bf2f1 VP |
376 | return 0; |
377 | } | |
378 | ||
b02a7f22 PM |
379 | /** |
380 | * hpet_enable - Try to setup the HPET timer. Returns 1 on success. | |
610bf2f1 VP |
381 | */ |
382 | int __init hpet_enable(void) | |
383 | { | |
384 | unsigned long id; | |
a6825f1c | 385 | int i; |
610bf2f1 VP |
386 | |
387 | if (!is_hpet_capable()) | |
388 | return 0; | |
389 | ||
390 | hpet_set_mapping(); | |
391 | ||
392 | /* | |
393 | * Read the period and check for a sane value: | |
394 | */ | |
395 | hpet_period = hpet_readl(HPET_PERIOD); | |
a6825f1c TG |
396 | |
397 | /* | |
398 | * AMD SB700 based systems with spread spectrum enabled use a | |
399 | * SMM based HPET emulation to provide proper frequency | |
400 | * setting. The SMM code is initialized with the first HPET | |
401 | * register access and takes some time to complete. During | |
402 | * this time the config register reads 0xffffffff. We check | |
403 | * for max. 1000 loops whether the config register reads a non | |
404 | * 0xffffffff value to make sure that HPET is up and running | |
405 | * before we go further. A counting loop is safe, as the HPET | |
406 | * access takes thousands of CPU cycles. On non SB700 based | |
407 | * machines this check is only done once and has no side | |
408 | * effects. | |
409 | */ | |
410 | for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) { | |
411 | if (i == 1000) { | |
412 | printk(KERN_WARNING | |
413 | "HPET config register value = 0xFFFFFFFF. " | |
414 | "Disabling HPET\n"); | |
415 | goto out_nohpet; | |
416 | } | |
417 | } | |
418 | ||
610bf2f1 VP |
419 | if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD) |
420 | goto out_nohpet; | |
421 | ||
422 | /* | |
423 | * Read the HPET ID register to retrieve the IRQ routing | |
424 | * information and the number of channels | |
425 | */ | |
426 | id = hpet_readl(HPET_ID); | |
427 | ||
428 | #ifdef CONFIG_HPET_EMULATE_RTC | |
429 | /* | |
430 | * The legacy routing mode needs at least two channels, tick timer | |
431 | * and the rtc emulation channel. | |
432 | */ | |
433 | if (!(id & HPET_ID_NUMBER)) | |
434 | goto out_nohpet; | |
435 | #endif | |
436 | ||
437 | if (hpet_clocksource_register()) | |
438 | goto out_nohpet; | |
439 | ||
e9e2cdb4 | 440 | if (id & HPET_ID_LEGSUP) { |
610bf2f1 | 441 | hpet_legacy_clockevent_register(); |
e9e2cdb4 TG |
442 | return 1; |
443 | } | |
444 | return 0; | |
5d0cf410 | 445 | |
e9e2cdb4 | 446 | out_nohpet: |
06a24dec | 447 | hpet_clear_mapping(); |
399afa4f | 448 | boot_hpet_disable = 1; |
e9e2cdb4 TG |
449 | return 0; |
450 | } | |
451 | ||
28769149 TG |
452 | /* |
453 | * Needs to be late, as the reserve_timer code calls kalloc ! | |
454 | * | |
455 | * Not a problem on i386 as hpet_enable is called from late_time_init, | |
456 | * but on x86_64 it is necessary ! | |
457 | */ | |
458 | static __init int hpet_late_init(void) | |
459 | { | |
59c69f2a | 460 | if (boot_hpet_disable) |
28769149 TG |
461 | return -ENODEV; |
462 | ||
59c69f2a VP |
463 | if (!hpet_address) { |
464 | if (!force_hpet_address) | |
465 | return -ENODEV; | |
466 | ||
467 | hpet_address = force_hpet_address; | |
468 | hpet_enable(); | |
469 | if (!hpet_virt_address) | |
470 | return -ENODEV; | |
471 | } | |
472 | ||
28769149 | 473 | hpet_reserve_platform_timers(hpet_readl(HPET_ID)); |
59c69f2a | 474 | |
28769149 TG |
475 | return 0; |
476 | } | |
477 | fs_initcall(hpet_late_init); | |
478 | ||
c86c7fbc OH |
479 | void hpet_disable(void) |
480 | { | |
481 | if (is_hpet_capable()) { | |
482 | unsigned long cfg = hpet_readl(HPET_CFG); | |
483 | ||
484 | if (hpet_legacy_int_enabled) { | |
485 | cfg &= ~HPET_CFG_LEGACY; | |
486 | hpet_legacy_int_enabled = 0; | |
487 | } | |
488 | cfg &= ~HPET_CFG_ENABLE; | |
489 | hpet_writel(cfg, HPET_CFG); | |
490 | } | |
491 | } | |
492 | ||
e9e2cdb4 TG |
493 | #ifdef CONFIG_HPET_EMULATE_RTC |
494 | ||
495 | /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET | |
496 | * is enabled, we support RTC interrupt functionality in software. | |
497 | * RTC has 3 kinds of interrupts: | |
498 | * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock | |
499 | * is updated | |
500 | * 2) Alarm Interrupt - generate an interrupt at a specific time of day | |
501 | * 3) Periodic Interrupt - generate periodic interrupt, with frequencies | |
502 | * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2) | |
503 | * (1) and (2) above are implemented using polling at a frequency of | |
504 | * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt | |
505 | * overhead. (DEFAULT_RTC_INT_FREQ) | |
506 | * For (3), we use interrupts at 64Hz or user specified periodic | |
507 | * frequency, whichever is higher. | |
508 | */ | |
509 | #include <linux/mc146818rtc.h> | |
510 | #include <linux/rtc.h> | |
1bdbdaac | 511 | #include <asm/rtc.h> |
e9e2cdb4 TG |
512 | |
513 | #define DEFAULT_RTC_INT_FREQ 64 | |
514 | #define DEFAULT_RTC_SHIFT 6 | |
515 | #define RTC_NUM_INTS 1 | |
516 | ||
517 | static unsigned long hpet_rtc_flags; | |
7e2a31da | 518 | static int hpet_prev_update_sec; |
e9e2cdb4 TG |
519 | static struct rtc_time hpet_alarm_time; |
520 | static unsigned long hpet_pie_count; | |
521 | static unsigned long hpet_t1_cmp; | |
522 | static unsigned long hpet_default_delta; | |
523 | static unsigned long hpet_pie_delta; | |
524 | static unsigned long hpet_pie_limit; | |
525 | ||
1bdbdaac BW |
526 | static rtc_irq_handler irq_handler; |
527 | ||
528 | /* | |
529 | * Registers a IRQ handler. | |
530 | */ | |
531 | int hpet_register_irq_handler(rtc_irq_handler handler) | |
532 | { | |
533 | if (!is_hpet_enabled()) | |
534 | return -ENODEV; | |
535 | if (irq_handler) | |
536 | return -EBUSY; | |
537 | ||
538 | irq_handler = handler; | |
539 | ||
540 | return 0; | |
541 | } | |
542 | EXPORT_SYMBOL_GPL(hpet_register_irq_handler); | |
543 | ||
544 | /* | |
545 | * Deregisters the IRQ handler registered with hpet_register_irq_handler() | |
546 | * and does cleanup. | |
547 | */ | |
548 | void hpet_unregister_irq_handler(rtc_irq_handler handler) | |
549 | { | |
550 | if (!is_hpet_enabled()) | |
551 | return; | |
552 | ||
553 | irq_handler = NULL; | |
554 | hpet_rtc_flags = 0; | |
555 | } | |
556 | EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler); | |
557 | ||
e9e2cdb4 TG |
558 | /* |
559 | * Timer 1 for RTC emulation. We use one shot mode, as periodic mode | |
560 | * is not supported by all HPET implementations for timer 1. | |
561 | * | |
562 | * hpet_rtc_timer_init() is called when the rtc is initialized. | |
563 | */ | |
564 | int hpet_rtc_timer_init(void) | |
565 | { | |
566 | unsigned long cfg, cnt, delta, flags; | |
567 | ||
568 | if (!is_hpet_enabled()) | |
569 | return 0; | |
570 | ||
571 | if (!hpet_default_delta) { | |
572 | uint64_t clc; | |
573 | ||
574 | clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC; | |
575 | clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT; | |
576 | hpet_default_delta = (unsigned long) clc; | |
577 | } | |
578 | ||
579 | if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit) | |
580 | delta = hpet_default_delta; | |
581 | else | |
582 | delta = hpet_pie_delta; | |
583 | ||
584 | local_irq_save(flags); | |
585 | ||
586 | cnt = delta + hpet_readl(HPET_COUNTER); | |
587 | hpet_writel(cnt, HPET_T1_CMP); | |
588 | hpet_t1_cmp = cnt; | |
589 | ||
590 | cfg = hpet_readl(HPET_T1_CFG); | |
591 | cfg &= ~HPET_TN_PERIODIC; | |
592 | cfg |= HPET_TN_ENABLE | HPET_TN_32BIT; | |
593 | hpet_writel(cfg, HPET_T1_CFG); | |
594 | ||
595 | local_irq_restore(flags); | |
596 | ||
597 | return 1; | |
598 | } | |
1bdbdaac | 599 | EXPORT_SYMBOL_GPL(hpet_rtc_timer_init); |
e9e2cdb4 TG |
600 | |
601 | /* | |
602 | * The functions below are called from rtc driver. | |
603 | * Return 0 if HPET is not being used. | |
604 | * Otherwise do the necessary changes and return 1. | |
605 | */ | |
606 | int hpet_mask_rtc_irq_bit(unsigned long bit_mask) | |
607 | { | |
608 | if (!is_hpet_enabled()) | |
609 | return 0; | |
610 | ||
611 | hpet_rtc_flags &= ~bit_mask; | |
612 | return 1; | |
613 | } | |
1bdbdaac | 614 | EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit); |
e9e2cdb4 TG |
615 | |
616 | int hpet_set_rtc_irq_bit(unsigned long bit_mask) | |
617 | { | |
618 | unsigned long oldbits = hpet_rtc_flags; | |
619 | ||
620 | if (!is_hpet_enabled()) | |
621 | return 0; | |
622 | ||
623 | hpet_rtc_flags |= bit_mask; | |
624 | ||
7e2a31da DB |
625 | if ((bit_mask & RTC_UIE) && !(oldbits & RTC_UIE)) |
626 | hpet_prev_update_sec = -1; | |
627 | ||
e9e2cdb4 TG |
628 | if (!oldbits) |
629 | hpet_rtc_timer_init(); | |
630 | ||
631 | return 1; | |
632 | } | |
1bdbdaac | 633 | EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit); |
e9e2cdb4 TG |
634 | |
635 | int hpet_set_alarm_time(unsigned char hrs, unsigned char min, | |
636 | unsigned char sec) | |
637 | { | |
638 | if (!is_hpet_enabled()) | |
639 | return 0; | |
640 | ||
641 | hpet_alarm_time.tm_hour = hrs; | |
642 | hpet_alarm_time.tm_min = min; | |
643 | hpet_alarm_time.tm_sec = sec; | |
644 | ||
645 | return 1; | |
646 | } | |
1bdbdaac | 647 | EXPORT_SYMBOL_GPL(hpet_set_alarm_time); |
e9e2cdb4 TG |
648 | |
649 | int hpet_set_periodic_freq(unsigned long freq) | |
650 | { | |
651 | uint64_t clc; | |
652 | ||
653 | if (!is_hpet_enabled()) | |
654 | return 0; | |
655 | ||
656 | if (freq <= DEFAULT_RTC_INT_FREQ) | |
657 | hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq; | |
658 | else { | |
659 | clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC; | |
660 | do_div(clc, freq); | |
661 | clc >>= hpet_clockevent.shift; | |
662 | hpet_pie_delta = (unsigned long) clc; | |
663 | } | |
664 | return 1; | |
665 | } | |
1bdbdaac | 666 | EXPORT_SYMBOL_GPL(hpet_set_periodic_freq); |
e9e2cdb4 TG |
667 | |
668 | int hpet_rtc_dropped_irq(void) | |
669 | { | |
670 | return is_hpet_enabled(); | |
671 | } | |
1bdbdaac | 672 | EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq); |
e9e2cdb4 TG |
673 | |
674 | static void hpet_rtc_timer_reinit(void) | |
675 | { | |
676 | unsigned long cfg, delta; | |
677 | int lost_ints = -1; | |
678 | ||
679 | if (unlikely(!hpet_rtc_flags)) { | |
680 | cfg = hpet_readl(HPET_T1_CFG); | |
681 | cfg &= ~HPET_TN_ENABLE; | |
682 | hpet_writel(cfg, HPET_T1_CFG); | |
683 | return; | |
684 | } | |
685 | ||
686 | if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit) | |
687 | delta = hpet_default_delta; | |
688 | else | |
689 | delta = hpet_pie_delta; | |
690 | ||
691 | /* | |
692 | * Increment the comparator value until we are ahead of the | |
693 | * current count. | |
694 | */ | |
695 | do { | |
696 | hpet_t1_cmp += delta; | |
697 | hpet_writel(hpet_t1_cmp, HPET_T1_CMP); | |
698 | lost_ints++; | |
699 | } while ((long)(hpet_readl(HPET_COUNTER) - hpet_t1_cmp) > 0); | |
700 | ||
701 | if (lost_ints) { | |
702 | if (hpet_rtc_flags & RTC_PIE) | |
703 | hpet_pie_count += lost_ints; | |
704 | if (printk_ratelimit()) | |
7e2a31da | 705 | printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n", |
e9e2cdb4 TG |
706 | lost_ints); |
707 | } | |
708 | } | |
709 | ||
710 | irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id) | |
711 | { | |
712 | struct rtc_time curr_time; | |
713 | unsigned long rtc_int_flag = 0; | |
714 | ||
715 | hpet_rtc_timer_reinit(); | |
1bdbdaac | 716 | memset(&curr_time, 0, sizeof(struct rtc_time)); |
e9e2cdb4 TG |
717 | |
718 | if (hpet_rtc_flags & (RTC_UIE | RTC_AIE)) | |
1bdbdaac | 719 | get_rtc_time(&curr_time); |
e9e2cdb4 TG |
720 | |
721 | if (hpet_rtc_flags & RTC_UIE && | |
722 | curr_time.tm_sec != hpet_prev_update_sec) { | |
7e2a31da DB |
723 | if (hpet_prev_update_sec >= 0) |
724 | rtc_int_flag = RTC_UF; | |
e9e2cdb4 TG |
725 | hpet_prev_update_sec = curr_time.tm_sec; |
726 | } | |
727 | ||
728 | if (hpet_rtc_flags & RTC_PIE && | |
729 | ++hpet_pie_count >= hpet_pie_limit) { | |
730 | rtc_int_flag |= RTC_PF; | |
731 | hpet_pie_count = 0; | |
732 | } | |
733 | ||
8ee291f8 | 734 | if (hpet_rtc_flags & RTC_AIE && |
e9e2cdb4 TG |
735 | (curr_time.tm_sec == hpet_alarm_time.tm_sec) && |
736 | (curr_time.tm_min == hpet_alarm_time.tm_min) && | |
737 | (curr_time.tm_hour == hpet_alarm_time.tm_hour)) | |
738 | rtc_int_flag |= RTC_AF; | |
739 | ||
740 | if (rtc_int_flag) { | |
741 | rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8)); | |
1bdbdaac BW |
742 | if (irq_handler) |
743 | irq_handler(rtc_int_flag, dev_id); | |
e9e2cdb4 TG |
744 | } |
745 | return IRQ_HANDLED; | |
746 | } | |
1bdbdaac | 747 | EXPORT_SYMBOL_GPL(hpet_rtc_interrupt); |
e9e2cdb4 | 748 | #endif |