blob: ea230ec69057a67477c0f69ef284a9668893b8a1 [file] [log] [blame]
john stultz5d0cf412006-06-26 00:25:12 -07001#include <linux/clocksource.h>
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08002#include <linux/clockchips.h>
Thomas Gleixner28769142007-10-12 23:04:06 +02003#include <linux/delay.h>
john stultz5d0cf412006-06-26 00:25:12 -07004#include <linux/errno.h>
5#include <linux/hpet.h>
6#include <linux/init.h>
Maxim Levitsky399afa42007-03-29 15:46:48 +02007#include <linux/sysdev.h>
8#include <linux/pm.h>
john stultz5d0cf412006-06-26 00:25:12 -07009
Thomas Gleixner28769142007-10-12 23:04:06 +020010#include <asm/fixmap.h>
john stultz5d0cf412006-06-26 00:25:12 -070011#include <asm/hpet.h>
Thomas Gleixner06a24de2007-10-12 23:04:06 +020012#include <asm/i8253.h>
john stultz5d0cf412006-06-26 00:25:12 -070013#include <asm/io.h>
14
Jim Cromie7f9f3032006-06-26 00:25:15 -070015#define HPET_MASK CLOCKSOURCE_MASK(32)
john stultz5d0cf412006-06-26 00:25:12 -070016#define HPET_SHIFT 22
17
Pavel Machekb10db7f2008-01-30 13:30:00 +010018/* FSEC = 10^-15
19 NSEC = 10^-9 */
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -030020#define FSEC_PER_NSEC 1000000L
john stultz5d0cf412006-06-26 00:25:12 -070021
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080022/*
23 * HPET address is set in acpi/boot.c, when an ACPI entry exists
24 */
25unsigned long hpet_address;
Thomas Gleixner06a24de2007-10-12 23:04:06 +020026static void __iomem *hpet_virt_address;
john stultz5d0cf412006-06-26 00:25:12 -070027
Chris Wright31c435d2007-10-12 23:04:23 +020028unsigned long hpet_readl(unsigned long a)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080029{
30 return readl(hpet_virt_address + a);
31}
32
33static inline void hpet_writel(unsigned long d, unsigned long a)
34{
35 writel(d, hpet_virt_address + a);
36}
37
Thomas Gleixner28769142007-10-12 23:04:06 +020038#ifdef CONFIG_X86_64
39
40#include <asm/pgtable.h>
41
42static inline void hpet_set_mapping(void)
43{
44 set_fixmap_nocache(FIX_HPET_BASE, hpet_address);
45 __set_fixmap(VSYSCALL_HPET, hpet_address, PAGE_KERNEL_VSYSCALL_NOCACHE);
46 hpet_virt_address = (void __iomem *)fix_to_virt(FIX_HPET_BASE);
47}
48
49static inline void hpet_clear_mapping(void)
50{
51 hpet_virt_address = NULL;
52}
53
54#else
55
Thomas Gleixner06a24de2007-10-12 23:04:06 +020056static inline void hpet_set_mapping(void)
57{
58 hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
59}
60
61static inline void hpet_clear_mapping(void)
62{
63 iounmap(hpet_virt_address);
64 hpet_virt_address = NULL;
65}
Thomas Gleixner28769142007-10-12 23:04:06 +020066#endif
Thomas Gleixner06a24de2007-10-12 23:04:06 +020067
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080068/*
69 * HPET command line enable / disable
70 */
71static int boot_hpet_disable;
Thomas Gleixnerb17530b2007-10-19 20:35:02 +020072int hpet_force_user;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080073
74static int __init hpet_setup(char* str)
75{
76 if (str) {
77 if (!strncmp("disable", str, 7))
78 boot_hpet_disable = 1;
Thomas Gleixnerb17530b2007-10-19 20:35:02 +020079 if (!strncmp("force", str, 5))
80 hpet_force_user = 1;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080081 }
82 return 1;
83}
84__setup("hpet=", hpet_setup);
85
Thomas Gleixner28769142007-10-12 23:04:06 +020086static int __init disable_hpet(char *str)
87{
88 boot_hpet_disable = 1;
89 return 1;
90}
91__setup("nohpet", disable_hpet);
92
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080093static inline int is_hpet_capable(void)
94{
95 return (!boot_hpet_disable && hpet_address);
96}
97
98/*
99 * HPET timer interrupt enable / disable
100 */
101static int hpet_legacy_int_enabled;
102
103/**
104 * is_hpet_enabled - check whether the hpet timer interrupt is enabled
105 */
106int is_hpet_enabled(void)
107{
108 return is_hpet_capable() && hpet_legacy_int_enabled;
109}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100110EXPORT_SYMBOL_GPL(is_hpet_enabled);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800111
112/*
113 * When the hpet driver (/dev/hpet) is enabled, we need to reserve
114 * timer 0 and timer 1 in case of RTC emulation.
115 */
116#ifdef CONFIG_HPET
117static void hpet_reserve_platform_timers(unsigned long id)
118{
119 struct hpet __iomem *hpet = hpet_virt_address;
Balaji Rao37a47db82008-01-30 13:30:03 +0100120 struct hpet_timer __iomem *timer = &hpet->hpet_timers[2];
121 unsigned int nrtimers, i;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800122 struct hpet_data hd;
123
124 nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
125
126 memset(&hd, 0, sizeof (hd));
127 hd.hd_phys_address = hpet_address;
Thomas Gleixner06a24de2007-10-12 23:04:06 +0200128 hd.hd_address = hpet;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800129 hd.hd_nirqs = nrtimers;
130 hd.hd_flags = HPET_DATA_PLATFORM;
131 hpet_reserve_timer(&hd, 0);
132
133#ifdef CONFIG_HPET_EMULATE_RTC
134 hpet_reserve_timer(&hd, 1);
135#endif
Thomas Gleixner5761d642008-04-04 16:26:10 +0200136
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800137 hd.hd_irq[0] = HPET_LEGACY_8254;
138 hd.hd_irq[1] = HPET_LEGACY_RTC;
139
Ingo Molnarfc3fbc42008-04-27 14:04:14 +0200140 for (i = 2; i < nrtimers; timer++, i++) {
141 hd.hd_irq[i] = (readl(&timer->hpet_config) & Tn_INT_ROUTE_CNF_MASK) >>
Thomas Gleixner5761d642008-04-04 16:26:10 +0200142 Tn_INT_ROUTE_CNF_SHIFT;
Ingo Molnarfc3fbc42008-04-27 14:04:14 +0200143 }
Thomas Gleixner5761d642008-04-04 16:26:10 +0200144
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800145 hpet_alloc(&hd);
Thomas Gleixner5761d642008-04-04 16:26:10 +0200146
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800147}
148#else
149static void hpet_reserve_platform_timers(unsigned long id) { }
150#endif
151
152/*
153 * Common hpet info
154 */
155static unsigned long hpet_period;
156
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200157static void hpet_legacy_set_mode(enum clock_event_mode mode,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800158 struct clock_event_device *evt);
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200159static int hpet_legacy_next_event(unsigned long delta,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800160 struct clock_event_device *evt);
161
162/*
163 * The hpet clock event device
164 */
165static struct clock_event_device hpet_clockevent = {
166 .name = "hpet",
167 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200168 .set_mode = hpet_legacy_set_mode,
169 .set_next_event = hpet_legacy_next_event,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800170 .shift = 32,
171 .irq = 0,
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200172 .rating = 50,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800173};
174
175static void hpet_start_counter(void)
176{
177 unsigned long cfg = hpet_readl(HPET_CFG);
178
179 cfg &= ~HPET_CFG_ENABLE;
180 hpet_writel(cfg, HPET_CFG);
181 hpet_writel(0, HPET_COUNTER);
182 hpet_writel(0, HPET_COUNTER + 4);
183 cfg |= HPET_CFG_ENABLE;
184 hpet_writel(cfg, HPET_CFG);
185}
186
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200187static void hpet_resume_device(void)
188{
Venki Pallipadibfe0c1c2007-10-12 23:04:24 +0200189 force_hpet_resume();
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200190}
191
192static void hpet_restart_counter(void)
193{
194 hpet_resume_device();
195 hpet_start_counter();
196}
197
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200198static void hpet_enable_legacy_int(void)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800199{
200 unsigned long cfg = hpet_readl(HPET_CFG);
201
202 cfg |= HPET_CFG_LEGACY;
203 hpet_writel(cfg, HPET_CFG);
204 hpet_legacy_int_enabled = 1;
205}
206
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200207static void hpet_legacy_clockevent_register(void)
208{
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200209 /* Start HPET legacy interrupts */
210 hpet_enable_legacy_int();
211
212 /*
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300213 * The mult factor is defined as (include/linux/clockchips.h)
214 * mult/2^shift = cyc/ns (in contrast to ns/cyc in clocksource.h)
215 * hpet_period is in units of femtoseconds (per cycle), so
216 * mult/2^shift = cyc/ns = 10^6/hpet_period
217 * mult = (10^6 * 2^shift)/hpet_period
218 * mult = (FSEC_PER_NSEC << hpet_clockevent.shift)/hpet_period
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200219 */
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300220 hpet_clockevent.mult = div_sc((unsigned long) FSEC_PER_NSEC,
221 hpet_period, hpet_clockevent.shift);
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200222 /* Calculate the min / max delta */
223 hpet_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF,
224 &hpet_clockevent);
225 hpet_clockevent.min_delta_ns = clockevent_delta2ns(0x30,
226 &hpet_clockevent);
227
228 /*
229 * Start hpet with the boot cpu mask and make it
230 * global after the IO_APIC has been initialized.
231 */
232 hpet_clockevent.cpumask = cpumask_of_cpu(smp_processor_id());
233 clockevents_register_device(&hpet_clockevent);
234 global_clock_event = &hpet_clockevent;
235 printk(KERN_DEBUG "hpet clockevent registered\n");
236}
237
238static void hpet_legacy_set_mode(enum clock_event_mode mode,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800239 struct clock_event_device *evt)
240{
241 unsigned long cfg, cmp, now;
242 uint64_t delta;
243
244 switch(mode) {
245 case CLOCK_EVT_MODE_PERIODIC:
246 delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * hpet_clockevent.mult;
247 delta >>= hpet_clockevent.shift;
248 now = hpet_readl(HPET_COUNTER);
249 cmp = now + (unsigned long) delta;
250 cfg = hpet_readl(HPET_T0_CFG);
251 cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
252 HPET_TN_SETVAL | HPET_TN_32BIT;
253 hpet_writel(cfg, HPET_T0_CFG);
254 /*
255 * The first write after writing TN_SETVAL to the
256 * config register sets the counter value, the second
257 * write sets the period.
258 */
259 hpet_writel(cmp, HPET_T0_CMP);
260 udelay(1);
261 hpet_writel((unsigned long) delta, HPET_T0_CMP);
262 break;
263
264 case CLOCK_EVT_MODE_ONESHOT:
265 cfg = hpet_readl(HPET_T0_CFG);
266 cfg &= ~HPET_TN_PERIODIC;
267 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
268 hpet_writel(cfg, HPET_T0_CFG);
269 break;
270
271 case CLOCK_EVT_MODE_UNUSED:
272 case CLOCK_EVT_MODE_SHUTDOWN:
273 cfg = hpet_readl(HPET_T0_CFG);
274 cfg &= ~HPET_TN_ENABLE;
275 hpet_writel(cfg, HPET_T0_CFG);
276 break;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700277
278 case CLOCK_EVT_MODE_RESUME:
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200279 hpet_enable_legacy_int();
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700280 break;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800281 }
282}
283
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200284static int hpet_legacy_next_event(unsigned long delta,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800285 struct clock_event_device *evt)
286{
287 unsigned long cnt;
288
289 cnt = hpet_readl(HPET_COUNTER);
290 cnt += delta;
291 hpet_writel(cnt, HPET_T0_CMP);
292
Thomas Gleixnerc7f6d152007-03-27 09:08:26 +0200293 return ((long)(hpet_readl(HPET_COUNTER) - cnt ) > 0) ? -ETIME : 0;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800294}
295
296/*
john stultz6bb74df2007-03-05 00:30:50 -0800297 * Clock source related code
298 */
299static cycle_t read_hpet(void)
300{
301 return (cycle_t)hpet_readl(HPET_COUNTER);
302}
303
Thomas Gleixner28769142007-10-12 23:04:06 +0200304#ifdef CONFIG_X86_64
305static cycle_t __vsyscall_fn vread_hpet(void)
306{
307 return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
308}
309#endif
310
john stultz6bb74df2007-03-05 00:30:50 -0800311static struct clocksource clocksource_hpet = {
312 .name = "hpet",
313 .rating = 250,
314 .read = read_hpet,
315 .mask = HPET_MASK,
316 .shift = HPET_SHIFT,
317 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200318 .resume = hpet_restart_counter,
Thomas Gleixner28769142007-10-12 23:04:06 +0200319#ifdef CONFIG_X86_64
320 .vread = vread_hpet,
321#endif
john stultz6bb74df2007-03-05 00:30:50 -0800322};
323
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200324static int hpet_clocksource_register(void)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800325{
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300326 u64 start, now;
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200327 cycle_t t1;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800328
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800329 /* Start the counter */
330 hpet_start_counter();
331
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200332 /* Verify whether hpet counter works */
333 t1 = read_hpet();
334 rdtscll(start);
335
336 /*
337 * We don't know the TSC frequency yet, but waiting for
338 * 200000 TSC cycles is safe:
339 * 4 GHz == 50us
340 * 1 GHz == 200us
341 */
342 do {
343 rep_nop();
344 rdtscll(now);
345 } while ((now - start) < 200000UL);
346
347 if (t1 == read_hpet()) {
348 printk(KERN_WARNING
349 "HPET counter not counting. HPET disabled\n");
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200350 return -ENODEV;
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200351 }
352
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300353 /*
354 * The definition of mult is (include/linux/clocksource.h)
355 * mult/2^shift = ns/cyc and hpet_period is in units of fsec/cyc
356 * so we first need to convert hpet_period to ns/cyc units:
357 * mult/2^shift = ns/cyc = hpet_period/10^6
358 * mult = (hpet_period * 2^shift)/10^6
359 * mult = (hpet_period << shift)/FSEC_PER_NSEC
john stultz6bb74df2007-03-05 00:30:50 -0800360 */
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300361 clocksource_hpet.mult = div_sc(hpet_period, FSEC_PER_NSEC, HPET_SHIFT);
john stultz6bb74df2007-03-05 00:30:50 -0800362
363 clocksource_register(&clocksource_hpet);
364
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200365 return 0;
366}
367
Pavel Machekb02a7f22008-02-05 00:48:13 +0100368/**
369 * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200370 */
371int __init hpet_enable(void)
372{
373 unsigned long id;
374
375 if (!is_hpet_capable())
376 return 0;
377
378 hpet_set_mapping();
379
380 /*
381 * Read the period and check for a sane value:
382 */
383 hpet_period = hpet_readl(HPET_PERIOD);
384 if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
385 goto out_nohpet;
386
387 /*
388 * Read the HPET ID register to retrieve the IRQ routing
389 * information and the number of channels
390 */
391 id = hpet_readl(HPET_ID);
392
393#ifdef CONFIG_HPET_EMULATE_RTC
394 /*
395 * The legacy routing mode needs at least two channels, tick timer
396 * and the rtc emulation channel.
397 */
398 if (!(id & HPET_ID_NUMBER))
399 goto out_nohpet;
400#endif
401
402 if (hpet_clocksource_register())
403 goto out_nohpet;
404
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800405 if (id & HPET_ID_LEGSUP) {
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200406 hpet_legacy_clockevent_register();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800407 return 1;
408 }
409 return 0;
410
411out_nohpet:
Thomas Gleixner06a24de2007-10-12 23:04:06 +0200412 hpet_clear_mapping();
Maxim Levitsky399afa42007-03-29 15:46:48 +0200413 boot_hpet_disable = 1;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800414 return 0;
415}
416
Thomas Gleixner28769142007-10-12 23:04:06 +0200417/*
418 * Needs to be late, as the reserve_timer code calls kalloc !
419 *
420 * Not a problem on i386 as hpet_enable is called from late_time_init,
421 * but on x86_64 it is necessary !
422 */
423static __init int hpet_late_init(void)
424{
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200425 if (boot_hpet_disable)
Thomas Gleixner28769142007-10-12 23:04:06 +0200426 return -ENODEV;
427
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200428 if (!hpet_address) {
429 if (!force_hpet_address)
430 return -ENODEV;
431
432 hpet_address = force_hpet_address;
433 hpet_enable();
434 if (!hpet_virt_address)
435 return -ENODEV;
436 }
437
Thomas Gleixner28769142007-10-12 23:04:06 +0200438 hpet_reserve_platform_timers(hpet_readl(HPET_ID));
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200439
Thomas Gleixner28769142007-10-12 23:04:06 +0200440 return 0;
441}
442fs_initcall(hpet_late_init);
443
OGAWA Hirofumic86c7fb2007-12-03 17:17:10 +0100444void hpet_disable(void)
445{
446 if (is_hpet_capable()) {
447 unsigned long cfg = hpet_readl(HPET_CFG);
448
449 if (hpet_legacy_int_enabled) {
450 cfg &= ~HPET_CFG_LEGACY;
451 hpet_legacy_int_enabled = 0;
452 }
453 cfg &= ~HPET_CFG_ENABLE;
454 hpet_writel(cfg, HPET_CFG);
455 }
456}
457
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800458#ifdef CONFIG_HPET_EMULATE_RTC
459
460/* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
461 * is enabled, we support RTC interrupt functionality in software.
462 * RTC has 3 kinds of interrupts:
463 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
464 * is updated
465 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
466 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
467 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
468 * (1) and (2) above are implemented using polling at a frequency of
469 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
470 * overhead. (DEFAULT_RTC_INT_FREQ)
471 * For (3), we use interrupts at 64Hz or user specified periodic
472 * frequency, whichever is higher.
473 */
474#include <linux/mc146818rtc.h>
475#include <linux/rtc.h>
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100476#include <asm/rtc.h>
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800477
478#define DEFAULT_RTC_INT_FREQ 64
479#define DEFAULT_RTC_SHIFT 6
480#define RTC_NUM_INTS 1
481
482static unsigned long hpet_rtc_flags;
483static unsigned long hpet_prev_update_sec;
484static struct rtc_time hpet_alarm_time;
485static unsigned long hpet_pie_count;
486static unsigned long hpet_t1_cmp;
487static unsigned long hpet_default_delta;
488static unsigned long hpet_pie_delta;
489static unsigned long hpet_pie_limit;
490
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100491static rtc_irq_handler irq_handler;
492
493/*
494 * Registers a IRQ handler.
495 */
496int hpet_register_irq_handler(rtc_irq_handler handler)
497{
498 if (!is_hpet_enabled())
499 return -ENODEV;
500 if (irq_handler)
501 return -EBUSY;
502
503 irq_handler = handler;
504
505 return 0;
506}
507EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
508
509/*
510 * Deregisters the IRQ handler registered with hpet_register_irq_handler()
511 * and does cleanup.
512 */
513void hpet_unregister_irq_handler(rtc_irq_handler handler)
514{
515 if (!is_hpet_enabled())
516 return;
517
518 irq_handler = NULL;
519 hpet_rtc_flags = 0;
520}
521EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
522
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800523/*
524 * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
525 * is not supported by all HPET implementations for timer 1.
526 *
527 * hpet_rtc_timer_init() is called when the rtc is initialized.
528 */
529int hpet_rtc_timer_init(void)
530{
531 unsigned long cfg, cnt, delta, flags;
532
533 if (!is_hpet_enabled())
534 return 0;
535
536 if (!hpet_default_delta) {
537 uint64_t clc;
538
539 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
540 clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
541 hpet_default_delta = (unsigned long) clc;
542 }
543
544 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
545 delta = hpet_default_delta;
546 else
547 delta = hpet_pie_delta;
548
549 local_irq_save(flags);
550
551 cnt = delta + hpet_readl(HPET_COUNTER);
552 hpet_writel(cnt, HPET_T1_CMP);
553 hpet_t1_cmp = cnt;
554
555 cfg = hpet_readl(HPET_T1_CFG);
556 cfg &= ~HPET_TN_PERIODIC;
557 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
558 hpet_writel(cfg, HPET_T1_CFG);
559
560 local_irq_restore(flags);
561
562 return 1;
563}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100564EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800565
566/*
567 * The functions below are called from rtc driver.
568 * Return 0 if HPET is not being used.
569 * Otherwise do the necessary changes and return 1.
570 */
571int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
572{
573 if (!is_hpet_enabled())
574 return 0;
575
576 hpet_rtc_flags &= ~bit_mask;
577 return 1;
578}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100579EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800580
581int hpet_set_rtc_irq_bit(unsigned long bit_mask)
582{
583 unsigned long oldbits = hpet_rtc_flags;
584
585 if (!is_hpet_enabled())
586 return 0;
587
588 hpet_rtc_flags |= bit_mask;
589
590 if (!oldbits)
591 hpet_rtc_timer_init();
592
593 return 1;
594}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100595EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800596
597int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
598 unsigned char sec)
599{
600 if (!is_hpet_enabled())
601 return 0;
602
603 hpet_alarm_time.tm_hour = hrs;
604 hpet_alarm_time.tm_min = min;
605 hpet_alarm_time.tm_sec = sec;
606
607 return 1;
608}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100609EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800610
611int hpet_set_periodic_freq(unsigned long freq)
612{
613 uint64_t clc;
614
615 if (!is_hpet_enabled())
616 return 0;
617
618 if (freq <= DEFAULT_RTC_INT_FREQ)
619 hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
620 else {
621 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
622 do_div(clc, freq);
623 clc >>= hpet_clockevent.shift;
624 hpet_pie_delta = (unsigned long) clc;
625 }
626 return 1;
627}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100628EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800629
630int hpet_rtc_dropped_irq(void)
631{
632 return is_hpet_enabled();
633}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100634EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800635
636static void hpet_rtc_timer_reinit(void)
637{
638 unsigned long cfg, delta;
639 int lost_ints = -1;
640
641 if (unlikely(!hpet_rtc_flags)) {
642 cfg = hpet_readl(HPET_T1_CFG);
643 cfg &= ~HPET_TN_ENABLE;
644 hpet_writel(cfg, HPET_T1_CFG);
645 return;
646 }
647
648 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
649 delta = hpet_default_delta;
650 else
651 delta = hpet_pie_delta;
652
653 /*
654 * Increment the comparator value until we are ahead of the
655 * current count.
656 */
657 do {
658 hpet_t1_cmp += delta;
659 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
660 lost_ints++;
661 } while ((long)(hpet_readl(HPET_COUNTER) - hpet_t1_cmp) > 0);
662
663 if (lost_ints) {
664 if (hpet_rtc_flags & RTC_PIE)
665 hpet_pie_count += lost_ints;
666 if (printk_ratelimit())
667 printk(KERN_WARNING "rtc: lost %d interrupts\n",
668 lost_ints);
669 }
670}
671
672irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
673{
674 struct rtc_time curr_time;
675 unsigned long rtc_int_flag = 0;
676
677 hpet_rtc_timer_reinit();
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100678 memset(&curr_time, 0, sizeof(struct rtc_time));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800679
680 if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100681 get_rtc_time(&curr_time);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800682
683 if (hpet_rtc_flags & RTC_UIE &&
684 curr_time.tm_sec != hpet_prev_update_sec) {
685 rtc_int_flag = RTC_UF;
686 hpet_prev_update_sec = curr_time.tm_sec;
687 }
688
689 if (hpet_rtc_flags & RTC_PIE &&
690 ++hpet_pie_count >= hpet_pie_limit) {
691 rtc_int_flag |= RTC_PF;
692 hpet_pie_count = 0;
693 }
694
Bernhard Walle8ee291f2008-01-15 16:44:38 +0100695 if (hpet_rtc_flags & RTC_AIE &&
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800696 (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
697 (curr_time.tm_min == hpet_alarm_time.tm_min) &&
698 (curr_time.tm_hour == hpet_alarm_time.tm_hour))
699 rtc_int_flag |= RTC_AF;
700
701 if (rtc_int_flag) {
702 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100703 if (irq_handler)
704 irq_handler(rtc_int_flag, dev_id);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800705 }
706 return IRQ_HANDLED;
707}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100708EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800709#endif