initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / char / hpet.c
blob22d46cdac3e2acf3923d6edca0ea0bb9279d0db8
1 /*
2 * Intel & MS High Precision Event Timer Implementation.
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>
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.
14 #include <linux/config.h>
15 #include <linux/interrupt.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
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>
25 #include <linux/proc_fs.h>
26 #include <linux/spinlock.h>
27 #include <linux/sysctl.h>
28 #include <linux/wait.h>
29 #include <linux/bcd.h>
30 #include <linux/seq_file.h>
32 #include <asm/current.h>
33 #include <asm/uaccess.h>
34 #include <asm/system.h>
35 #include <asm/io.h>
36 #include <asm/irq.h>
37 #include <asm/bitops.h>
38 #include <asm/div64.h>
40 #include <linux/acpi.h>
41 #include <acpi/acpi_bus.h>
42 #include <linux/hpet.h>
45 * The High Precision Event Timer driver.
46 * This driver is closely modelled after the rtc.c driver.
47 * http://www.intel.com/labs/platcomp/hpet/hpetspec.htm
49 #define HPET_USER_FREQ (64)
50 #define HPET_DRIFT (500)
52 static u32 hpet_ntimer, hpet_nhpet, hpet_max_freq = HPET_USER_FREQ;
54 /* A lock for concurrent access by app and isr hpet activity. */
55 static spinlock_t hpet_lock = SPIN_LOCK_UNLOCKED;
56 /* A lock for concurrent intermodule access to hpet and isr hpet activity. */
57 static spinlock_t hpet_task_lock = SPIN_LOCK_UNLOCKED;
59 #define HPET_DEV_NAME (7)
61 struct hpet_dev {
62 struct hpets *hd_hpets;
63 struct hpet __iomem *hd_hpet;
64 struct hpet_timer __iomem *hd_timer;
65 unsigned long hd_ireqfreq;
66 unsigned long hd_irqdata;
67 wait_queue_head_t hd_waitqueue;
68 struct fasync_struct *hd_async_queue;
69 struct hpet_task *hd_task;
70 unsigned int hd_flags;
71 unsigned int hd_irq;
72 unsigned int hd_hdwirq;
73 char hd_name[HPET_DEV_NAME];
76 struct hpets {
77 struct hpets *hp_next;
78 struct hpet __iomem *hp_hpet;
79 unsigned long hp_period;
80 unsigned long hp_delta;
81 unsigned int hp_ntimer;
82 unsigned int hp_which;
83 struct hpet_dev hp_dev[1];
86 static struct hpets *hpets;
88 #define HPET_OPEN 0x0001
89 #define HPET_IE 0x0002 /* interrupt enabled */
90 #define HPET_PERIODIC 0x0004
92 #if BITS_PER_LONG == 64
93 #define write_counter(V, MC) writeq(V, MC)
94 #define read_counter(MC) readq(MC)
95 #else
96 #define write_counter(V, MC) writel(V, MC)
97 #define read_counter(MC) readl(MC)
98 #endif
100 #ifndef readq
101 static unsigned long long __inline readq(void __iomem *addr)
103 return readl(addr) | (((unsigned long long)readl(addr + 4)) << 32LL);
105 #endif
107 #ifndef writeq
108 static void __inline writeq(unsigned long long v, void __iomem *addr)
110 writel(v & 0xffffffff, addr);
111 writel(v >> 32, addr + 4);
113 #endif
115 static irqreturn_t hpet_interrupt(int irq, void *data, struct pt_regs *regs)
117 struct hpet_dev *devp;
118 unsigned long isr;
120 devp = data;
122 spin_lock(&hpet_lock);
123 devp->hd_irqdata++;
126 * For non-periodic timers, increment the accumulator.
127 * This has the effect of treating non-periodic like periodic.
129 if ((devp->hd_flags & (HPET_IE | HPET_PERIODIC)) == HPET_IE) {
130 unsigned long m, t;
132 t = devp->hd_ireqfreq;
133 m = read_counter(&devp->hd_hpet->hpet_mc);
134 write_counter(t + m + devp->hd_hpets->hp_delta,
135 &devp->hd_timer->hpet_compare);
138 isr = (1 << (devp - devp->hd_hpets->hp_dev));
139 writeq(isr, &devp->hd_hpet->hpet_isr);
140 spin_unlock(&hpet_lock);
142 spin_lock(&hpet_task_lock);
143 if (devp->hd_task)
144 devp->hd_task->ht_func(devp->hd_task->ht_data);
145 spin_unlock(&hpet_task_lock);
147 wake_up_interruptible(&devp->hd_waitqueue);
149 kill_fasync(&devp->hd_async_queue, SIGIO, POLL_IN);
151 return IRQ_HANDLED;
154 static int hpet_open(struct inode *inode, struct file *file)
156 struct hpet_dev *devp;
157 struct hpets *hpetp;
158 int i;
160 if (file->f_mode & FMODE_WRITE)
161 return -EINVAL;
163 spin_lock_irq(&hpet_lock);
165 for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next)
166 for (i = 0; i < hpetp->hp_ntimer; i++)
167 if (hpetp->hp_dev[i].hd_flags & HPET_OPEN
168 || hpetp->hp_dev[i].hd_task)
169 continue;
170 else {
171 devp = &hpetp->hp_dev[i];
172 break;
175 if (!devp) {
176 spin_unlock_irq(&hpet_lock);
177 return -EBUSY;
180 file->private_data = devp;
181 devp->hd_irqdata = 0;
182 devp->hd_flags |= HPET_OPEN;
183 spin_unlock_irq(&hpet_lock);
185 return 0;
188 static ssize_t
189 hpet_read(struct file *file, char __user *buf, size_t count, loff_t * ppos)
191 DECLARE_WAITQUEUE(wait, current);
192 unsigned long data;
193 ssize_t retval;
194 struct hpet_dev *devp;
196 devp = file->private_data;
197 if (!devp->hd_ireqfreq)
198 return -EIO;
200 if (count < sizeof(unsigned long))
201 return -EINVAL;
203 add_wait_queue(&devp->hd_waitqueue, &wait);
205 for ( ; ; ) {
206 set_current_state(TASK_INTERRUPTIBLE);
208 spin_lock_irq(&hpet_lock);
209 data = devp->hd_irqdata;
210 devp->hd_irqdata = 0;
211 spin_unlock_irq(&hpet_lock);
213 if (data)
214 break;
215 else if (file->f_flags & O_NONBLOCK) {
216 retval = -EAGAIN;
217 goto out;
218 } else if (signal_pending(current)) {
219 retval = -ERESTARTSYS;
220 goto out;
222 schedule();
225 retval = put_user(data, (unsigned long __user *)buf);
226 if (!retval)
227 retval = sizeof(unsigned long);
228 out:
229 __set_current_state(TASK_RUNNING);
230 remove_wait_queue(&devp->hd_waitqueue, &wait);
232 return retval;
235 static unsigned int hpet_poll(struct file *file, poll_table * wait)
237 unsigned long v;
238 struct hpet_dev *devp;
240 devp = file->private_data;
242 if (!devp->hd_ireqfreq)
243 return 0;
245 poll_wait(file, &devp->hd_waitqueue, wait);
247 spin_lock_irq(&hpet_lock);
248 v = devp->hd_irqdata;
249 spin_unlock_irq(&hpet_lock);
251 if (v != 0)
252 return POLLIN | POLLRDNORM;
254 return 0;
257 static int hpet_mmap(struct file *file, struct vm_area_struct *vma)
259 #ifdef CONFIG_HPET_MMAP
260 struct hpet_dev *devp;
261 unsigned long addr;
263 if (((vma->vm_end - vma->vm_start) != PAGE_SIZE) || vma->vm_pgoff)
264 return -EINVAL;
266 devp = file->private_data;
267 addr = (unsigned long)devp->hd_hpet;
269 if (addr & (PAGE_SIZE - 1))
270 return -ENOSYS;
272 vma->vm_flags |= VM_IO;
273 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
274 addr = __pa(addr);
276 if (remap_page_range
277 (vma, vma->vm_start, addr, PAGE_SIZE, vma->vm_page_prot)) {
278 printk(KERN_ERR "remap_page_range failed in hpet.c\n");
279 return -EAGAIN;
282 return 0;
283 #else
284 return -ENOSYS;
285 #endif
288 static int hpet_fasync(int fd, struct file *file, int on)
290 struct hpet_dev *devp;
292 devp = file->private_data;
294 if (fasync_helper(fd, file, on, &devp->hd_async_queue) >= 0)
295 return 0;
296 else
297 return -EIO;
300 static int hpet_release(struct inode *inode, struct file *file)
302 struct hpet_dev *devp;
303 struct hpet_timer __iomem *timer;
304 int irq = 0;
306 devp = file->private_data;
307 timer = devp->hd_timer;
309 spin_lock_irq(&hpet_lock);
311 writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK),
312 &timer->hpet_config);
314 irq = devp->hd_irq;
315 devp->hd_irq = 0;
317 devp->hd_ireqfreq = 0;
319 if (devp->hd_flags & HPET_PERIODIC
320 && readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
321 unsigned long v;
323 v = readq(&timer->hpet_config);
324 v ^= Tn_TYPE_CNF_MASK;
325 writeq(v, &timer->hpet_config);
328 devp->hd_flags &= ~(HPET_OPEN | HPET_IE | HPET_PERIODIC);
329 spin_unlock_irq(&hpet_lock);
331 if (irq)
332 free_irq(irq, devp);
334 if (file->f_flags & FASYNC)
335 hpet_fasync(-1, file, 0);
337 file->private_data = NULL;
338 return 0;
341 static int hpet_ioctl_common(struct hpet_dev *, int, unsigned long, int);
343 static int
344 hpet_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
345 unsigned long arg)
347 struct hpet_dev *devp;
349 devp = file->private_data;
350 return hpet_ioctl_common(devp, cmd, arg, 0);
353 static int hpet_ioctl_ieon(struct hpet_dev *devp)
355 struct hpet_timer __iomem *timer;
356 struct hpet __iomem *hpet;
357 struct hpets *hpetp;
358 int irq;
359 unsigned long g, v, t, m;
360 unsigned long flags, isr;
362 timer = devp->hd_timer;
363 hpet = devp->hd_hpet;
364 hpetp = devp->hd_hpets;
366 v = readq(&timer->hpet_config);
367 spin_lock_irq(&hpet_lock);
369 if (devp->hd_flags & HPET_IE) {
370 spin_unlock_irq(&hpet_lock);
371 return -EBUSY;
374 devp->hd_flags |= HPET_IE;
375 spin_unlock_irq(&hpet_lock);
377 t = readq(&timer->hpet_config);
378 irq = devp->hd_hdwirq;
380 if (irq) {
381 sprintf(devp->hd_name, "hpet%d", (int)(devp - hpetp->hp_dev));
383 if (request_irq
384 (irq, hpet_interrupt, SA_INTERRUPT, devp->hd_name, (void *)devp)) {
385 printk(KERN_ERR "hpet: IRQ %d is not free\n", irq);
386 irq = 0;
390 if (irq == 0) {
391 spin_lock_irq(&hpet_lock);
392 devp->hd_flags ^= HPET_IE;
393 spin_unlock_irq(&hpet_lock);
394 return -EIO;
397 devp->hd_irq = irq;
398 t = devp->hd_ireqfreq;
399 v = readq(&timer->hpet_config);
400 g = v | Tn_INT_ENB_CNF_MASK;
402 if (devp->hd_flags & HPET_PERIODIC) {
403 write_counter(t, &timer->hpet_compare);
404 g |= Tn_TYPE_CNF_MASK;
405 v |= Tn_TYPE_CNF_MASK;
406 writeq(v, &timer->hpet_config);
407 v |= Tn_VAL_SET_CNF_MASK;
408 writeq(v, &timer->hpet_config);
409 local_irq_save(flags);
410 m = read_counter(&hpet->hpet_mc);
411 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
412 } else {
413 local_irq_save(flags);
414 m = read_counter(&hpet->hpet_mc);
415 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
418 isr = (1 << (devp - hpets->hp_dev));
419 writeq(isr, &hpet->hpet_isr);
420 writeq(g, &timer->hpet_config);
421 local_irq_restore(flags);
423 return 0;
426 static inline unsigned long hpet_time_div(unsigned long dis)
428 unsigned long long m = 1000000000000000ULL;
430 do_div(m, dis);
432 return (unsigned long)m;
435 static int
436 hpet_ioctl_common(struct hpet_dev *devp, int cmd, unsigned long arg, int kernel)
438 struct hpet_timer __iomem *timer;
439 struct hpet __iomem *hpet;
440 struct hpets *hpetp;
441 int err;
442 unsigned long v;
444 switch (cmd) {
445 case HPET_IE_OFF:
446 case HPET_INFO:
447 case HPET_EPI:
448 case HPET_DPI:
449 case HPET_IRQFREQ:
450 timer = devp->hd_timer;
451 hpet = devp->hd_hpet;
452 hpetp = devp->hd_hpets;
453 break;
454 case HPET_IE_ON:
455 return hpet_ioctl_ieon(devp);
456 default:
457 return -EINVAL;
460 err = 0;
462 switch (cmd) {
463 case HPET_IE_OFF:
464 if ((devp->hd_flags & HPET_IE) == 0)
465 break;
466 v = readq(&timer->hpet_config);
467 v &= ~Tn_INT_ENB_CNF_MASK;
468 writeq(v, &timer->hpet_config);
469 if (devp->hd_irq) {
470 free_irq(devp->hd_irq, devp);
471 devp->hd_irq = 0;
473 devp->hd_flags ^= HPET_IE;
474 break;
475 case HPET_INFO:
477 struct hpet_info info;
479 info.hi_ireqfreq = hpet_time_div(hpetp->hp_period *
480 devp->hd_ireqfreq);
481 info.hi_flags =
482 readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK;
483 info.hi_hpet = devp->hd_hpets->hp_which;
484 info.hi_timer = devp - devp->hd_hpets->hp_dev;
485 if (copy_to_user((void __user *)arg, &info, sizeof(info)))
486 err = -EFAULT;
487 break;
489 case HPET_EPI:
490 v = readq(&timer->hpet_config);
491 if ((v & Tn_PER_INT_CAP_MASK) == 0) {
492 err = -ENXIO;
493 break;
495 devp->hd_flags |= HPET_PERIODIC;
496 break;
497 case HPET_DPI:
498 v = readq(&timer->hpet_config);
499 if ((v & Tn_PER_INT_CAP_MASK) == 0) {
500 err = -ENXIO;
501 break;
503 if (devp->hd_flags & HPET_PERIODIC &&
504 readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
505 v = readq(&timer->hpet_config);
506 v ^= Tn_TYPE_CNF_MASK;
507 writeq(v, &timer->hpet_config);
509 devp->hd_flags &= ~HPET_PERIODIC;
510 break;
511 case HPET_IRQFREQ:
512 if (!kernel && (arg > hpet_max_freq) &&
513 !capable(CAP_SYS_RESOURCE)) {
514 err = -EACCES;
515 break;
518 if (arg & (arg - 1)) {
519 err = -EINVAL;
520 break;
523 devp->hd_ireqfreq = hpet_time_div(hpetp->hp_period * arg);
526 return err;
529 static struct file_operations hpet_fops = {
530 .owner = THIS_MODULE,
531 .llseek = no_llseek,
532 .read = hpet_read,
533 .poll = hpet_poll,
534 .ioctl = hpet_ioctl,
535 .open = hpet_open,
536 .release = hpet_release,
537 .fasync = hpet_fasync,
538 .mmap = hpet_mmap,
541 EXPORT_SYMBOL(hpet_alloc);
542 EXPORT_SYMBOL(hpet_register);
543 EXPORT_SYMBOL(hpet_unregister);
544 EXPORT_SYMBOL(hpet_control);
546 int hpet_register(struct hpet_task *tp, int periodic)
548 unsigned int i;
549 u64 mask;
550 struct hpet_timer __iomem *timer;
551 struct hpet_dev *devp;
552 struct hpets *hpetp;
554 switch (periodic) {
555 case 1:
556 mask = Tn_PER_INT_CAP_MASK;
557 break;
558 case 0:
559 mask = 0;
560 break;
561 default:
562 return -EINVAL;
565 spin_lock_irq(&hpet_task_lock);
566 spin_lock(&hpet_lock);
568 for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next)
569 for (timer = hpetp->hp_hpet->hpet_timers, i = 0;
570 i < hpetp->hp_ntimer; i++, timer++) {
571 if ((readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK)
572 != mask)
573 continue;
575 devp = &hpetp->hp_dev[i];
577 if (devp->hd_flags & HPET_OPEN || devp->hd_task) {
578 devp = NULL;
579 continue;
582 tp->ht_opaque = devp;
583 devp->hd_task = tp;
584 break;
587 spin_unlock(&hpet_lock);
588 spin_unlock_irq(&hpet_task_lock);
590 if (tp->ht_opaque)
591 return 0;
592 else
593 return -EBUSY;
596 static inline int hpet_tpcheck(struct hpet_task *tp)
598 struct hpet_dev *devp;
599 struct hpets *hpetp;
601 devp = tp->ht_opaque;
603 if (!devp)
604 return -ENXIO;
606 for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
607 if (devp >= hpetp->hp_dev
608 && devp < (hpetp->hp_dev + hpetp->hp_ntimer)
609 && devp->hd_hpet == hpetp->hp_hpet)
610 return 0;
612 return -ENXIO;
615 int hpet_unregister(struct hpet_task *tp)
617 struct hpet_dev *devp;
618 struct hpet_timer __iomem *timer;
619 int err;
621 if ((err = hpet_tpcheck(tp)))
622 return err;
624 spin_lock_irq(&hpet_task_lock);
625 spin_lock(&hpet_lock);
627 devp = tp->ht_opaque;
628 if (devp->hd_task != tp) {
629 spin_unlock(&hpet_lock);
630 spin_unlock_irq(&hpet_task_lock);
631 return -ENXIO;
634 timer = devp->hd_timer;
635 writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK),
636 &timer->hpet_config);
637 devp->hd_flags &= ~(HPET_IE | HPET_PERIODIC);
638 devp->hd_task = NULL;
639 spin_unlock(&hpet_lock);
640 spin_unlock_irq(&hpet_task_lock);
642 return 0;
645 int hpet_control(struct hpet_task *tp, unsigned int cmd, unsigned long arg)
647 struct hpet_dev *devp;
648 int err;
650 if ((err = hpet_tpcheck(tp)))
651 return err;
653 spin_lock_irq(&hpet_lock);
654 devp = tp->ht_opaque;
655 if (devp->hd_task != tp) {
656 spin_unlock_irq(&hpet_lock);
657 return -ENXIO;
659 spin_unlock_irq(&hpet_lock);
660 return hpet_ioctl_common(devp, cmd, arg, 1);
663 #ifdef CONFIG_TIME_INTERPOLATION
665 static struct time_interpolator hpet_interpolator = {
666 .source = TIME_SOURCE_MMIO64,
667 .shift = 10
670 #endif
672 static ctl_table hpet_table[] = {
674 .ctl_name = 1,
675 .procname = "max-user-freq",
676 .data = &hpet_max_freq,
677 .maxlen = sizeof(int),
678 .mode = 0644,
679 .proc_handler = &proc_dointvec,
681 {.ctl_name = 0}
684 static ctl_table hpet_root[] = {
686 .ctl_name = 1,
687 .procname = "hpet",
688 .maxlen = 0,
689 .mode = 0555,
690 .child = hpet_table,
692 {.ctl_name = 0}
695 static ctl_table dev_root[] = {
697 .ctl_name = CTL_DEV,
698 .procname = "dev",
699 .maxlen = 0,
700 .mode = 0555,
701 .child = hpet_root,
703 {.ctl_name = 0}
706 static struct ctl_table_header *sysctl_header;
709 * Adjustment for when arming the timer with
710 * initial conditions. That is, main counter
711 * ticks expired before interrupts are enabled.
713 #define TICK_CALIBRATE (1000UL)
715 static unsigned long __init hpet_calibrate(struct hpets *hpetp)
717 struct hpet_timer __iomem *timer = NULL;
718 unsigned long t, m, count, i, flags, start;
719 struct hpet_dev *devp;
720 int j;
721 struct hpet __iomem *hpet;
723 for (j = 0, devp = hpetp->hp_dev; j < hpetp->hp_ntimer; j++, devp++)
724 if ((devp->hd_flags & HPET_OPEN) == 0) {
725 timer = devp->hd_timer;
726 break;
729 if (!timer)
730 return 0;
732 hpet = hpets->hp_hpet;
733 t = read_counter(&timer->hpet_compare);
735 i = 0;
736 count = hpet_time_div(hpetp->hp_period * TICK_CALIBRATE);
738 local_irq_save(flags);
740 start = read_counter(&hpet->hpet_mc);
742 do {
743 m = read_counter(&hpet->hpet_mc);
744 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
745 } while (i++, (m - start) < count);
747 local_irq_restore(flags);
749 return (m - start) / i;
752 int __init hpet_alloc(struct hpet_data *hdp)
754 u64 cap, mcfg;
755 struct hpet_dev *devp;
756 u32 i, ntimer;
757 struct hpets *hpetp;
758 size_t siz;
759 struct hpet __iomem *hpet;
760 static struct hpets *last __initdata = (struct hpets *)0;
761 unsigned long ns;
764 * hpet_alloc can be called by platform dependent code.
765 * if platform dependent code has allocated the hpet
766 * ACPI also reports hpet, then we catch it here.
768 for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
769 if (hpetp->hp_hpet == hdp->hd_address)
770 return 0;
772 siz = sizeof(struct hpets) + ((hdp->hd_nirqs - 1) *
773 sizeof(struct hpet_dev));
775 hpetp = kmalloc(siz, GFP_KERNEL);
777 if (!hpetp)
778 return -ENOMEM;
780 memset(hpetp, 0, siz);
782 hpetp->hp_which = hpet_nhpet++;
783 hpetp->hp_hpet = hdp->hd_address;
785 hpetp->hp_ntimer = hdp->hd_nirqs;
787 for (i = 0; i < hdp->hd_nirqs; i++)
788 hpetp->hp_dev[i].hd_hdwirq = hdp->hd_irq[i];
790 hpet = hpetp->hp_hpet;
792 cap = readq(&hpet->hpet_cap);
794 ntimer = ((cap & HPET_NUM_TIM_CAP_MASK) >> HPET_NUM_TIM_CAP_SHIFT) + 1;
796 if (hpetp->hp_ntimer != ntimer) {
797 printk(KERN_WARNING "hpet: number irqs doesn't agree"
798 " with number of timers\n");
799 kfree(hpetp);
800 return -ENODEV;
803 if (last)
804 last->hp_next = hpetp;
805 else
806 hpets = hpetp;
808 last = hpetp;
810 hpetp->hp_period = (cap & HPET_COUNTER_CLK_PERIOD_MASK) >>
811 HPET_COUNTER_CLK_PERIOD_SHIFT;
813 printk(KERN_INFO "hpet%d: at MMIO 0x%p, IRQ%s",
814 hpetp->hp_which, hpet, hpetp->hp_ntimer > 1 ? "s" : "");
815 for (i = 0; i < hpetp->hp_ntimer; i++)
816 printk("%s %d", i > 0 ? "," : "", hdp->hd_irq[i]);
817 printk("\n");
819 ns = hpetp->hp_period; /* femptoseconds, 10^-15 */
820 do_div(ns, 1000000); /* convert to nanoseconds, 10^-9 */
821 printk(KERN_INFO "hpet%d: %ldns tick, %d %d-bit timers\n",
822 hpetp->hp_which, ns, hpetp->hp_ntimer,
823 cap & HPET_COUNTER_SIZE_MASK ? 64 : 32);
825 mcfg = readq(&hpet->hpet_config);
826 if ((mcfg & HPET_ENABLE_CNF_MASK) == 0) {
827 write_counter(0L, &hpet->hpet_mc);
828 mcfg |= HPET_ENABLE_CNF_MASK;
829 writeq(mcfg, &hpet->hpet_config);
832 for (i = 0, devp = hpetp->hp_dev; i < hpetp->hp_ntimer;
833 i++, hpet_ntimer++, devp++) {
834 unsigned long v;
835 struct hpet_timer __iomem *timer;
837 timer = &hpet->hpet_timers[devp - hpetp->hp_dev];
838 v = readq(&timer->hpet_config);
840 devp->hd_hpets = hpetp;
841 devp->hd_hpet = hpet;
842 devp->hd_timer = timer;
845 * If the timer was reserved by platform code,
846 * then make timer unavailable for opens.
848 if (hdp->hd_state & (1 << i)) {
849 devp->hd_flags = HPET_OPEN;
850 continue;
853 init_waitqueue_head(&devp->hd_waitqueue);
856 hpetp->hp_delta = hpet_calibrate(hpetp);
858 return 0;
861 static acpi_status __init hpet_resources(struct acpi_resource *res, void *data)
863 struct hpet_data *hdp;
864 acpi_status status;
865 struct acpi_resource_address64 addr;
866 struct hpets *hpetp;
868 hdp = data;
870 status = acpi_resource_to_address64(res, &addr);
872 if (ACPI_SUCCESS(status)) {
873 unsigned long size;
875 size = addr.max_address_range - addr.min_address_range + 1;
876 hdp->hd_address = ioremap(addr.min_address_range, size);
878 for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
879 if (hpetp->hp_hpet == hdp->hd_address)
880 return -EBUSY;
881 } else if (res->id == ACPI_RSTYPE_EXT_IRQ) {
882 struct acpi_resource_ext_irq *irqp;
883 int i;
885 irqp = &res->data.extended_irq;
887 if (irqp->number_of_interrupts > 0) {
888 hdp->hd_nirqs = irqp->number_of_interrupts;
890 for (i = 0; i < hdp->hd_nirqs; i++)
891 hdp->hd_irq[i] =
892 acpi_register_gsi(irqp->interrupts[i],
893 irqp->edge_level,
894 irqp->active_high_low);
898 return AE_OK;
901 static int __init hpet_acpi_add(struct acpi_device *device)
903 acpi_status result;
904 struct hpet_data data;
906 memset(&data, 0, sizeof(data));
908 result =
909 acpi_walk_resources(device->handle, METHOD_NAME__CRS,
910 hpet_resources, &data);
912 if (ACPI_FAILURE(result))
913 return -ENODEV;
915 if (!data.hd_address || !data.hd_nirqs) {
916 printk("%s: no address or irqs in _CRS\n", __FUNCTION__);
917 return -ENODEV;
920 return hpet_alloc(&data);
923 static int __init hpet_acpi_remove(struct acpi_device *device, int type)
925 return 0;
928 static struct acpi_driver hpet_acpi_driver = {
929 .name = "hpet",
930 .ids = "PNP0103",
931 .ops = {
932 .add = hpet_acpi_add,
933 .remove = hpet_acpi_remove,
937 static struct miscdevice hpet_misc = { HPET_MINOR, "hpet", &hpet_fops };
939 static int __init hpet_init(void)
941 (void)acpi_bus_register_driver(&hpet_acpi_driver);
943 if (hpets) {
944 if (misc_register(&hpet_misc))
945 return -ENODEV;
947 sysctl_header = register_sysctl_table(dev_root, 0);
949 #ifdef CONFIG_TIME_INTERPOLATION
951 struct hpet *hpet;
953 hpet = hpets->hp_hpet;
954 hpet_interpolator.addr = &hpets->hp_hpet->hpet_mc;
955 hpet_interpolator.frequency = hpet_time_div(hpets->hp_period);
956 hpet_interpolator.drift = hpet_interpolator.frequency *
957 HPET_DRIFT / 1000000;
958 register_time_interpolator(&hpet_interpolator);
960 #endif
961 return 0;
962 } else
963 return -ENODEV;
966 static void __exit hpet_exit(void)
968 acpi_bus_unregister_driver(&hpet_acpi_driver);
970 if (hpets)
971 unregister_sysctl_table(sysctl_header);
973 return;
976 module_init(hpet_init);
977 module_exit(hpet_exit);
978 MODULE_AUTHOR("Bob Picco <Robert.Picco@hp.com>");
979 MODULE_LICENSE("GPL");