[PATCH] hpet: remove unused variable
[safe/jmp/linux-2.6] / drivers / char / hpet.c
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
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>
32
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>
39
40 #include <linux/acpi.h>
41 #include <acpi/acpi_bus.h>
42 #include <linux/hpet.h>
43
44 /*
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
48  */
49 #define HPET_USER_FREQ  (64)
50 #define HPET_DRIFT      (500)
51
52 static u32 hpet_nhpet, hpet_max_freq = HPET_USER_FREQ;
53
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);
58
59 #define HPET_DEV_NAME   (7)
60
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];
74 };
75
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];
86 };
87
88 static struct hpets *hpets;
89
90 #define HPET_OPEN               0x0001
91 #define HPET_IE                 0x0002  /* interrupt enabled */
92 #define HPET_PERIODIC           0x0004
93
94 #if BITS_PER_LONG == 64
95 #define write_counter(V, MC)    writeq(V, MC)
96 #define read_counter(MC)        readq(MC)
97 #else
98 #define write_counter(V, MC)    writel(V, MC)
99 #define read_counter(MC)        readl(MC)
100 #endif
101
102 #ifndef readq
103 static inline unsigned long long readq(void __iomem *addr)
104 {
105         return readl(addr) | (((unsigned long long)readl(addr + 4)) << 32LL);
106 }
107 #endif
108
109 #ifndef writeq
110 static inline void writeq(unsigned long long v, void __iomem *addr)
111 {
112         writel(v & 0xffffffff, addr);
113         writel(v >> 32, addr + 4);
114 }
115 #endif
116
117 static irqreturn_t hpet_interrupt(int irq, void *data, struct pt_regs *regs)
118 {
119         struct hpet_dev *devp;
120         unsigned long isr;
121
122         devp = data;
123
124         spin_lock(&hpet_lock);
125         devp->hd_irqdata++;
126
127         /*
128          * For non-periodic timers, increment the accumulator.
129          * This has the effect of treating non-periodic like periodic.
130          */
131         if ((devp->hd_flags & (HPET_IE | HPET_PERIODIC)) == HPET_IE) {
132                 unsigned long m, t;
133
134                 t = devp->hd_ireqfreq;
135                 m = read_counter(&devp->hd_hpet->hpet_mc);
136                 write_counter(t + m + devp->hd_hpets->hp_delta,
137                               &devp->hd_timer->hpet_compare);
138         }
139
140         isr = (1 << (devp - devp->hd_hpets->hp_dev));
141         writeq(isr, &devp->hd_hpet->hpet_isr);
142         spin_unlock(&hpet_lock);
143
144         spin_lock(&hpet_task_lock);
145         if (devp->hd_task)
146                 devp->hd_task->ht_func(devp->hd_task->ht_data);
147         spin_unlock(&hpet_task_lock);
148
149         wake_up_interruptible(&devp->hd_waitqueue);
150
151         kill_fasync(&devp->hd_async_queue, SIGIO, POLL_IN);
152
153         return IRQ_HANDLED;
154 }
155
156 static int hpet_open(struct inode *inode, struct file *file)
157 {
158         struct hpet_dev *devp;
159         struct hpets *hpetp;
160         int i;
161
162         if (file->f_mode & FMODE_WRITE)
163                 return -EINVAL;
164
165         spin_lock_irq(&hpet_lock);
166
167         for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next)
168                 for (i = 0; i < hpetp->hp_ntimer; i++)
169                         if (hpetp->hp_dev[i].hd_flags & HPET_OPEN
170                             || hpetp->hp_dev[i].hd_task)
171                                 continue;
172                         else {
173                                 devp = &hpetp->hp_dev[i];
174                                 break;
175                         }
176
177         if (!devp) {
178                 spin_unlock_irq(&hpet_lock);
179                 return -EBUSY;
180         }
181
182         file->private_data = devp;
183         devp->hd_irqdata = 0;
184         devp->hd_flags |= HPET_OPEN;
185         spin_unlock_irq(&hpet_lock);
186
187         return 0;
188 }
189
190 static ssize_t
191 hpet_read(struct file *file, char __user *buf, size_t count, loff_t * ppos)
192 {
193         DECLARE_WAITQUEUE(wait, current);
194         unsigned long data;
195         ssize_t retval;
196         struct hpet_dev *devp;
197
198         devp = file->private_data;
199         if (!devp->hd_ireqfreq)
200                 return -EIO;
201
202         if (count < sizeof(unsigned long))
203                 return -EINVAL;
204
205         add_wait_queue(&devp->hd_waitqueue, &wait);
206
207         for ( ; ; ) {
208                 set_current_state(TASK_INTERRUPTIBLE);
209
210                 spin_lock_irq(&hpet_lock);
211                 data = devp->hd_irqdata;
212                 devp->hd_irqdata = 0;
213                 spin_unlock_irq(&hpet_lock);
214
215                 if (data)
216                         break;
217                 else if (file->f_flags & O_NONBLOCK) {
218                         retval = -EAGAIN;
219                         goto out;
220                 } else if (signal_pending(current)) {
221                         retval = -ERESTARTSYS;
222                         goto out;
223                 }
224                 schedule();
225         }
226
227         retval = put_user(data, (unsigned long __user *)buf);
228         if (!retval)
229                 retval = sizeof(unsigned long);
230 out:
231         __set_current_state(TASK_RUNNING);
232         remove_wait_queue(&devp->hd_waitqueue, &wait);
233
234         return retval;
235 }
236
237 static unsigned int hpet_poll(struct file *file, poll_table * wait)
238 {
239         unsigned long v;
240         struct hpet_dev *devp;
241
242         devp = file->private_data;
243
244         if (!devp->hd_ireqfreq)
245                 return 0;
246
247         poll_wait(file, &devp->hd_waitqueue, wait);
248
249         spin_lock_irq(&hpet_lock);
250         v = devp->hd_irqdata;
251         spin_unlock_irq(&hpet_lock);
252
253         if (v != 0)
254                 return POLLIN | POLLRDNORM;
255
256         return 0;
257 }
258
259 static int hpet_mmap(struct file *file, struct vm_area_struct *vma)
260 {
261 #ifdef  CONFIG_HPET_MMAP
262         struct hpet_dev *devp;
263         unsigned long addr;
264
265         if (((vma->vm_end - vma->vm_start) != PAGE_SIZE) || vma->vm_pgoff)
266                 return -EINVAL;
267
268         devp = file->private_data;
269         addr = devp->hd_hpets->hp_hpet_phys;
270
271         if (addr & (PAGE_SIZE - 1))
272                 return -ENOSYS;
273
274         vma->vm_flags |= VM_IO;
275         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
276
277         if (io_remap_pfn_range(vma, vma->vm_start, addr >> PAGE_SHIFT,
278                                         PAGE_SIZE, vma->vm_page_prot)) {
279                 printk(KERN_ERR "remap_pfn_range failed in hpet.c\n");
280                 return -EAGAIN;
281         }
282
283         return 0;
284 #else
285         return -ENOSYS;
286 #endif
287 }
288
289 static int hpet_fasync(int fd, struct file *file, int on)
290 {
291         struct hpet_dev *devp;
292
293         devp = file->private_data;
294
295         if (fasync_helper(fd, file, on, &devp->hd_async_queue) >= 0)
296                 return 0;
297         else
298                 return -EIO;
299 }
300
301 static int hpet_release(struct inode *inode, struct file *file)
302 {
303         struct hpet_dev *devp;
304         struct hpet_timer __iomem *timer;
305         int irq = 0;
306
307         devp = file->private_data;
308         timer = devp->hd_timer;
309
310         spin_lock_irq(&hpet_lock);
311
312         writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK),
313                &timer->hpet_config);
314
315         irq = devp->hd_irq;
316         devp->hd_irq = 0;
317
318         devp->hd_ireqfreq = 0;
319
320         if (devp->hd_flags & HPET_PERIODIC
321             && readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
322                 unsigned long v;
323
324                 v = readq(&timer->hpet_config);
325                 v ^= Tn_TYPE_CNF_MASK;
326                 writeq(v, &timer->hpet_config);
327         }
328
329         devp->hd_flags &= ~(HPET_OPEN | HPET_IE | HPET_PERIODIC);
330         spin_unlock_irq(&hpet_lock);
331
332         if (irq)
333                 free_irq(irq, devp);
334
335         if (file->f_flags & FASYNC)
336                 hpet_fasync(-1, file, 0);
337
338         file->private_data = NULL;
339         return 0;
340 }
341
342 static int hpet_ioctl_common(struct hpet_dev *, int, unsigned long, int);
343
344 static int
345 hpet_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
346            unsigned long arg)
347 {
348         struct hpet_dev *devp;
349
350         devp = file->private_data;
351         return hpet_ioctl_common(devp, cmd, arg, 0);
352 }
353
354 static int hpet_ioctl_ieon(struct hpet_dev *devp)
355 {
356         struct hpet_timer __iomem *timer;
357         struct hpet __iomem *hpet;
358         struct hpets *hpetp;
359         int irq;
360         unsigned long g, v, t, m;
361         unsigned long flags, isr;
362
363         timer = devp->hd_timer;
364         hpet = devp->hd_hpet;
365         hpetp = devp->hd_hpets;
366
367         if (!devp->hd_ireqfreq)
368                 return -EIO;
369
370         v = readq(&timer->hpet_config);
371         spin_lock_irq(&hpet_lock);
372
373         if (devp->hd_flags & HPET_IE) {
374                 spin_unlock_irq(&hpet_lock);
375                 return -EBUSY;
376         }
377
378         devp->hd_flags |= HPET_IE;
379         spin_unlock_irq(&hpet_lock);
380
381         t = readq(&timer->hpet_config);
382         irq = devp->hd_hdwirq;
383
384         if (irq) {
385                 sprintf(devp->hd_name, "hpet%d", (int)(devp - hpetp->hp_dev));
386
387                 if (request_irq
388                     (irq, hpet_interrupt, SA_INTERRUPT, devp->hd_name, (void *)devp)) {
389                         printk(KERN_ERR "hpet: IRQ %d is not free\n", irq);
390                         irq = 0;
391                 }
392         }
393
394         if (irq == 0) {
395                 spin_lock_irq(&hpet_lock);
396                 devp->hd_flags ^= HPET_IE;
397                 spin_unlock_irq(&hpet_lock);
398                 return -EIO;
399         }
400
401         devp->hd_irq = irq;
402         t = devp->hd_ireqfreq;
403         v = readq(&timer->hpet_config);
404         g = v | Tn_INT_ENB_CNF_MASK;
405
406         if (devp->hd_flags & HPET_PERIODIC) {
407                 write_counter(t, &timer->hpet_compare);
408                 g |= Tn_TYPE_CNF_MASK;
409                 v |= Tn_TYPE_CNF_MASK;
410                 writeq(v, &timer->hpet_config);
411                 v |= Tn_VAL_SET_CNF_MASK;
412                 writeq(v, &timer->hpet_config);
413                 local_irq_save(flags);
414                 m = read_counter(&hpet->hpet_mc);
415                 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
416         } else {
417                 local_irq_save(flags);
418                 m = read_counter(&hpet->hpet_mc);
419                 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
420         }
421
422         isr = (1 << (devp - hpets->hp_dev));
423         writeq(isr, &hpet->hpet_isr);
424         writeq(g, &timer->hpet_config);
425         local_irq_restore(flags);
426
427         return 0;
428 }
429
430 /* converts Hz to number of timer ticks */
431 static inline unsigned long hpet_time_div(struct hpets *hpets,
432                                           unsigned long dis)
433 {
434         unsigned long long m;
435
436         m = hpets->hp_tick_freq + (dis >> 1);
437         do_div(m, dis);
438         return (unsigned long)m;
439 }
440
441 static int
442 hpet_ioctl_common(struct hpet_dev *devp, int cmd, unsigned long arg, int kernel)
443 {
444         struct hpet_timer __iomem *timer;
445         struct hpet __iomem *hpet;
446         struct hpets *hpetp;
447         int err;
448         unsigned long v;
449
450         switch (cmd) {
451         case HPET_IE_OFF:
452         case HPET_INFO:
453         case HPET_EPI:
454         case HPET_DPI:
455         case HPET_IRQFREQ:
456                 timer = devp->hd_timer;
457                 hpet = devp->hd_hpet;
458                 hpetp = devp->hd_hpets;
459                 break;
460         case HPET_IE_ON:
461                 return hpet_ioctl_ieon(devp);
462         default:
463                 return -EINVAL;
464         }
465
466         err = 0;
467
468         switch (cmd) {
469         case HPET_IE_OFF:
470                 if ((devp->hd_flags & HPET_IE) == 0)
471                         break;
472                 v = readq(&timer->hpet_config);
473                 v &= ~Tn_INT_ENB_CNF_MASK;
474                 writeq(v, &timer->hpet_config);
475                 if (devp->hd_irq) {
476                         free_irq(devp->hd_irq, devp);
477                         devp->hd_irq = 0;
478                 }
479                 devp->hd_flags ^= HPET_IE;
480                 break;
481         case HPET_INFO:
482                 {
483                         struct hpet_info info;
484
485                         info.hi_ireqfreq = hpet_time_div(hpetp,
486                                                          devp->hd_ireqfreq);
487                         info.hi_flags =
488                             readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK;
489                         info.hi_hpet = devp->hd_hpets->hp_which;
490                         info.hi_timer = devp - devp->hd_hpets->hp_dev;
491                         if (copy_to_user((void __user *)arg, &info, sizeof(info)))
492                                 err = -EFAULT;
493                         break;
494                 }
495         case HPET_EPI:
496                 v = readq(&timer->hpet_config);
497                 if ((v & Tn_PER_INT_CAP_MASK) == 0) {
498                         err = -ENXIO;
499                         break;
500                 }
501                 devp->hd_flags |= HPET_PERIODIC;
502                 break;
503         case HPET_DPI:
504                 v = readq(&timer->hpet_config);
505                 if ((v & Tn_PER_INT_CAP_MASK) == 0) {
506                         err = -ENXIO;
507                         break;
508                 }
509                 if (devp->hd_flags & HPET_PERIODIC &&
510                     readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
511                         v = readq(&timer->hpet_config);
512                         v ^= Tn_TYPE_CNF_MASK;
513                         writeq(v, &timer->hpet_config);
514                 }
515                 devp->hd_flags &= ~HPET_PERIODIC;
516                 break;
517         case HPET_IRQFREQ:
518                 if (!kernel && (arg > hpet_max_freq) &&
519                     !capable(CAP_SYS_RESOURCE)) {
520                         err = -EACCES;
521                         break;
522                 }
523
524                 if (!arg || (arg & (arg - 1))) {
525                         err = -EINVAL;
526                         break;
527                 }
528
529                 devp->hd_ireqfreq = hpet_time_div(hpetp, arg);
530         }
531
532         return err;
533 }
534
535 static struct file_operations hpet_fops = {
536         .owner = THIS_MODULE,
537         .llseek = no_llseek,
538         .read = hpet_read,
539         .poll = hpet_poll,
540         .ioctl = hpet_ioctl,
541         .open = hpet_open,
542         .release = hpet_release,
543         .fasync = hpet_fasync,
544         .mmap = hpet_mmap,
545 };
546
547 EXPORT_SYMBOL(hpet_alloc);
548 EXPORT_SYMBOL(hpet_register);
549 EXPORT_SYMBOL(hpet_unregister);
550 EXPORT_SYMBOL(hpet_control);
551
552 int hpet_register(struct hpet_task *tp, int periodic)
553 {
554         unsigned int i;
555         u64 mask;
556         struct hpet_timer __iomem *timer;
557         struct hpet_dev *devp;
558         struct hpets *hpetp;
559
560         switch (periodic) {
561         case 1:
562                 mask = Tn_PER_INT_CAP_MASK;
563                 break;
564         case 0:
565                 mask = 0;
566                 break;
567         default:
568                 return -EINVAL;
569         }
570
571         spin_lock_irq(&hpet_task_lock);
572         spin_lock(&hpet_lock);
573
574         for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next)
575                 for (timer = hpetp->hp_hpet->hpet_timers, i = 0;
576                      i < hpetp->hp_ntimer; i++, timer++) {
577                         if ((readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK)
578                             != mask)
579                                 continue;
580
581                         devp = &hpetp->hp_dev[i];
582
583                         if (devp->hd_flags & HPET_OPEN || devp->hd_task) {
584                                 devp = NULL;
585                                 continue;
586                         }
587
588                         tp->ht_opaque = devp;
589                         devp->hd_task = tp;
590                         break;
591                 }
592
593         spin_unlock(&hpet_lock);
594         spin_unlock_irq(&hpet_task_lock);
595
596         if (tp->ht_opaque)
597                 return 0;
598         else
599                 return -EBUSY;
600 }
601
602 static inline int hpet_tpcheck(struct hpet_task *tp)
603 {
604         struct hpet_dev *devp;
605         struct hpets *hpetp;
606
607         devp = tp->ht_opaque;
608
609         if (!devp)
610                 return -ENXIO;
611
612         for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
613                 if (devp >= hpetp->hp_dev
614                     && devp < (hpetp->hp_dev + hpetp->hp_ntimer)
615                     && devp->hd_hpet == hpetp->hp_hpet)
616                         return 0;
617
618         return -ENXIO;
619 }
620
621 int hpet_unregister(struct hpet_task *tp)
622 {
623         struct hpet_dev *devp;
624         struct hpet_timer __iomem *timer;
625         int err;
626
627         if ((err = hpet_tpcheck(tp)))
628                 return err;
629
630         spin_lock_irq(&hpet_task_lock);
631         spin_lock(&hpet_lock);
632
633         devp = tp->ht_opaque;
634         if (devp->hd_task != tp) {
635                 spin_unlock(&hpet_lock);
636                 spin_unlock_irq(&hpet_task_lock);
637                 return -ENXIO;
638         }
639
640         timer = devp->hd_timer;
641         writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK),
642                &timer->hpet_config);
643         devp->hd_flags &= ~(HPET_IE | HPET_PERIODIC);
644         devp->hd_task = NULL;
645         spin_unlock(&hpet_lock);
646         spin_unlock_irq(&hpet_task_lock);
647
648         return 0;
649 }
650
651 int hpet_control(struct hpet_task *tp, unsigned int cmd, unsigned long arg)
652 {
653         struct hpet_dev *devp;
654         int err;
655
656         if ((err = hpet_tpcheck(tp)))
657                 return err;
658
659         spin_lock_irq(&hpet_lock);
660         devp = tp->ht_opaque;
661         if (devp->hd_task != tp) {
662                 spin_unlock_irq(&hpet_lock);
663                 return -ENXIO;
664         }
665         spin_unlock_irq(&hpet_lock);
666         return hpet_ioctl_common(devp, cmd, arg, 1);
667 }
668
669 static ctl_table hpet_table[] = {
670         {
671          .ctl_name = 1,
672          .procname = "max-user-freq",
673          .data = &hpet_max_freq,
674          .maxlen = sizeof(int),
675          .mode = 0644,
676          .proc_handler = &proc_dointvec,
677          },
678         {.ctl_name = 0}
679 };
680
681 static ctl_table hpet_root[] = {
682         {
683          .ctl_name = 1,
684          .procname = "hpet",
685          .maxlen = 0,
686          .mode = 0555,
687          .child = hpet_table,
688          },
689         {.ctl_name = 0}
690 };
691
692 static ctl_table dev_root[] = {
693         {
694          .ctl_name = CTL_DEV,
695          .procname = "dev",
696          .maxlen = 0,
697          .mode = 0555,
698          .child = hpet_root,
699          },
700         {.ctl_name = 0}
701 };
702
703 static struct ctl_table_header *sysctl_header;
704
705 static void hpet_register_interpolator(struct hpets *hpetp)
706 {
707 #ifdef  CONFIG_TIME_INTERPOLATION
708         struct time_interpolator *ti;
709
710         ti = kmalloc(sizeof(*ti), GFP_KERNEL);
711         if (!ti)
712                 return;
713
714         memset(ti, 0, sizeof(*ti));
715         ti->source = TIME_SOURCE_MMIO64;
716         ti->shift = 10;
717         ti->addr = &hpetp->hp_hpet->hpet_mc;
718         ti->frequency = hpetp->hp_tick_freq;
719         ti->drift = HPET_DRIFT;
720         ti->mask = -1;
721
722         hpetp->hp_interpolator = ti;
723         register_time_interpolator(ti);
724 #endif
725 }
726
727 /*
728  * Adjustment for when arming the timer with
729  * initial conditions.  That is, main counter
730  * ticks expired before interrupts are enabled.
731  */
732 #define TICK_CALIBRATE  (1000UL)
733
734 static unsigned long hpet_calibrate(struct hpets *hpetp)
735 {
736         struct hpet_timer __iomem *timer = NULL;
737         unsigned long t, m, count, i, flags, start;
738         struct hpet_dev *devp;
739         int j;
740         struct hpet __iomem *hpet;
741
742         for (j = 0, devp = hpetp->hp_dev; j < hpetp->hp_ntimer; j++, devp++)
743                 if ((devp->hd_flags & HPET_OPEN) == 0) {
744                         timer = devp->hd_timer;
745                         break;
746                 }
747
748         if (!timer)
749                 return 0;
750
751         hpet = hpets->hp_hpet;
752         t = read_counter(&timer->hpet_compare);
753
754         i = 0;
755         count = hpet_time_div(hpetp, TICK_CALIBRATE);
756
757         local_irq_save(flags);
758
759         start = read_counter(&hpet->hpet_mc);
760
761         do {
762                 m = read_counter(&hpet->hpet_mc);
763                 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
764         } while (i++, (m - start) < count);
765
766         local_irq_restore(flags);
767
768         return (m - start) / i;
769 }
770
771 int hpet_alloc(struct hpet_data *hdp)
772 {
773         u64 cap, mcfg;
774         struct hpet_dev *devp;
775         u32 i, ntimer;
776         struct hpets *hpetp;
777         size_t siz;
778         struct hpet __iomem *hpet;
779         static struct hpets *last = (struct hpets *)0;
780         unsigned long ns, period;
781         unsigned long long temp;
782
783         /*
784          * hpet_alloc can be called by platform dependent code.
785          * if platform dependent code has allocated the hpet
786          * ACPI also reports hpet, then we catch it here.
787          */
788         for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
789                 if (hpetp->hp_hpet == hdp->hd_address)
790                         return 0;
791
792         siz = sizeof(struct hpets) + ((hdp->hd_nirqs - 1) *
793                                       sizeof(struct hpet_dev));
794
795         hpetp = kmalloc(siz, GFP_KERNEL);
796
797         if (!hpetp)
798                 return -ENOMEM;
799
800         memset(hpetp, 0, siz);
801
802         hpetp->hp_which = hpet_nhpet++;
803         hpetp->hp_hpet = hdp->hd_address;
804         hpetp->hp_hpet_phys = hdp->hd_phys_address;
805
806         hpetp->hp_ntimer = hdp->hd_nirqs;
807
808         for (i = 0; i < hdp->hd_nirqs; i++)
809                 hpetp->hp_dev[i].hd_hdwirq = hdp->hd_irq[i];
810
811         hpet = hpetp->hp_hpet;
812
813         cap = readq(&hpet->hpet_cap);
814
815         ntimer = ((cap & HPET_NUM_TIM_CAP_MASK) >> HPET_NUM_TIM_CAP_SHIFT) + 1;
816
817         if (hpetp->hp_ntimer != ntimer) {
818                 printk(KERN_WARNING "hpet: number irqs doesn't agree"
819                        " with number of timers\n");
820                 kfree(hpetp);
821                 return -ENODEV;
822         }
823
824         if (last)
825                 last->hp_next = hpetp;
826         else
827                 hpets = hpetp;
828
829         last = hpetp;
830
831         period = (cap & HPET_COUNTER_CLK_PERIOD_MASK) >>
832                 HPET_COUNTER_CLK_PERIOD_SHIFT; /* fs, 10^-15 */
833         temp = 1000000000000000uLL; /* 10^15 femtoseconds per second */
834         temp += period >> 1; /* round */
835         do_div(temp, period);
836         hpetp->hp_tick_freq = temp; /* ticks per second */
837
838         printk(KERN_INFO "hpet%d: at MMIO 0x%lx, IRQ%s",
839                 hpetp->hp_which, hdp->hd_phys_address,
840                 hpetp->hp_ntimer > 1 ? "s" : "");
841         for (i = 0; i < hpetp->hp_ntimer; i++)
842                 printk("%s %d", i > 0 ? "," : "", hdp->hd_irq[i]);
843         printk("\n");
844
845         ns = period / 1000000;  /* convert to nanoseconds, 10^-9 */
846         printk(KERN_INFO "hpet%d: %ldns tick, %d %d-bit timers\n",
847                 hpetp->hp_which, ns, hpetp->hp_ntimer,
848                 cap & HPET_COUNTER_SIZE_MASK ? 64 : 32);
849
850         mcfg = readq(&hpet->hpet_config);
851         if ((mcfg & HPET_ENABLE_CNF_MASK) == 0) {
852                 write_counter(0L, &hpet->hpet_mc);
853                 mcfg |= HPET_ENABLE_CNF_MASK;
854                 writeq(mcfg, &hpet->hpet_config);
855         }
856
857         for (i = 0, devp = hpetp->hp_dev; i < hpetp->hp_ntimer; i++, devp++) {
858                 unsigned long v;
859                 struct hpet_timer __iomem *timer;
860
861                 timer = &hpet->hpet_timers[devp - hpetp->hp_dev];
862                 v = readq(&timer->hpet_config);
863
864                 devp->hd_hpets = hpetp;
865                 devp->hd_hpet = hpet;
866                 devp->hd_timer = timer;
867
868                 /*
869                  * If the timer was reserved by platform code,
870                  * then make timer unavailable for opens.
871                  */
872                 if (hdp->hd_state & (1 << i)) {
873                         devp->hd_flags = HPET_OPEN;
874                         continue;
875                 }
876
877                 init_waitqueue_head(&devp->hd_waitqueue);
878         }
879
880         hpetp->hp_delta = hpet_calibrate(hpetp);
881         hpet_register_interpolator(hpetp);
882
883         return 0;
884 }
885
886 static acpi_status hpet_resources(struct acpi_resource *res, void *data)
887 {
888         struct hpet_data *hdp;
889         acpi_status status;
890         struct acpi_resource_address64 addr;
891         struct hpets *hpetp;
892
893         hdp = data;
894
895         status = acpi_resource_to_address64(res, &addr);
896
897         if (ACPI_SUCCESS(status)) {
898                 unsigned long size;
899
900                 size = addr.max_address_range - addr.min_address_range + 1;
901                 hdp->hd_phys_address = addr.min_address_range;
902                 hdp->hd_address = ioremap(addr.min_address_range, size);
903
904                 for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
905                         if (hpetp->hp_hpet == hdp->hd_address)
906                                 return -EBUSY;
907         } else if (res->id == ACPI_RSTYPE_EXT_IRQ) {
908                 struct acpi_resource_ext_irq *irqp;
909                 int i;
910
911                 irqp = &res->data.extended_irq;
912
913                 if (irqp->number_of_interrupts > 0) {
914                         hdp->hd_nirqs = irqp->number_of_interrupts;
915
916                         for (i = 0; i < hdp->hd_nirqs; i++) {
917                                 int rc =
918                                     acpi_register_gsi(irqp->interrupts[i],
919                                                       irqp->edge_level,
920                                                       irqp->active_high_low);
921                                 if (rc < 0)
922                                         return AE_ERROR;
923                                 hdp->hd_irq[i] = rc;
924                         }
925                 }
926         }
927
928         return AE_OK;
929 }
930
931 static int hpet_acpi_add(struct acpi_device *device)
932 {
933         acpi_status result;
934         struct hpet_data data;
935
936         memset(&data, 0, sizeof(data));
937
938         result =
939             acpi_walk_resources(device->handle, METHOD_NAME__CRS,
940                                 hpet_resources, &data);
941
942         if (ACPI_FAILURE(result))
943                 return -ENODEV;
944
945         if (!data.hd_address || !data.hd_nirqs) {
946                 printk("%s: no address or irqs in _CRS\n", __FUNCTION__);
947                 return -ENODEV;
948         }
949
950         return hpet_alloc(&data);
951 }
952
953 static int hpet_acpi_remove(struct acpi_device *device, int type)
954 {
955         /* XXX need to unregister interpolator, dealloc mem, etc */
956         return -EINVAL;
957 }
958
959 static struct acpi_driver hpet_acpi_driver = {
960         .name = "hpet",
961         .ids = "PNP0103",
962         .ops = {
963                 .add = hpet_acpi_add,
964                 .remove = hpet_acpi_remove,
965                 },
966 };
967
968 static struct miscdevice hpet_misc = { HPET_MINOR, "hpet", &hpet_fops };
969
970 static int __init hpet_init(void)
971 {
972         int result;
973
974         result = misc_register(&hpet_misc);
975         if (result < 0)
976                 return -ENODEV;
977
978         sysctl_header = register_sysctl_table(dev_root, 0);
979
980         result = acpi_bus_register_driver(&hpet_acpi_driver);
981         if (result < 0) {
982                 if (sysctl_header)
983                         unregister_sysctl_table(sysctl_header);
984                 misc_deregister(&hpet_misc);
985                 return result;
986         }
987
988         return 0;
989 }
990
991 static void __exit hpet_exit(void)
992 {
993         acpi_bus_unregister_driver(&hpet_acpi_driver);
994
995         if (sysctl_header)
996                 unregister_sysctl_table(sysctl_header);
997         misc_deregister(&hpet_misc);
998
999         return;
1000 }
1001
1002 module_init(hpet_init);
1003 module_exit(hpet_exit);
1004 MODULE_AUTHOR("Bob Picco <Robert.Picco@hp.com>");
1005 MODULE_LICENSE("GPL");