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