Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Intel & MS High Precision Event Timer Implementation. |
| 3 | * |
| 4 | * Copyright (C) 2003 Intel Corporation |
| 5 | * Venki Pallipadi |
| 6 | * (c) Copyright 2004 Hewlett-Packard Development Company, L.P. |
| 7 | * Bob Picco <robert.picco@hp.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/kernel.h> |
Arnd Bergmann | 48b8188 | 2008-05-20 19:15:59 +0200 | [diff] [blame] | 17 | #include <linux/smp_lock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/types.h> |
| 19 | #include <linux/miscdevice.h> |
| 20 | #include <linux/major.h> |
| 21 | #include <linux/ioport.h> |
| 22 | #include <linux/fcntl.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/poll.h> |
Al Viro | f23f6e0 | 2006-10-20 15:17:02 -0400 | [diff] [blame] | 25 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/proc_fs.h> |
| 27 | #include <linux/spinlock.h> |
| 28 | #include <linux/sysctl.h> |
| 29 | #include <linux/wait.h> |
| 30 | #include <linux/bcd.h> |
| 31 | #include <linux/seq_file.h> |
| 32 | #include <linux/bitops.h> |
Tony Luck | 0aa366f | 2007-07-20 11:22:30 -0700 | [diff] [blame] | 33 | #include <linux/clocksource.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | #include <asm/current.h> |
| 36 | #include <asm/uaccess.h> |
| 37 | #include <asm/system.h> |
| 38 | #include <asm/io.h> |
| 39 | #include <asm/irq.h> |
| 40 | #include <asm/div64.h> |
| 41 | |
| 42 | #include <linux/acpi.h> |
| 43 | #include <acpi/acpi_bus.h> |
| 44 | #include <linux/hpet.h> |
| 45 | |
| 46 | /* |
| 47 | * The High Precision Event Timer driver. |
| 48 | * This driver is closely modelled after the rtc.c driver. |
Alex Williamson | 9680382 | 2005-09-06 15:17:54 -0700 | [diff] [blame] | 49 | * http://www.intel.com/hardwaredesign/hpetspec.htm |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | */ |
| 51 | #define HPET_USER_FREQ (64) |
| 52 | #define HPET_DRIFT (500) |
| 53 | |
Randy Dunlap | 757c472 | 2005-10-30 15:03:42 -0800 | [diff] [blame] | 54 | #define HPET_RANGE_SIZE 1024 /* from HPET spec */ |
| 55 | |
Tony Luck | 0aa366f | 2007-07-20 11:22:30 -0700 | [diff] [blame] | 56 | #if BITS_PER_LONG == 64 |
| 57 | #define write_counter(V, MC) writeq(V, MC) |
| 58 | #define read_counter(MC) readq(MC) |
| 59 | #else |
| 60 | #define write_counter(V, MC) writel(V, MC) |
| 61 | #define read_counter(MC) readl(MC) |
| 62 | #endif |
| 63 | |
Clemens Ladisch | 642d30b | 2005-10-30 15:03:31 -0800 | [diff] [blame] | 64 | static u32 hpet_nhpet, hpet_max_freq = HPET_USER_FREQ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
S.Çağlar Onur | 3dffec4 | 2007-09-26 12:15:33 +0300 | [diff] [blame] | 66 | /* This clocksource driver currently only works on ia64 */ |
| 67 | #ifdef CONFIG_IA64 |
Tony Luck | 0aa366f | 2007-07-20 11:22:30 -0700 | [diff] [blame] | 68 | static void __iomem *hpet_mctr; |
| 69 | |
| 70 | static cycle_t read_hpet(void) |
| 71 | { |
| 72 | return (cycle_t)read_counter((void __iomem *)hpet_mctr); |
| 73 | } |
| 74 | |
| 75 | static struct clocksource clocksource_hpet = { |
| 76 | .name = "hpet", |
| 77 | .rating = 250, |
| 78 | .read = read_hpet, |
Al Viro | 712aaa1 | 2007-07-26 17:34:49 +0100 | [diff] [blame] | 79 | .mask = CLOCKSOURCE_MASK(64), |
Tony Luck | 0aa366f | 2007-07-20 11:22:30 -0700 | [diff] [blame] | 80 | .mult = 0, /*to be caluclated*/ |
| 81 | .shift = 10, |
| 82 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 83 | }; |
| 84 | static struct clocksource *hpet_clocksource; |
S.Çağlar Onur | 3dffec4 | 2007-09-26 12:15:33 +0300 | [diff] [blame] | 85 | #endif |
Tony Luck | 0aa366f | 2007-07-20 11:22:30 -0700 | [diff] [blame] | 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | /* A lock for concurrent access by app and isr hpet activity. */ |
| 88 | static DEFINE_SPINLOCK(hpet_lock); |
| 89 | /* A lock for concurrent intermodule access to hpet and isr hpet activity. */ |
| 90 | static DEFINE_SPINLOCK(hpet_task_lock); |
| 91 | |
| 92 | #define HPET_DEV_NAME (7) |
| 93 | |
| 94 | struct hpet_dev { |
| 95 | struct hpets *hd_hpets; |
| 96 | struct hpet __iomem *hd_hpet; |
| 97 | struct hpet_timer __iomem *hd_timer; |
| 98 | unsigned long hd_ireqfreq; |
| 99 | unsigned long hd_irqdata; |
| 100 | wait_queue_head_t hd_waitqueue; |
| 101 | struct fasync_struct *hd_async_queue; |
| 102 | struct hpet_task *hd_task; |
| 103 | unsigned int hd_flags; |
| 104 | unsigned int hd_irq; |
| 105 | unsigned int hd_hdwirq; |
| 106 | char hd_name[HPET_DEV_NAME]; |
| 107 | }; |
| 108 | |
| 109 | struct hpets { |
| 110 | struct hpets *hp_next; |
| 111 | struct hpet __iomem *hp_hpet; |
| 112 | unsigned long hp_hpet_phys; |
Tony Luck | 0aa366f | 2007-07-20 11:22:30 -0700 | [diff] [blame] | 113 | struct clocksource *hp_clocksource; |
Clemens Ladisch | ba3f213 | 2005-10-30 15:03:31 -0800 | [diff] [blame] | 114 | unsigned long long hp_tick_freq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | unsigned long hp_delta; |
| 116 | unsigned int hp_ntimer; |
| 117 | unsigned int hp_which; |
| 118 | struct hpet_dev hp_dev[1]; |
| 119 | }; |
| 120 | |
| 121 | static struct hpets *hpets; |
| 122 | |
| 123 | #define HPET_OPEN 0x0001 |
| 124 | #define HPET_IE 0x0002 /* interrupt enabled */ |
| 125 | #define HPET_PERIODIC 0x0004 |
Clemens Ladisch | 0d29086 | 2005-10-30 15:03:34 -0800 | [diff] [blame] | 126 | #define HPET_SHARED_IRQ 0x0008 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
| 129 | #ifndef readq |
Adrian Bunk | 887c27f | 2005-09-10 00:26:52 -0700 | [diff] [blame] | 130 | static inline unsigned long long readq(void __iomem *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
| 132 | return readl(addr) | (((unsigned long long)readl(addr + 4)) << 32LL); |
| 133 | } |
| 134 | #endif |
| 135 | |
| 136 | #ifndef writeq |
Adrian Bunk | 887c27f | 2005-09-10 00:26:52 -0700 | [diff] [blame] | 137 | static inline void writeq(unsigned long long v, void __iomem *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
| 139 | writel(v & 0xffffffff, addr); |
| 140 | writel(v >> 32, addr + 4); |
| 141 | } |
| 142 | #endif |
| 143 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 144 | static irqreturn_t hpet_interrupt(int irq, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | { |
| 146 | struct hpet_dev *devp; |
| 147 | unsigned long isr; |
| 148 | |
| 149 | devp = data; |
Clemens Ladisch | 0d29086 | 2005-10-30 15:03:34 -0800 | [diff] [blame] | 150 | isr = 1 << (devp - devp->hd_hpets->hp_dev); |
| 151 | |
| 152 | if ((devp->hd_flags & HPET_SHARED_IRQ) && |
| 153 | !(isr & readl(&devp->hd_hpet->hpet_isr))) |
| 154 | return IRQ_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
| 156 | spin_lock(&hpet_lock); |
| 157 | devp->hd_irqdata++; |
| 158 | |
| 159 | /* |
| 160 | * For non-periodic timers, increment the accumulator. |
| 161 | * This has the effect of treating non-periodic like periodic. |
| 162 | */ |
| 163 | if ((devp->hd_flags & (HPET_IE | HPET_PERIODIC)) == HPET_IE) { |
| 164 | unsigned long m, t; |
| 165 | |
| 166 | t = devp->hd_ireqfreq; |
| 167 | m = read_counter(&devp->hd_hpet->hpet_mc); |
| 168 | write_counter(t + m + devp->hd_hpets->hp_delta, |
| 169 | &devp->hd_timer->hpet_compare); |
| 170 | } |
| 171 | |
Clemens Ladisch | 0d29086 | 2005-10-30 15:03:34 -0800 | [diff] [blame] | 172 | if (devp->hd_flags & HPET_SHARED_IRQ) |
| 173 | writel(isr, &devp->hd_hpet->hpet_isr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | spin_unlock(&hpet_lock); |
| 175 | |
| 176 | spin_lock(&hpet_task_lock); |
| 177 | if (devp->hd_task) |
| 178 | devp->hd_task->ht_func(devp->hd_task->ht_data); |
| 179 | spin_unlock(&hpet_task_lock); |
| 180 | |
| 181 | wake_up_interruptible(&devp->hd_waitqueue); |
| 182 | |
| 183 | kill_fasync(&devp->hd_async_queue, SIGIO, POLL_IN); |
| 184 | |
| 185 | return IRQ_HANDLED; |
| 186 | } |
| 187 | |
| 188 | static int hpet_open(struct inode *inode, struct file *file) |
| 189 | { |
| 190 | struct hpet_dev *devp; |
| 191 | struct hpets *hpetp; |
| 192 | int i; |
| 193 | |
| 194 | if (file->f_mode & FMODE_WRITE) |
| 195 | return -EINVAL; |
| 196 | |
Arnd Bergmann | 48b8188 | 2008-05-20 19:15:59 +0200 | [diff] [blame] | 197 | lock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | spin_lock_irq(&hpet_lock); |
| 199 | |
| 200 | for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next) |
| 201 | for (i = 0; i < hpetp->hp_ntimer; i++) |
| 202 | if (hpetp->hp_dev[i].hd_flags & HPET_OPEN |
| 203 | || hpetp->hp_dev[i].hd_task) |
| 204 | continue; |
| 205 | else { |
| 206 | devp = &hpetp->hp_dev[i]; |
| 207 | break; |
| 208 | } |
| 209 | |
| 210 | if (!devp) { |
| 211 | spin_unlock_irq(&hpet_lock); |
Arnd Bergmann | 48b8188 | 2008-05-20 19:15:59 +0200 | [diff] [blame] | 212 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | return -EBUSY; |
| 214 | } |
| 215 | |
| 216 | file->private_data = devp; |
| 217 | devp->hd_irqdata = 0; |
| 218 | devp->hd_flags |= HPET_OPEN; |
| 219 | spin_unlock_irq(&hpet_lock); |
Arnd Bergmann | 48b8188 | 2008-05-20 19:15:59 +0200 | [diff] [blame] | 220 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static ssize_t |
| 226 | hpet_read(struct file *file, char __user *buf, size_t count, loff_t * ppos) |
| 227 | { |
| 228 | DECLARE_WAITQUEUE(wait, current); |
| 229 | unsigned long data; |
| 230 | ssize_t retval; |
| 231 | struct hpet_dev *devp; |
| 232 | |
| 233 | devp = file->private_data; |
| 234 | if (!devp->hd_ireqfreq) |
| 235 | return -EIO; |
| 236 | |
| 237 | if (count < sizeof(unsigned long)) |
| 238 | return -EINVAL; |
| 239 | |
| 240 | add_wait_queue(&devp->hd_waitqueue, &wait); |
| 241 | |
| 242 | for ( ; ; ) { |
| 243 | set_current_state(TASK_INTERRUPTIBLE); |
| 244 | |
| 245 | spin_lock_irq(&hpet_lock); |
| 246 | data = devp->hd_irqdata; |
| 247 | devp->hd_irqdata = 0; |
| 248 | spin_unlock_irq(&hpet_lock); |
| 249 | |
| 250 | if (data) |
| 251 | break; |
| 252 | else if (file->f_flags & O_NONBLOCK) { |
| 253 | retval = -EAGAIN; |
| 254 | goto out; |
| 255 | } else if (signal_pending(current)) { |
| 256 | retval = -ERESTARTSYS; |
| 257 | goto out; |
| 258 | } |
| 259 | schedule(); |
| 260 | } |
| 261 | |
| 262 | retval = put_user(data, (unsigned long __user *)buf); |
| 263 | if (!retval) |
| 264 | retval = sizeof(unsigned long); |
| 265 | out: |
| 266 | __set_current_state(TASK_RUNNING); |
| 267 | remove_wait_queue(&devp->hd_waitqueue, &wait); |
| 268 | |
| 269 | return retval; |
| 270 | } |
| 271 | |
| 272 | static unsigned int hpet_poll(struct file *file, poll_table * wait) |
| 273 | { |
| 274 | unsigned long v; |
| 275 | struct hpet_dev *devp; |
| 276 | |
| 277 | devp = file->private_data; |
| 278 | |
| 279 | if (!devp->hd_ireqfreq) |
| 280 | return 0; |
| 281 | |
| 282 | poll_wait(file, &devp->hd_waitqueue, wait); |
| 283 | |
| 284 | spin_lock_irq(&hpet_lock); |
| 285 | v = devp->hd_irqdata; |
| 286 | spin_unlock_irq(&hpet_lock); |
| 287 | |
| 288 | if (v != 0) |
| 289 | return POLLIN | POLLRDNORM; |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static int hpet_mmap(struct file *file, struct vm_area_struct *vma) |
| 295 | { |
| 296 | #ifdef CONFIG_HPET_MMAP |
| 297 | struct hpet_dev *devp; |
| 298 | unsigned long addr; |
| 299 | |
| 300 | if (((vma->vm_end - vma->vm_start) != PAGE_SIZE) || vma->vm_pgoff) |
| 301 | return -EINVAL; |
| 302 | |
| 303 | devp = file->private_data; |
| 304 | addr = devp->hd_hpets->hp_hpet_phys; |
| 305 | |
| 306 | if (addr & (PAGE_SIZE - 1)) |
| 307 | return -ENOSYS; |
| 308 | |
| 309 | vma->vm_flags |= VM_IO; |
| 310 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
| 312 | if (io_remap_pfn_range(vma, vma->vm_start, addr >> PAGE_SHIFT, |
| 313 | PAGE_SIZE, vma->vm_page_prot)) { |
Randy Dunlap | 3e6716e | 2005-10-30 15:03:44 -0800 | [diff] [blame] | 314 | printk(KERN_ERR "%s: io_remap_pfn_range failed\n", |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 315 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | return -EAGAIN; |
| 317 | } |
| 318 | |
| 319 | return 0; |
| 320 | #else |
| 321 | return -ENOSYS; |
| 322 | #endif |
| 323 | } |
| 324 | |
| 325 | static int hpet_fasync(int fd, struct file *file, int on) |
| 326 | { |
| 327 | struct hpet_dev *devp; |
| 328 | |
| 329 | devp = file->private_data; |
| 330 | |
| 331 | if (fasync_helper(fd, file, on, &devp->hd_async_queue) >= 0) |
| 332 | return 0; |
| 333 | else |
| 334 | return -EIO; |
| 335 | } |
| 336 | |
| 337 | static int hpet_release(struct inode *inode, struct file *file) |
| 338 | { |
| 339 | struct hpet_dev *devp; |
| 340 | struct hpet_timer __iomem *timer; |
| 341 | int irq = 0; |
| 342 | |
| 343 | devp = file->private_data; |
| 344 | timer = devp->hd_timer; |
| 345 | |
| 346 | spin_lock_irq(&hpet_lock); |
| 347 | |
| 348 | writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK), |
| 349 | &timer->hpet_config); |
| 350 | |
| 351 | irq = devp->hd_irq; |
| 352 | devp->hd_irq = 0; |
| 353 | |
| 354 | devp->hd_ireqfreq = 0; |
| 355 | |
| 356 | if (devp->hd_flags & HPET_PERIODIC |
| 357 | && readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) { |
| 358 | unsigned long v; |
| 359 | |
| 360 | v = readq(&timer->hpet_config); |
| 361 | v ^= Tn_TYPE_CNF_MASK; |
| 362 | writeq(v, &timer->hpet_config); |
| 363 | } |
| 364 | |
| 365 | devp->hd_flags &= ~(HPET_OPEN | HPET_IE | HPET_PERIODIC); |
| 366 | spin_unlock_irq(&hpet_lock); |
| 367 | |
| 368 | if (irq) |
| 369 | free_irq(irq, devp); |
| 370 | |
| 371 | if (file->f_flags & FASYNC) |
| 372 | hpet_fasync(-1, file, 0); |
| 373 | |
| 374 | file->private_data = NULL; |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | static int hpet_ioctl_common(struct hpet_dev *, int, unsigned long, int); |
| 379 | |
| 380 | static int |
| 381 | hpet_ioctl(struct inode *inode, struct file *file, unsigned int cmd, |
| 382 | unsigned long arg) |
| 383 | { |
| 384 | struct hpet_dev *devp; |
| 385 | |
| 386 | devp = file->private_data; |
| 387 | return hpet_ioctl_common(devp, cmd, arg, 0); |
| 388 | } |
| 389 | |
| 390 | static int hpet_ioctl_ieon(struct hpet_dev *devp) |
| 391 | { |
| 392 | struct hpet_timer __iomem *timer; |
| 393 | struct hpet __iomem *hpet; |
| 394 | struct hpets *hpetp; |
| 395 | int irq; |
| 396 | unsigned long g, v, t, m; |
| 397 | unsigned long flags, isr; |
| 398 | |
| 399 | timer = devp->hd_timer; |
| 400 | hpet = devp->hd_hpet; |
| 401 | hpetp = devp->hd_hpets; |
| 402 | |
Clemens Ladisch | 9090e6d | 2005-10-30 15:03:29 -0800 | [diff] [blame] | 403 | if (!devp->hd_ireqfreq) |
| 404 | return -EIO; |
| 405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | spin_lock_irq(&hpet_lock); |
| 407 | |
| 408 | if (devp->hd_flags & HPET_IE) { |
| 409 | spin_unlock_irq(&hpet_lock); |
| 410 | return -EBUSY; |
| 411 | } |
| 412 | |
| 413 | devp->hd_flags |= HPET_IE; |
Clemens Ladisch | 0d29086 | 2005-10-30 15:03:34 -0800 | [diff] [blame] | 414 | |
| 415 | if (readl(&timer->hpet_config) & Tn_INT_TYPE_CNF_MASK) |
| 416 | devp->hd_flags |= HPET_SHARED_IRQ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | spin_unlock_irq(&hpet_lock); |
| 418 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | irq = devp->hd_hdwirq; |
| 420 | |
| 421 | if (irq) { |
Clemens Ladisch | 0d29086 | 2005-10-30 15:03:34 -0800 | [diff] [blame] | 422 | unsigned long irq_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
Clemens Ladisch | 0d29086 | 2005-10-30 15:03:34 -0800 | [diff] [blame] | 424 | sprintf(devp->hd_name, "hpet%d", (int)(devp - hpetp->hp_dev)); |
| 425 | irq_flags = devp->hd_flags & HPET_SHARED_IRQ |
Thomas Gleixner | 0f2ed4c | 2006-07-01 19:29:33 -0700 | [diff] [blame] | 426 | ? IRQF_SHARED : IRQF_DISABLED; |
Clemens Ladisch | 0d29086 | 2005-10-30 15:03:34 -0800 | [diff] [blame] | 427 | if (request_irq(irq, hpet_interrupt, irq_flags, |
| 428 | devp->hd_name, (void *)devp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | printk(KERN_ERR "hpet: IRQ %d is not free\n", irq); |
| 430 | irq = 0; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | if (irq == 0) { |
| 435 | spin_lock_irq(&hpet_lock); |
| 436 | devp->hd_flags ^= HPET_IE; |
| 437 | spin_unlock_irq(&hpet_lock); |
| 438 | return -EIO; |
| 439 | } |
| 440 | |
| 441 | devp->hd_irq = irq; |
| 442 | t = devp->hd_ireqfreq; |
| 443 | v = readq(&timer->hpet_config); |
| 444 | g = v | Tn_INT_ENB_CNF_MASK; |
| 445 | |
| 446 | if (devp->hd_flags & HPET_PERIODIC) { |
| 447 | write_counter(t, &timer->hpet_compare); |
| 448 | g |= Tn_TYPE_CNF_MASK; |
| 449 | v |= Tn_TYPE_CNF_MASK; |
| 450 | writeq(v, &timer->hpet_config); |
| 451 | v |= Tn_VAL_SET_CNF_MASK; |
| 452 | writeq(v, &timer->hpet_config); |
| 453 | local_irq_save(flags); |
| 454 | m = read_counter(&hpet->hpet_mc); |
| 455 | write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare); |
| 456 | } else { |
| 457 | local_irq_save(flags); |
| 458 | m = read_counter(&hpet->hpet_mc); |
| 459 | write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare); |
| 460 | } |
| 461 | |
Clemens Ladisch | 0d29086 | 2005-10-30 15:03:34 -0800 | [diff] [blame] | 462 | if (devp->hd_flags & HPET_SHARED_IRQ) { |
Clemens Ladisch | 3d5640d | 2005-10-30 15:03:39 -0800 | [diff] [blame] | 463 | isr = 1 << (devp - devp->hd_hpets->hp_dev); |
Clemens Ladisch | 0d29086 | 2005-10-30 15:03:34 -0800 | [diff] [blame] | 464 | writel(isr, &hpet->hpet_isr); |
| 465 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | writeq(g, &timer->hpet_config); |
| 467 | local_irq_restore(flags); |
| 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | |
Clemens Ladisch | ba3f213 | 2005-10-30 15:03:31 -0800 | [diff] [blame] | 472 | /* converts Hz to number of timer ticks */ |
| 473 | static inline unsigned long hpet_time_div(struct hpets *hpets, |
| 474 | unsigned long dis) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | { |
Clemens Ladisch | ba3f213 | 2005-10-30 15:03:31 -0800 | [diff] [blame] | 476 | unsigned long long m; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | |
Clemens Ladisch | ba3f213 | 2005-10-30 15:03:31 -0800 | [diff] [blame] | 478 | m = hpets->hp_tick_freq + (dis >> 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | do_div(m, dis); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | return (unsigned long)m; |
| 481 | } |
| 482 | |
| 483 | static int |
| 484 | hpet_ioctl_common(struct hpet_dev *devp, int cmd, unsigned long arg, int kernel) |
| 485 | { |
| 486 | struct hpet_timer __iomem *timer; |
| 487 | struct hpet __iomem *hpet; |
| 488 | struct hpets *hpetp; |
| 489 | int err; |
| 490 | unsigned long v; |
| 491 | |
| 492 | switch (cmd) { |
| 493 | case HPET_IE_OFF: |
| 494 | case HPET_INFO: |
| 495 | case HPET_EPI: |
| 496 | case HPET_DPI: |
| 497 | case HPET_IRQFREQ: |
| 498 | timer = devp->hd_timer; |
| 499 | hpet = devp->hd_hpet; |
| 500 | hpetp = devp->hd_hpets; |
| 501 | break; |
| 502 | case HPET_IE_ON: |
| 503 | return hpet_ioctl_ieon(devp); |
| 504 | default: |
| 505 | return -EINVAL; |
| 506 | } |
| 507 | |
| 508 | err = 0; |
| 509 | |
| 510 | switch (cmd) { |
| 511 | case HPET_IE_OFF: |
| 512 | if ((devp->hd_flags & HPET_IE) == 0) |
| 513 | break; |
| 514 | v = readq(&timer->hpet_config); |
| 515 | v &= ~Tn_INT_ENB_CNF_MASK; |
| 516 | writeq(v, &timer->hpet_config); |
| 517 | if (devp->hd_irq) { |
| 518 | free_irq(devp->hd_irq, devp); |
| 519 | devp->hd_irq = 0; |
| 520 | } |
| 521 | devp->hd_flags ^= HPET_IE; |
| 522 | break; |
| 523 | case HPET_INFO: |
| 524 | { |
| 525 | struct hpet_info info; |
| 526 | |
Clemens Ladisch | af95ead | 2005-10-30 15:03:38 -0800 | [diff] [blame] | 527 | if (devp->hd_ireqfreq) |
| 528 | info.hi_ireqfreq = |
| 529 | hpet_time_div(hpetp, devp->hd_ireqfreq); |
| 530 | else |
| 531 | info.hi_ireqfreq = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | info.hi_flags = |
| 533 | readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK; |
Clemens Ladisch | c860ed9 | 2005-10-30 15:03:40 -0800 | [diff] [blame] | 534 | info.hi_hpet = hpetp->hp_which; |
| 535 | info.hi_timer = devp - hpetp->hp_dev; |
Clemens Ladisch | 8e8505b | 2005-10-30 15:03:37 -0800 | [diff] [blame] | 536 | if (kernel) |
| 537 | memcpy((void *)arg, &info, sizeof(info)); |
| 538 | else |
| 539 | if (copy_to_user((void __user *)arg, &info, |
| 540 | sizeof(info))) |
| 541 | err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | break; |
| 543 | } |
| 544 | case HPET_EPI: |
| 545 | v = readq(&timer->hpet_config); |
| 546 | if ((v & Tn_PER_INT_CAP_MASK) == 0) { |
| 547 | err = -ENXIO; |
| 548 | break; |
| 549 | } |
| 550 | devp->hd_flags |= HPET_PERIODIC; |
| 551 | break; |
| 552 | case HPET_DPI: |
| 553 | v = readq(&timer->hpet_config); |
| 554 | if ((v & Tn_PER_INT_CAP_MASK) == 0) { |
| 555 | err = -ENXIO; |
| 556 | break; |
| 557 | } |
| 558 | if (devp->hd_flags & HPET_PERIODIC && |
| 559 | readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) { |
| 560 | v = readq(&timer->hpet_config); |
| 561 | v ^= Tn_TYPE_CNF_MASK; |
| 562 | writeq(v, &timer->hpet_config); |
| 563 | } |
| 564 | devp->hd_flags &= ~HPET_PERIODIC; |
| 565 | break; |
| 566 | case HPET_IRQFREQ: |
| 567 | if (!kernel && (arg > hpet_max_freq) && |
| 568 | !capable(CAP_SYS_RESOURCE)) { |
| 569 | err = -EACCES; |
| 570 | break; |
| 571 | } |
| 572 | |
Clemens Ladisch | 189e2dd | 2005-10-30 15:03:33 -0800 | [diff] [blame] | 573 | if (!arg) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | err = -EINVAL; |
| 575 | break; |
| 576 | } |
| 577 | |
Clemens Ladisch | ba3f213 | 2005-10-30 15:03:31 -0800 | [diff] [blame] | 578 | devp->hd_ireqfreq = hpet_time_div(hpetp, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | return err; |
| 582 | } |
| 583 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 584 | static const struct file_operations hpet_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | .owner = THIS_MODULE, |
| 586 | .llseek = no_llseek, |
| 587 | .read = hpet_read, |
| 588 | .poll = hpet_poll, |
| 589 | .ioctl = hpet_ioctl, |
| 590 | .open = hpet_open, |
| 591 | .release = hpet_release, |
| 592 | .fasync = hpet_fasync, |
| 593 | .mmap = hpet_mmap, |
| 594 | }; |
| 595 | |
Randy Dunlap | 3e6716e | 2005-10-30 15:03:44 -0800 | [diff] [blame] | 596 | static int hpet_is_known(struct hpet_data *hdp) |
| 597 | { |
| 598 | struct hpets *hpetp; |
| 599 | |
| 600 | for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next) |
| 601 | if (hpetp->hp_hpet_phys == hdp->hd_phys_address) |
| 602 | return 1; |
| 603 | |
| 604 | return 0; |
| 605 | } |
| 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | static inline int hpet_tpcheck(struct hpet_task *tp) |
| 608 | { |
| 609 | struct hpet_dev *devp; |
| 610 | struct hpets *hpetp; |
| 611 | |
| 612 | devp = tp->ht_opaque; |
| 613 | |
| 614 | if (!devp) |
| 615 | return -ENXIO; |
| 616 | |
| 617 | for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next) |
| 618 | if (devp >= hpetp->hp_dev |
| 619 | && devp < (hpetp->hp_dev + hpetp->hp_ntimer) |
| 620 | && devp->hd_hpet == hpetp->hp_hpet) |
| 621 | return 0; |
| 622 | |
| 623 | return -ENXIO; |
| 624 | } |
| 625 | |
Adrian Bunk | 372572e | 2008-07-25 01:48:11 -0700 | [diff] [blame] | 626 | #if 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | int hpet_unregister(struct hpet_task *tp) |
| 628 | { |
| 629 | struct hpet_dev *devp; |
| 630 | struct hpet_timer __iomem *timer; |
| 631 | int err; |
| 632 | |
| 633 | if ((err = hpet_tpcheck(tp))) |
| 634 | return err; |
| 635 | |
| 636 | spin_lock_irq(&hpet_task_lock); |
| 637 | spin_lock(&hpet_lock); |
| 638 | |
| 639 | devp = tp->ht_opaque; |
| 640 | if (devp->hd_task != tp) { |
| 641 | spin_unlock(&hpet_lock); |
| 642 | spin_unlock_irq(&hpet_task_lock); |
| 643 | return -ENXIO; |
| 644 | } |
| 645 | |
| 646 | timer = devp->hd_timer; |
| 647 | writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK), |
| 648 | &timer->hpet_config); |
| 649 | devp->hd_flags &= ~(HPET_IE | HPET_PERIODIC); |
| 650 | devp->hd_task = NULL; |
| 651 | spin_unlock(&hpet_lock); |
| 652 | spin_unlock_irq(&hpet_task_lock); |
| 653 | |
| 654 | return 0; |
| 655 | } |
Adrian Bunk | 372572e | 2008-07-25 01:48:11 -0700 | [diff] [blame] | 656 | #endif /* 0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | static ctl_table hpet_table[] = { |
| 659 | { |
Eric W. Biederman | 2294336 | 2007-02-14 00:33:51 -0800 | [diff] [blame] | 660 | .ctl_name = CTL_UNNUMBERED, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | .procname = "max-user-freq", |
| 662 | .data = &hpet_max_freq, |
| 663 | .maxlen = sizeof(int), |
| 664 | .mode = 0644, |
| 665 | .proc_handler = &proc_dointvec, |
| 666 | }, |
| 667 | {.ctl_name = 0} |
| 668 | }; |
| 669 | |
| 670 | static ctl_table hpet_root[] = { |
| 671 | { |
Eric W. Biederman | 2294336 | 2007-02-14 00:33:51 -0800 | [diff] [blame] | 672 | .ctl_name = CTL_UNNUMBERED, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | .procname = "hpet", |
| 674 | .maxlen = 0, |
| 675 | .mode = 0555, |
| 676 | .child = hpet_table, |
| 677 | }, |
| 678 | {.ctl_name = 0} |
| 679 | }; |
| 680 | |
| 681 | static ctl_table dev_root[] = { |
| 682 | { |
| 683 | .ctl_name = CTL_DEV, |
| 684 | .procname = "dev", |
| 685 | .maxlen = 0, |
| 686 | .mode = 0555, |
| 687 | .child = hpet_root, |
| 688 | }, |
| 689 | {.ctl_name = 0} |
| 690 | }; |
| 691 | |
| 692 | static struct ctl_table_header *sysctl_header; |
| 693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | /* |
| 695 | * Adjustment for when arming the timer with |
| 696 | * initial conditions. That is, main counter |
| 697 | * ticks expired before interrupts are enabled. |
| 698 | */ |
| 699 | #define TICK_CALIBRATE (1000UL) |
| 700 | |
| 701 | static unsigned long hpet_calibrate(struct hpets *hpetp) |
| 702 | { |
| 703 | struct hpet_timer __iomem *timer = NULL; |
| 704 | unsigned long t, m, count, i, flags, start; |
| 705 | struct hpet_dev *devp; |
| 706 | int j; |
| 707 | struct hpet __iomem *hpet; |
| 708 | |
| 709 | for (j = 0, devp = hpetp->hp_dev; j < hpetp->hp_ntimer; j++, devp++) |
| 710 | if ((devp->hd_flags & HPET_OPEN) == 0) { |
| 711 | timer = devp->hd_timer; |
| 712 | break; |
| 713 | } |
| 714 | |
| 715 | if (!timer) |
| 716 | return 0; |
| 717 | |
Clemens Ladisch | 3d5640d | 2005-10-30 15:03:39 -0800 | [diff] [blame] | 718 | hpet = hpetp->hp_hpet; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | t = read_counter(&timer->hpet_compare); |
| 720 | |
| 721 | i = 0; |
Clemens Ladisch | ba3f213 | 2005-10-30 15:03:31 -0800 | [diff] [blame] | 722 | count = hpet_time_div(hpetp, TICK_CALIBRATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | |
| 724 | local_irq_save(flags); |
| 725 | |
| 726 | start = read_counter(&hpet->hpet_mc); |
| 727 | |
| 728 | do { |
| 729 | m = read_counter(&hpet->hpet_mc); |
| 730 | write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare); |
| 731 | } while (i++, (m - start) < count); |
| 732 | |
| 733 | local_irq_restore(flags); |
| 734 | |
| 735 | return (m - start) / i; |
| 736 | } |
| 737 | |
| 738 | int hpet_alloc(struct hpet_data *hdp) |
| 739 | { |
Thomas Gleixner | 5761d64 | 2008-04-04 16:26:10 +0200 | [diff] [blame] | 740 | u64 cap, mcfg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | struct hpet_dev *devp; |
Thomas Gleixner | 5761d64 | 2008-04-04 16:26:10 +0200 | [diff] [blame] | 742 | u32 i, ntimer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | struct hpets *hpetp; |
| 744 | size_t siz; |
| 745 | struct hpet __iomem *hpet; |
Randy Dunlap | 3e6716e | 2005-10-30 15:03:44 -0800 | [diff] [blame] | 746 | static struct hpets *last = NULL; |
Thomas Gleixner | 5761d64 | 2008-04-04 16:26:10 +0200 | [diff] [blame] | 747 | unsigned long period; |
Clemens Ladisch | ba3f213 | 2005-10-30 15:03:31 -0800 | [diff] [blame] | 748 | unsigned long long temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | |
| 750 | /* |
| 751 | * hpet_alloc can be called by platform dependent code. |
Randy Dunlap | 3e6716e | 2005-10-30 15:03:44 -0800 | [diff] [blame] | 752 | * If platform dependent code has allocated the hpet that |
| 753 | * ACPI has also reported, then we catch it here. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | */ |
Randy Dunlap | 3e6716e | 2005-10-30 15:03:44 -0800 | [diff] [blame] | 755 | if (hpet_is_known(hdp)) { |
| 756 | printk(KERN_DEBUG "%s: duplicate HPET ignored\n", |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 757 | __func__); |
Randy Dunlap | 3e6716e | 2005-10-30 15:03:44 -0800 | [diff] [blame] | 758 | return 0; |
| 759 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | |
| 761 | siz = sizeof(struct hpets) + ((hdp->hd_nirqs - 1) * |
| 762 | sizeof(struct hpet_dev)); |
| 763 | |
Randy Dunlap | 3e6716e | 2005-10-30 15:03:44 -0800 | [diff] [blame] | 764 | hpetp = kzalloc(siz, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | |
| 766 | if (!hpetp) |
| 767 | return -ENOMEM; |
| 768 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | hpetp->hp_which = hpet_nhpet++; |
| 770 | hpetp->hp_hpet = hdp->hd_address; |
| 771 | hpetp->hp_hpet_phys = hdp->hd_phys_address; |
| 772 | |
| 773 | hpetp->hp_ntimer = hdp->hd_nirqs; |
Thomas Gleixner | 5761d64 | 2008-04-04 16:26:10 +0200 | [diff] [blame] | 774 | |
| 775 | for (i = 0; i < hdp->hd_nirqs; i++) |
| 776 | hpetp->hp_dev[i].hd_hdwirq = hdp->hd_irq[i]; |
| 777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | hpet = hpetp->hp_hpet; |
| 779 | |
| 780 | cap = readq(&hpet->hpet_cap); |
| 781 | |
| 782 | ntimer = ((cap & HPET_NUM_TIM_CAP_MASK) >> HPET_NUM_TIM_CAP_SHIFT) + 1; |
| 783 | |
| 784 | if (hpetp->hp_ntimer != ntimer) { |
| 785 | printk(KERN_WARNING "hpet: number irqs doesn't agree" |
| 786 | " with number of timers\n"); |
| 787 | kfree(hpetp); |
| 788 | return -ENODEV; |
| 789 | } |
| 790 | |
| 791 | if (last) |
| 792 | last->hp_next = hpetp; |
| 793 | else |
| 794 | hpets = hpetp; |
| 795 | |
| 796 | last = hpetp; |
| 797 | |
Clemens Ladisch | ba3f213 | 2005-10-30 15:03:31 -0800 | [diff] [blame] | 798 | period = (cap & HPET_COUNTER_CLK_PERIOD_MASK) >> |
| 799 | HPET_COUNTER_CLK_PERIOD_SHIFT; /* fs, 10^-15 */ |
| 800 | temp = 1000000000000000uLL; /* 10^15 femtoseconds per second */ |
| 801 | temp += period >> 1; /* round */ |
| 802 | do_div(temp, period); |
| 803 | hpetp->hp_tick_freq = temp; /* ticks per second */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
Andi Kleen | 3034d11 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 805 | printk(KERN_INFO "hpet%d: at MMIO 0x%lx, IRQ%s", |
| 806 | hpetp->hp_which, hdp->hd_phys_address, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | hpetp->hp_ntimer > 1 ? "s" : ""); |
| 808 | for (i = 0; i < hpetp->hp_ntimer; i++) |
Thomas Gleixner | 5761d64 | 2008-04-04 16:26:10 +0200 | [diff] [blame] | 809 | printk("%s %d", i > 0 ? "," : "", hdp->hd_irq[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | printk("\n"); |
| 811 | |
Clemens Ladisch | 318db8f | 2005-10-30 15:03:41 -0800 | [diff] [blame] | 812 | printk(KERN_INFO "hpet%u: %u %d-bit timers, %Lu Hz\n", |
| 813 | hpetp->hp_which, hpetp->hp_ntimer, |
| 814 | cap & HPET_COUNTER_SIZE_MASK ? 64 : 32, hpetp->hp_tick_freq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | |
| 816 | mcfg = readq(&hpet->hpet_config); |
| 817 | if ((mcfg & HPET_ENABLE_CNF_MASK) == 0) { |
| 818 | write_counter(0L, &hpet->hpet_mc); |
| 819 | mcfg |= HPET_ENABLE_CNF_MASK; |
| 820 | writeq(mcfg, &hpet->hpet_config); |
| 821 | } |
| 822 | |
Clemens Ladisch | 642d30b | 2005-10-30 15:03:31 -0800 | [diff] [blame] | 823 | for (i = 0, devp = hpetp->hp_dev; i < hpetp->hp_ntimer; i++, devp++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | struct hpet_timer __iomem *timer; |
| 825 | |
| 826 | timer = &hpet->hpet_timers[devp - hpetp->hp_dev]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | |
| 828 | devp->hd_hpets = hpetp; |
| 829 | devp->hd_hpet = hpet; |
| 830 | devp->hd_timer = timer; |
| 831 | |
| 832 | /* |
| 833 | * If the timer was reserved by platform code, |
| 834 | * then make timer unavailable for opens. |
| 835 | */ |
| 836 | if (hdp->hd_state & (1 << i)) { |
| 837 | devp->hd_flags = HPET_OPEN; |
| 838 | continue; |
| 839 | } |
| 840 | |
| 841 | init_waitqueue_head(&devp->hd_waitqueue); |
| 842 | } |
| 843 | |
| 844 | hpetp->hp_delta = hpet_calibrate(hpetp); |
Tony Luck | 0aa366f | 2007-07-20 11:22:30 -0700 | [diff] [blame] | 845 | |
Linus Torvalds | 3b2b64f | 2007-08-31 20:13:57 -0700 | [diff] [blame] | 846 | /* This clocksource driver currently only works on ia64 */ |
| 847 | #ifdef CONFIG_IA64 |
Tony Luck | 0aa366f | 2007-07-20 11:22:30 -0700 | [diff] [blame] | 848 | if (!hpet_clocksource) { |
| 849 | hpet_mctr = (void __iomem *)&hpetp->hp_hpet->hpet_mc; |
| 850 | CLKSRC_FSYS_MMIO_SET(clocksource_hpet.fsys_mmio, hpet_mctr); |
| 851 | clocksource_hpet.mult = clocksource_hz2mult(hpetp->hp_tick_freq, |
| 852 | clocksource_hpet.shift); |
| 853 | clocksource_register(&clocksource_hpet); |
| 854 | hpetp->hp_clocksource = &clocksource_hpet; |
| 855 | hpet_clocksource = &clocksource_hpet; |
| 856 | } |
Linus Torvalds | 3b2b64f | 2007-08-31 20:13:57 -0700 | [diff] [blame] | 857 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | static acpi_status hpet_resources(struct acpi_resource *res, void *data) |
| 863 | { |
| 864 | struct hpet_data *hdp; |
| 865 | acpi_status status; |
| 866 | struct acpi_resource_address64 addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | |
| 868 | hdp = data; |
| 869 | |
| 870 | status = acpi_resource_to_address64(res, &addr); |
| 871 | |
| 872 | if (ACPI_SUCCESS(status)) { |
Bob Moore | 50eca3e | 2005-09-30 19:03:00 -0400 | [diff] [blame] | 873 | hdp->hd_phys_address = addr.minimum; |
Bjorn Helgaas | 9224a86 | 2006-03-28 17:04:00 -0500 | [diff] [blame] | 874 | hdp->hd_address = ioremap(addr.minimum, addr.address_length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | |
Randy Dunlap | 3e6716e | 2005-10-30 15:03:44 -0800 | [diff] [blame] | 876 | if (hpet_is_known(hdp)) { |
| 877 | printk(KERN_DEBUG "%s: 0x%lx is busy\n", |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 878 | __func__, hdp->hd_phys_address); |
Randy Dunlap | 3e6716e | 2005-10-30 15:03:44 -0800 | [diff] [blame] | 879 | iounmap(hdp->hd_address); |
Zhao Yakui | 78e1ca4 | 2007-09-20 21:23:13 -0400 | [diff] [blame] | 880 | return AE_ALREADY_EXISTS; |
Randy Dunlap | 3e6716e | 2005-10-30 15:03:44 -0800 | [diff] [blame] | 881 | } |
Bob Moore | 50eca3e | 2005-09-30 19:03:00 -0400 | [diff] [blame] | 882 | } else if (res->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32) { |
| 883 | struct acpi_resource_fixed_memory32 *fixmem32; |
Randy Dunlap | 757c472 | 2005-10-30 15:03:42 -0800 | [diff] [blame] | 884 | |
| 885 | fixmem32 = &res->data.fixed_memory32; |
| 886 | if (!fixmem32) |
Zhao Yakui | 78e1ca4 | 2007-09-20 21:23:13 -0400 | [diff] [blame] | 887 | return AE_NO_MEMORY; |
Randy Dunlap | 757c472 | 2005-10-30 15:03:42 -0800 | [diff] [blame] | 888 | |
Bob Moore | 50eca3e | 2005-09-30 19:03:00 -0400 | [diff] [blame] | 889 | hdp->hd_phys_address = fixmem32->address; |
| 890 | hdp->hd_address = ioremap(fixmem32->address, |
Randy Dunlap | 757c472 | 2005-10-30 15:03:42 -0800 | [diff] [blame] | 891 | HPET_RANGE_SIZE); |
| 892 | |
Randy Dunlap | 3e6716e | 2005-10-30 15:03:44 -0800 | [diff] [blame] | 893 | if (hpet_is_known(hdp)) { |
| 894 | printk(KERN_DEBUG "%s: 0x%lx is busy\n", |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 895 | __func__, hdp->hd_phys_address); |
Randy Dunlap | 3e6716e | 2005-10-30 15:03:44 -0800 | [diff] [blame] | 896 | iounmap(hdp->hd_address); |
Zhao Yakui | 78e1ca4 | 2007-09-20 21:23:13 -0400 | [diff] [blame] | 897 | return AE_ALREADY_EXISTS; |
Randy Dunlap | 3e6716e | 2005-10-30 15:03:44 -0800 | [diff] [blame] | 898 | } |
Bob Moore | 50eca3e | 2005-09-30 19:03:00 -0400 | [diff] [blame] | 899 | } else if (res->type == ACPI_RESOURCE_TYPE_EXTENDED_IRQ) { |
| 900 | struct acpi_resource_extended_irq *irqp; |
Bjorn Helgaas | be5efff | 2006-02-14 13:53:02 -0800 | [diff] [blame] | 901 | int i, irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | |
| 903 | irqp = &res->data.extended_irq; |
| 904 | |
Bjorn Helgaas | be5efff | 2006-02-14 13:53:02 -0800 | [diff] [blame] | 905 | for (i = 0; i < irqp->interrupt_count; i++) { |
| 906 | irq = acpi_register_gsi(irqp->interrupts[i], |
| 907 | irqp->triggering, irqp->polarity); |
| 908 | if (irq < 0) |
| 909 | return AE_ERROR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | |
Bjorn Helgaas | be5efff | 2006-02-14 13:53:02 -0800 | [diff] [blame] | 911 | hdp->hd_irq[hdp->hd_nirqs] = irq; |
| 912 | hdp->hd_nirqs++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | |
| 916 | return AE_OK; |
| 917 | } |
| 918 | |
| 919 | static int hpet_acpi_add(struct acpi_device *device) |
| 920 | { |
| 921 | acpi_status result; |
| 922 | struct hpet_data data; |
| 923 | |
| 924 | memset(&data, 0, sizeof(data)); |
| 925 | |
| 926 | result = |
| 927 | acpi_walk_resources(device->handle, METHOD_NAME__CRS, |
| 928 | hpet_resources, &data); |
| 929 | |
| 930 | if (ACPI_FAILURE(result)) |
| 931 | return -ENODEV; |
| 932 | |
| 933 | if (!data.hd_address || !data.hd_nirqs) { |
Harvey Harrison | bf9d892 | 2008-04-30 00:55:10 -0700 | [diff] [blame] | 934 | printk("%s: no address or irqs in _CRS\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | return -ENODEV; |
| 936 | } |
| 937 | |
| 938 | return hpet_alloc(&data); |
| 939 | } |
| 940 | |
| 941 | static int hpet_acpi_remove(struct acpi_device *device, int type) |
| 942 | { |
Tony Luck | 0aa366f | 2007-07-20 11:22:30 -0700 | [diff] [blame] | 943 | /* XXX need to unregister clocksource, dealloc mem, etc */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | return -EINVAL; |
| 945 | } |
| 946 | |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 947 | static const struct acpi_device_id hpet_device_ids[] = { |
| 948 | {"PNP0103", 0}, |
| 949 | {"", 0}, |
| 950 | }; |
| 951 | MODULE_DEVICE_TABLE(acpi, hpet_device_ids); |
| 952 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | static struct acpi_driver hpet_acpi_driver = { |
| 954 | .name = "hpet", |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 955 | .ids = hpet_device_ids, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | .ops = { |
| 957 | .add = hpet_acpi_add, |
| 958 | .remove = hpet_acpi_remove, |
| 959 | }, |
| 960 | }; |
| 961 | |
| 962 | static struct miscdevice hpet_misc = { HPET_MINOR, "hpet", &hpet_fops }; |
| 963 | |
| 964 | static int __init hpet_init(void) |
| 965 | { |
| 966 | int result; |
| 967 | |
| 968 | result = misc_register(&hpet_misc); |
| 969 | if (result < 0) |
| 970 | return -ENODEV; |
| 971 | |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 972 | sysctl_header = register_sysctl_table(dev_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | |
| 974 | result = acpi_bus_register_driver(&hpet_acpi_driver); |
| 975 | if (result < 0) { |
| 976 | if (sysctl_header) |
| 977 | unregister_sysctl_table(sysctl_header); |
| 978 | misc_deregister(&hpet_misc); |
| 979 | return result; |
| 980 | } |
| 981 | |
| 982 | return 0; |
| 983 | } |
| 984 | |
| 985 | static void __exit hpet_exit(void) |
| 986 | { |
| 987 | acpi_bus_unregister_driver(&hpet_acpi_driver); |
| 988 | |
| 989 | if (sysctl_header) |
| 990 | unregister_sysctl_table(sysctl_header); |
| 991 | misc_deregister(&hpet_misc); |
| 992 | |
| 993 | return; |
| 994 | } |
| 995 | |
| 996 | module_init(hpet_init); |
| 997 | module_exit(hpet_exit); |
| 998 | MODULE_AUTHOR("Bob Picco <Robert.Picco@hp.com>"); |
| 999 | MODULE_LICENSE("GPL"); |