blob: 82d459186fd8019c2b1fb92aaad37f81b6439a93 [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 Wright31c435d72007-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
Thomas Gleixner28769142007-10-12 23:04:06 +020039#include <asm/pgtable.h>
Yinghai Lu2387ce52008-07-13 14:50:56 -070040#endif
Thomas Gleixner28769142007-10-12 23:04:06 +020041
Thomas Gleixner06a24de2007-10-12 23:04:06 +020042static inline void hpet_set_mapping(void)
43{
44 hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
Yinghai Lu2387ce52008-07-13 14:50:56 -070045#ifdef CONFIG_X86_64
46 __set_fixmap(VSYSCALL_HPET, hpet_address, PAGE_KERNEL_VSYSCALL_NOCACHE);
47#endif
Thomas Gleixner06a24de2007-10-12 23:04:06 +020048}
49
50static inline void hpet_clear_mapping(void)
51{
52 iounmap(hpet_virt_address);
53 hpet_virt_address = NULL;
54}
55
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080056/*
57 * HPET command line enable / disable
58 */
59static int boot_hpet_disable;
Thomas Gleixnerb17530b2007-10-19 20:35:02 +020060int hpet_force_user;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080061
62static int __init hpet_setup(char* str)
63{
64 if (str) {
65 if (!strncmp("disable", str, 7))
66 boot_hpet_disable = 1;
Thomas Gleixnerb17530b2007-10-19 20:35:02 +020067 if (!strncmp("force", str, 5))
68 hpet_force_user = 1;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080069 }
70 return 1;
71}
72__setup("hpet=", hpet_setup);
73
Thomas Gleixner28769142007-10-12 23:04:06 +020074static int __init disable_hpet(char *str)
75{
76 boot_hpet_disable = 1;
77 return 1;
78}
79__setup("nohpet", disable_hpet);
80
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080081static inline int is_hpet_capable(void)
82{
83 return (!boot_hpet_disable && hpet_address);
84}
85
86/*
87 * HPET timer interrupt enable / disable
88 */
89static int hpet_legacy_int_enabled;
90
91/**
92 * is_hpet_enabled - check whether the hpet timer interrupt is enabled
93 */
94int is_hpet_enabled(void)
95{
96 return is_hpet_capable() && hpet_legacy_int_enabled;
97}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +010098EXPORT_SYMBOL_GPL(is_hpet_enabled);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080099
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
105static void hpet_reserve_platform_timers(unsigned long id)
106{
107 struct hpet __iomem *hpet = hpet_virt_address;
Balaji Rao37a47db82008-01-30 13:30:03 +0100108 struct hpet_timer __iomem *timer = &hpet->hpet_timers[2];
109 unsigned int nrtimers, i;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800110 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;
Thomas Gleixner06a24de2007-10-12 23:04:06 +0200116 hd.hd_address = hpet;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800117 hd.hd_nirqs = nrtimers;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800118 hpet_reserve_timer(&hd, 0);
119
120#ifdef CONFIG_HPET_EMULATE_RTC
121 hpet_reserve_timer(&hd, 1);
122#endif
Thomas Gleixner5761d642008-04-04 16:26:10 +0200123
David Brownell64a76f62008-07-29 12:47:38 -0700124 /*
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 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800129 hd.hd_irq[0] = HPET_LEGACY_8254;
130 hd.hd_irq[1] = HPET_LEGACY_RTC;
131
Ingo Molnarfc3fbc42008-04-27 14:04:14 +0200132 for (i = 2; i < nrtimers; timer++, i++) {
133 hd.hd_irq[i] = (readl(&timer->hpet_config) & Tn_INT_ROUTE_CNF_MASK) >>
Thomas Gleixner5761d642008-04-04 16:26:10 +0200134 Tn_INT_ROUTE_CNF_SHIFT;
Ingo Molnarfc3fbc42008-04-27 14:04:14 +0200135 }
Thomas Gleixner5761d642008-04-04 16:26:10 +0200136
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800137 hpet_alloc(&hd);
Thomas Gleixner5761d642008-04-04 16:26:10 +0200138
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800139}
140#else
141static void hpet_reserve_platform_timers(unsigned long id) { }
142#endif
143
144/*
145 * Common hpet info
146 */
147static unsigned long hpet_period;
148
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200149static void hpet_legacy_set_mode(enum clock_event_mode mode,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800150 struct clock_event_device *evt);
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200151static int hpet_legacy_next_event(unsigned long delta,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800152 struct clock_event_device *evt);
153
154/*
155 * The hpet clock event device
156 */
157static struct clock_event_device hpet_clockevent = {
158 .name = "hpet",
159 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200160 .set_mode = hpet_legacy_set_mode,
161 .set_next_event = hpet_legacy_next_event,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800162 .shift = 32,
163 .irq = 0,
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200164 .rating = 50,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800165};
166
167static 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
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200179static void hpet_resume_device(void)
180{
Venki Pallipadibfe0c1c2007-10-12 23:04:24 +0200181 force_hpet_resume();
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200182}
183
184static void hpet_restart_counter(void)
185{
186 hpet_resume_device();
187 hpet_start_counter();
188}
189
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200190static void hpet_enable_legacy_int(void)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800191{
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
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200199static void hpet_legacy_clockevent_register(void)
200{
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200201 /* Start HPET legacy interrupts */
202 hpet_enable_legacy_int();
203
204 /*
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300205 * 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
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200211 */
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300212 hpet_clockevent.mult = div_sc((unsigned long) FSEC_PER_NSEC,
213 hpet_period, hpet_clockevent.shift);
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200214 /* Calculate the min / max delta */
215 hpet_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF,
216 &hpet_clockevent);
217 hpet_clockevent.min_delta_ns = clockevent_delta2ns(0x30,
218 &hpet_clockevent);
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
230static void hpet_legacy_set_mode(enum clock_event_mode mode,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800231 struct clock_event_device *evt)
232{
233 unsigned long cfg, cmp, now;
234 uint64_t delta;
235
236 switch(mode) {
237 case CLOCK_EVT_MODE_PERIODIC:
238 delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * hpet_clockevent.mult;
239 delta >>= hpet_clockevent.shift;
240 now = hpet_readl(HPET_COUNTER);
241 cmp = now + (unsigned long) delta;
242 cfg = hpet_readl(HPET_T0_CFG);
243 cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
244 HPET_TN_SETVAL | HPET_TN_32BIT;
245 hpet_writel(cfg, HPET_T0_CFG);
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 */
251 hpet_writel(cmp, HPET_T0_CMP);
252 udelay(1);
253 hpet_writel((unsigned long) delta, HPET_T0_CMP);
254 break;
255
256 case CLOCK_EVT_MODE_ONESHOT:
257 cfg = hpet_readl(HPET_T0_CFG);
258 cfg &= ~HPET_TN_PERIODIC;
259 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
260 hpet_writel(cfg, HPET_T0_CFG);
261 break;
262
263 case CLOCK_EVT_MODE_UNUSED:
264 case CLOCK_EVT_MODE_SHUTDOWN:
265 cfg = hpet_readl(HPET_T0_CFG);
266 cfg &= ~HPET_TN_ENABLE;
267 hpet_writel(cfg, HPET_T0_CFG);
268 break;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700269
270 case CLOCK_EVT_MODE_RESUME:
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200271 hpet_enable_legacy_int();
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700272 break;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800273 }
274}
275
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200276static int hpet_legacy_next_event(unsigned long delta,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800277 struct clock_event_device *evt)
278{
279 unsigned long cnt;
280
281 cnt = hpet_readl(HPET_COUNTER);
282 cnt += delta;
283 hpet_writel(cnt, HPET_T0_CMP);
284
Thomas Gleixnerc7f6d152007-03-27 09:08:26 +0200285 return ((long)(hpet_readl(HPET_COUNTER) - cnt ) > 0) ? -ETIME : 0;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800286}
287
288/*
john stultz6bb74df2007-03-05 00:30:50 -0800289 * Clock source related code
290 */
291static cycle_t read_hpet(void)
292{
293 return (cycle_t)hpet_readl(HPET_COUNTER);
294}
295
Thomas Gleixner28769142007-10-12 23:04:06 +0200296#ifdef CONFIG_X86_64
297static cycle_t __vsyscall_fn vread_hpet(void)
298{
299 return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
300}
301#endif
302
john stultz6bb74df2007-03-05 00:30:50 -0800303static struct clocksource clocksource_hpet = {
304 .name = "hpet",
305 .rating = 250,
306 .read = read_hpet,
307 .mask = HPET_MASK,
308 .shift = HPET_SHIFT,
309 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200310 .resume = hpet_restart_counter,
Thomas Gleixner28769142007-10-12 23:04:06 +0200311#ifdef CONFIG_X86_64
312 .vread = vread_hpet,
313#endif
john stultz6bb74df2007-03-05 00:30:50 -0800314};
315
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200316static int hpet_clocksource_register(void)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800317{
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300318 u64 start, now;
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200319 cycle_t t1;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800320
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800321 /* Start the counter */
322 hpet_start_counter();
323
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200324 /* Verify whether hpet counter works */
325 t1 = read_hpet();
326 rdtscll(start);
327
328 /*
329 * We don't know the TSC frequency yet, but waiting for
330 * 200000 TSC cycles is safe:
331 * 4 GHz == 50us
332 * 1 GHz == 200us
333 */
334 do {
335 rep_nop();
336 rdtscll(now);
337 } while ((now - start) < 200000UL);
338
339 if (t1 == read_hpet()) {
340 printk(KERN_WARNING
341 "HPET counter not counting. HPET disabled\n");
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200342 return -ENODEV;
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200343 }
344
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300345 /*
346 * The definition of mult is (include/linux/clocksource.h)
347 * mult/2^shift = ns/cyc and hpet_period is in units of fsec/cyc
348 * so we first need to convert hpet_period to ns/cyc units:
349 * mult/2^shift = ns/cyc = hpet_period/10^6
350 * mult = (hpet_period * 2^shift)/10^6
351 * mult = (hpet_period << shift)/FSEC_PER_NSEC
john stultz6bb74df2007-03-05 00:30:50 -0800352 */
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300353 clocksource_hpet.mult = div_sc(hpet_period, FSEC_PER_NSEC, HPET_SHIFT);
john stultz6bb74df2007-03-05 00:30:50 -0800354
355 clocksource_register(&clocksource_hpet);
356
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200357 return 0;
358}
359
Pavel Machekb02a7f22008-02-05 00:48:13 +0100360/**
361 * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200362 */
363int __init hpet_enable(void)
364{
365 unsigned long id;
366
367 if (!is_hpet_capable())
368 return 0;
369
370 hpet_set_mapping();
371
372 /*
373 * Read the period and check for a sane value:
374 */
375 hpet_period = hpet_readl(HPET_PERIOD);
376 if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
377 goto out_nohpet;
378
379 /*
380 * Read the HPET ID register to retrieve the IRQ routing
381 * information and the number of channels
382 */
383 id = hpet_readl(HPET_ID);
384
385#ifdef CONFIG_HPET_EMULATE_RTC
386 /*
387 * The legacy routing mode needs at least two channels, tick timer
388 * and the rtc emulation channel.
389 */
390 if (!(id & HPET_ID_NUMBER))
391 goto out_nohpet;
392#endif
393
394 if (hpet_clocksource_register())
395 goto out_nohpet;
396
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800397 if (id & HPET_ID_LEGSUP) {
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200398 hpet_legacy_clockevent_register();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800399 return 1;
400 }
401 return 0;
402
403out_nohpet:
Thomas Gleixner06a24de2007-10-12 23:04:06 +0200404 hpet_clear_mapping();
Maxim Levitsky399afa42007-03-29 15:46:48 +0200405 boot_hpet_disable = 1;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800406 return 0;
407}
408
Thomas Gleixner28769142007-10-12 23:04:06 +0200409/*
410 * Needs to be late, as the reserve_timer code calls kalloc !
411 *
412 * Not a problem on i386 as hpet_enable is called from late_time_init,
413 * but on x86_64 it is necessary !
414 */
415static __init int hpet_late_init(void)
416{
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200417 if (boot_hpet_disable)
Thomas Gleixner28769142007-10-12 23:04:06 +0200418 return -ENODEV;
419
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200420 if (!hpet_address) {
421 if (!force_hpet_address)
422 return -ENODEV;
423
424 hpet_address = force_hpet_address;
425 hpet_enable();
426 if (!hpet_virt_address)
427 return -ENODEV;
428 }
429
Thomas Gleixner28769142007-10-12 23:04:06 +0200430 hpet_reserve_platform_timers(hpet_readl(HPET_ID));
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200431
Thomas Gleixner28769142007-10-12 23:04:06 +0200432 return 0;
433}
434fs_initcall(hpet_late_init);
435
OGAWA Hirofumic86c7fb2007-12-03 17:17:10 +0100436void hpet_disable(void)
437{
438 if (is_hpet_capable()) {
439 unsigned long cfg = hpet_readl(HPET_CFG);
440
441 if (hpet_legacy_int_enabled) {
442 cfg &= ~HPET_CFG_LEGACY;
443 hpet_legacy_int_enabled = 0;
444 }
445 cfg &= ~HPET_CFG_ENABLE;
446 hpet_writel(cfg, HPET_CFG);
447 }
448}
449
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800450#ifdef CONFIG_HPET_EMULATE_RTC
451
452/* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
453 * is enabled, we support RTC interrupt functionality in software.
454 * RTC has 3 kinds of interrupts:
455 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
456 * is updated
457 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
458 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
459 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
460 * (1) and (2) above are implemented using polling at a frequency of
461 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
462 * overhead. (DEFAULT_RTC_INT_FREQ)
463 * For (3), we use interrupts at 64Hz or user specified periodic
464 * frequency, whichever is higher.
465 */
466#include <linux/mc146818rtc.h>
467#include <linux/rtc.h>
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100468#include <asm/rtc.h>
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800469
470#define DEFAULT_RTC_INT_FREQ 64
471#define DEFAULT_RTC_SHIFT 6
472#define RTC_NUM_INTS 1
473
474static unsigned long hpet_rtc_flags;
David Brownell7e2a31d2008-07-23 21:30:47 -0700475static int hpet_prev_update_sec;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800476static struct rtc_time hpet_alarm_time;
477static unsigned long hpet_pie_count;
478static unsigned long hpet_t1_cmp;
479static unsigned long hpet_default_delta;
480static unsigned long hpet_pie_delta;
481static unsigned long hpet_pie_limit;
482
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100483static rtc_irq_handler irq_handler;
484
485/*
486 * Registers a IRQ handler.
487 */
488int hpet_register_irq_handler(rtc_irq_handler handler)
489{
490 if (!is_hpet_enabled())
491 return -ENODEV;
492 if (irq_handler)
493 return -EBUSY;
494
495 irq_handler = handler;
496
497 return 0;
498}
499EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
500
501/*
502 * Deregisters the IRQ handler registered with hpet_register_irq_handler()
503 * and does cleanup.
504 */
505void hpet_unregister_irq_handler(rtc_irq_handler handler)
506{
507 if (!is_hpet_enabled())
508 return;
509
510 irq_handler = NULL;
511 hpet_rtc_flags = 0;
512}
513EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
514
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800515/*
516 * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
517 * is not supported by all HPET implementations for timer 1.
518 *
519 * hpet_rtc_timer_init() is called when the rtc is initialized.
520 */
521int hpet_rtc_timer_init(void)
522{
523 unsigned long cfg, cnt, delta, flags;
524
525 if (!is_hpet_enabled())
526 return 0;
527
528 if (!hpet_default_delta) {
529 uint64_t clc;
530
531 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
532 clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
533 hpet_default_delta = (unsigned long) clc;
534 }
535
536 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
537 delta = hpet_default_delta;
538 else
539 delta = hpet_pie_delta;
540
541 local_irq_save(flags);
542
543 cnt = delta + hpet_readl(HPET_COUNTER);
544 hpet_writel(cnt, HPET_T1_CMP);
545 hpet_t1_cmp = cnt;
546
547 cfg = hpet_readl(HPET_T1_CFG);
548 cfg &= ~HPET_TN_PERIODIC;
549 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
550 hpet_writel(cfg, HPET_T1_CFG);
551
552 local_irq_restore(flags);
553
554 return 1;
555}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100556EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800557
558/*
559 * The functions below are called from rtc driver.
560 * Return 0 if HPET is not being used.
561 * Otherwise do the necessary changes and return 1.
562 */
563int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
564{
565 if (!is_hpet_enabled())
566 return 0;
567
568 hpet_rtc_flags &= ~bit_mask;
569 return 1;
570}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100571EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800572
573int hpet_set_rtc_irq_bit(unsigned long bit_mask)
574{
575 unsigned long oldbits = hpet_rtc_flags;
576
577 if (!is_hpet_enabled())
578 return 0;
579
580 hpet_rtc_flags |= bit_mask;
581
David Brownell7e2a31d2008-07-23 21:30:47 -0700582 if ((bit_mask & RTC_UIE) && !(oldbits & RTC_UIE))
583 hpet_prev_update_sec = -1;
584
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800585 if (!oldbits)
586 hpet_rtc_timer_init();
587
588 return 1;
589}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100590EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800591
592int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
593 unsigned char sec)
594{
595 if (!is_hpet_enabled())
596 return 0;
597
598 hpet_alarm_time.tm_hour = hrs;
599 hpet_alarm_time.tm_min = min;
600 hpet_alarm_time.tm_sec = sec;
601
602 return 1;
603}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100604EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800605
606int hpet_set_periodic_freq(unsigned long freq)
607{
608 uint64_t clc;
609
610 if (!is_hpet_enabled())
611 return 0;
612
613 if (freq <= DEFAULT_RTC_INT_FREQ)
614 hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
615 else {
616 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
617 do_div(clc, freq);
618 clc >>= hpet_clockevent.shift;
619 hpet_pie_delta = (unsigned long) clc;
620 }
621 return 1;
622}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100623EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800624
625int hpet_rtc_dropped_irq(void)
626{
627 return is_hpet_enabled();
628}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100629EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800630
631static void hpet_rtc_timer_reinit(void)
632{
633 unsigned long cfg, delta;
634 int lost_ints = -1;
635
636 if (unlikely(!hpet_rtc_flags)) {
637 cfg = hpet_readl(HPET_T1_CFG);
638 cfg &= ~HPET_TN_ENABLE;
639 hpet_writel(cfg, HPET_T1_CFG);
640 return;
641 }
642
643 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
644 delta = hpet_default_delta;
645 else
646 delta = hpet_pie_delta;
647
648 /*
649 * Increment the comparator value until we are ahead of the
650 * current count.
651 */
652 do {
653 hpet_t1_cmp += delta;
654 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
655 lost_ints++;
656 } while ((long)(hpet_readl(HPET_COUNTER) - hpet_t1_cmp) > 0);
657
658 if (lost_ints) {
659 if (hpet_rtc_flags & RTC_PIE)
660 hpet_pie_count += lost_ints;
661 if (printk_ratelimit())
David Brownell7e2a31d2008-07-23 21:30:47 -0700662 printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n",
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800663 lost_ints);
664 }
665}
666
667irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
668{
669 struct rtc_time curr_time;
670 unsigned long rtc_int_flag = 0;
671
672 hpet_rtc_timer_reinit();
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100673 memset(&curr_time, 0, sizeof(struct rtc_time));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800674
675 if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100676 get_rtc_time(&curr_time);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800677
678 if (hpet_rtc_flags & RTC_UIE &&
679 curr_time.tm_sec != hpet_prev_update_sec) {
David Brownell7e2a31d2008-07-23 21:30:47 -0700680 if (hpet_prev_update_sec >= 0)
681 rtc_int_flag = RTC_UF;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800682 hpet_prev_update_sec = curr_time.tm_sec;
683 }
684
685 if (hpet_rtc_flags & RTC_PIE &&
686 ++hpet_pie_count >= hpet_pie_limit) {
687 rtc_int_flag |= RTC_PF;
688 hpet_pie_count = 0;
689 }
690
Bernhard Walle8ee291f2008-01-15 16:44:38 +0100691 if (hpet_rtc_flags & RTC_AIE &&
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800692 (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
693 (curr_time.tm_min == hpet_alarm_time.tm_min) &&
694 (curr_time.tm_hour == hpet_alarm_time.tm_hour))
695 rtc_int_flag |= RTC_AF;
696
697 if (rtc_int_flag) {
698 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100699 if (irq_handler)
700 irq_handler(rtc_int_flag, dev_id);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800701 }
702 return IRQ_HANDLED;
703}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100704EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800705#endif