[PATCH] hpet: fix HPET_INFO calls from kernel space
[linux-2.6/verdex.git] / drivers / char / hpet.c
blobc85d11de5febcf76153d468f1955aea68d2779d4
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>
31 #include <linux/bitops.h>
33 #include <asm/current.h>
34 #include <asm/uaccess.h>
35 #include <asm/system.h>
36 #include <asm/io.h>
37 #include <asm/irq.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/hardwaredesign/hpetspec.htm
49 #define HPET_USER_FREQ (64)
50 #define HPET_DRIFT (500)
52 static u32 hpet_nhpet, hpet_max_freq = HPET_USER_FREQ;
54 /* A lock for concurrent access by app and isr hpet activity. */
55 static DEFINE_SPINLOCK(hpet_lock);
56 /* A lock for concurrent intermodule access to hpet and isr hpet activity. */
57 static DEFINE_SPINLOCK(hpet_task_lock);
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_hpet_phys;
80 struct time_interpolator *hp_interpolator;
81 unsigned long long hp_tick_freq;
82 unsigned long hp_delta;
83 unsigned int hp_ntimer;
84 unsigned int hp_which;
85 struct hpet_dev hp_dev[1];
88 static struct hpets *hpets;
90 #define HPET_OPEN 0x0001
91 #define HPET_IE 0x0002 /* interrupt enabled */
92 #define HPET_PERIODIC 0x0004
93 #define HPET_SHARED_IRQ 0x0008
95 #if BITS_PER_LONG == 64
96 #define write_counter(V, MC) writeq(V, MC)
97 #define read_counter(MC) readq(MC)
98 #else
99 #define write_counter(V, MC) writel(V, MC)
100 #define read_counter(MC) readl(MC)
101 #endif
103 #ifndef readq
104 static inline unsigned long long readq(void __iomem *addr)
106 return readl(addr) | (((unsigned long long)readl(addr + 4)) << 32LL);
108 #endif
110 #ifndef writeq
111 static inline void writeq(unsigned long long v, void __iomem *addr)
113 writel(v & 0xffffffff, addr);
114 writel(v >> 32, addr + 4);
116 #endif
118 static irqreturn_t hpet_interrupt(int irq, void *data, struct pt_regs *regs)
120 struct hpet_dev *devp;
121 unsigned long isr;
123 devp = data;
124 isr = 1 << (devp - devp->hd_hpets->hp_dev);
126 if ((devp->hd_flags & HPET_SHARED_IRQ) &&
127 !(isr & readl(&devp->hd_hpet->hpet_isr)))
128 return IRQ_NONE;
130 spin_lock(&hpet_lock);
131 devp->hd_irqdata++;
134 * For non-periodic timers, increment the accumulator.
135 * This has the effect of treating non-periodic like periodic.
137 if ((devp->hd_flags & (HPET_IE | HPET_PERIODIC)) == HPET_IE) {
138 unsigned long m, t;
140 t = devp->hd_ireqfreq;
141 m = read_counter(&devp->hd_hpet->hpet_mc);
142 write_counter(t + m + devp->hd_hpets->hp_delta,
143 &devp->hd_timer->hpet_compare);
146 if (devp->hd_flags & HPET_SHARED_IRQ)
147 writel(isr, &devp->hd_hpet->hpet_isr);
148 spin_unlock(&hpet_lock);
150 spin_lock(&hpet_task_lock);
151 if (devp->hd_task)
152 devp->hd_task->ht_func(devp->hd_task->ht_data);
153 spin_unlock(&hpet_task_lock);
155 wake_up_interruptible(&devp->hd_waitqueue);
157 kill_fasync(&devp->hd_async_queue, SIGIO, POLL_IN);
159 return IRQ_HANDLED;
162 static int hpet_open(struct inode *inode, struct file *file)
164 struct hpet_dev *devp;
165 struct hpets *hpetp;
166 int i;
168 if (file->f_mode & FMODE_WRITE)
169 return -EINVAL;
171 spin_lock_irq(&hpet_lock);
173 for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next)
174 for (i = 0; i < hpetp->hp_ntimer; i++)
175 if (hpetp->hp_dev[i].hd_flags & HPET_OPEN
176 || hpetp->hp_dev[i].hd_task)
177 continue;
178 else {
179 devp = &hpetp->hp_dev[i];
180 break;
183 if (!devp) {
184 spin_unlock_irq(&hpet_lock);
185 return -EBUSY;
188 file->private_data = devp;
189 devp->hd_irqdata = 0;
190 devp->hd_flags |= HPET_OPEN;
191 spin_unlock_irq(&hpet_lock);
193 return 0;
196 static ssize_t
197 hpet_read(struct file *file, char __user *buf, size_t count, loff_t * ppos)
199 DECLARE_WAITQUEUE(wait, current);
200 unsigned long data;
201 ssize_t retval;
202 struct hpet_dev *devp;
204 devp = file->private_data;
205 if (!devp->hd_ireqfreq)
206 return -EIO;
208 if (count < sizeof(unsigned long))
209 return -EINVAL;
211 add_wait_queue(&devp->hd_waitqueue, &wait);
213 for ( ; ; ) {
214 set_current_state(TASK_INTERRUPTIBLE);
216 spin_lock_irq(&hpet_lock);
217 data = devp->hd_irqdata;
218 devp->hd_irqdata = 0;
219 spin_unlock_irq(&hpet_lock);
221 if (data)
222 break;
223 else if (file->f_flags & O_NONBLOCK) {
224 retval = -EAGAIN;
225 goto out;
226 } else if (signal_pending(current)) {
227 retval = -ERESTARTSYS;
228 goto out;
230 schedule();
233 retval = put_user(data, (unsigned long __user *)buf);
234 if (!retval)
235 retval = sizeof(unsigned long);
236 out:
237 __set_current_state(TASK_RUNNING);
238 remove_wait_queue(&devp->hd_waitqueue, &wait);
240 return retval;
243 static unsigned int hpet_poll(struct file *file, poll_table * wait)
245 unsigned long v;
246 struct hpet_dev *devp;
248 devp = file->private_data;
250 if (!devp->hd_ireqfreq)
251 return 0;
253 poll_wait(file, &devp->hd_waitqueue, wait);
255 spin_lock_irq(&hpet_lock);
256 v = devp->hd_irqdata;
257 spin_unlock_irq(&hpet_lock);
259 if (v != 0)
260 return POLLIN | POLLRDNORM;
262 return 0;
265 static int hpet_mmap(struct file *file, struct vm_area_struct *vma)
267 #ifdef CONFIG_HPET_MMAP
268 struct hpet_dev *devp;
269 unsigned long addr;
271 if (((vma->vm_end - vma->vm_start) != PAGE_SIZE) || vma->vm_pgoff)
272 return -EINVAL;
274 devp = file->private_data;
275 addr = devp->hd_hpets->hp_hpet_phys;
277 if (addr & (PAGE_SIZE - 1))
278 return -ENOSYS;
280 vma->vm_flags |= VM_IO;
281 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
283 if (io_remap_pfn_range(vma, vma->vm_start, addr >> PAGE_SHIFT,
284 PAGE_SIZE, vma->vm_page_prot)) {
285 printk(KERN_ERR "remap_pfn_range failed in hpet.c\n");
286 return -EAGAIN;
289 return 0;
290 #else
291 return -ENOSYS;
292 #endif
295 static int hpet_fasync(int fd, struct file *file, int on)
297 struct hpet_dev *devp;
299 devp = file->private_data;
301 if (fasync_helper(fd, file, on, &devp->hd_async_queue) >= 0)
302 return 0;
303 else
304 return -EIO;
307 static int hpet_release(struct inode *inode, struct file *file)
309 struct hpet_dev *devp;
310 struct hpet_timer __iomem *timer;
311 int irq = 0;
313 devp = file->private_data;
314 timer = devp->hd_timer;
316 spin_lock_irq(&hpet_lock);
318 writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK),
319 &timer->hpet_config);
321 irq = devp->hd_irq;
322 devp->hd_irq = 0;
324 devp->hd_ireqfreq = 0;
326 if (devp->hd_flags & HPET_PERIODIC
327 && readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
328 unsigned long v;
330 v = readq(&timer->hpet_config);
331 v ^= Tn_TYPE_CNF_MASK;
332 writeq(v, &timer->hpet_config);
335 devp->hd_flags &= ~(HPET_OPEN | HPET_IE | HPET_PERIODIC);
336 spin_unlock_irq(&hpet_lock);
338 if (irq)
339 free_irq(irq, devp);
341 if (file->f_flags & FASYNC)
342 hpet_fasync(-1, file, 0);
344 file->private_data = NULL;
345 return 0;
348 static int hpet_ioctl_common(struct hpet_dev *, int, unsigned long, int);
350 static int
351 hpet_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
352 unsigned long arg)
354 struct hpet_dev *devp;
356 devp = file->private_data;
357 return hpet_ioctl_common(devp, cmd, arg, 0);
360 static int hpet_ioctl_ieon(struct hpet_dev *devp)
362 struct hpet_timer __iomem *timer;
363 struct hpet __iomem *hpet;
364 struct hpets *hpetp;
365 int irq;
366 unsigned long g, v, t, m;
367 unsigned long flags, isr;
369 timer = devp->hd_timer;
370 hpet = devp->hd_hpet;
371 hpetp = devp->hd_hpets;
373 if (!devp->hd_ireqfreq)
374 return -EIO;
376 spin_lock_irq(&hpet_lock);
378 if (devp->hd_flags & HPET_IE) {
379 spin_unlock_irq(&hpet_lock);
380 return -EBUSY;
383 devp->hd_flags |= HPET_IE;
385 if (readl(&timer->hpet_config) & Tn_INT_TYPE_CNF_MASK)
386 devp->hd_flags |= HPET_SHARED_IRQ;
387 spin_unlock_irq(&hpet_lock);
389 irq = devp->hd_hdwirq;
391 if (irq) {
392 unsigned long irq_flags;
394 sprintf(devp->hd_name, "hpet%d", (int)(devp - hpetp->hp_dev));
395 irq_flags = devp->hd_flags & HPET_SHARED_IRQ
396 ? SA_SHIRQ : SA_INTERRUPT;
397 if (request_irq(irq, hpet_interrupt, irq_flags,
398 devp->hd_name, (void *)devp)) {
399 printk(KERN_ERR "hpet: IRQ %d is not free\n", irq);
400 irq = 0;
404 if (irq == 0) {
405 spin_lock_irq(&hpet_lock);
406 devp->hd_flags ^= HPET_IE;
407 spin_unlock_irq(&hpet_lock);
408 return -EIO;
411 devp->hd_irq = irq;
412 t = devp->hd_ireqfreq;
413 v = readq(&timer->hpet_config);
414 g = v | Tn_INT_ENB_CNF_MASK;
416 if (devp->hd_flags & HPET_PERIODIC) {
417 write_counter(t, &timer->hpet_compare);
418 g |= Tn_TYPE_CNF_MASK;
419 v |= Tn_TYPE_CNF_MASK;
420 writeq(v, &timer->hpet_config);
421 v |= Tn_VAL_SET_CNF_MASK;
422 writeq(v, &timer->hpet_config);
423 local_irq_save(flags);
424 m = read_counter(&hpet->hpet_mc);
425 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
426 } else {
427 local_irq_save(flags);
428 m = read_counter(&hpet->hpet_mc);
429 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
432 if (devp->hd_flags & HPET_SHARED_IRQ) {
433 isr = 1 << (devp - hpets->hp_dev);
434 writel(isr, &hpet->hpet_isr);
436 writeq(g, &timer->hpet_config);
437 local_irq_restore(flags);
439 return 0;
442 /* converts Hz to number of timer ticks */
443 static inline unsigned long hpet_time_div(struct hpets *hpets,
444 unsigned long dis)
446 unsigned long long m;
448 m = hpets->hp_tick_freq + (dis >> 1);
449 do_div(m, dis);
450 return (unsigned long)m;
453 static int
454 hpet_ioctl_common(struct hpet_dev *devp, int cmd, unsigned long arg, int kernel)
456 struct hpet_timer __iomem *timer;
457 struct hpet __iomem *hpet;
458 struct hpets *hpetp;
459 int err;
460 unsigned long v;
462 switch (cmd) {
463 case HPET_IE_OFF:
464 case HPET_INFO:
465 case HPET_EPI:
466 case HPET_DPI:
467 case HPET_IRQFREQ:
468 timer = devp->hd_timer;
469 hpet = devp->hd_hpet;
470 hpetp = devp->hd_hpets;
471 break;
472 case HPET_IE_ON:
473 return hpet_ioctl_ieon(devp);
474 default:
475 return -EINVAL;
478 err = 0;
480 switch (cmd) {
481 case HPET_IE_OFF:
482 if ((devp->hd_flags & HPET_IE) == 0)
483 break;
484 v = readq(&timer->hpet_config);
485 v &= ~Tn_INT_ENB_CNF_MASK;
486 writeq(v, &timer->hpet_config);
487 if (devp->hd_irq) {
488 free_irq(devp->hd_irq, devp);
489 devp->hd_irq = 0;
491 devp->hd_flags ^= HPET_IE;
492 break;
493 case HPET_INFO:
495 struct hpet_info info;
497 info.hi_ireqfreq = hpet_time_div(hpetp,
498 devp->hd_ireqfreq);
499 info.hi_flags =
500 readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK;
501 info.hi_hpet = devp->hd_hpets->hp_which;
502 info.hi_timer = devp - devp->hd_hpets->hp_dev;
503 if (kernel)
504 memcpy((void *)arg, &info, sizeof(info));
505 else
506 if (copy_to_user((void __user *)arg, &info,
507 sizeof(info)))
508 err = -EFAULT;
509 break;
511 case HPET_EPI:
512 v = readq(&timer->hpet_config);
513 if ((v & Tn_PER_INT_CAP_MASK) == 0) {
514 err = -ENXIO;
515 break;
517 devp->hd_flags |= HPET_PERIODIC;
518 break;
519 case HPET_DPI:
520 v = readq(&timer->hpet_config);
521 if ((v & Tn_PER_INT_CAP_MASK) == 0) {
522 err = -ENXIO;
523 break;
525 if (devp->hd_flags & HPET_PERIODIC &&
526 readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
527 v = readq(&timer->hpet_config);
528 v ^= Tn_TYPE_CNF_MASK;
529 writeq(v, &timer->hpet_config);
531 devp->hd_flags &= ~HPET_PERIODIC;
532 break;
533 case HPET_IRQFREQ:
534 if (!kernel && (arg > hpet_max_freq) &&
535 !capable(CAP_SYS_RESOURCE)) {
536 err = -EACCES;
537 break;
540 if (!arg) {
541 err = -EINVAL;
542 break;
545 devp->hd_ireqfreq = hpet_time_div(hpetp, arg);
548 return err;
551 static struct file_operations hpet_fops = {
552 .owner = THIS_MODULE,
553 .llseek = no_llseek,
554 .read = hpet_read,
555 .poll = hpet_poll,
556 .ioctl = hpet_ioctl,
557 .open = hpet_open,
558 .release = hpet_release,
559 .fasync = hpet_fasync,
560 .mmap = hpet_mmap,
563 EXPORT_SYMBOL(hpet_alloc);
564 EXPORT_SYMBOL(hpet_register);
565 EXPORT_SYMBOL(hpet_unregister);
566 EXPORT_SYMBOL(hpet_control);
568 int hpet_register(struct hpet_task *tp, int periodic)
570 unsigned int i;
571 u64 mask;
572 struct hpet_timer __iomem *timer;
573 struct hpet_dev *devp;
574 struct hpets *hpetp;
576 switch (periodic) {
577 case 1:
578 mask = Tn_PER_INT_CAP_MASK;
579 break;
580 case 0:
581 mask = 0;
582 break;
583 default:
584 return -EINVAL;
587 spin_lock_irq(&hpet_task_lock);
588 spin_lock(&hpet_lock);
590 for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next)
591 for (timer = hpetp->hp_hpet->hpet_timers, i = 0;
592 i < hpetp->hp_ntimer; i++, timer++) {
593 if ((readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK)
594 != mask)
595 continue;
597 devp = &hpetp->hp_dev[i];
599 if (devp->hd_flags & HPET_OPEN || devp->hd_task) {
600 devp = NULL;
601 continue;
604 tp->ht_opaque = devp;
605 devp->hd_task = tp;
606 break;
609 spin_unlock(&hpet_lock);
610 spin_unlock_irq(&hpet_task_lock);
612 if (tp->ht_opaque)
613 return 0;
614 else
615 return -EBUSY;
618 static inline int hpet_tpcheck(struct hpet_task *tp)
620 struct hpet_dev *devp;
621 struct hpets *hpetp;
623 devp = tp->ht_opaque;
625 if (!devp)
626 return -ENXIO;
628 for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
629 if (devp >= hpetp->hp_dev
630 && devp < (hpetp->hp_dev + hpetp->hp_ntimer)
631 && devp->hd_hpet == hpetp->hp_hpet)
632 return 0;
634 return -ENXIO;
637 int hpet_unregister(struct hpet_task *tp)
639 struct hpet_dev *devp;
640 struct hpet_timer __iomem *timer;
641 int err;
643 if ((err = hpet_tpcheck(tp)))
644 return err;
646 spin_lock_irq(&hpet_task_lock);
647 spin_lock(&hpet_lock);
649 devp = tp->ht_opaque;
650 if (devp->hd_task != tp) {
651 spin_unlock(&hpet_lock);
652 spin_unlock_irq(&hpet_task_lock);
653 return -ENXIO;
656 timer = devp->hd_timer;
657 writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK),
658 &timer->hpet_config);
659 devp->hd_flags &= ~(HPET_IE | HPET_PERIODIC);
660 devp->hd_task = NULL;
661 spin_unlock(&hpet_lock);
662 spin_unlock_irq(&hpet_task_lock);
664 return 0;
667 int hpet_control(struct hpet_task *tp, unsigned int cmd, unsigned long arg)
669 struct hpet_dev *devp;
670 int err;
672 if ((err = hpet_tpcheck(tp)))
673 return err;
675 spin_lock_irq(&hpet_lock);
676 devp = tp->ht_opaque;
677 if (devp->hd_task != tp) {
678 spin_unlock_irq(&hpet_lock);
679 return -ENXIO;
681 spin_unlock_irq(&hpet_lock);
682 return hpet_ioctl_common(devp, cmd, arg, 1);
685 static ctl_table hpet_table[] = {
687 .ctl_name = 1,
688 .procname = "max-user-freq",
689 .data = &hpet_max_freq,
690 .maxlen = sizeof(int),
691 .mode = 0644,
692 .proc_handler = &proc_dointvec,
694 {.ctl_name = 0}
697 static ctl_table hpet_root[] = {
699 .ctl_name = 1,
700 .procname = "hpet",
701 .maxlen = 0,
702 .mode = 0555,
703 .child = hpet_table,
705 {.ctl_name = 0}
708 static ctl_table dev_root[] = {
710 .ctl_name = CTL_DEV,
711 .procname = "dev",
712 .maxlen = 0,
713 .mode = 0555,
714 .child = hpet_root,
716 {.ctl_name = 0}
719 static struct ctl_table_header *sysctl_header;
721 static void hpet_register_interpolator(struct hpets *hpetp)
723 #ifdef CONFIG_TIME_INTERPOLATION
724 struct time_interpolator *ti;
726 ti = kmalloc(sizeof(*ti), GFP_KERNEL);
727 if (!ti)
728 return;
730 memset(ti, 0, sizeof(*ti));
731 ti->source = TIME_SOURCE_MMIO64;
732 ti->shift = 10;
733 ti->addr = &hpetp->hp_hpet->hpet_mc;
734 ti->frequency = hpetp->hp_tick_freq;
735 ti->drift = HPET_DRIFT;
736 ti->mask = -1;
738 hpetp->hp_interpolator = ti;
739 register_time_interpolator(ti);
740 #endif
744 * Adjustment for when arming the timer with
745 * initial conditions. That is, main counter
746 * ticks expired before interrupts are enabled.
748 #define TICK_CALIBRATE (1000UL)
750 static unsigned long hpet_calibrate(struct hpets *hpetp)
752 struct hpet_timer __iomem *timer = NULL;
753 unsigned long t, m, count, i, flags, start;
754 struct hpet_dev *devp;
755 int j;
756 struct hpet __iomem *hpet;
758 for (j = 0, devp = hpetp->hp_dev; j < hpetp->hp_ntimer; j++, devp++)
759 if ((devp->hd_flags & HPET_OPEN) == 0) {
760 timer = devp->hd_timer;
761 break;
764 if (!timer)
765 return 0;
767 hpet = hpets->hp_hpet;
768 t = read_counter(&timer->hpet_compare);
770 i = 0;
771 count = hpet_time_div(hpetp, TICK_CALIBRATE);
773 local_irq_save(flags);
775 start = read_counter(&hpet->hpet_mc);
777 do {
778 m = read_counter(&hpet->hpet_mc);
779 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
780 } while (i++, (m - start) < count);
782 local_irq_restore(flags);
784 return (m - start) / i;
787 int hpet_alloc(struct hpet_data *hdp)
789 u64 cap, mcfg;
790 struct hpet_dev *devp;
791 u32 i, ntimer;
792 struct hpets *hpetp;
793 size_t siz;
794 struct hpet __iomem *hpet;
795 static struct hpets *last = (struct hpets *)0;
796 unsigned long ns, period;
797 unsigned long long temp;
800 * hpet_alloc can be called by platform dependent code.
801 * if platform dependent code has allocated the hpet
802 * ACPI also reports hpet, then we catch it here.
804 for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
805 if (hpetp->hp_hpet == hdp->hd_address)
806 return 0;
808 siz = sizeof(struct hpets) + ((hdp->hd_nirqs - 1) *
809 sizeof(struct hpet_dev));
811 hpetp = kmalloc(siz, GFP_KERNEL);
813 if (!hpetp)
814 return -ENOMEM;
816 memset(hpetp, 0, siz);
818 hpetp->hp_which = hpet_nhpet++;
819 hpetp->hp_hpet = hdp->hd_address;
820 hpetp->hp_hpet_phys = hdp->hd_phys_address;
822 hpetp->hp_ntimer = hdp->hd_nirqs;
824 for (i = 0; i < hdp->hd_nirqs; i++)
825 hpetp->hp_dev[i].hd_hdwirq = hdp->hd_irq[i];
827 hpet = hpetp->hp_hpet;
829 cap = readq(&hpet->hpet_cap);
831 ntimer = ((cap & HPET_NUM_TIM_CAP_MASK) >> HPET_NUM_TIM_CAP_SHIFT) + 1;
833 if (hpetp->hp_ntimer != ntimer) {
834 printk(KERN_WARNING "hpet: number irqs doesn't agree"
835 " with number of timers\n");
836 kfree(hpetp);
837 return -ENODEV;
840 if (last)
841 last->hp_next = hpetp;
842 else
843 hpets = hpetp;
845 last = hpetp;
847 period = (cap & HPET_COUNTER_CLK_PERIOD_MASK) >>
848 HPET_COUNTER_CLK_PERIOD_SHIFT; /* fs, 10^-15 */
849 temp = 1000000000000000uLL; /* 10^15 femtoseconds per second */
850 temp += period >> 1; /* round */
851 do_div(temp, period);
852 hpetp->hp_tick_freq = temp; /* ticks per second */
854 printk(KERN_INFO "hpet%d: at MMIO 0x%lx, IRQ%s",
855 hpetp->hp_which, hdp->hd_phys_address,
856 hpetp->hp_ntimer > 1 ? "s" : "");
857 for (i = 0; i < hpetp->hp_ntimer; i++)
858 printk("%s %d", i > 0 ? "," : "", hdp->hd_irq[i]);
859 printk("\n");
861 ns = period / 1000000; /* convert to nanoseconds, 10^-9 */
862 printk(KERN_INFO "hpet%d: %ldns tick, %d %d-bit timers\n",
863 hpetp->hp_which, ns, hpetp->hp_ntimer,
864 cap & HPET_COUNTER_SIZE_MASK ? 64 : 32);
866 mcfg = readq(&hpet->hpet_config);
867 if ((mcfg & HPET_ENABLE_CNF_MASK) == 0) {
868 write_counter(0L, &hpet->hpet_mc);
869 mcfg |= HPET_ENABLE_CNF_MASK;
870 writeq(mcfg, &hpet->hpet_config);
873 for (i = 0, devp = hpetp->hp_dev; i < hpetp->hp_ntimer; i++, devp++) {
874 struct hpet_timer __iomem *timer;
876 timer = &hpet->hpet_timers[devp - hpetp->hp_dev];
878 devp->hd_hpets = hpetp;
879 devp->hd_hpet = hpet;
880 devp->hd_timer = timer;
883 * If the timer was reserved by platform code,
884 * then make timer unavailable for opens.
886 if (hdp->hd_state & (1 << i)) {
887 devp->hd_flags = HPET_OPEN;
888 continue;
891 init_waitqueue_head(&devp->hd_waitqueue);
894 hpetp->hp_delta = hpet_calibrate(hpetp);
895 hpet_register_interpolator(hpetp);
897 return 0;
900 static acpi_status hpet_resources(struct acpi_resource *res, void *data)
902 struct hpet_data *hdp;
903 acpi_status status;
904 struct acpi_resource_address64 addr;
905 struct hpets *hpetp;
907 hdp = data;
909 status = acpi_resource_to_address64(res, &addr);
911 if (ACPI_SUCCESS(status)) {
912 unsigned long size;
914 size = addr.max_address_range - addr.min_address_range + 1;
915 hdp->hd_phys_address = addr.min_address_range;
916 hdp->hd_address = ioremap(addr.min_address_range, size);
918 for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
919 if (hpetp->hp_hpet == hdp->hd_address)
920 return -EBUSY;
921 } else if (res->id == ACPI_RSTYPE_EXT_IRQ) {
922 struct acpi_resource_ext_irq *irqp;
923 int i;
925 irqp = &res->data.extended_irq;
927 if (irqp->number_of_interrupts > 0) {
928 hdp->hd_nirqs = irqp->number_of_interrupts;
930 for (i = 0; i < hdp->hd_nirqs; i++) {
931 int rc =
932 acpi_register_gsi(irqp->interrupts[i],
933 irqp->edge_level,
934 irqp->active_high_low);
935 if (rc < 0)
936 return AE_ERROR;
937 hdp->hd_irq[i] = rc;
942 return AE_OK;
945 static int hpet_acpi_add(struct acpi_device *device)
947 acpi_status result;
948 struct hpet_data data;
950 memset(&data, 0, sizeof(data));
952 result =
953 acpi_walk_resources(device->handle, METHOD_NAME__CRS,
954 hpet_resources, &data);
956 if (ACPI_FAILURE(result))
957 return -ENODEV;
959 if (!data.hd_address || !data.hd_nirqs) {
960 printk("%s: no address or irqs in _CRS\n", __FUNCTION__);
961 return -ENODEV;
964 return hpet_alloc(&data);
967 static int hpet_acpi_remove(struct acpi_device *device, int type)
969 /* XXX need to unregister interpolator, dealloc mem, etc */
970 return -EINVAL;
973 static struct acpi_driver hpet_acpi_driver = {
974 .name = "hpet",
975 .ids = "PNP0103",
976 .ops = {
977 .add = hpet_acpi_add,
978 .remove = hpet_acpi_remove,
982 static struct miscdevice hpet_misc = { HPET_MINOR, "hpet", &hpet_fops };
984 static int __init hpet_init(void)
986 int result;
988 result = misc_register(&hpet_misc);
989 if (result < 0)
990 return -ENODEV;
992 sysctl_header = register_sysctl_table(dev_root, 0);
994 result = acpi_bus_register_driver(&hpet_acpi_driver);
995 if (result < 0) {
996 if (sysctl_header)
997 unregister_sysctl_table(sysctl_header);
998 misc_deregister(&hpet_misc);
999 return result;
1002 return 0;
1005 static void __exit hpet_exit(void)
1007 acpi_bus_unregister_driver(&hpet_acpi_driver);
1009 if (sysctl_header)
1010 unregister_sysctl_table(sysctl_header);
1011 misc_deregister(&hpet_misc);
1013 return;
1016 module_init(hpet_init);
1017 module_exit(hpet_exit);
1018 MODULE_AUTHOR("Bob Picco <Robert.Picco@hp.com>");
1019 MODULE_LICENSE("GPL");