blob: 5c8da2c2c185e260c7a0fe88c0588c9626302b45 [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>
Ingo Molnar4588c1f2008-09-06 14:19:17 +02003#include <linux/interrupt.h>
4#include <linux/sysdev.h>
Thomas Gleixner28769142007-10-12 23:04:06 +02005#include <linux/delay.h>
john stultz5d0cf412006-06-26 00:25:12 -07006#include <linux/errno.h>
7#include <linux/hpet.h>
8#include <linux/init.h>
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07009#include <linux/cpu.h>
Ingo Molnar4588c1f2008-09-06 14:19:17 +020010#include <linux/pm.h>
11#include <linux/io.h>
john stultz5d0cf412006-06-26 00:25:12 -070012
Thomas Gleixner28769142007-10-12 23:04:06 +020013#include <asm/fixmap.h>
Thomas Gleixner06a24de2007-10-12 23:04:06 +020014#include <asm/i8253.h>
Ingo Molnar4588c1f2008-09-06 14:19:17 +020015#include <asm/hpet.h>
john stultz5d0cf412006-06-26 00:25:12 -070016
Ingo Molnar4588c1f2008-09-06 14:19:17 +020017#define HPET_MASK CLOCKSOURCE_MASK(32)
18#define HPET_SHIFT 22
john stultz5d0cf412006-06-26 00:25:12 -070019
Pavel Machekb10db7f2008-01-30 13:30:00 +010020/* FSEC = 10^-15
21 NSEC = 10^-9 */
Ingo Molnar4588c1f2008-09-06 14:19:17 +020022#define FSEC_PER_NSEC 1000000L
john stultz5d0cf412006-06-26 00:25:12 -070023
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -070024#define HPET_DEV_USED_BIT 2
25#define HPET_DEV_USED (1 << HPET_DEV_USED_BIT)
26#define HPET_DEV_VALID 0x8
27#define HPET_DEV_FSB_CAP 0x1000
28#define HPET_DEV_PERI_CAP 0x2000
29
30#define EVT_TO_HPET_DEV(evt) container_of(evt, struct hpet_dev, evt)
31
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080032/*
33 * HPET address is set in acpi/boot.c, when an ACPI entry exists
34 */
Ingo Molnar4588c1f2008-09-06 14:19:17 +020035unsigned long hpet_address;
Ingo Molnare951e4a2008-11-25 08:42:01 +010036#ifdef CONFIG_PCI_MSI
Hannes Eder3b71e9e2008-11-23 20:19:33 +010037static unsigned long hpet_num_timers;
Ingo Molnare951e4a2008-11-25 08:42:01 +010038#endif
Ingo Molnar4588c1f2008-09-06 14:19:17 +020039static void __iomem *hpet_virt_address;
john stultz5d0cf412006-06-26 00:25:12 -070040
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070041struct hpet_dev {
Ingo Molnar4588c1f2008-09-06 14:19:17 +020042 struct clock_event_device evt;
43 unsigned int num;
44 int cpu;
45 unsigned int irq;
46 unsigned int flags;
47 char name[10];
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070048};
49
Chris Wright31c435d2007-10-12 23:04:23 +020050unsigned long hpet_readl(unsigned long a)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080051{
52 return readl(hpet_virt_address + a);
53}
54
55static inline void hpet_writel(unsigned long d, unsigned long a)
56{
57 writel(d, hpet_virt_address + a);
58}
59
Thomas Gleixner28769142007-10-12 23:04:06 +020060#ifdef CONFIG_X86_64
Thomas Gleixner28769142007-10-12 23:04:06 +020061#include <asm/pgtable.h>
Yinghai Lu2387ce52008-07-13 14:50:56 -070062#endif
Thomas Gleixner28769142007-10-12 23:04:06 +020063
Thomas Gleixner06a24de2007-10-12 23:04:06 +020064static inline void hpet_set_mapping(void)
65{
66 hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
Yinghai Lu2387ce52008-07-13 14:50:56 -070067#ifdef CONFIG_X86_64
68 __set_fixmap(VSYSCALL_HPET, hpet_address, PAGE_KERNEL_VSYSCALL_NOCACHE);
69#endif
Thomas Gleixner06a24de2007-10-12 23:04:06 +020070}
71
72static inline void hpet_clear_mapping(void)
73{
74 iounmap(hpet_virt_address);
75 hpet_virt_address = NULL;
76}
77
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080078/*
79 * HPET command line enable / disable
80 */
81static int boot_hpet_disable;
Thomas Gleixnerb17530b2007-10-19 20:35:02 +020082int hpet_force_user;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080083
Ingo Molnar4588c1f2008-09-06 14:19:17 +020084static int __init hpet_setup(char *str)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080085{
86 if (str) {
87 if (!strncmp("disable", str, 7))
88 boot_hpet_disable = 1;
Thomas Gleixnerb17530b2007-10-19 20:35:02 +020089 if (!strncmp("force", str, 5))
90 hpet_force_user = 1;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080091 }
92 return 1;
93}
94__setup("hpet=", hpet_setup);
95
Thomas Gleixner28769142007-10-12 23:04:06 +020096static int __init disable_hpet(char *str)
97{
98 boot_hpet_disable = 1;
99 return 1;
100}
101__setup("nohpet", disable_hpet);
102
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800103static inline int is_hpet_capable(void)
104{
Ingo Molnar4588c1f2008-09-06 14:19:17 +0200105 return !boot_hpet_disable && hpet_address;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800106}
107
108/*
109 * HPET timer interrupt enable / disable
110 */
111static int hpet_legacy_int_enabled;
112
113/**
114 * is_hpet_enabled - check whether the hpet timer interrupt is enabled
115 */
116int is_hpet_enabled(void)
117{
118 return is_hpet_capable() && hpet_legacy_int_enabled;
119}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100120EXPORT_SYMBOL_GPL(is_hpet_enabled);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800121
122/*
123 * When the hpet driver (/dev/hpet) is enabled, we need to reserve
124 * timer 0 and timer 1 in case of RTC emulation.
125 */
126#ifdef CONFIG_HPET
Venki Pallipadif0ed4e62008-09-08 10:18:40 -0700127
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700128static void hpet_reserve_msi_timers(struct hpet_data *hd);
Venki Pallipadif0ed4e62008-09-08 10:18:40 -0700129
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800130static void hpet_reserve_platform_timers(unsigned long id)
131{
132 struct hpet __iomem *hpet = hpet_virt_address;
Balaji Rao37a47db82008-01-30 13:30:03 +0100133 struct hpet_timer __iomem *timer = &hpet->hpet_timers[2];
134 unsigned int nrtimers, i;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800135 struct hpet_data hd;
136
137 nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
138
Ingo Molnar4588c1f2008-09-06 14:19:17 +0200139 memset(&hd, 0, sizeof(hd));
140 hd.hd_phys_address = hpet_address;
141 hd.hd_address = hpet;
142 hd.hd_nirqs = nrtimers;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800143 hpet_reserve_timer(&hd, 0);
144
145#ifdef CONFIG_HPET_EMULATE_RTC
146 hpet_reserve_timer(&hd, 1);
147#endif
Thomas Gleixner5761d642008-04-04 16:26:10 +0200148
David Brownell64a76f62008-07-29 12:47:38 -0700149 /*
150 * NOTE that hd_irq[] reflects IOAPIC input pins (LEGACY_8254
151 * is wrong for i8259!) not the output IRQ. Many BIOS writers
152 * don't bother configuring *any* comparator interrupts.
153 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800154 hd.hd_irq[0] = HPET_LEGACY_8254;
155 hd.hd_irq[1] = HPET_LEGACY_RTC;
156
Ingo Molnarfc3fbc42008-04-27 14:04:14 +0200157 for (i = 2; i < nrtimers; timer++, i++) {
Ingo Molnar4588c1f2008-09-06 14:19:17 +0200158 hd.hd_irq[i] = (readl(&timer->hpet_config) &
159 Tn_INT_ROUTE_CNF_MASK) >> Tn_INT_ROUTE_CNF_SHIFT;
Ingo Molnarfc3fbc42008-04-27 14:04:14 +0200160 }
Thomas Gleixner5761d642008-04-04 16:26:10 +0200161
Venki Pallipadif0ed4e62008-09-08 10:18:40 -0700162 hpet_reserve_msi_timers(&hd);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700163
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800164 hpet_alloc(&hd);
Thomas Gleixner5761d642008-04-04 16:26:10 +0200165
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800166}
167#else
168static void hpet_reserve_platform_timers(unsigned long id) { }
169#endif
170
171/*
172 * Common hpet info
173 */
174static unsigned long hpet_period;
175
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200176static void hpet_legacy_set_mode(enum clock_event_mode mode,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800177 struct clock_event_device *evt);
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200178static int hpet_legacy_next_event(unsigned long delta,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800179 struct clock_event_device *evt);
180
181/*
182 * The hpet clock event device
183 */
184static struct clock_event_device hpet_clockevent = {
185 .name = "hpet",
186 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200187 .set_mode = hpet_legacy_set_mode,
188 .set_next_event = hpet_legacy_next_event,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800189 .shift = 32,
190 .irq = 0,
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200191 .rating = 50,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800192};
193
194static void hpet_start_counter(void)
195{
196 unsigned long cfg = hpet_readl(HPET_CFG);
197
198 cfg &= ~HPET_CFG_ENABLE;
199 hpet_writel(cfg, HPET_CFG);
200 hpet_writel(0, HPET_COUNTER);
201 hpet_writel(0, HPET_COUNTER + 4);
202 cfg |= HPET_CFG_ENABLE;
203 hpet_writel(cfg, HPET_CFG);
204}
205
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200206static void hpet_resume_device(void)
207{
Venki Pallipadibfe0c1c2007-10-12 23:04:24 +0200208 force_hpet_resume();
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200209}
210
211static void hpet_restart_counter(void)
212{
213 hpet_resume_device();
214 hpet_start_counter();
215}
216
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200217static void hpet_enable_legacy_int(void)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800218{
219 unsigned long cfg = hpet_readl(HPET_CFG);
220
221 cfg |= HPET_CFG_LEGACY;
222 hpet_writel(cfg, HPET_CFG);
223 hpet_legacy_int_enabled = 1;
224}
225
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200226static void hpet_legacy_clockevent_register(void)
227{
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200228 /* Start HPET legacy interrupts */
229 hpet_enable_legacy_int();
230
231 /*
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300232 * The mult factor is defined as (include/linux/clockchips.h)
233 * mult/2^shift = cyc/ns (in contrast to ns/cyc in clocksource.h)
234 * hpet_period is in units of femtoseconds (per cycle), so
235 * mult/2^shift = cyc/ns = 10^6/hpet_period
236 * mult = (10^6 * 2^shift)/hpet_period
237 * mult = (FSEC_PER_NSEC << hpet_clockevent.shift)/hpet_period
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200238 */
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300239 hpet_clockevent.mult = div_sc((unsigned long) FSEC_PER_NSEC,
240 hpet_period, hpet_clockevent.shift);
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200241 /* Calculate the min / max delta */
242 hpet_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF,
243 &hpet_clockevent);
Thomas Gleixner7cfb04352008-09-03 21:37:24 +0000244 /* 5 usec minimum reprogramming delta. */
245 hpet_clockevent.min_delta_ns = 5000;
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200246
247 /*
248 * Start hpet with the boot cpu mask and make it
249 * global after the IO_APIC has been initialized.
250 */
Rusty Russell320ab2b2008-12-13 21:20:26 +1030251 hpet_clockevent.cpumask = cpumask_of(smp_processor_id());
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200252 clockevents_register_device(&hpet_clockevent);
253 global_clock_event = &hpet_clockevent;
254 printk(KERN_DEBUG "hpet clockevent registered\n");
255}
256
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700257static int hpet_setup_msi_irq(unsigned int irq);
258
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700259static void hpet_set_mode(enum clock_event_mode mode,
260 struct clock_event_device *evt, int timer)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800261{
262 unsigned long cfg, cmp, now;
263 uint64_t delta;
264
Ingo Molnar4588c1f2008-09-06 14:19:17 +0200265 switch (mode) {
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800266 case CLOCK_EVT_MODE_PERIODIC:
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700267 delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * evt->mult;
268 delta >>= evt->shift;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800269 now = hpet_readl(HPET_COUNTER);
270 cmp = now + (unsigned long) delta;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700271 cfg = hpet_readl(HPET_Tn_CFG(timer));
john stultzb13e2462009-02-12 18:48:53 -0800272 /* Make sure we use edge triggered interrupts */
273 cfg &= ~HPET_TN_LEVEL;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800274 cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
275 HPET_TN_SETVAL | HPET_TN_32BIT;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700276 hpet_writel(cfg, HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800277 /*
278 * The first write after writing TN_SETVAL to the
279 * config register sets the counter value, the second
280 * write sets the period.
281 */
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700282 hpet_writel(cmp, HPET_Tn_CMP(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800283 udelay(1);
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700284 hpet_writel((unsigned long) delta, HPET_Tn_CMP(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800285 break;
286
287 case CLOCK_EVT_MODE_ONESHOT:
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700288 cfg = hpet_readl(HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800289 cfg &= ~HPET_TN_PERIODIC;
290 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700291 hpet_writel(cfg, HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800292 break;
293
294 case CLOCK_EVT_MODE_UNUSED:
295 case CLOCK_EVT_MODE_SHUTDOWN:
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700296 cfg = hpet_readl(HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800297 cfg &= ~HPET_TN_ENABLE;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700298 hpet_writel(cfg, HPET_Tn_CFG(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800299 break;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700300
301 case CLOCK_EVT_MODE_RESUME:
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700302 if (timer == 0) {
303 hpet_enable_legacy_int();
304 } else {
305 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
306 hpet_setup_msi_irq(hdev->irq);
307 disable_irq(hdev->irq);
Rusty Russell0de26522008-12-13 21:20:26 +1030308 irq_set_affinity(hdev->irq, cpumask_of(hdev->cpu));
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700309 enable_irq(hdev->irq);
310 }
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700311 break;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800312 }
313}
314
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700315static int hpet_next_event(unsigned long delta,
316 struct clock_event_device *evt, int timer)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800317{
Thomas Gleixnerf7676252008-09-06 03:03:32 +0200318 u32 cnt;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800319
320 cnt = hpet_readl(HPET_COUNTER);
Thomas Gleixnerf7676252008-09-06 03:03:32 +0200321 cnt += (u32) delta;
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700322 hpet_writel(cnt, HPET_Tn_CMP(timer));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800323
Thomas Gleixner72d43d92008-09-06 03:06:08 +0200324 /*
325 * We need to read back the CMP register to make sure that
326 * what we wrote hit the chip before we compare it to the
327 * counter.
328 */
Matt Fleming89d77a12008-11-02 16:04:20 +0000329 WARN_ON_ONCE((u32)hpet_readl(HPET_Tn_CMP(timer)) != cnt);
Thomas Gleixner72d43d92008-09-06 03:06:08 +0200330
Thomas Gleixnerf7676252008-09-06 03:03:32 +0200331 return (s32)((u32)hpet_readl(HPET_COUNTER) - cnt) >= 0 ? -ETIME : 0;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800332}
333
venkatesh.pallipadi@intel.comb40d5752008-09-05 18:02:16 -0700334static void hpet_legacy_set_mode(enum clock_event_mode mode,
335 struct clock_event_device *evt)
336{
337 hpet_set_mode(mode, evt, 0);
338}
339
340static int hpet_legacy_next_event(unsigned long delta,
341 struct clock_event_device *evt)
342{
343 return hpet_next_event(delta, evt, 0);
344}
345
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800346/*
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700347 * HPET MSI Support
348 */
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700349#ifdef CONFIG_PCI_MSI
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700350
351static DEFINE_PER_CPU(struct hpet_dev *, cpu_hpet_dev);
352static struct hpet_dev *hpet_devs;
353
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700354void hpet_msi_unmask(unsigned int irq)
355{
356 struct hpet_dev *hdev = get_irq_data(irq);
357 unsigned long cfg;
358
359 /* unmask it */
360 cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
361 cfg |= HPET_TN_FSB;
362 hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
363}
364
365void hpet_msi_mask(unsigned int irq)
366{
367 unsigned long cfg;
368 struct hpet_dev *hdev = get_irq_data(irq);
369
370 /* mask it */
371 cfg = hpet_readl(HPET_Tn_CFG(hdev->num));
372 cfg &= ~HPET_TN_FSB;
373 hpet_writel(cfg, HPET_Tn_CFG(hdev->num));
374}
375
376void hpet_msi_write(unsigned int irq, struct msi_msg *msg)
377{
378 struct hpet_dev *hdev = get_irq_data(irq);
379
380 hpet_writel(msg->data, HPET_Tn_ROUTE(hdev->num));
381 hpet_writel(msg->address_lo, HPET_Tn_ROUTE(hdev->num) + 4);
382}
383
384void hpet_msi_read(unsigned int irq, struct msi_msg *msg)
385{
386 struct hpet_dev *hdev = get_irq_data(irq);
387
388 msg->data = hpet_readl(HPET_Tn_ROUTE(hdev->num));
389 msg->address_lo = hpet_readl(HPET_Tn_ROUTE(hdev->num) + 4);
390 msg->address_hi = 0;
391}
392
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700393static void hpet_msi_set_mode(enum clock_event_mode mode,
394 struct clock_event_device *evt)
395{
396 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
397 hpet_set_mode(mode, evt, hdev->num);
398}
399
400static int hpet_msi_next_event(unsigned long delta,
401 struct clock_event_device *evt)
402{
403 struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt);
404 return hpet_next_event(delta, evt, hdev->num);
405}
406
407static int hpet_setup_msi_irq(unsigned int irq)
408{
409 if (arch_setup_hpet_msi(irq)) {
410 destroy_irq(irq);
411 return -EINVAL;
412 }
413 return 0;
414}
415
416static int hpet_assign_irq(struct hpet_dev *dev)
417{
418 unsigned int irq;
419
420 irq = create_irq();
421 if (!irq)
422 return -EINVAL;
423
424 set_irq_data(irq, dev);
425
426 if (hpet_setup_msi_irq(irq))
427 return -EINVAL;
428
429 dev->irq = irq;
430 return 0;
431}
432
433static irqreturn_t hpet_interrupt_handler(int irq, void *data)
434{
435 struct hpet_dev *dev = (struct hpet_dev *)data;
436 struct clock_event_device *hevt = &dev->evt;
437
438 if (!hevt->event_handler) {
439 printk(KERN_INFO "Spurious HPET timer interrupt on HPET timer %d\n",
440 dev->num);
441 return IRQ_HANDLED;
442 }
443
444 hevt->event_handler(hevt);
445 return IRQ_HANDLED;
446}
447
448static int hpet_setup_irq(struct hpet_dev *dev)
449{
450
451 if (request_irq(dev->irq, hpet_interrupt_handler,
Matt Fleming5ceb1a02008-11-02 22:23:13 +0000452 IRQF_DISABLED|IRQF_NOBALANCING, dev->name, dev))
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700453 return -1;
454
455 disable_irq(dev->irq);
Rusty Russell0de26522008-12-13 21:20:26 +1030456 irq_set_affinity(dev->irq, cpumask_of(dev->cpu));
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700457 enable_irq(dev->irq);
458
Yinghai Luc81bba42008-09-25 11:53:11 -0700459 printk(KERN_DEBUG "hpet: %s irq %d for MSI\n",
460 dev->name, dev->irq);
461
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700462 return 0;
463}
464
465/* This should be called in specific @cpu */
466static void init_one_hpet_msi_clockevent(struct hpet_dev *hdev, int cpu)
467{
468 struct clock_event_device *evt = &hdev->evt;
469 uint64_t hpet_freq;
470
471 WARN_ON(cpu != smp_processor_id());
472 if (!(hdev->flags & HPET_DEV_VALID))
473 return;
474
475 if (hpet_setup_msi_irq(hdev->irq))
476 return;
477
478 hdev->cpu = cpu;
479 per_cpu(cpu_hpet_dev, cpu) = hdev;
480 evt->name = hdev->name;
481 hpet_setup_irq(hdev);
482 evt->irq = hdev->irq;
483
484 evt->rating = 110;
485 evt->features = CLOCK_EVT_FEAT_ONESHOT;
486 if (hdev->flags & HPET_DEV_PERI_CAP)
487 evt->features |= CLOCK_EVT_FEAT_PERIODIC;
488
489 evt->set_mode = hpet_msi_set_mode;
490 evt->set_next_event = hpet_msi_next_event;
491 evt->shift = 32;
492
493 /*
494 * The period is a femto seconds value. We need to calculate the
495 * scaled math multiplication factor for nanosecond to hpet tick
496 * conversion.
497 */
498 hpet_freq = 1000000000000000ULL;
499 do_div(hpet_freq, hpet_period);
500 evt->mult = div_sc((unsigned long) hpet_freq,
501 NSEC_PER_SEC, evt->shift);
502 /* Calculate the max delta */
503 evt->max_delta_ns = clockevent_delta2ns(0x7FFFFFFF, evt);
504 /* 5 usec minimum reprogramming delta. */
505 evt->min_delta_ns = 5000;
506
Rusty Russell320ab2b2008-12-13 21:20:26 +1030507 evt->cpumask = cpumask_of(hdev->cpu);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700508 clockevents_register_device(evt);
509}
510
511#ifdef CONFIG_HPET
512/* Reserve at least one timer for userspace (/dev/hpet) */
513#define RESERVE_TIMERS 1
514#else
515#define RESERVE_TIMERS 0
516#endif
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700517
518static void hpet_msi_capability_lookup(unsigned int start_timer)
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700519{
520 unsigned int id;
521 unsigned int num_timers;
522 unsigned int num_timers_used = 0;
523 int i;
524
525 id = hpet_readl(HPET_ID);
526
527 num_timers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT);
528 num_timers++; /* Value read out starts from 0 */
529
530 hpet_devs = kzalloc(sizeof(struct hpet_dev) * num_timers, GFP_KERNEL);
531 if (!hpet_devs)
532 return;
533
534 hpet_num_timers = num_timers;
535
536 for (i = start_timer; i < num_timers - RESERVE_TIMERS; i++) {
537 struct hpet_dev *hdev = &hpet_devs[num_timers_used];
538 unsigned long cfg = hpet_readl(HPET_Tn_CFG(i));
539
540 /* Only consider HPET timer with MSI support */
541 if (!(cfg & HPET_TN_FSB_CAP))
542 continue;
543
544 hdev->flags = 0;
545 if (cfg & HPET_TN_PERIODIC_CAP)
546 hdev->flags |= HPET_DEV_PERI_CAP;
547 hdev->num = i;
548
549 sprintf(hdev->name, "hpet%d", i);
550 if (hpet_assign_irq(hdev))
551 continue;
552
553 hdev->flags |= HPET_DEV_FSB_CAP;
554 hdev->flags |= HPET_DEV_VALID;
555 num_timers_used++;
556 if (num_timers_used == num_possible_cpus())
557 break;
558 }
559
560 printk(KERN_INFO "HPET: %d timers in total, %d timers will be used for per-cpu timer\n",
561 num_timers, num_timers_used);
562}
563
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700564#ifdef CONFIG_HPET
565static void hpet_reserve_msi_timers(struct hpet_data *hd)
566{
567 int i;
568
569 if (!hpet_devs)
570 return;
571
572 for (i = 0; i < hpet_num_timers; i++) {
573 struct hpet_dev *hdev = &hpet_devs[i];
574
575 if (!(hdev->flags & HPET_DEV_VALID))
576 continue;
577
578 hd->hd_irq[hdev->num] = hdev->irq;
579 hpet_reserve_timer(hd, hdev->num);
580 }
581}
582#endif
583
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700584static struct hpet_dev *hpet_get_unused_timer(void)
585{
586 int i;
587
588 if (!hpet_devs)
589 return NULL;
590
591 for (i = 0; i < hpet_num_timers; i++) {
592 struct hpet_dev *hdev = &hpet_devs[i];
593
594 if (!(hdev->flags & HPET_DEV_VALID))
595 continue;
596 if (test_and_set_bit(HPET_DEV_USED_BIT,
597 (unsigned long *)&hdev->flags))
598 continue;
599 return hdev;
600 }
601 return NULL;
602}
603
604struct hpet_work_struct {
605 struct delayed_work work;
606 struct completion complete;
607};
608
609static void hpet_work(struct work_struct *w)
610{
611 struct hpet_dev *hdev;
612 int cpu = smp_processor_id();
613 struct hpet_work_struct *hpet_work;
614
615 hpet_work = container_of(w, struct hpet_work_struct, work.work);
616
617 hdev = hpet_get_unused_timer();
618 if (hdev)
619 init_one_hpet_msi_clockevent(hdev, cpu);
620
621 complete(&hpet_work->complete);
622}
623
624static int hpet_cpuhp_notify(struct notifier_block *n,
625 unsigned long action, void *hcpu)
626{
627 unsigned long cpu = (unsigned long)hcpu;
628 struct hpet_work_struct work;
629 struct hpet_dev *hdev = per_cpu(cpu_hpet_dev, cpu);
630
631 switch (action & 0xf) {
632 case CPU_ONLINE:
Thomas Gleixner336f6c32009-01-22 09:50:44 +0100633 INIT_DELAYED_WORK_ON_STACK(&work.work, hpet_work);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700634 init_completion(&work.complete);
635 /* FIXME: add schedule_work_on() */
636 schedule_delayed_work_on(cpu, &work.work, 0);
637 wait_for_completion(&work.complete);
Thomas Gleixner336f6c32009-01-22 09:50:44 +0100638 destroy_timer_on_stack(&work.work.timer);
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700639 break;
640 case CPU_DEAD:
641 if (hdev) {
642 free_irq(hdev->irq, hdev);
643 hdev->flags &= ~HPET_DEV_USED;
644 per_cpu(cpu_hpet_dev, cpu) = NULL;
645 }
646 break;
647 }
648 return NOTIFY_OK;
649}
650#else
651
Steven Noonanba374c92008-09-08 16:19:09 -0700652static int hpet_setup_msi_irq(unsigned int irq)
653{
654 return 0;
655}
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700656static void hpet_msi_capability_lookup(unsigned int start_timer)
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700657{
658 return;
659}
660
Venki Pallipadi5f79f2f2008-09-24 10:03:17 -0700661#ifdef CONFIG_HPET
662static void hpet_reserve_msi_timers(struct hpet_data *hd)
663{
664 return;
665}
666#endif
667
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700668static int hpet_cpuhp_notify(struct notifier_block *n,
669 unsigned long action, void *hcpu)
670{
671 return NOTIFY_OK;
672}
673
674#endif
675
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -0700676/*
john stultz6bb74df2007-03-05 00:30:50 -0800677 * Clock source related code
678 */
679static cycle_t read_hpet(void)
680{
681 return (cycle_t)hpet_readl(HPET_COUNTER);
682}
683
Thomas Gleixner28769142007-10-12 23:04:06 +0200684#ifdef CONFIG_X86_64
685static cycle_t __vsyscall_fn vread_hpet(void)
686{
687 return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
688}
689#endif
690
john stultz6bb74df2007-03-05 00:30:50 -0800691static struct clocksource clocksource_hpet = {
692 .name = "hpet",
693 .rating = 250,
694 .read = read_hpet,
695 .mask = HPET_MASK,
696 .shift = HPET_SHIFT,
697 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200698 .resume = hpet_restart_counter,
Thomas Gleixner28769142007-10-12 23:04:06 +0200699#ifdef CONFIG_X86_64
700 .vread = vread_hpet,
701#endif
john stultz6bb74df2007-03-05 00:30:50 -0800702};
703
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200704static int hpet_clocksource_register(void)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800705{
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300706 u64 start, now;
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200707 cycle_t t1;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800708
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800709 /* Start the counter */
710 hpet_start_counter();
711
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200712 /* Verify whether hpet counter works */
713 t1 = read_hpet();
714 rdtscll(start);
715
716 /*
717 * We don't know the TSC frequency yet, but waiting for
718 * 200000 TSC cycles is safe:
719 * 4 GHz == 50us
720 * 1 GHz == 200us
721 */
722 do {
723 rep_nop();
724 rdtscll(now);
725 } while ((now - start) < 200000UL);
726
727 if (t1 == read_hpet()) {
728 printk(KERN_WARNING
729 "HPET counter not counting. HPET disabled\n");
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200730 return -ENODEV;
Thomas Gleixner075bcd12007-07-21 17:11:12 +0200731 }
732
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300733 /*
734 * The definition of mult is (include/linux/clocksource.h)
735 * mult/2^shift = ns/cyc and hpet_period is in units of fsec/cyc
736 * so we first need to convert hpet_period to ns/cyc units:
737 * mult/2^shift = ns/cyc = hpet_period/10^6
738 * mult = (hpet_period * 2^shift)/10^6
739 * mult = (hpet_period << shift)/FSEC_PER_NSEC
john stultz6bb74df2007-03-05 00:30:50 -0800740 */
Carlos R. Mafra6fd592d2008-05-05 20:11:22 -0300741 clocksource_hpet.mult = div_sc(hpet_period, FSEC_PER_NSEC, HPET_SHIFT);
john stultz6bb74df2007-03-05 00:30:50 -0800742
743 clocksource_register(&clocksource_hpet);
744
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200745 return 0;
746}
747
Pavel Machekb02a7f22008-02-05 00:48:13 +0100748/**
749 * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200750 */
751int __init hpet_enable(void)
752{
753 unsigned long id;
Thomas Gleixnera6825f12008-08-14 12:17:06 +0200754 int i;
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200755
756 if (!is_hpet_capable())
757 return 0;
758
759 hpet_set_mapping();
760
761 /*
762 * Read the period and check for a sane value:
763 */
764 hpet_period = hpet_readl(HPET_PERIOD);
Thomas Gleixnera6825f12008-08-14 12:17:06 +0200765
766 /*
767 * AMD SB700 based systems with spread spectrum enabled use a
768 * SMM based HPET emulation to provide proper frequency
769 * setting. The SMM code is initialized with the first HPET
770 * register access and takes some time to complete. During
771 * this time the config register reads 0xffffffff. We check
772 * for max. 1000 loops whether the config register reads a non
773 * 0xffffffff value to make sure that HPET is up and running
774 * before we go further. A counting loop is safe, as the HPET
775 * access takes thousands of CPU cycles. On non SB700 based
776 * machines this check is only done once and has no side
777 * effects.
778 */
779 for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) {
780 if (i == 1000) {
781 printk(KERN_WARNING
782 "HPET config register value = 0xFFFFFFFF. "
783 "Disabling HPET\n");
784 goto out_nohpet;
785 }
786 }
787
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200788 if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
789 goto out_nohpet;
790
791 /*
792 * Read the HPET ID register to retrieve the IRQ routing
793 * information and the number of channels
794 */
795 id = hpet_readl(HPET_ID);
796
797#ifdef CONFIG_HPET_EMULATE_RTC
798 /*
799 * The legacy routing mode needs at least two channels, tick timer
800 * and the rtc emulation channel.
801 */
802 if (!(id & HPET_ID_NUMBER))
803 goto out_nohpet;
804#endif
805
806 if (hpet_clocksource_register())
807 goto out_nohpet;
808
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800809 if (id & HPET_ID_LEGSUP) {
Venki Pallipadi610bf2f2007-10-12 23:04:23 +0200810 hpet_legacy_clockevent_register();
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700811 hpet_msi_capability_lookup(2);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800812 return 1;
813 }
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700814 hpet_msi_capability_lookup(0);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800815 return 0;
816
817out_nohpet:
Thomas Gleixner06a24de2007-10-12 23:04:06 +0200818 hpet_clear_mapping();
Janne Kulmalabacbe992008-12-16 13:39:57 +0200819 hpet_address = 0;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800820 return 0;
821}
822
Thomas Gleixner28769142007-10-12 23:04:06 +0200823/*
824 * Needs to be late, as the reserve_timer code calls kalloc !
825 *
826 * Not a problem on i386 as hpet_enable is called from late_time_init,
827 * but on x86_64 it is necessary !
828 */
829static __init int hpet_late_init(void)
830{
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700831 int cpu;
832
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200833 if (boot_hpet_disable)
Thomas Gleixner28769142007-10-12 23:04:06 +0200834 return -ENODEV;
835
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200836 if (!hpet_address) {
837 if (!force_hpet_address)
838 return -ENODEV;
839
840 hpet_address = force_hpet_address;
841 hpet_enable();
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200842 }
843
Jeremy Fitzhardinge39c04b52008-12-16 12:32:23 -0800844 if (!hpet_virt_address)
845 return -ENODEV;
846
Thomas Gleixner28769142007-10-12 23:04:06 +0200847 hpet_reserve_platform_timers(hpet_readl(HPET_ID));
Venki Pallipadi59c69f22007-10-12 23:04:23 +0200848
venkatesh.pallipadi@intel.com26afe5f2008-09-05 18:02:18 -0700849 for_each_online_cpu(cpu) {
850 hpet_cpuhp_notify(NULL, CPU_ONLINE, (void *)(long)cpu);
851 }
852
853 /* This notifier should be called after workqueue is ready */
854 hotcpu_notifier(hpet_cpuhp_notify, -20);
855
Thomas Gleixner28769142007-10-12 23:04:06 +0200856 return 0;
857}
858fs_initcall(hpet_late_init);
859
OGAWA Hirofumic86c7fb2007-12-03 17:17:10 +0100860void hpet_disable(void)
861{
862 if (is_hpet_capable()) {
863 unsigned long cfg = hpet_readl(HPET_CFG);
864
865 if (hpet_legacy_int_enabled) {
866 cfg &= ~HPET_CFG_LEGACY;
867 hpet_legacy_int_enabled = 0;
868 }
869 cfg &= ~HPET_CFG_ENABLE;
870 hpet_writel(cfg, HPET_CFG);
871 }
872}
873
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800874#ifdef CONFIG_HPET_EMULATE_RTC
875
876/* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
877 * is enabled, we support RTC interrupt functionality in software.
878 * RTC has 3 kinds of interrupts:
879 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
880 * is updated
881 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
882 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
883 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
884 * (1) and (2) above are implemented using polling at a frequency of
885 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
886 * overhead. (DEFAULT_RTC_INT_FREQ)
887 * For (3), we use interrupts at 64Hz or user specified periodic
888 * frequency, whichever is higher.
889 */
890#include <linux/mc146818rtc.h>
891#include <linux/rtc.h>
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100892#include <asm/rtc.h>
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800893
894#define DEFAULT_RTC_INT_FREQ 64
895#define DEFAULT_RTC_SHIFT 6
896#define RTC_NUM_INTS 1
897
898static unsigned long hpet_rtc_flags;
David Brownell7e2a31d2008-07-23 21:30:47 -0700899static int hpet_prev_update_sec;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800900static struct rtc_time hpet_alarm_time;
901static unsigned long hpet_pie_count;
902static unsigned long hpet_t1_cmp;
903static unsigned long hpet_default_delta;
904static unsigned long hpet_pie_delta;
905static unsigned long hpet_pie_limit;
906
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100907static rtc_irq_handler irq_handler;
908
909/*
910 * Registers a IRQ handler.
911 */
912int hpet_register_irq_handler(rtc_irq_handler handler)
913{
914 if (!is_hpet_enabled())
915 return -ENODEV;
916 if (irq_handler)
917 return -EBUSY;
918
919 irq_handler = handler;
920
921 return 0;
922}
923EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
924
925/*
926 * Deregisters the IRQ handler registered with hpet_register_irq_handler()
927 * and does cleanup.
928 */
929void hpet_unregister_irq_handler(rtc_irq_handler handler)
930{
931 if (!is_hpet_enabled())
932 return;
933
934 irq_handler = NULL;
935 hpet_rtc_flags = 0;
936}
937EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
938
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800939/*
940 * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
941 * is not supported by all HPET implementations for timer 1.
942 *
943 * hpet_rtc_timer_init() is called when the rtc is initialized.
944 */
945int hpet_rtc_timer_init(void)
946{
947 unsigned long cfg, cnt, delta, flags;
948
949 if (!is_hpet_enabled())
950 return 0;
951
952 if (!hpet_default_delta) {
953 uint64_t clc;
954
955 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
956 clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
957 hpet_default_delta = (unsigned long) clc;
958 }
959
960 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
961 delta = hpet_default_delta;
962 else
963 delta = hpet_pie_delta;
964
965 local_irq_save(flags);
966
967 cnt = delta + hpet_readl(HPET_COUNTER);
968 hpet_writel(cnt, HPET_T1_CMP);
969 hpet_t1_cmp = cnt;
970
971 cfg = hpet_readl(HPET_T1_CFG);
972 cfg &= ~HPET_TN_PERIODIC;
973 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
974 hpet_writel(cfg, HPET_T1_CFG);
975
976 local_irq_restore(flags);
977
978 return 1;
979}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100980EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800981
982/*
983 * The functions below are called from rtc driver.
984 * Return 0 if HPET is not being used.
985 * Otherwise do the necessary changes and return 1.
986 */
987int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
988{
989 if (!is_hpet_enabled())
990 return 0;
991
992 hpet_rtc_flags &= ~bit_mask;
993 return 1;
994}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +0100995EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800996
997int hpet_set_rtc_irq_bit(unsigned long bit_mask)
998{
999 unsigned long oldbits = hpet_rtc_flags;
1000
1001 if (!is_hpet_enabled())
1002 return 0;
1003
1004 hpet_rtc_flags |= bit_mask;
1005
David Brownell7e2a31d2008-07-23 21:30:47 -07001006 if ((bit_mask & RTC_UIE) && !(oldbits & RTC_UIE))
1007 hpet_prev_update_sec = -1;
1008
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001009 if (!oldbits)
1010 hpet_rtc_timer_init();
1011
1012 return 1;
1013}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001014EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001015
1016int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
1017 unsigned char sec)
1018{
1019 if (!is_hpet_enabled())
1020 return 0;
1021
1022 hpet_alarm_time.tm_hour = hrs;
1023 hpet_alarm_time.tm_min = min;
1024 hpet_alarm_time.tm_sec = sec;
1025
1026 return 1;
1027}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001028EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001029
1030int hpet_set_periodic_freq(unsigned long freq)
1031{
1032 uint64_t clc;
1033
1034 if (!is_hpet_enabled())
1035 return 0;
1036
1037 if (freq <= DEFAULT_RTC_INT_FREQ)
1038 hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
1039 else {
1040 clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
1041 do_div(clc, freq);
1042 clc >>= hpet_clockevent.shift;
1043 hpet_pie_delta = (unsigned long) clc;
1044 }
1045 return 1;
1046}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001047EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001048
1049int hpet_rtc_dropped_irq(void)
1050{
1051 return is_hpet_enabled();
1052}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001053EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001054
1055static void hpet_rtc_timer_reinit(void)
1056{
1057 unsigned long cfg, delta;
1058 int lost_ints = -1;
1059
1060 if (unlikely(!hpet_rtc_flags)) {
1061 cfg = hpet_readl(HPET_T1_CFG);
1062 cfg &= ~HPET_TN_ENABLE;
1063 hpet_writel(cfg, HPET_T1_CFG);
1064 return;
1065 }
1066
1067 if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
1068 delta = hpet_default_delta;
1069 else
1070 delta = hpet_pie_delta;
1071
1072 /*
1073 * Increment the comparator value until we are ahead of the
1074 * current count.
1075 */
1076 do {
1077 hpet_t1_cmp += delta;
1078 hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
1079 lost_ints++;
1080 } while ((long)(hpet_readl(HPET_COUNTER) - hpet_t1_cmp) > 0);
1081
1082 if (lost_ints) {
1083 if (hpet_rtc_flags & RTC_PIE)
1084 hpet_pie_count += lost_ints;
1085 if (printk_ratelimit())
David Brownell7e2a31d2008-07-23 21:30:47 -07001086 printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n",
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001087 lost_ints);
1088 }
1089}
1090
1091irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
1092{
1093 struct rtc_time curr_time;
1094 unsigned long rtc_int_flag = 0;
1095
1096 hpet_rtc_timer_reinit();
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001097 memset(&curr_time, 0, sizeof(struct rtc_time));
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001098
1099 if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001100 get_rtc_time(&curr_time);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001101
1102 if (hpet_rtc_flags & RTC_UIE &&
1103 curr_time.tm_sec != hpet_prev_update_sec) {
David Brownell7e2a31d2008-07-23 21:30:47 -07001104 if (hpet_prev_update_sec >= 0)
1105 rtc_int_flag = RTC_UF;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001106 hpet_prev_update_sec = curr_time.tm_sec;
1107 }
1108
1109 if (hpet_rtc_flags & RTC_PIE &&
1110 ++hpet_pie_count >= hpet_pie_limit) {
1111 rtc_int_flag |= RTC_PF;
1112 hpet_pie_count = 0;
1113 }
1114
Bernhard Walle8ee291f2008-01-15 16:44:38 +01001115 if (hpet_rtc_flags & RTC_AIE &&
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001116 (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
1117 (curr_time.tm_min == hpet_alarm_time.tm_min) &&
1118 (curr_time.tm_hour == hpet_alarm_time.tm_hour))
1119 rtc_int_flag |= RTC_AF;
1120
1121 if (rtc_int_flag) {
1122 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001123 if (irq_handler)
1124 irq_handler(rtc_int_flag, dev_id);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001125 }
1126 return IRQ_HANDLED;
1127}
Bernhard Walle1bdbdaa2008-01-30 13:33:28 +01001128EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001129#endif