[PATCH] hpet: disallow zero interrupt frequency
[linux-2.6.git] / 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_ntimer, 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 hp_period;
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 static inline unsigned long hpet_time_div(unsigned long dis)
431 {
432         unsigned long long m = 1000000000000000ULL;
433
434         do_div(m, dis);
435
436         return (unsigned long)m;
437 }
438
439 static int
440 hpet_ioctl_common(struct hpet_dev *devp, int cmd, unsigned long arg, int kernel)
441 {
442         struct hpet_timer __iomem *timer;
443         struct hpet __iomem *hpet;
444         struct hpets *hpetp;
445         int err;
446         unsigned long v;
447
448         switch (cmd) {
449         case HPET_IE_OFF:
450         case HPET_INFO:
451         case HPET_EPI:
452         case HPET_DPI:
453         case HPET_IRQFREQ:
454                 timer = devp->hd_timer;
455                 hpet = devp->hd_hpet;
456                 hpetp = devp->hd_hpets;
457                 break;
458         case HPET_IE_ON:
459                 return hpet_ioctl_ieon(devp);
460         default:
461                 return -EINVAL;
462         }
463
464         err = 0;
465
466         switch (cmd) {
467         case HPET_IE_OFF:
468                 if ((devp->hd_flags & HPET_IE) == 0)
469                         break;
470                 v = readq(&timer->hpet_config);
471                 v &= ~Tn_INT_ENB_CNF_MASK;
472                 writeq(v, &timer->hpet_config);
473                 if (devp->hd_irq) {
474                         free_irq(devp->hd_irq, devp);
475                         devp->hd_irq = 0;
476                 }
477                 devp->hd_flags ^= HPET_IE;
478                 break;
479         case HPET_INFO:
480                 {
481                         struct hpet_info info;
482
483                         info.hi_ireqfreq = hpet_time_div(hpetp->hp_period *
484                                                          devp->hd_ireqfreq);
485                         info.hi_flags =
486                             readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK;
487                         info.hi_hpet = devp->hd_hpets->hp_which;
488                         info.hi_timer = devp - devp->hd_hpets->hp_dev;
489                         if (copy_to_user((void __user *)arg, &info, sizeof(info)))
490                                 err = -EFAULT;
491                         break;
492                 }
493         case HPET_EPI:
494                 v = readq(&timer->hpet_config);
495                 if ((v & Tn_PER_INT_CAP_MASK) == 0) {
496                         err = -ENXIO;
497                         break;
498                 }
499                 devp->hd_flags |= HPET_PERIODIC;
500                 break;
501         case HPET_DPI:
502                 v = readq(&timer->hpet_config);
503                 if ((v & Tn_PER_INT_CAP_MASK) == 0) {
504                         err = -ENXIO;
505                         break;
506                 }
507                 if (devp->hd_flags & HPET_PERIODIC &&
508                     readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
509                         v = readq(&timer->hpet_config);
510                         v ^= Tn_TYPE_CNF_MASK;
511                         writeq(v, &timer->hpet_config);
512                 }
513                 devp->hd_flags &= ~HPET_PERIODIC;
514                 break;
515         case HPET_IRQFREQ:
516                 if (!kernel && (arg > hpet_max_freq) &&
517                     !capable(CAP_SYS_RESOURCE)) {
518                         err = -EACCES;
519                         break;
520                 }
521
522                 if (!arg || (arg & (arg - 1))) {
523                         err = -EINVAL;
524                         break;
525                 }
526
527                 devp->hd_ireqfreq = hpet_time_div(hpetp->hp_period * arg);
528         }
529
530         return err;
531 }
532
533 static struct file_operations hpet_fops = {
534         .owner = THIS_MODULE,
535         .llseek = no_llseek,
536         .read = hpet_read,
537         .poll = hpet_poll,
538         .ioctl = hpet_ioctl,
539         .open = hpet_open,
540         .release = hpet_release,
541         .fasync = hpet_fasync,
542         .mmap = hpet_mmap,
543 };
544
545 EXPORT_SYMBOL(hpet_alloc);
546 EXPORT_SYMBOL(hpet_register);
547 EXPORT_SYMBOL(hpet_unregister);
548 EXPORT_SYMBOL(hpet_control);
549
550 int hpet_register(struct hpet_task *tp, int periodic)
551 {
552         unsigned int i;
553         u64 mask;
554         struct hpet_timer __iomem *timer;
555         struct hpet_dev *devp;
556         struct hpets *hpetp;
557
558         switch (periodic) {
559         case 1:
560                 mask = Tn_PER_INT_CAP_MASK;
561                 break;
562         case 0:
563                 mask = 0;
564                 break;
565         default:
566                 return -EINVAL;
567         }
568
569         spin_lock_irq(&hpet_task_lock);
570         spin_lock(&hpet_lock);
571
572         for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next)
573                 for (timer = hpetp->hp_hpet->hpet_timers, i = 0;
574                      i < hpetp->hp_ntimer; i++, timer++) {
575                         if ((readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK)
576                             != mask)
577                                 continue;
578
579                         devp = &hpetp->hp_dev[i];
580
581                         if (devp->hd_flags & HPET_OPEN || devp->hd_task) {
582                                 devp = NULL;
583                                 continue;
584                         }
585
586                         tp->ht_opaque = devp;
587                         devp->hd_task = tp;
588                         break;
589                 }
590
591         spin_unlock(&hpet_lock);
592         spin_unlock_irq(&hpet_task_lock);
593
594         if (tp->ht_opaque)
595                 return 0;
596         else
597                 return -EBUSY;
598 }
599
600 static inline int hpet_tpcheck(struct hpet_task *tp)
601 {
602         struct hpet_dev *devp;
603         struct hpets *hpetp;
604
605         devp = tp->ht_opaque;
606
607         if (!devp)
608                 return -ENXIO;
609
610         for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
611                 if (devp >= hpetp->hp_dev
612                     && devp < (hpetp->hp_dev + hpetp->hp_ntimer)
613                     && devp->hd_hpet == hpetp->hp_hpet)
614                         return 0;
615
616         return -ENXIO;
617 }
618
619 int hpet_unregister(struct hpet_task *tp)
620 {
621         struct hpet_dev *devp;
622         struct hpet_timer __iomem *timer;
623         int err;
624
625         if ((err = hpet_tpcheck(tp)))
626                 return err;
627
628         spin_lock_irq(&hpet_task_lock);
629         spin_lock(&hpet_lock);
630
631         devp = tp->ht_opaque;
632         if (devp->hd_task != tp) {
633                 spin_unlock(&hpet_lock);
634                 spin_unlock_irq(&hpet_task_lock);
635                 return -ENXIO;
636         }
637
638         timer = devp->hd_timer;
639         writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK),
640                &timer->hpet_config);
641         devp->hd_flags &= ~(HPET_IE | HPET_PERIODIC);
642         devp->hd_task = NULL;
643         spin_unlock(&hpet_lock);
644         spin_unlock_irq(&hpet_task_lock);
645
646         return 0;
647 }
648
649 int hpet_control(struct hpet_task *tp, unsigned int cmd, unsigned long arg)
650 {
651         struct hpet_dev *devp;
652         int err;
653
654         if ((err = hpet_tpcheck(tp)))
655                 return err;
656
657         spin_lock_irq(&hpet_lock);
658         devp = tp->ht_opaque;
659         if (devp->hd_task != tp) {
660                 spin_unlock_irq(&hpet_lock);
661                 return -ENXIO;
662         }
663         spin_unlock_irq(&hpet_lock);
664         return hpet_ioctl_common(devp, cmd, arg, 1);
665 }
666
667 static ctl_table hpet_table[] = {
668         {
669          .ctl_name = 1,
670          .procname = "max-user-freq",
671          .data = &hpet_max_freq,
672          .maxlen = sizeof(int),
673          .mode = 0644,
674          .proc_handler = &proc_dointvec,
675          },
676         {.ctl_name = 0}
677 };
678
679 static ctl_table hpet_root[] = {
680         {
681          .ctl_name = 1,
682          .procname = "hpet",
683          .maxlen = 0,
684          .mode = 0555,
685          .child = hpet_table,
686          },
687         {.ctl_name = 0}
688 };
689
690 static ctl_table dev_root[] = {
691         {
692          .ctl_name = CTL_DEV,
693          .procname = "dev",
694          .maxlen = 0,
695          .mode = 0555,
696          .child = hpet_root,
697          },
698         {.ctl_name = 0}
699 };
700
701 static struct ctl_table_header *sysctl_header;
702
703 static void hpet_register_interpolator(struct hpets *hpetp)
704 {
705 #ifdef  CONFIG_TIME_INTERPOLATION
706         struct time_interpolator *ti;
707
708         ti = kmalloc(sizeof(*ti), GFP_KERNEL);
709         if (!ti)
710                 return;
711
712         memset(ti, 0, sizeof(*ti));
713         ti->source = TIME_SOURCE_MMIO64;
714         ti->shift = 10;
715         ti->addr = &hpetp->hp_hpet->hpet_mc;
716         ti->frequency = hpet_time_div(hpets->hp_period);
717         ti->drift = HPET_DRIFT;
718         ti->mask = -1;
719
720         hpetp->hp_interpolator = ti;
721         register_time_interpolator(ti);
722 #endif
723 }
724
725 /*
726  * Adjustment for when arming the timer with
727  * initial conditions.  That is, main counter
728  * ticks expired before interrupts are enabled.
729  */
730 #define TICK_CALIBRATE  (1000UL)
731
732 static unsigned long hpet_calibrate(struct hpets *hpetp)
733 {
734         struct hpet_timer __iomem *timer = NULL;
735         unsigned long t, m, count, i, flags, start;
736         struct hpet_dev *devp;
737         int j;
738         struct hpet __iomem *hpet;
739
740         for (j = 0, devp = hpetp->hp_dev; j < hpetp->hp_ntimer; j++, devp++)
741                 if ((devp->hd_flags & HPET_OPEN) == 0) {
742                         timer = devp->hd_timer;
743                         break;
744                 }
745
746         if (!timer)
747                 return 0;
748
749         hpet = hpets->hp_hpet;
750         t = read_counter(&timer->hpet_compare);
751
752         i = 0;
753         count = hpet_time_div(hpetp->hp_period * TICK_CALIBRATE);
754
755         local_irq_save(flags);
756
757         start = read_counter(&hpet->hpet_mc);
758
759         do {
760                 m = read_counter(&hpet->hpet_mc);
761                 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
762         } while (i++, (m - start) < count);
763
764         local_irq_restore(flags);
765
766         return (m - start) / i;
767 }
768
769 int hpet_alloc(struct hpet_data *hdp)
770 {
771         u64 cap, mcfg;
772         struct hpet_dev *devp;
773         u32 i, ntimer;
774         struct hpets *hpetp;
775         size_t siz;
776         struct hpet __iomem *hpet;
777         static struct hpets *last = (struct hpets *)0;
778         unsigned long ns;
779
780         /*
781          * hpet_alloc can be called by platform dependent code.
782          * if platform dependent code has allocated the hpet
783          * ACPI also reports hpet, then we catch it here.
784          */
785         for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
786                 if (hpetp->hp_hpet == hdp->hd_address)
787                         return 0;
788
789         siz = sizeof(struct hpets) + ((hdp->hd_nirqs - 1) *
790                                       sizeof(struct hpet_dev));
791
792         hpetp = kmalloc(siz, GFP_KERNEL);
793
794         if (!hpetp)
795                 return -ENOMEM;
796
797         memset(hpetp, 0, siz);
798
799         hpetp->hp_which = hpet_nhpet++;
800         hpetp->hp_hpet = hdp->hd_address;
801         hpetp->hp_hpet_phys = hdp->hd_phys_address;
802
803         hpetp->hp_ntimer = hdp->hd_nirqs;
804
805         for (i = 0; i < hdp->hd_nirqs; i++)
806                 hpetp->hp_dev[i].hd_hdwirq = hdp->hd_irq[i];
807
808         hpet = hpetp->hp_hpet;
809
810         cap = readq(&hpet->hpet_cap);
811
812         ntimer = ((cap & HPET_NUM_TIM_CAP_MASK) >> HPET_NUM_TIM_CAP_SHIFT) + 1;
813
814         if (hpetp->hp_ntimer != ntimer) {
815                 printk(KERN_WARNING "hpet: number irqs doesn't agree"
816                        " with number of timers\n");
817                 kfree(hpetp);
818                 return -ENODEV;
819         }
820
821         if (last)
822                 last->hp_next = hpetp;
823         else
824                 hpets = hpetp;
825
826         last = hpetp;
827
828         hpetp->hp_period = (cap & HPET_COUNTER_CLK_PERIOD_MASK) >>
829             HPET_COUNTER_CLK_PERIOD_SHIFT;
830
831         printk(KERN_INFO "hpet%d: at MMIO 0x%lx, IRQ%s",
832                 hpetp->hp_which, hdp->hd_phys_address,
833                 hpetp->hp_ntimer > 1 ? "s" : "");
834         for (i = 0; i < hpetp->hp_ntimer; i++)
835                 printk("%s %d", i > 0 ? "," : "", hdp->hd_irq[i]);
836         printk("\n");
837
838         ns = hpetp->hp_period;  /* femptoseconds, 10^-15 */
839         ns /= 1000000;          /* convert to nanoseconds, 10^-9 */
840         printk(KERN_INFO "hpet%d: %ldns tick, %d %d-bit timers\n",
841                 hpetp->hp_which, ns, hpetp->hp_ntimer,
842                 cap & HPET_COUNTER_SIZE_MASK ? 64 : 32);
843
844         mcfg = readq(&hpet->hpet_config);
845         if ((mcfg & HPET_ENABLE_CNF_MASK) == 0) {
846                 write_counter(0L, &hpet->hpet_mc);
847                 mcfg |= HPET_ENABLE_CNF_MASK;
848                 writeq(mcfg, &hpet->hpet_config);
849         }
850
851         for (i = 0, devp = hpetp->hp_dev; i < hpetp->hp_ntimer;
852              i++, hpet_ntimer++, devp++) {
853                 unsigned long v;
854                 struct hpet_timer __iomem *timer;
855
856                 timer = &hpet->hpet_timers[devp - hpetp->hp_dev];
857                 v = readq(&timer->hpet_config);
858
859                 devp->hd_hpets = hpetp;
860                 devp->hd_hpet = hpet;
861                 devp->hd_timer = timer;
862
863                 /*
864                  * If the timer was reserved by platform code,
865                  * then make timer unavailable for opens.
866                  */
867                 if (hdp->hd_state & (1 << i)) {
868                         devp->hd_flags = HPET_OPEN;
869                         continue;
870                 }
871
872                 init_waitqueue_head(&devp->hd_waitqueue);
873         }
874
875         hpetp->hp_delta = hpet_calibrate(hpetp);
876         hpet_register_interpolator(hpetp);
877
878         return 0;
879 }
880
881 static acpi_status hpet_resources(struct acpi_resource *res, void *data)
882 {
883         struct hpet_data *hdp;
884         acpi_status status;
885         struct acpi_resource_address64 addr;
886         struct hpets *hpetp;
887
888         hdp = data;
889
890         status = acpi_resource_to_address64(res, &addr);
891
892         if (ACPI_SUCCESS(status)) {
893                 unsigned long size;
894
895                 size = addr.max_address_range - addr.min_address_range + 1;
896                 hdp->hd_phys_address = addr.min_address_range;
897                 hdp->hd_address = ioremap(addr.min_address_range, size);
898
899                 for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
900                         if (hpetp->hp_hpet == hdp->hd_address)
901                                 return -EBUSY;
902         } else if (res->id == ACPI_RSTYPE_EXT_IRQ) {
903                 struct acpi_resource_ext_irq *irqp;
904                 int i;
905
906                 irqp = &res->data.extended_irq;
907
908                 if (irqp->number_of_interrupts > 0) {
909                         hdp->hd_nirqs = irqp->number_of_interrupts;
910
911                         for (i = 0; i < hdp->hd_nirqs; i++) {
912                                 int rc =
913                                     acpi_register_gsi(irqp->interrupts[i],
914                                                       irqp->edge_level,
915                                                       irqp->active_high_low);
916                                 if (rc < 0)
917                                         return AE_ERROR;
918                                 hdp->hd_irq[i] = rc;
919                         }
920                 }
921         }
922
923         return AE_OK;
924 }
925
926 static int hpet_acpi_add(struct acpi_device *device)
927 {
928         acpi_status result;
929         struct hpet_data data;
930
931         memset(&data, 0, sizeof(data));
932
933         result =
934             acpi_walk_resources(device->handle, METHOD_NAME__CRS,
935                                 hpet_resources, &data);
936
937         if (ACPI_FAILURE(result))
938                 return -ENODEV;
939
940         if (!data.hd_address || !data.hd_nirqs) {
941                 printk("%s: no address or irqs in _CRS\n", __FUNCTION__);
942                 return -ENODEV;
943         }
944
945         return hpet_alloc(&data);
946 }
947
948 static int hpet_acpi_remove(struct acpi_device *device, int type)
949 {
950         /* XXX need to unregister interpolator, dealloc mem, etc */
951         return -EINVAL;
952 }
953
954 static struct acpi_driver hpet_acpi_driver = {
955         .name = "hpet",
956         .ids = "PNP0103",
957         .ops = {
958                 .add = hpet_acpi_add,
959                 .remove = hpet_acpi_remove,
960                 },
961 };
962
963 static struct miscdevice hpet_misc = { HPET_MINOR, "hpet", &hpet_fops };
964
965 static int __init hpet_init(void)
966 {
967         int result;
968
969         result = misc_register(&hpet_misc);
970         if (result < 0)
971                 return -ENODEV;
972
973         sysctl_header = register_sysctl_table(dev_root, 0);
974
975         result = acpi_bus_register_driver(&hpet_acpi_driver);
976         if (result < 0) {
977                 if (sysctl_header)
978                         unregister_sysctl_table(sysctl_header);
979                 misc_deregister(&hpet_misc);
980                 return result;
981         }
982
983         return 0;
984 }
985
986 static void __exit hpet_exit(void)
987 {
988         acpi_bus_unregister_driver(&hpet_acpi_driver);
989
990         if (sysctl_header)
991                 unregister_sysctl_table(sysctl_header);
992         misc_deregister(&hpet_misc);
993
994         return;
995 }
996
997 module_init(hpet_init);
998 module_exit(hpet_exit);
999 MODULE_AUTHOR("Bob Picco <Robert.Picco@hp.com>");
1000 MODULE_LICENSE("GPL");