blob: 73deaffadd036a578984bb8ad6e0455f1044558a [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;
118 hd.hd_flags = HPET_DATA_PLATFORM;
119 hpet_reserve_timer(&hd, 0);
120
121#ifdef CONFIG_HPET_EMULATE_RTC
122 hpet_reserve_timer(&hd, 1);
123#endif
Thomas Gleixner5761d642008-04-04 16:26:10 +0200124
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800125 hd.hd_irq[0] = HPET_LEGACY_8254;
126 hd.hd_irq[1] = HPET_LEGACY_RTC;
127
Ingo Molnarfc3fbc42008-04-27 14:04:14 +0200128 for (i = 2; i < nrtimers; timer++, i++) {
129 hd.hd_irq[i] = (readl(&timer->hpet_config) & Tn_INT_ROUTE_CNF_MASK) >>
Thomas Gleixner5761d642008-04-04 16:26:10 +0200130 Tn_INT_ROUTE_CNF_SHIFT;
Ingo Molnarfc3fbc42008-04-27 14:04:14 +0200131 }
Thomas Gleixner5761d642008-04-04 16:26:10 +0200132
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800133 hpet_alloc(&hd);
Thomas Gleixner5761d642008-04-04 16:26:10 +0200134
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800135}
136#else
137static void hpet_reserve_platform_timers(unsigned long id) { }
138#endif
139
140/*
141 * Common hpet info
142 */
143static unsigned long hpet_period;
144
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200145static void hpet_legacy_set_mode(enum clock_event_mode mode,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800146 struct clock_event_device *evt);
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200147static int hpet_legacy_next_event(unsigned long delta,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800148 struct clock_event_device *evt);
149
150/*
151 * The hpet clock event device
152 */
153static struct clock_event_device hpet_clockevent = {
154 .name = "hpet",
155 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200156 .set_mode = hpet_legacy_set_mode,
157 .set_next_event = hpet_legacy_next_event,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800158 .shift = 32,
159 .irq = 0,
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200160 .rating = 50,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800161};
162
163static void hpet_start_counter(void)
164{
165 unsigned long cfg = hpet_readl(HPET_CFG);
166
167 cfg &= ~HPET_CFG_ENABLE;
168 hpet_writel(cfg, HPET_CFG);
169 hpet_writel(0, HPET_COUNTER);
170 hpet_writel(0, HPET_COUNTER + 4);
171 cfg |= HPET_CFG_ENABLE;
172 hpet_writel(cfg, HPET_CFG);
173}
174
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200175static void hpet_resume_device(void)
176{
Venki Pallipadibfe0c1c2007-10-12 23:04:24 +0200177 force_hpet_resume();
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200178}
179
180static void hpet_restart_counter(void)
181{
182 hpet_resume_device();
183 hpet_start_counter();
184}
185
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200186static void hpet_enable_legacy_int(void)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800187{
188 unsigned long cfg = hpet_readl(HPET_CFG);
189
190 cfg |= HPET_CFG_LEGACY;
191 hpet_writel(cfg, HPET_CFG);
192 hpet_legacy_int_enabled = 1;
193}
194
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200195static void hpet_legacy_clockevent_register(void)
196{
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200197 /* Start HPET legacy interrupts */
198 hpet_enable_legacy_int();
199
200 /*
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300201 * The mult factor is defined as (include/linux/clockchips.h)
202 * mult/2^shift = cyc/ns (in contrast to ns/cyc in clocksource.h)
203 * hpet_period is in units of femtoseconds (per cycle), so
204 * mult/2^shift = cyc/ns = 10^6/hpet_period
205 * mult = (10^6 * 2^shift)/hpet_period
206 * mult = (FSEC_PER_NSEC << hpet_clockevent.shift)/hpet_period
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200207 */
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300208 hpet_clockevent.mult = div_sc((unsigned long) FSEC_PER_NSEC,
209 hpet_period, hpet_clockevent.shift);
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200210 /* Calculate the min / max delta */
211 hpet_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF,
212 &hpet_clockevent);
Thomas Gleixner7cfb04352008-09-03 21:37:24 +0000213 /* 5 usec minimum reprogramming delta. */
214 hpet_clockevent.min_delta_ns = 5000;
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200215
216 /*
217 * Start hpet with the boot cpu mask and make it
218 * global after the IO_APIC has been initialized.
219 */
220 hpet_clockevent.cpumask = cpumask_of_cpu(smp_processor_id());
221 clockevents_register_device(&hpet_clockevent);
222 global_clock_event = &hpet_clockevent;
223 printk(KERN_DEBUG "hpet clockevent registered\n");
224}
225
226static void hpet_legacy_set_mode(enum clock_event_mode mode,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800227 struct clock_event_device *evt)
228{
229 unsigned long cfg, cmp, now;
230 uint64_t delta;
231
232 switch(mode) {
233 case CLOCK_EVT_MODE_PERIODIC:
234 delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * hpet_clockevent.mult;
235 delta >>= hpet_clockevent.shift;
236 now = hpet_readl(HPET_COUNTER);
237 cmp = now + (unsigned long) delta;
238 cfg = hpet_readl(HPET_T0_CFG);
239 cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
240 HPET_TN_SETVAL | HPET_TN_32BIT;
241 hpet_writel(cfg, HPET_T0_CFG);
242 /*
243 * The first write after writing TN_SETVAL to the
244 * config register sets the counter value, the second
245 * write sets the period.
246 */
247 hpet_writel(cmp, HPET_T0_CMP);
248 udelay(1);
249 hpet_writel((unsigned long) delta, HPET_T0_CMP);
250 break;
251
252 case CLOCK_EVT_MODE_ONESHOT:
253 cfg = hpet_readl(HPET_T0_CFG);
254 cfg &= ~HPET_TN_PERIODIC;
255 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
256 hpet_writel(cfg, HPET_T0_CFG);
257 break;
258
259 case CLOCK_EVT_MODE_UNUSED:
260 case CLOCK_EVT_MODE_SHUTDOWN:
261 cfg = hpet_readl(HPET_T0_CFG);
262 cfg &= ~HPET_TN_ENABLE;
263 hpet_writel(cfg, HPET_T0_CFG);
264 break;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700265
266 case CLOCK_EVT_MODE_RESUME:
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200267 hpet_enable_legacy_int();
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700268 break;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800269 }
270}
271
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200272static int hpet_legacy_next_event(unsigned long delta,
Thomas Gleixnerf7676252008-09-06 03:03:32 +0200273 struct clock_event_device *evt)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800274{
Thomas Gleixnerf7676252008-09-06 03:03:32 +0200275 u32 cnt;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800276
277 cnt = hpet_readl(HPET_COUNTER);
Thomas Gleixnerf7676252008-09-06 03:03:32 +0200278 cnt += (u32) delta;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800279 hpet_writel(cnt, HPET_T0_CMP);
280
Thomas Gleixner72d43d92008-09-06 03:06:08 +0200281 /*
282 * We need to read back the CMP register to make sure that
283 * what we wrote hit the chip before we compare it to the
284 * counter.
285 */
286 WARN_ON((u32)hpet_readl(HPET_T0_CMP) != cnt);
287
Thomas Gleixnerf7676252008-09-06 03:03:32 +0200288 return (s32)((u32)hpet_readl(HPET_COUNTER) - cnt) >= 0 ? -ETIME : 0;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800289}
290
291/*
john stultz6bb74df2007-03-05 00:30:50 -0800292 * Clock source related code
293 */
294static cycle_t read_hpet(void)
295{
296 return (cycle_t)hpet_readl(HPET_COUNTER);
297}
298
Thomas Gleixner28769142007-10-12 23:04:06 +0200299#ifdef CONFIG_X86_64
300static cycle_t __vsyscall_fn vread_hpet(void)
301{
302 return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
303}
304#endif
305
john stultz6bb74df2007-03-05 00:30:50 -0800306static struct clocksource clocksource_hpet = {
307 .name = "hpet",
308 .rating = 250,
309 .read = read_hpet,
310 .mask = HPET_MASK,
311 .shift = HPET_SHIFT,
312 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200313 .resume = hpet_restart_counter,
Thomas Gleixner28769142007-10-12 23:04:06 +0200314#ifdef CONFIG_X86_64
315 .vread = vread_hpet,
316#endif
john stultz6bb74df2007-03-05 00:30:50 -0800317};
318
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200319static int hpet_clocksource_register(void)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800320{
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300321 u64 start, now;
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200322 cycle_t t1;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800323
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800324 /* Start the counter */
325 hpet_start_counter();
326
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200327 /* Verify whether hpet counter works */
328 t1 = read_hpet();
329 rdtscll(start);
330
331 /*
332 * We don't know the TSC frequency yet, but waiting for
333 * 200000 TSC cycles is safe:
334 * 4 GHz == 50us
335 * 1 GHz == 200us
336 */
337 do {
338 rep_nop();
339 rdtscll(now);
340 } while ((now - start) < 200000UL);
341
342 if (t1 == read_hpet()) {
343 printk(KERN_WARNING
344 "HPET counter not counting. HPET disabled\n");
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200345 return -ENODEV;
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200346 }
347
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300348 /*
349 * The definition of mult is (include/linux/clocksource.h)
350 * mult/2^shift = ns/cyc and hpet_period is in units of fsec/cyc
351 * so we first need to convert hpet_period to ns/cyc units:
352 * mult/2^shift = ns/cyc = hpet_period/10^6
353 * mult = (hpet_period * 2^shift)/10^6
354 * mult = (hpet_period << shift)/FSEC_PER_NSEC
john stultz6bb74df2007-03-05 00:30:50 -0800355 */
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300356 clocksource_hpet.mult = div_sc(hpet_period, FSEC_PER_NSEC, HPET_SHIFT);
john stultz6bb74df2007-03-05 00:30:50 -0800357
358 clocksource_register(&clocksource_hpet);
359
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200360 return 0;
361}
362
Pavel Machekb02a7f22008-02-05 00:48:13 +0100363/**
364 * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200365 */
366int __init hpet_enable(void)
367{
368 unsigned long id;
Thomas Gleixnera6825f12008-08-14 12:17:06 +0200369 int i;
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200370
371 if (!is_hpet_capable())
372 return 0;
373
374 hpet_set_mapping();
375
376 /*
377 * Read the period and check for a sane value:
378 */
379 hpet_period = hpet_readl(HPET_PERIOD);
Thomas Gleixnera6825f12008-08-14 12:17:06 +0200380
381 /*
382 * AMD SB700 based systems with spread spectrum enabled use a
383 * SMM based HPET emulation to provide proper frequency
384 * setting. The SMM code is initialized with the first HPET
385 * register access and takes some time to complete. During
386 * this time the config register reads 0xffffffff. We check
387 * for max. 1000 loops whether the config register reads a non
388 * 0xffffffff value to make sure that HPET is up and running
389 * before we go further. A counting loop is safe, as the HPET
390 * access takes thousands of CPU cycles. On non SB700 based
391 * machines this check is only done once and has no side
392 * effects.
393 */
394 for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) {
395 if (i == 1000) {
396 printk(KERN_WARNING
397 "HPET config register value = 0xFFFFFFFF. "
398 "Disabling HPET\n");
399 goto out_nohpet;
400 }
401 }
402
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200403 if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
404 goto out_nohpet;
405
406 /*
407 * Read the HPET ID register to retrieve the IRQ routing
408 * information and the number of channels
409 */
410 id = hpet_readl(HPET_ID);
411
412#ifdef CONFIG_HPET_EMULATE_RTC
413 /*
414 * The legacy routing mode needs at least two channels, tick timer
415 * and the rtc emulation channel.
416 */
417 if (!(id & HPET_ID_NUMBER))
418 goto out_nohpet;
419#endif
420
421 if (hpet_clocksource_register())
422 goto out_nohpet;
423
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800424 if (id & HPET_ID_LEGSUP) {
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200425 hpet_legacy_clockevent_register();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800426 return 1;
427 }
428 return 0;
429
430out_nohpet:
Thomas Gleixner06a24de2007-10-12 23:04:06 +0200431 hpet_clear_mapping();
Maxim Levitsky399afa42007-03-29 15:46:48 +0200432 boot_hpet_disable = 1;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800433 return 0;
434}
435
Thomas Gleixner28769142007-10-12 23:04:06 +0200436/*
437 * Needs to be late, as the reserve_timer code calls kalloc !
438 *
439 * Not a problem on i386 as hpet_enable is called from late_time_init,
440 * but on x86_64 it is necessary !
441 */
442static __init int hpet_late_init(void)
443{
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200444 if (boot_hpet_disable)
Thomas Gleixner28769142007-10-12 23:04:06 +0200445 return -ENODEV;
446
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200447 if (!hpet_address) {
448 if (!force_hpet_address)
449 return -ENODEV;
450
451 hpet_address = force_hpet_address;
452 hpet_enable();
453 if (!hpet_virt_address)
454 return -ENODEV;
455 }
456
Thomas Gleixner28769142007-10-12 23:04:06 +0200457 hpet_reserve_platform_timers(hpet_readl(HPET_ID));
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200458
Thomas Gleixner28769142007-10-12 23:04:06 +0200459 return 0;
460}
461fs_initcall(hpet_late_init);
462
OGAWA Hirofumic86c7fb2007-12-03 17:17:10 +0100463void hpet_disable(void)
464{
465 if (is_hpet_capable()) {
466 unsigned long cfg = hpet_readl(HPET_CFG);
467
468 if (hpet_legacy_int_enabled) {
469 cfg &= ~HPET_CFG_LEGACY;
470 hpet_legacy_int_enabled = 0;
471 }
472 cfg &= ~HPET_CFG_ENABLE;
473 hpet_writel(cfg, HPET_CFG);
474 }
475}
476
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800477#ifdef CONFIG_HPET_EMULATE_RTC
478
479/* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
480 * is enabled, we support RTC interrupt functionality in software.
481 * RTC has 3 kinds of interrupts:
482 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
483 * is updated
484 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
485 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
486 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
487 * (1) and (2) above are implemented using polling at a frequency of
488 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
489 * overhead. (DEFAULT_RTC_INT_FREQ)
490 * For (3), we use interrupts at 64Hz or user specified periodic
491 * frequency, whichever is higher.
492 */
493#include <linux/mc146818rtc.h>
494#include <linux/rtc.h>
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100495#include <asm/rtc.h>
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800496
497#define DEFAULT_RTC_INT_FREQ 64
498#define DEFAULT_RTC_SHIFT 6
499#define RTC_NUM_INTS 1
500
501static unsigned long hpet_rtc_flags;
David Brownell7e2a31d2008-07-23 21:30:47 -0700502static int hpet_prev_update_sec;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800503static struct rtc_time hpet_alarm_time;
504static unsigned long hpet_pie_count;
505static unsigned long hpet_t1_cmp;
506static unsigned long hpet_default_delta;
507static unsigned long hpet_pie_delta;
508static unsigned long hpet_pie_limit;
509
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100510static rtc_irq_handler irq_handler;
511
512/*
513 * Registers a IRQ handler.
514 */
515int hpet_register_irq_handler(rtc_irq_handler handler)
516{
517 if (!is_hpet_enabled())
518 return -ENODEV;
519 if (irq_handler)
520 return -EBUSY;
521
522 irq_handler = handler;
523
524 return 0;
525}
526EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
527
528/*
529 * Deregisters the IRQ handler registered with hpet_register_irq_handler()
530 * and does cleanup.
531 */
532void hpet_unregister_irq_handler(rtc_irq_handler handler)
533{
534 if (!is_hpet_enabled())
535 return;
536
537 irq_handler = NULL;
538 hpet_rtc_flags = 0;
539}
540EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
541
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800542/*
543 * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
544 * is not supported by all HPET implementations for timer 1.
545 *
546 * hpet_rtc_timer_init() is called when the rtc is initialized.
547 */
548int hpet_rtc_timer_init(void)
549{
550 unsigned long cfg, cnt, delta, flags;
551
552 if (!is_hpet_enabled())
553 return 0;
554
555 if (!hpet_default_delta) {
556 uint64_t clc;
557
558 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
559 clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
560 hpet_default_delta = (unsigned long) clc;
561 }
562
563 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
564 delta = hpet_default_delta;
565 else
566 delta = hpet_pie_delta;
567
568 local_irq_save(flags);
569
570 cnt = delta + hpet_readl(HPET_COUNTER);
571 hpet_writel(cnt, HPET_T1_CMP);
572 hpet_t1_cmp = cnt;
573
574 cfg = hpet_readl(HPET_T1_CFG);
575 cfg &= ~HPET_TN_PERIODIC;
576 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
577 hpet_writel(cfg, HPET_T1_CFG);
578
579 local_irq_restore(flags);
580
581 return 1;
582}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100583EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800584
585/*
586 * The functions below are called from rtc driver.
587 * Return 0 if HPET is not being used.
588 * Otherwise do the necessary changes and return 1.
589 */
590int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
591{
592 if (!is_hpet_enabled())
593 return 0;
594
595 hpet_rtc_flags &= ~bit_mask;
596 return 1;
597}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100598EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800599
600int hpet_set_rtc_irq_bit(unsigned long bit_mask)
601{
602 unsigned long oldbits = hpet_rtc_flags;
603
604 if (!is_hpet_enabled())
605 return 0;
606
607 hpet_rtc_flags |= bit_mask;
608
David Brownell7e2a31d2008-07-23 21:30:47 -0700609 if ((bit_mask & RTC_UIE) && !(oldbits & RTC_UIE))
610 hpet_prev_update_sec = -1;
611
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800612 if (!oldbits)
613 hpet_rtc_timer_init();
614
615 return 1;
616}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100617EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800618
619int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
620 unsigned char sec)
621{
622 if (!is_hpet_enabled())
623 return 0;
624
625 hpet_alarm_time.tm_hour = hrs;
626 hpet_alarm_time.tm_min = min;
627 hpet_alarm_time.tm_sec = sec;
628
629 return 1;
630}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100631EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800632
633int hpet_set_periodic_freq(unsigned long freq)
634{
635 uint64_t clc;
636
637 if (!is_hpet_enabled())
638 return 0;
639
640 if (freq <= DEFAULT_RTC_INT_FREQ)
641 hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
642 else {
643 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
644 do_div(clc, freq);
645 clc >>= hpet_clockevent.shift;
646 hpet_pie_delta = (unsigned long) clc;
647 }
648 return 1;
649}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100650EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800651
652int hpet_rtc_dropped_irq(void)
653{
654 return is_hpet_enabled();
655}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100656EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800657
658static void hpet_rtc_timer_reinit(void)
659{
660 unsigned long cfg, delta;
661 int lost_ints = -1;
662
663 if (unlikely(!hpet_rtc_flags)) {
664 cfg = hpet_readl(HPET_T1_CFG);
665 cfg &= ~HPET_TN_ENABLE;
666 hpet_writel(cfg, HPET_T1_CFG);
667 return;
668 }
669
670 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
671 delta = hpet_default_delta;
672 else
673 delta = hpet_pie_delta;
674
675 /*
676 * Increment the comparator value until we are ahead of the
677 * current count.
678 */
679 do {
680 hpet_t1_cmp += delta;
681 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
682 lost_ints++;
683 } while ((long)(hpet_readl(HPET_COUNTER) - hpet_t1_cmp) > 0);
684
685 if (lost_ints) {
686 if (hpet_rtc_flags & RTC_PIE)
687 hpet_pie_count += lost_ints;
688 if (printk_ratelimit())
David Brownell7e2a31d2008-07-23 21:30:47 -0700689 printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n",
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800690 lost_ints);
691 }
692}
693
694irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
695{
696 struct rtc_time curr_time;
697 unsigned long rtc_int_flag = 0;
698
699 hpet_rtc_timer_reinit();
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100700 memset(&curr_time, 0, sizeof(struct rtc_time));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800701
702 if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100703 get_rtc_time(&curr_time);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800704
705 if (hpet_rtc_flags & RTC_UIE &&
706 curr_time.tm_sec != hpet_prev_update_sec) {
David Brownell7e2a31d2008-07-23 21:30:47 -0700707 if (hpet_prev_update_sec >= 0)
708 rtc_int_flag = RTC_UF;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800709 hpet_prev_update_sec = curr_time.tm_sec;
710 }
711
712 if (hpet_rtc_flags & RTC_PIE &&
713 ++hpet_pie_count >= hpet_pie_limit) {
714 rtc_int_flag |= RTC_PF;
715 hpet_pie_count = 0;
716 }
717
Bernhard Walle8ee291f2008-01-15 16:44:38 +0100718 if (hpet_rtc_flags & RTC_AIE &&
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800719 (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
720 (curr_time.tm_min == hpet_alarm_time.tm_min) &&
721 (curr_time.tm_hour == hpet_alarm_time.tm_hour))
722 rtc_int_flag |= RTC_AF;
723
724 if (rtc_int_flag) {
725 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100726 if (irq_handler)
727 irq_handler(rtc_int_flag, dev_id);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800728 }
729 return IRQ_HANDLED;
730}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100731EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800732#endif