2 * Intel & MS High Precision Event Timer Implementation.
4 * Copyright (C) 2003 Intel Corporation
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/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/smp_lock.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>
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>
33 #include <linux/clocksource.h>
35 #include <asm/current.h>
36 #include <asm/uaccess.h>
37 #include <asm/system.h>
40 #include <asm/div64.h>
42 #include <linux/acpi.h>
43 #include <acpi/acpi_bus.h>
44 #include <linux/hpet.h>
47 * The High Precision Event Timer driver.
48 * This driver is closely modelled after the rtc.c driver.
49 * http://www.intel.com/hardwaredesign/hpetspec.htm
51 #define HPET_USER_FREQ (64)
52 #define HPET_DRIFT (500)
54 #define HPET_RANGE_SIZE 1024 /* from HPET spec */
56 #if BITS_PER_LONG == 64
57 #define write_counter(V, MC) writeq(V, MC)
58 #define read_counter(MC) readq(MC)
60 #define write_counter(V, MC) writel(V, MC)
61 #define read_counter(MC) readl(MC)
64 static u32 hpet_nhpet
, hpet_max_freq
= HPET_USER_FREQ
;
66 /* This clocksource driver currently only works on ia64 */
68 static void __iomem
*hpet_mctr
;
70 static cycle_t
read_hpet(void)
72 return (cycle_t
)read_counter((void __iomem
*)hpet_mctr
);
75 static struct clocksource clocksource_hpet
= {
79 .mask
= CLOCKSOURCE_MASK(64),
80 .mult
= 0, /*to be caluclated*/
82 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
84 static struct clocksource
*hpet_clocksource
;
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
);
92 #define HPET_DEV_NAME (7)
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
;
105 unsigned int hd_hdwirq
;
106 char hd_name
[HPET_DEV_NAME
];
110 struct hpets
*hp_next
;
111 struct hpet __iomem
*hp_hpet
;
112 unsigned long hp_hpet_phys
;
113 struct clocksource
*hp_clocksource
;
114 unsigned long long hp_tick_freq
;
115 unsigned long hp_delta
;
116 unsigned int hp_ntimer
;
117 unsigned int hp_which
;
118 struct hpet_dev hp_dev
[1];
121 static struct hpets
*hpets
;
123 #define HPET_OPEN 0x0001
124 #define HPET_IE 0x0002 /* interrupt enabled */
125 #define HPET_PERIODIC 0x0004
126 #define HPET_SHARED_IRQ 0x0008
130 static inline unsigned long long readq(void __iomem
*addr
)
132 return readl(addr
) | (((unsigned long long)readl(addr
+ 4)) << 32LL);
137 static inline void writeq(unsigned long long v
, void __iomem
*addr
)
139 writel(v
& 0xffffffff, addr
);
140 writel(v
>> 32, addr
+ 4);
144 static irqreturn_t
hpet_interrupt(int irq
, void *data
)
146 struct hpet_dev
*devp
;
150 isr
= 1 << (devp
- devp
->hd_hpets
->hp_dev
);
152 if ((devp
->hd_flags
& HPET_SHARED_IRQ
) &&
153 !(isr
& readl(&devp
->hd_hpet
->hpet_isr
)))
156 spin_lock(&hpet_lock
);
160 * For non-periodic timers, increment the accumulator.
161 * This has the effect of treating non-periodic like periodic.
163 if ((devp
->hd_flags
& (HPET_IE
| HPET_PERIODIC
)) == HPET_IE
) {
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
);
172 if (devp
->hd_flags
& HPET_SHARED_IRQ
)
173 writel(isr
, &devp
->hd_hpet
->hpet_isr
);
174 spin_unlock(&hpet_lock
);
176 spin_lock(&hpet_task_lock
);
178 devp
->hd_task
->ht_func(devp
->hd_task
->ht_data
);
179 spin_unlock(&hpet_task_lock
);
181 wake_up_interruptible(&devp
->hd_waitqueue
);
183 kill_fasync(&devp
->hd_async_queue
, SIGIO
, POLL_IN
);
188 static int hpet_open(struct inode
*inode
, struct file
*file
)
190 struct hpet_dev
*devp
;
194 if (file
->f_mode
& FMODE_WRITE
)
198 spin_lock_irq(&hpet_lock
);
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
)
206 devp
= &hpetp
->hp_dev
[i
];
211 spin_unlock_irq(&hpet_lock
);
216 file
->private_data
= devp
;
217 devp
->hd_irqdata
= 0;
218 devp
->hd_flags
|= HPET_OPEN
;
219 spin_unlock_irq(&hpet_lock
);
226 hpet_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
* ppos
)
228 DECLARE_WAITQUEUE(wait
, current
);
231 struct hpet_dev
*devp
;
233 devp
= file
->private_data
;
234 if (!devp
->hd_ireqfreq
)
237 if (count
< sizeof(unsigned long))
240 add_wait_queue(&devp
->hd_waitqueue
, &wait
);
243 set_current_state(TASK_INTERRUPTIBLE
);
245 spin_lock_irq(&hpet_lock
);
246 data
= devp
->hd_irqdata
;
247 devp
->hd_irqdata
= 0;
248 spin_unlock_irq(&hpet_lock
);
252 else if (file
->f_flags
& O_NONBLOCK
) {
255 } else if (signal_pending(current
)) {
256 retval
= -ERESTARTSYS
;
262 retval
= put_user(data
, (unsigned long __user
*)buf
);
264 retval
= sizeof(unsigned long);
266 __set_current_state(TASK_RUNNING
);
267 remove_wait_queue(&devp
->hd_waitqueue
, &wait
);
272 static unsigned int hpet_poll(struct file
*file
, poll_table
* wait
)
275 struct hpet_dev
*devp
;
277 devp
= file
->private_data
;
279 if (!devp
->hd_ireqfreq
)
282 poll_wait(file
, &devp
->hd_waitqueue
, wait
);
284 spin_lock_irq(&hpet_lock
);
285 v
= devp
->hd_irqdata
;
286 spin_unlock_irq(&hpet_lock
);
289 return POLLIN
| POLLRDNORM
;
294 static int hpet_mmap(struct file
*file
, struct vm_area_struct
*vma
)
296 #ifdef CONFIG_HPET_MMAP
297 struct hpet_dev
*devp
;
300 if (((vma
->vm_end
- vma
->vm_start
) != PAGE_SIZE
) || vma
->vm_pgoff
)
303 devp
= file
->private_data
;
304 addr
= devp
->hd_hpets
->hp_hpet_phys
;
306 if (addr
& (PAGE_SIZE
- 1))
309 vma
->vm_flags
|= VM_IO
;
310 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
312 if (io_remap_pfn_range(vma
, vma
->vm_start
, addr
>> PAGE_SHIFT
,
313 PAGE_SIZE
, vma
->vm_page_prot
)) {
314 printk(KERN_ERR
"%s: io_remap_pfn_range failed\n",
325 static int hpet_fasync(int fd
, struct file
*file
, int on
)
327 struct hpet_dev
*devp
;
329 devp
= file
->private_data
;
331 if (fasync_helper(fd
, file
, on
, &devp
->hd_async_queue
) >= 0)
337 static int hpet_release(struct inode
*inode
, struct file
*file
)
339 struct hpet_dev
*devp
;
340 struct hpet_timer __iomem
*timer
;
343 devp
= file
->private_data
;
344 timer
= devp
->hd_timer
;
346 spin_lock_irq(&hpet_lock
);
348 writeq((readq(&timer
->hpet_config
) & ~Tn_INT_ENB_CNF_MASK
),
349 &timer
->hpet_config
);
354 devp
->hd_ireqfreq
= 0;
356 if (devp
->hd_flags
& HPET_PERIODIC
357 && readq(&timer
->hpet_config
) & Tn_TYPE_CNF_MASK
) {
360 v
= readq(&timer
->hpet_config
);
361 v
^= Tn_TYPE_CNF_MASK
;
362 writeq(v
, &timer
->hpet_config
);
365 devp
->hd_flags
&= ~(HPET_OPEN
| HPET_IE
| HPET_PERIODIC
);
366 spin_unlock_irq(&hpet_lock
);
371 if (file
->f_flags
& FASYNC
)
372 hpet_fasync(-1, file
, 0);
374 file
->private_data
= NULL
;
378 static int hpet_ioctl_common(struct hpet_dev
*, int, unsigned long, int);
381 hpet_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
384 struct hpet_dev
*devp
;
386 devp
= file
->private_data
;
387 return hpet_ioctl_common(devp
, cmd
, arg
, 0);
390 static int hpet_ioctl_ieon(struct hpet_dev
*devp
)
392 struct hpet_timer __iomem
*timer
;
393 struct hpet __iomem
*hpet
;
396 unsigned long g
, v
, t
, m
;
397 unsigned long flags
, isr
;
399 timer
= devp
->hd_timer
;
400 hpet
= devp
->hd_hpet
;
401 hpetp
= devp
->hd_hpets
;
403 if (!devp
->hd_ireqfreq
)
406 spin_lock_irq(&hpet_lock
);
408 if (devp
->hd_flags
& HPET_IE
) {
409 spin_unlock_irq(&hpet_lock
);
413 devp
->hd_flags
|= HPET_IE
;
415 if (readl(&timer
->hpet_config
) & Tn_INT_TYPE_CNF_MASK
)
416 devp
->hd_flags
|= HPET_SHARED_IRQ
;
417 spin_unlock_irq(&hpet_lock
);
419 irq
= devp
->hd_hdwirq
;
422 unsigned long irq_flags
;
424 sprintf(devp
->hd_name
, "hpet%d", (int)(devp
- hpetp
->hp_dev
));
425 irq_flags
= devp
->hd_flags
& HPET_SHARED_IRQ
426 ? IRQF_SHARED
: IRQF_DISABLED
;
427 if (request_irq(irq
, hpet_interrupt
, irq_flags
,
428 devp
->hd_name
, (void *)devp
)) {
429 printk(KERN_ERR
"hpet: IRQ %d is not free\n", irq
);
435 spin_lock_irq(&hpet_lock
);
436 devp
->hd_flags
^= HPET_IE
;
437 spin_unlock_irq(&hpet_lock
);
442 t
= devp
->hd_ireqfreq
;
443 v
= readq(&timer
->hpet_config
);
444 g
= v
| Tn_INT_ENB_CNF_MASK
;
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
);
457 local_irq_save(flags
);
458 m
= read_counter(&hpet
->hpet_mc
);
459 write_counter(t
+ m
+ hpetp
->hp_delta
, &timer
->hpet_compare
);
462 if (devp
->hd_flags
& HPET_SHARED_IRQ
) {
463 isr
= 1 << (devp
- devp
->hd_hpets
->hp_dev
);
464 writel(isr
, &hpet
->hpet_isr
);
466 writeq(g
, &timer
->hpet_config
);
467 local_irq_restore(flags
);
472 /* converts Hz to number of timer ticks */
473 static inline unsigned long hpet_time_div(struct hpets
*hpets
,
476 unsigned long long m
;
478 m
= hpets
->hp_tick_freq
+ (dis
>> 1);
480 return (unsigned long)m
;
484 hpet_ioctl_common(struct hpet_dev
*devp
, int cmd
, unsigned long arg
, int kernel
)
486 struct hpet_timer __iomem
*timer
;
487 struct hpet __iomem
*hpet
;
498 timer
= devp
->hd_timer
;
499 hpet
= devp
->hd_hpet
;
500 hpetp
= devp
->hd_hpets
;
503 return hpet_ioctl_ieon(devp
);
512 if ((devp
->hd_flags
& HPET_IE
) == 0)
514 v
= readq(&timer
->hpet_config
);
515 v
&= ~Tn_INT_ENB_CNF_MASK
;
516 writeq(v
, &timer
->hpet_config
);
518 free_irq(devp
->hd_irq
, devp
);
521 devp
->hd_flags
^= HPET_IE
;
525 struct hpet_info info
;
527 if (devp
->hd_ireqfreq
)
529 hpet_time_div(hpetp
, devp
->hd_ireqfreq
);
531 info
.hi_ireqfreq
= 0;
533 readq(&timer
->hpet_config
) & Tn_PER_INT_CAP_MASK
;
534 info
.hi_hpet
= hpetp
->hp_which
;
535 info
.hi_timer
= devp
- hpetp
->hp_dev
;
537 memcpy((void *)arg
, &info
, sizeof(info
));
539 if (copy_to_user((void __user
*)arg
, &info
,
545 v
= readq(&timer
->hpet_config
);
546 if ((v
& Tn_PER_INT_CAP_MASK
) == 0) {
550 devp
->hd_flags
|= HPET_PERIODIC
;
553 v
= readq(&timer
->hpet_config
);
554 if ((v
& Tn_PER_INT_CAP_MASK
) == 0) {
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
);
564 devp
->hd_flags
&= ~HPET_PERIODIC
;
567 if (!kernel
&& (arg
> hpet_max_freq
) &&
568 !capable(CAP_SYS_RESOURCE
)) {
578 devp
->hd_ireqfreq
= hpet_time_div(hpetp
, arg
);
584 static const struct file_operations hpet_fops
= {
585 .owner
= THIS_MODULE
,
591 .release
= hpet_release
,
592 .fasync
= hpet_fasync
,
596 static int hpet_is_known(struct hpet_data
*hdp
)
600 for (hpetp
= hpets
; hpetp
; hpetp
= hpetp
->hp_next
)
601 if (hpetp
->hp_hpet_phys
== hdp
->hd_phys_address
)
607 static inline int hpet_tpcheck(struct hpet_task
*tp
)
609 struct hpet_dev
*devp
;
612 devp
= tp
->ht_opaque
;
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
)
626 int hpet_unregister(struct hpet_task
*tp
)
628 struct hpet_dev
*devp
;
629 struct hpet_timer __iomem
*timer
;
632 if ((err
= hpet_tpcheck(tp
)))
635 spin_lock_irq(&hpet_task_lock
);
636 spin_lock(&hpet_lock
);
638 devp
= tp
->ht_opaque
;
639 if (devp
->hd_task
!= tp
) {
640 spin_unlock(&hpet_lock
);
641 spin_unlock_irq(&hpet_task_lock
);
645 timer
= devp
->hd_timer
;
646 writeq((readq(&timer
->hpet_config
) & ~Tn_INT_ENB_CNF_MASK
),
647 &timer
->hpet_config
);
648 devp
->hd_flags
&= ~(HPET_IE
| HPET_PERIODIC
);
649 devp
->hd_task
= NULL
;
650 spin_unlock(&hpet_lock
);
651 spin_unlock_irq(&hpet_task_lock
);
656 static ctl_table hpet_table
[] = {
658 .ctl_name
= CTL_UNNUMBERED
,
659 .procname
= "max-user-freq",
660 .data
= &hpet_max_freq
,
661 .maxlen
= sizeof(int),
663 .proc_handler
= &proc_dointvec
,
668 static ctl_table hpet_root
[] = {
670 .ctl_name
= CTL_UNNUMBERED
,
679 static ctl_table dev_root
[] = {
690 static struct ctl_table_header
*sysctl_header
;
693 * Adjustment for when arming the timer with
694 * initial conditions. That is, main counter
695 * ticks expired before interrupts are enabled.
697 #define TICK_CALIBRATE (1000UL)
699 static unsigned long hpet_calibrate(struct hpets
*hpetp
)
701 struct hpet_timer __iomem
*timer
= NULL
;
702 unsigned long t
, m
, count
, i
, flags
, start
;
703 struct hpet_dev
*devp
;
705 struct hpet __iomem
*hpet
;
707 for (j
= 0, devp
= hpetp
->hp_dev
; j
< hpetp
->hp_ntimer
; j
++, devp
++)
708 if ((devp
->hd_flags
& HPET_OPEN
) == 0) {
709 timer
= devp
->hd_timer
;
716 hpet
= hpetp
->hp_hpet
;
717 t
= read_counter(&timer
->hpet_compare
);
720 count
= hpet_time_div(hpetp
, TICK_CALIBRATE
);
722 local_irq_save(flags
);
724 start
= read_counter(&hpet
->hpet_mc
);
727 m
= read_counter(&hpet
->hpet_mc
);
728 write_counter(t
+ m
+ hpetp
->hp_delta
, &timer
->hpet_compare
);
729 } while (i
++, (m
- start
) < count
);
731 local_irq_restore(flags
);
733 return (m
- start
) / i
;
736 int hpet_alloc(struct hpet_data
*hdp
)
739 struct hpet_dev
*devp
;
743 struct hpet __iomem
*hpet
;
744 static struct hpets
*last
= NULL
;
745 unsigned long period
;
746 unsigned long long temp
;
749 * hpet_alloc can be called by platform dependent code.
750 * If platform dependent code has allocated the hpet that
751 * ACPI has also reported, then we catch it here.
753 if (hpet_is_known(hdp
)) {
754 printk(KERN_DEBUG
"%s: duplicate HPET ignored\n",
759 siz
= sizeof(struct hpets
) + ((hdp
->hd_nirqs
- 1) *
760 sizeof(struct hpet_dev
));
762 hpetp
= kzalloc(siz
, GFP_KERNEL
);
767 hpetp
->hp_which
= hpet_nhpet
++;
768 hpetp
->hp_hpet
= hdp
->hd_address
;
769 hpetp
->hp_hpet_phys
= hdp
->hd_phys_address
;
771 hpetp
->hp_ntimer
= hdp
->hd_nirqs
;
773 for (i
= 0; i
< hdp
->hd_nirqs
; i
++)
774 hpetp
->hp_dev
[i
].hd_hdwirq
= hdp
->hd_irq
[i
];
776 hpet
= hpetp
->hp_hpet
;
778 cap
= readq(&hpet
->hpet_cap
);
780 ntimer
= ((cap
& HPET_NUM_TIM_CAP_MASK
) >> HPET_NUM_TIM_CAP_SHIFT
) + 1;
782 if (hpetp
->hp_ntimer
!= ntimer
) {
783 printk(KERN_WARNING
"hpet: number irqs doesn't agree"
784 " with number of timers\n");
790 last
->hp_next
= hpetp
;
796 period
= (cap
& HPET_COUNTER_CLK_PERIOD_MASK
) >>
797 HPET_COUNTER_CLK_PERIOD_SHIFT
; /* fs, 10^-15 */
798 temp
= 1000000000000000uLL; /* 10^15 femtoseconds per second */
799 temp
+= period
>> 1; /* round */
800 do_div(temp
, period
);
801 hpetp
->hp_tick_freq
= temp
; /* ticks per second */
803 printk(KERN_INFO
"hpet%d: at MMIO 0x%lx, IRQ%s",
804 hpetp
->hp_which
, hdp
->hd_phys_address
,
805 hpetp
->hp_ntimer
> 1 ? "s" : "");
806 for (i
= 0; i
< hpetp
->hp_ntimer
; i
++)
807 printk("%s %d", i
> 0 ? "," : "", hdp
->hd_irq
[i
]);
810 printk(KERN_INFO
"hpet%u: %u %d-bit timers, %Lu Hz\n",
811 hpetp
->hp_which
, hpetp
->hp_ntimer
,
812 cap
& HPET_COUNTER_SIZE_MASK
? 64 : 32, hpetp
->hp_tick_freq
);
814 mcfg
= readq(&hpet
->hpet_config
);
815 if ((mcfg
& HPET_ENABLE_CNF_MASK
) == 0) {
816 write_counter(0L, &hpet
->hpet_mc
);
817 mcfg
|= HPET_ENABLE_CNF_MASK
;
818 writeq(mcfg
, &hpet
->hpet_config
);
821 for (i
= 0, devp
= hpetp
->hp_dev
; i
< hpetp
->hp_ntimer
; i
++, devp
++) {
822 struct hpet_timer __iomem
*timer
;
824 timer
= &hpet
->hpet_timers
[devp
- hpetp
->hp_dev
];
826 devp
->hd_hpets
= hpetp
;
827 devp
->hd_hpet
= hpet
;
828 devp
->hd_timer
= timer
;
831 * If the timer was reserved by platform code,
832 * then make timer unavailable for opens.
834 if (hdp
->hd_state
& (1 << i
)) {
835 devp
->hd_flags
= HPET_OPEN
;
839 init_waitqueue_head(&devp
->hd_waitqueue
);
842 hpetp
->hp_delta
= hpet_calibrate(hpetp
);
844 /* This clocksource driver currently only works on ia64 */
846 if (!hpet_clocksource
) {
847 hpet_mctr
= (void __iomem
*)&hpetp
->hp_hpet
->hpet_mc
;
848 CLKSRC_FSYS_MMIO_SET(clocksource_hpet
.fsys_mmio
, hpet_mctr
);
849 clocksource_hpet
.mult
= clocksource_hz2mult(hpetp
->hp_tick_freq
,
850 clocksource_hpet
.shift
);
851 clocksource_register(&clocksource_hpet
);
852 hpetp
->hp_clocksource
= &clocksource_hpet
;
853 hpet_clocksource
= &clocksource_hpet
;
860 static acpi_status
hpet_resources(struct acpi_resource
*res
, void *data
)
862 struct hpet_data
*hdp
;
864 struct acpi_resource_address64 addr
;
868 status
= acpi_resource_to_address64(res
, &addr
);
870 if (ACPI_SUCCESS(status
)) {
871 hdp
->hd_phys_address
= addr
.minimum
;
872 hdp
->hd_address
= ioremap(addr
.minimum
, addr
.address_length
);
874 if (hpet_is_known(hdp
)) {
875 printk(KERN_DEBUG
"%s: 0x%lx is busy\n",
876 __func__
, hdp
->hd_phys_address
);
877 iounmap(hdp
->hd_address
);
878 return AE_ALREADY_EXISTS
;
880 } else if (res
->type
== ACPI_RESOURCE_TYPE_FIXED_MEMORY32
) {
881 struct acpi_resource_fixed_memory32
*fixmem32
;
883 fixmem32
= &res
->data
.fixed_memory32
;
887 hdp
->hd_phys_address
= fixmem32
->address
;
888 hdp
->hd_address
= ioremap(fixmem32
->address
,
891 if (hpet_is_known(hdp
)) {
892 printk(KERN_DEBUG
"%s: 0x%lx is busy\n",
893 __func__
, hdp
->hd_phys_address
);
894 iounmap(hdp
->hd_address
);
895 return AE_ALREADY_EXISTS
;
897 } else if (res
->type
== ACPI_RESOURCE_TYPE_EXTENDED_IRQ
) {
898 struct acpi_resource_extended_irq
*irqp
;
901 irqp
= &res
->data
.extended_irq
;
903 for (i
= 0; i
< irqp
->interrupt_count
; i
++) {
904 irq
= acpi_register_gsi(irqp
->interrupts
[i
],
905 irqp
->triggering
, irqp
->polarity
);
909 hdp
->hd_irq
[hdp
->hd_nirqs
] = irq
;
917 static int hpet_acpi_add(struct acpi_device
*device
)
920 struct hpet_data data
;
922 memset(&data
, 0, sizeof(data
));
925 acpi_walk_resources(device
->handle
, METHOD_NAME__CRS
,
926 hpet_resources
, &data
);
928 if (ACPI_FAILURE(result
))
931 if (!data
.hd_address
|| !data
.hd_nirqs
) {
932 printk("%s: no address or irqs in _CRS\n", __func__
);
936 return hpet_alloc(&data
);
939 static int hpet_acpi_remove(struct acpi_device
*device
, int type
)
941 /* XXX need to unregister clocksource, dealloc mem, etc */
945 static const struct acpi_device_id hpet_device_ids
[] = {
949 MODULE_DEVICE_TABLE(acpi
, hpet_device_ids
);
951 static struct acpi_driver hpet_acpi_driver
= {
953 .ids
= hpet_device_ids
,
955 .add
= hpet_acpi_add
,
956 .remove
= hpet_acpi_remove
,
960 static struct miscdevice hpet_misc
= { HPET_MINOR
, "hpet", &hpet_fops
};
962 static int __init
hpet_init(void)
966 result
= misc_register(&hpet_misc
);
970 sysctl_header
= register_sysctl_table(dev_root
);
972 result
= acpi_bus_register_driver(&hpet_acpi_driver
);
975 unregister_sysctl_table(sysctl_header
);
976 misc_deregister(&hpet_misc
);
983 static void __exit
hpet_exit(void)
985 acpi_bus_unregister_driver(&hpet_acpi_driver
);
988 unregister_sysctl_table(sysctl_header
);
989 misc_deregister(&hpet_misc
);
994 module_init(hpet_init
);
995 module_exit(hpet_exit
);
996 MODULE_AUTHOR("Bob Picco <Robert.Picco@hp.com>");
997 MODULE_LICENSE("GPL");