15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/types.h>
21 #include <linux/fcntl.h>
23 #include <linux/poll.h>
27 #include <linux/sysctl.h>
28 #include <linux/wait.h>
31 #include <linux/bitops.h>
35 #include <linux/slab.h>
38 #include <asm/current.h>
40 #include <asm/div64.h>
44 #include <linux/hpet.h>
51 #define HPET_USER_FREQ (64)
52 #define HPET_DRIFT (500)
54 #define HPET_RANGE_SIZE 1024
61 #if BITS_PER_LONG == 64
62 #define write_counter(V, MC) writeq(V, MC)
63 #define read_counter(MC) readq(MC)
65 #define write_counter(V, MC) writel(V, MC)
66 #define read_counter(MC) readl(MC)
94 #define HPET_DEV_NAME (7)
124 #define HPET_OPEN 0x0001
125 #define HPET_IE 0x0002
126 #define HPET_PERIODIC 0x0004
127 #define HPET_SHARED_IRQ 0x0008
140 writel(v & 0xffffffff, addr);
141 writel(v >> 32, addr + 4);
151 isr = 1 << (devp - devp->
hd_hpets->hp_dev);
157 spin_lock(&hpet_lock);
167 struct hpets *hpetp = devp->
hd_hpets;
188 k = (mc - base + hpetp->
hp_delta) / t;
195 spin_unlock(&hpet_lock);
204 static void hpet_timer_set_irq(
struct hpet_dev *devp)
210 spin_lock_irq(&hpet_lock);
212 spin_unlock_irq(&hpet_lock);
219 v =
readl(&timer->hpet_config);
222 writel(v, &timer->hpet_config);
224 spin_unlock_irq(&hpet_lock);
253 spin_lock_irq(&hpet_lock);
254 v =
readl(&timer->hpet_config);
256 writel(v, &timer->hpet_config);
258 spin_unlock_irq(&hpet_lock);
273 spin_lock_irq(&hpet_lock);
275 for (devp =
NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->
hp_next)
285 spin_unlock_irq(&hpet_lock);
293 spin_unlock_irq(&hpet_lock);
296 hpet_timer_set_irq(devp);
313 if (count <
sizeof(
unsigned long))
321 spin_lock_irq(&hpet_lock);
324 spin_unlock_irq(&hpet_lock);
331 }
else if (signal_pending(
current)) {
338 retval =
put_user(data, (
unsigned long __user *)buf);
340 retval =
sizeof(
unsigned long);
360 spin_lock_irq(&hpet_lock);
362 spin_unlock_irq(&hpet_lock);
370 static int hpet_mmap(
struct file *file,
struct vm_area_struct *vma)
372 #ifdef CONFIG_HPET_MMAP
380 addr = devp->
hd_hpets->hp_hpet_phys;
413 static int hpet_release(
struct inode *inode,
struct file *file)
422 spin_lock_irq(&hpet_lock);
425 &timer->hpet_config);
436 v =
readq(&timer->hpet_config);
438 writeq(v, &timer->hpet_config);
442 spin_unlock_irq(&hpet_lock);
451 static int hpet_ioctl_ieon(
struct hpet_dev *devp)
457 unsigned long g,
v,
t,
m;
467 spin_lock_irq(&hpet_lock);
470 spin_unlock_irq(&hpet_lock);
476 if (
readl(&timer->hpet_config) & Tn_INT_TYPE_CNF_MASK)
478 spin_unlock_irq(&hpet_lock);
483 unsigned long irq_flags;
492 &timer->hpet_config);
494 &timer->hpet_compare);
496 isr = 1 << (devp - devp->
hd_hpets->hp_dev);
504 devp->
hd_name, (
void *)devp)) {
511 spin_lock_irq(&hpet_lock);
513 spin_unlock_irq(&hpet_lock);
519 v =
readq(&timer->hpet_config);
529 writeq(v, &timer->hpet_config);
553 isr = 1 << (devp - devp->
hd_hpets->hp_dev);
556 writeq(g, &timer->hpet_config);
563 static inline unsigned long hpet_time_div(
struct hpets *hpets,
566 unsigned long long m;
570 return (
unsigned long)
m;
574 hpet_ioctl_common(
struct hpet_dev *devp,
int cmd,
unsigned long arg,
594 return hpet_ioctl_ieon(devp);
605 v =
readq(&timer->hpet_config);
607 writeq(v, &timer->hpet_config);
616 memset(info, 0,
sizeof(*info));
627 v =
readq(&timer->hpet_config);
635 v =
readq(&timer->hpet_config);
636 if ((v & Tn_PER_INT_CAP_MASK) == 0) {
642 v =
readq(&timer->hpet_config);
644 writeq(v, &timer->hpet_config);
649 if ((arg > hpet_max_freq) &&
667 hpet_ioctl(
struct file *file,
unsigned int cmd,
unsigned long arg)
673 err = hpet_ioctl_common(file->
private_data, cmd, arg, &info);
677 (
copy_to_user((
void __user *)arg, &info,
sizeof(info))))
684 struct compat_hpet_info {
687 unsigned short hi_hpet;
688 unsigned short hi_timer;
692 hpet_compat_ioctl(
struct file *file,
unsigned int cmd,
unsigned long arg)
698 err = hpet_ioctl_common(file->
private_data, cmd, arg, &info);
702 struct compat_hpet_info
__user *
u = compat_ptr(arg);
719 .unlocked_ioctl = hpet_ioctl,
721 .compat_ioctl = hpet_compat_ioctl,
724 .release = hpet_release,
729 static int hpet_is_known(
struct hpet_data *hdp)
733 for (hpetp = hpets; hpetp; hpetp = hpetp->
hp_next)
743 .data = &hpet_max_freq,
744 .maxlen =
sizeof(
int),
778 #define TICK_CALIBRATE (1000UL)
780 static unsigned long __hpet_calibrate(
struct hpets *hpetp)
782 struct hpet_timer __iomem *timer =
NULL;
786 struct hpet __iomem *hpet;
788 for (j = 0, devp = hpetp->
hp_dev; j < hpetp->hp_ntimer; j++, devp++)
789 if ((devp->
hd_flags & HPET_OPEN) == 0) {
810 }
while (i++, (m - start) < count);
814 return (m - start) /
i;
817 static unsigned long hpet_calibrate(
struct hpets *hpetp)
819 unsigned long ret = -1;
828 tmp = __hpet_calibrate(hpetp);
844 struct hpet __iomem *hpet;
845 static struct hpets *
last;
847 unsigned long long temp;
855 if (hpet_is_known(hdp)) {
861 siz =
sizeof(
struct hpets) + ((hdp->hd_nirqs - 1) *
870 hpetp->
hp_hpet = hdp->hd_address;
875 for (i = 0; i < hdp->hd_nirqs; i++)
876 hpetp->
hp_dev[i].hd_hdwirq = hdp->hd_irq[i];
886 " with number of timers\n");
900 temp = 1000000000000000uLL;
906 hpetp->
hp_which, hdp->hd_phys_address,
913 remainder =
do_div(temp, 1000000);
915 "hpet%u: %u comparators, %d-bit %u.%06u MHz counter\n",
918 (
unsigned) temp, remainder);
927 for (i = 0, devp = hpetp->
hp_dev; i < hpetp->hp_ntimer; i++, devp++) {
928 struct hpet_timer __iomem *
timer;
940 if (hdp->hd_state & (1 << i)) {
948 hpetp->
hp_delta = hpet_calibrate(hpetp);
952 if (!hpet_clocksource) {
953 hpet_mctr = (
void __iomem *)&hpetp->
hp_hpet->hpet_mc;
954 clocksource_hpet.archdata.fsys_mmio = hpet_mctr;
955 clocksource_register_hz(&clocksource_hpet, hpetp->
hp_tick_freq);
957 hpet_clocksource = &clocksource_hpet;
975 hdp->hd_phys_address = addr.minimum;
976 hdp->hd_address =
ioremap(addr.minimum, addr.address_length);
978 if (hpet_is_known(hdp)) {
989 hdp->hd_phys_address = fixmem32->
address;
993 if (hpet_is_known(hdp)) {
1009 hdp->hd_irq[hdp->hd_nirqs] = irq;
1017 static int hpet_acpi_add(
struct acpi_device *
device)
1022 memset(&data, 0,
sizeof(data));
1026 hpet_resources, &data);
1031 if (!data.hd_address || !data.hd_nirqs) {
1032 if (data.hd_address)
1034 printk(
"%s: no address or irqs in _CRS\n", __func__);
1041 static int hpet_acpi_remove(
struct acpi_device *device,
int type)
1053 static struct acpi_driver hpet_acpi_driver = {
1055 .ids = hpet_device_ids,
1057 .add = hpet_acpi_add,
1058 .remove = hpet_acpi_remove,
1064 static int __init hpet_init(
void)
1085 static void __exit hpet_exit(
void)