blob: 20dc3be5ecfcf8fe987e41313902f41acdc4a2e0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/interrupt.h>
15#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/types.h>
18#include <linux/miscdevice.h>
19#include <linux/major.h>
20#include <linux/ioport.h>
21#include <linux/fcntl.h>
22#include <linux/init.h>
23#include <linux/poll.h>
Al Virof23f6e02006-10-20 15:17:02 -040024#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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.
Alex Williamson96803822005-09-06 15:17:54 -070047 * http://www.intel.com/hardwaredesign/hpetspec.htm
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49#define HPET_USER_FREQ (64)
50#define HPET_DRIFT (500)
51
Randy Dunlap757c4722005-10-30 15:03:42 -080052#define HPET_RANGE_SIZE 1024 /* from HPET spec */
53
Clemens Ladisch642d30b2005-10-30 15:03:31 -080054static u32 hpet_nhpet, hpet_max_freq = HPET_USER_FREQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/* A lock for concurrent access by app and isr hpet activity. */
57static DEFINE_SPINLOCK(hpet_lock);
58/* A lock for concurrent intermodule access to hpet and isr hpet activity. */
59static DEFINE_SPINLOCK(hpet_task_lock);
60
61#define HPET_DEV_NAME (7)
62
63struct hpet_dev {
64 struct hpets *hd_hpets;
65 struct hpet __iomem *hd_hpet;
66 struct hpet_timer __iomem *hd_timer;
67 unsigned long hd_ireqfreq;
68 unsigned long hd_irqdata;
69 wait_queue_head_t hd_waitqueue;
70 struct fasync_struct *hd_async_queue;
71 struct hpet_task *hd_task;
72 unsigned int hd_flags;
73 unsigned int hd_irq;
74 unsigned int hd_hdwirq;
75 char hd_name[HPET_DEV_NAME];
76};
77
78struct hpets {
79 struct hpets *hp_next;
80 struct hpet __iomem *hp_hpet;
81 unsigned long hp_hpet_phys;
82 struct time_interpolator *hp_interpolator;
Clemens Ladischba3f2132005-10-30 15:03:31 -080083 unsigned long long hp_tick_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 unsigned long hp_delta;
85 unsigned int hp_ntimer;
86 unsigned int hp_which;
87 struct hpet_dev hp_dev[1];
88};
89
90static struct hpets *hpets;
91
92#define HPET_OPEN 0x0001
93#define HPET_IE 0x0002 /* interrupt enabled */
94#define HPET_PERIODIC 0x0004
Clemens Ladisch0d290862005-10-30 15:03:34 -080095#define HPET_SHARED_IRQ 0x0008
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97#if BITS_PER_LONG == 64
98#define write_counter(V, MC) writeq(V, MC)
99#define read_counter(MC) readq(MC)
100#else
101#define write_counter(V, MC) writel(V, MC)
102#define read_counter(MC) readl(MC)
103#endif
104
105#ifndef readq
Adrian Bunk887c27f2005-09-10 00:26:52 -0700106static inline unsigned long long readq(void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 return readl(addr) | (((unsigned long long)readl(addr + 4)) << 32LL);
109}
110#endif
111
112#ifndef writeq
Adrian Bunk887c27f2005-09-10 00:26:52 -0700113static inline void writeq(unsigned long long v, void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 writel(v & 0xffffffff, addr);
116 writel(v >> 32, addr + 4);
117}
118#endif
119
David Howells7d12e782006-10-05 14:55:46 +0100120static irqreturn_t hpet_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 struct hpet_dev *devp;
123 unsigned long isr;
124
125 devp = data;
Clemens Ladisch0d290862005-10-30 15:03:34 -0800126 isr = 1 << (devp - devp->hd_hpets->hp_dev);
127
128 if ((devp->hd_flags & HPET_SHARED_IRQ) &&
129 !(isr & readl(&devp->hd_hpet->hpet_isr)))
130 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 spin_lock(&hpet_lock);
133 devp->hd_irqdata++;
134
135 /*
136 * For non-periodic timers, increment the accumulator.
137 * This has the effect of treating non-periodic like periodic.
138 */
139 if ((devp->hd_flags & (HPET_IE | HPET_PERIODIC)) == HPET_IE) {
140 unsigned long m, t;
141
142 t = devp->hd_ireqfreq;
143 m = read_counter(&devp->hd_hpet->hpet_mc);
144 write_counter(t + m + devp->hd_hpets->hp_delta,
145 &devp->hd_timer->hpet_compare);
146 }
147
Clemens Ladisch0d290862005-10-30 15:03:34 -0800148 if (devp->hd_flags & HPET_SHARED_IRQ)
149 writel(isr, &devp->hd_hpet->hpet_isr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 spin_unlock(&hpet_lock);
151
152 spin_lock(&hpet_task_lock);
153 if (devp->hd_task)
154 devp->hd_task->ht_func(devp->hd_task->ht_data);
155 spin_unlock(&hpet_task_lock);
156
157 wake_up_interruptible(&devp->hd_waitqueue);
158
159 kill_fasync(&devp->hd_async_queue, SIGIO, POLL_IN);
160
161 return IRQ_HANDLED;
162}
163
164static int hpet_open(struct inode *inode, struct file *file)
165{
166 struct hpet_dev *devp;
167 struct hpets *hpetp;
168 int i;
169
170 if (file->f_mode & FMODE_WRITE)
171 return -EINVAL;
172
173 spin_lock_irq(&hpet_lock);
174
175 for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next)
176 for (i = 0; i < hpetp->hp_ntimer; i++)
177 if (hpetp->hp_dev[i].hd_flags & HPET_OPEN
178 || hpetp->hp_dev[i].hd_task)
179 continue;
180 else {
181 devp = &hpetp->hp_dev[i];
182 break;
183 }
184
185 if (!devp) {
186 spin_unlock_irq(&hpet_lock);
187 return -EBUSY;
188 }
189
190 file->private_data = devp;
191 devp->hd_irqdata = 0;
192 devp->hd_flags |= HPET_OPEN;
193 spin_unlock_irq(&hpet_lock);
194
195 return 0;
196}
197
198static ssize_t
199hpet_read(struct file *file, char __user *buf, size_t count, loff_t * ppos)
200{
201 DECLARE_WAITQUEUE(wait, current);
202 unsigned long data;
203 ssize_t retval;
204 struct hpet_dev *devp;
205
206 devp = file->private_data;
207 if (!devp->hd_ireqfreq)
208 return -EIO;
209
210 if (count < sizeof(unsigned long))
211 return -EINVAL;
212
213 add_wait_queue(&devp->hd_waitqueue, &wait);
214
215 for ( ; ; ) {
216 set_current_state(TASK_INTERRUPTIBLE);
217
218 spin_lock_irq(&hpet_lock);
219 data = devp->hd_irqdata;
220 devp->hd_irqdata = 0;
221 spin_unlock_irq(&hpet_lock);
222
223 if (data)
224 break;
225 else if (file->f_flags & O_NONBLOCK) {
226 retval = -EAGAIN;
227 goto out;
228 } else if (signal_pending(current)) {
229 retval = -ERESTARTSYS;
230 goto out;
231 }
232 schedule();
233 }
234
235 retval = put_user(data, (unsigned long __user *)buf);
236 if (!retval)
237 retval = sizeof(unsigned long);
238out:
239 __set_current_state(TASK_RUNNING);
240 remove_wait_queue(&devp->hd_waitqueue, &wait);
241
242 return retval;
243}
244
245static unsigned int hpet_poll(struct file *file, poll_table * wait)
246{
247 unsigned long v;
248 struct hpet_dev *devp;
249
250 devp = file->private_data;
251
252 if (!devp->hd_ireqfreq)
253 return 0;
254
255 poll_wait(file, &devp->hd_waitqueue, wait);
256
257 spin_lock_irq(&hpet_lock);
258 v = devp->hd_irqdata;
259 spin_unlock_irq(&hpet_lock);
260
261 if (v != 0)
262 return POLLIN | POLLRDNORM;
263
264 return 0;
265}
266
267static int hpet_mmap(struct file *file, struct vm_area_struct *vma)
268{
269#ifdef CONFIG_HPET_MMAP
270 struct hpet_dev *devp;
271 unsigned long addr;
272
273 if (((vma->vm_end - vma->vm_start) != PAGE_SIZE) || vma->vm_pgoff)
274 return -EINVAL;
275
276 devp = file->private_data;
277 addr = devp->hd_hpets->hp_hpet_phys;
278
279 if (addr & (PAGE_SIZE - 1))
280 return -ENOSYS;
281
282 vma->vm_flags |= VM_IO;
283 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 if (io_remap_pfn_range(vma, vma->vm_start, addr >> PAGE_SHIFT,
286 PAGE_SIZE, vma->vm_page_prot)) {
Randy Dunlap3e6716e2005-10-30 15:03:44 -0800287 printk(KERN_ERR "%s: io_remap_pfn_range failed\n",
288 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return -EAGAIN;
290 }
291
292 return 0;
293#else
294 return -ENOSYS;
295#endif
296}
297
298static int hpet_fasync(int fd, struct file *file, int on)
299{
300 struct hpet_dev *devp;
301
302 devp = file->private_data;
303
304 if (fasync_helper(fd, file, on, &devp->hd_async_queue) >= 0)
305 return 0;
306 else
307 return -EIO;
308}
309
310static int hpet_release(struct inode *inode, struct file *file)
311{
312 struct hpet_dev *devp;
313 struct hpet_timer __iomem *timer;
314 int irq = 0;
315
316 devp = file->private_data;
317 timer = devp->hd_timer;
318
319 spin_lock_irq(&hpet_lock);
320
321 writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK),
322 &timer->hpet_config);
323
324 irq = devp->hd_irq;
325 devp->hd_irq = 0;
326
327 devp->hd_ireqfreq = 0;
328
329 if (devp->hd_flags & HPET_PERIODIC
330 && readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
331 unsigned long v;
332
333 v = readq(&timer->hpet_config);
334 v ^= Tn_TYPE_CNF_MASK;
335 writeq(v, &timer->hpet_config);
336 }
337
338 devp->hd_flags &= ~(HPET_OPEN | HPET_IE | HPET_PERIODIC);
339 spin_unlock_irq(&hpet_lock);
340
341 if (irq)
342 free_irq(irq, devp);
343
344 if (file->f_flags & FASYNC)
345 hpet_fasync(-1, file, 0);
346
347 file->private_data = NULL;
348 return 0;
349}
350
351static int hpet_ioctl_common(struct hpet_dev *, int, unsigned long, int);
352
353static int
354hpet_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
355 unsigned long arg)
356{
357 struct hpet_dev *devp;
358
359 devp = file->private_data;
360 return hpet_ioctl_common(devp, cmd, arg, 0);
361}
362
363static int hpet_ioctl_ieon(struct hpet_dev *devp)
364{
365 struct hpet_timer __iomem *timer;
366 struct hpet __iomem *hpet;
367 struct hpets *hpetp;
368 int irq;
369 unsigned long g, v, t, m;
370 unsigned long flags, isr;
371
372 timer = devp->hd_timer;
373 hpet = devp->hd_hpet;
374 hpetp = devp->hd_hpets;
375
Clemens Ladisch9090e6d2005-10-30 15:03:29 -0800376 if (!devp->hd_ireqfreq)
377 return -EIO;
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 spin_lock_irq(&hpet_lock);
380
381 if (devp->hd_flags & HPET_IE) {
382 spin_unlock_irq(&hpet_lock);
383 return -EBUSY;
384 }
385
386 devp->hd_flags |= HPET_IE;
Clemens Ladisch0d290862005-10-30 15:03:34 -0800387
388 if (readl(&timer->hpet_config) & Tn_INT_TYPE_CNF_MASK)
389 devp->hd_flags |= HPET_SHARED_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 spin_unlock_irq(&hpet_lock);
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 irq = devp->hd_hdwirq;
393
394 if (irq) {
Clemens Ladisch0d290862005-10-30 15:03:34 -0800395 unsigned long irq_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Clemens Ladisch0d290862005-10-30 15:03:34 -0800397 sprintf(devp->hd_name, "hpet%d", (int)(devp - hpetp->hp_dev));
398 irq_flags = devp->hd_flags & HPET_SHARED_IRQ
Thomas Gleixner0f2ed4c2006-07-01 19:29:33 -0700399 ? IRQF_SHARED : IRQF_DISABLED;
Clemens Ladisch0d290862005-10-30 15:03:34 -0800400 if (request_irq(irq, hpet_interrupt, irq_flags,
401 devp->hd_name, (void *)devp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 printk(KERN_ERR "hpet: IRQ %d is not free\n", irq);
403 irq = 0;
404 }
405 }
406
407 if (irq == 0) {
408 spin_lock_irq(&hpet_lock);
409 devp->hd_flags ^= HPET_IE;
410 spin_unlock_irq(&hpet_lock);
411 return -EIO;
412 }
413
414 devp->hd_irq = irq;
415 t = devp->hd_ireqfreq;
416 v = readq(&timer->hpet_config);
417 g = v | Tn_INT_ENB_CNF_MASK;
418
419 if (devp->hd_flags & HPET_PERIODIC) {
420 write_counter(t, &timer->hpet_compare);
421 g |= Tn_TYPE_CNF_MASK;
422 v |= Tn_TYPE_CNF_MASK;
423 writeq(v, &timer->hpet_config);
424 v |= Tn_VAL_SET_CNF_MASK;
425 writeq(v, &timer->hpet_config);
426 local_irq_save(flags);
427 m = read_counter(&hpet->hpet_mc);
428 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
429 } else {
430 local_irq_save(flags);
431 m = read_counter(&hpet->hpet_mc);
432 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
433 }
434
Clemens Ladisch0d290862005-10-30 15:03:34 -0800435 if (devp->hd_flags & HPET_SHARED_IRQ) {
Clemens Ladisch3d5640d2005-10-30 15:03:39 -0800436 isr = 1 << (devp - devp->hd_hpets->hp_dev);
Clemens Ladisch0d290862005-10-30 15:03:34 -0800437 writel(isr, &hpet->hpet_isr);
438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 writeq(g, &timer->hpet_config);
440 local_irq_restore(flags);
441
442 return 0;
443}
444
Clemens Ladischba3f2132005-10-30 15:03:31 -0800445/* converts Hz to number of timer ticks */
446static inline unsigned long hpet_time_div(struct hpets *hpets,
447 unsigned long dis)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Clemens Ladischba3f2132005-10-30 15:03:31 -0800449 unsigned long long m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Clemens Ladischba3f2132005-10-30 15:03:31 -0800451 m = hpets->hp_tick_freq + (dis >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 do_div(m, dis);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return (unsigned long)m;
454}
455
456static int
457hpet_ioctl_common(struct hpet_dev *devp, int cmd, unsigned long arg, int kernel)
458{
459 struct hpet_timer __iomem *timer;
460 struct hpet __iomem *hpet;
461 struct hpets *hpetp;
462 int err;
463 unsigned long v;
464
465 switch (cmd) {
466 case HPET_IE_OFF:
467 case HPET_INFO:
468 case HPET_EPI:
469 case HPET_DPI:
470 case HPET_IRQFREQ:
471 timer = devp->hd_timer;
472 hpet = devp->hd_hpet;
473 hpetp = devp->hd_hpets;
474 break;
475 case HPET_IE_ON:
476 return hpet_ioctl_ieon(devp);
477 default:
478 return -EINVAL;
479 }
480
481 err = 0;
482
483 switch (cmd) {
484 case HPET_IE_OFF:
485 if ((devp->hd_flags & HPET_IE) == 0)
486 break;
487 v = readq(&timer->hpet_config);
488 v &= ~Tn_INT_ENB_CNF_MASK;
489 writeq(v, &timer->hpet_config);
490 if (devp->hd_irq) {
491 free_irq(devp->hd_irq, devp);
492 devp->hd_irq = 0;
493 }
494 devp->hd_flags ^= HPET_IE;
495 break;
496 case HPET_INFO:
497 {
498 struct hpet_info info;
499
Clemens Ladischaf95ead2005-10-30 15:03:38 -0800500 if (devp->hd_ireqfreq)
501 info.hi_ireqfreq =
502 hpet_time_div(hpetp, devp->hd_ireqfreq);
503 else
504 info.hi_ireqfreq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 info.hi_flags =
506 readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK;
Clemens Ladischc860ed92005-10-30 15:03:40 -0800507 info.hi_hpet = hpetp->hp_which;
508 info.hi_timer = devp - hpetp->hp_dev;
Clemens Ladisch8e8505b2005-10-30 15:03:37 -0800509 if (kernel)
510 memcpy((void *)arg, &info, sizeof(info));
511 else
512 if (copy_to_user((void __user *)arg, &info,
513 sizeof(info)))
514 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 break;
516 }
517 case HPET_EPI:
518 v = readq(&timer->hpet_config);
519 if ((v & Tn_PER_INT_CAP_MASK) == 0) {
520 err = -ENXIO;
521 break;
522 }
523 devp->hd_flags |= HPET_PERIODIC;
524 break;
525 case HPET_DPI:
526 v = readq(&timer->hpet_config);
527 if ((v & Tn_PER_INT_CAP_MASK) == 0) {
528 err = -ENXIO;
529 break;
530 }
531 if (devp->hd_flags & HPET_PERIODIC &&
532 readq(&timer->hpet_config) & Tn_TYPE_CNF_MASK) {
533 v = readq(&timer->hpet_config);
534 v ^= Tn_TYPE_CNF_MASK;
535 writeq(v, &timer->hpet_config);
536 }
537 devp->hd_flags &= ~HPET_PERIODIC;
538 break;
539 case HPET_IRQFREQ:
540 if (!kernel && (arg > hpet_max_freq) &&
541 !capable(CAP_SYS_RESOURCE)) {
542 err = -EACCES;
543 break;
544 }
545
Clemens Ladisch189e2dd2005-10-30 15:03:33 -0800546 if (!arg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 err = -EINVAL;
548 break;
549 }
550
Clemens Ladischba3f2132005-10-30 15:03:31 -0800551 devp->hd_ireqfreq = hpet_time_div(hpetp, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553
554 return err;
555}
556
Arjan van de Ven62322d22006-07-03 00:24:21 -0700557static const struct file_operations hpet_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 .owner = THIS_MODULE,
559 .llseek = no_llseek,
560 .read = hpet_read,
561 .poll = hpet_poll,
562 .ioctl = hpet_ioctl,
563 .open = hpet_open,
564 .release = hpet_release,
565 .fasync = hpet_fasync,
566 .mmap = hpet_mmap,
567};
568
Randy Dunlap3e6716e2005-10-30 15:03:44 -0800569static int hpet_is_known(struct hpet_data *hdp)
570{
571 struct hpets *hpetp;
572
573 for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
574 if (hpetp->hp_hpet_phys == hdp->hd_phys_address)
575 return 1;
576
577 return 0;
578}
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580EXPORT_SYMBOL(hpet_alloc);
581EXPORT_SYMBOL(hpet_register);
582EXPORT_SYMBOL(hpet_unregister);
583EXPORT_SYMBOL(hpet_control);
584
585int hpet_register(struct hpet_task *tp, int periodic)
586{
587 unsigned int i;
588 u64 mask;
589 struct hpet_timer __iomem *timer;
590 struct hpet_dev *devp;
591 struct hpets *hpetp;
592
593 switch (periodic) {
594 case 1:
595 mask = Tn_PER_INT_CAP_MASK;
596 break;
597 case 0:
598 mask = 0;
599 break;
600 default:
601 return -EINVAL;
602 }
603
Clemens Ladisch7522e4e2005-10-30 15:03:39 -0800604 tp->ht_opaque = NULL;
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 spin_lock_irq(&hpet_task_lock);
607 spin_lock(&hpet_lock);
608
609 for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next)
610 for (timer = hpetp->hp_hpet->hpet_timers, i = 0;
611 i < hpetp->hp_ntimer; i++, timer++) {
612 if ((readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK)
613 != mask)
614 continue;
615
616 devp = &hpetp->hp_dev[i];
617
618 if (devp->hd_flags & HPET_OPEN || devp->hd_task) {
619 devp = NULL;
620 continue;
621 }
622
623 tp->ht_opaque = devp;
624 devp->hd_task = tp;
625 break;
626 }
627
628 spin_unlock(&hpet_lock);
629 spin_unlock_irq(&hpet_task_lock);
630
631 if (tp->ht_opaque)
632 return 0;
633 else
634 return -EBUSY;
635}
636
637static inline int hpet_tpcheck(struct hpet_task *tp)
638{
639 struct hpet_dev *devp;
640 struct hpets *hpetp;
641
642 devp = tp->ht_opaque;
643
644 if (!devp)
645 return -ENXIO;
646
647 for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
648 if (devp >= hpetp->hp_dev
649 && devp < (hpetp->hp_dev + hpetp->hp_ntimer)
650 && devp->hd_hpet == hpetp->hp_hpet)
651 return 0;
652
653 return -ENXIO;
654}
655
656int hpet_unregister(struct hpet_task *tp)
657{
658 struct hpet_dev *devp;
659 struct hpet_timer __iomem *timer;
660 int err;
661
662 if ((err = hpet_tpcheck(tp)))
663 return err;
664
665 spin_lock_irq(&hpet_task_lock);
666 spin_lock(&hpet_lock);
667
668 devp = tp->ht_opaque;
669 if (devp->hd_task != tp) {
670 spin_unlock(&hpet_lock);
671 spin_unlock_irq(&hpet_task_lock);
672 return -ENXIO;
673 }
674
675 timer = devp->hd_timer;
676 writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK),
677 &timer->hpet_config);
678 devp->hd_flags &= ~(HPET_IE | HPET_PERIODIC);
679 devp->hd_task = NULL;
680 spin_unlock(&hpet_lock);
681 spin_unlock_irq(&hpet_task_lock);
682
683 return 0;
684}
685
686int hpet_control(struct hpet_task *tp, unsigned int cmd, unsigned long arg)
687{
688 struct hpet_dev *devp;
689 int err;
690
691 if ((err = hpet_tpcheck(tp)))
692 return err;
693
694 spin_lock_irq(&hpet_lock);
695 devp = tp->ht_opaque;
696 if (devp->hd_task != tp) {
697 spin_unlock_irq(&hpet_lock);
698 return -ENXIO;
699 }
700 spin_unlock_irq(&hpet_lock);
701 return hpet_ioctl_common(devp, cmd, arg, 1);
702}
703
704static ctl_table hpet_table[] = {
705 {
706 .ctl_name = 1,
707 .procname = "max-user-freq",
708 .data = &hpet_max_freq,
709 .maxlen = sizeof(int),
710 .mode = 0644,
711 .proc_handler = &proc_dointvec,
712 },
713 {.ctl_name = 0}
714};
715
716static ctl_table hpet_root[] = {
717 {
718 .ctl_name = 1,
719 .procname = "hpet",
720 .maxlen = 0,
721 .mode = 0555,
722 .child = hpet_table,
723 },
724 {.ctl_name = 0}
725};
726
727static ctl_table dev_root[] = {
728 {
729 .ctl_name = CTL_DEV,
730 .procname = "dev",
731 .maxlen = 0,
732 .mode = 0555,
733 .child = hpet_root,
734 },
735 {.ctl_name = 0}
736};
737
738static struct ctl_table_header *sysctl_header;
739
740static void hpet_register_interpolator(struct hpets *hpetp)
741{
742#ifdef CONFIG_TIME_INTERPOLATION
743 struct time_interpolator *ti;
744
Randy Dunlap3e6716e2005-10-30 15:03:44 -0800745 ti = kzalloc(sizeof(*ti), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (!ti)
747 return;
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 ti->source = TIME_SOURCE_MMIO64;
750 ti->shift = 10;
751 ti->addr = &hpetp->hp_hpet->hpet_mc;
Clemens Ladischba3f2132005-10-30 15:03:31 -0800752 ti->frequency = hpetp->hp_tick_freq;
Alex Williamson96803822005-09-06 15:17:54 -0700753 ti->drift = HPET_DRIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 ti->mask = -1;
755
756 hpetp->hp_interpolator = ti;
757 register_time_interpolator(ti);
758#endif
759}
760
761/*
762 * Adjustment for when arming the timer with
763 * initial conditions. That is, main counter
764 * ticks expired before interrupts are enabled.
765 */
766#define TICK_CALIBRATE (1000UL)
767
768static unsigned long hpet_calibrate(struct hpets *hpetp)
769{
770 struct hpet_timer __iomem *timer = NULL;
771 unsigned long t, m, count, i, flags, start;
772 struct hpet_dev *devp;
773 int j;
774 struct hpet __iomem *hpet;
775
776 for (j = 0, devp = hpetp->hp_dev; j < hpetp->hp_ntimer; j++, devp++)
777 if ((devp->hd_flags & HPET_OPEN) == 0) {
778 timer = devp->hd_timer;
779 break;
780 }
781
782 if (!timer)
783 return 0;
784
Clemens Ladisch3d5640d2005-10-30 15:03:39 -0800785 hpet = hpetp->hp_hpet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 t = read_counter(&timer->hpet_compare);
787
788 i = 0;
Clemens Ladischba3f2132005-10-30 15:03:31 -0800789 count = hpet_time_div(hpetp, TICK_CALIBRATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 local_irq_save(flags);
792
793 start = read_counter(&hpet->hpet_mc);
794
795 do {
796 m = read_counter(&hpet->hpet_mc);
797 write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
798 } while (i++, (m - start) < count);
799
800 local_irq_restore(flags);
801
802 return (m - start) / i;
803}
804
805int hpet_alloc(struct hpet_data *hdp)
806{
807 u64 cap, mcfg;
808 struct hpet_dev *devp;
809 u32 i, ntimer;
810 struct hpets *hpetp;
811 size_t siz;
812 struct hpet __iomem *hpet;
Randy Dunlap3e6716e2005-10-30 15:03:44 -0800813 static struct hpets *last = NULL;
Clemens Ladisch318db8f2005-10-30 15:03:41 -0800814 unsigned long period;
Clemens Ladischba3f2132005-10-30 15:03:31 -0800815 unsigned long long temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 /*
818 * hpet_alloc can be called by platform dependent code.
Randy Dunlap3e6716e2005-10-30 15:03:44 -0800819 * If platform dependent code has allocated the hpet that
820 * ACPI has also reported, then we catch it here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 */
Randy Dunlap3e6716e2005-10-30 15:03:44 -0800822 if (hpet_is_known(hdp)) {
823 printk(KERN_DEBUG "%s: duplicate HPET ignored\n",
824 __FUNCTION__);
825 return 0;
826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 siz = sizeof(struct hpets) + ((hdp->hd_nirqs - 1) *
829 sizeof(struct hpet_dev));
830
Randy Dunlap3e6716e2005-10-30 15:03:44 -0800831 hpetp = kzalloc(siz, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 if (!hpetp)
834 return -ENOMEM;
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 hpetp->hp_which = hpet_nhpet++;
837 hpetp->hp_hpet = hdp->hd_address;
838 hpetp->hp_hpet_phys = hdp->hd_phys_address;
839
840 hpetp->hp_ntimer = hdp->hd_nirqs;
841
842 for (i = 0; i < hdp->hd_nirqs; i++)
843 hpetp->hp_dev[i].hd_hdwirq = hdp->hd_irq[i];
844
845 hpet = hpetp->hp_hpet;
846
847 cap = readq(&hpet->hpet_cap);
848
849 ntimer = ((cap & HPET_NUM_TIM_CAP_MASK) >> HPET_NUM_TIM_CAP_SHIFT) + 1;
850
851 if (hpetp->hp_ntimer != ntimer) {
852 printk(KERN_WARNING "hpet: number irqs doesn't agree"
853 " with number of timers\n");
854 kfree(hpetp);
855 return -ENODEV;
856 }
857
858 if (last)
859 last->hp_next = hpetp;
860 else
861 hpets = hpetp;
862
863 last = hpetp;
864
Clemens Ladischba3f2132005-10-30 15:03:31 -0800865 period = (cap & HPET_COUNTER_CLK_PERIOD_MASK) >>
866 HPET_COUNTER_CLK_PERIOD_SHIFT; /* fs, 10^-15 */
867 temp = 1000000000000000uLL; /* 10^15 femtoseconds per second */
868 temp += period >> 1; /* round */
869 do_div(temp, period);
870 hpetp->hp_tick_freq = temp; /* ticks per second */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Andi Kleen3034d112006-09-26 10:52:28 +0200872 printk(KERN_INFO "hpet%d: at MMIO 0x%lx, IRQ%s",
873 hpetp->hp_which, hdp->hd_phys_address,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 hpetp->hp_ntimer > 1 ? "s" : "");
875 for (i = 0; i < hpetp->hp_ntimer; i++)
876 printk("%s %d", i > 0 ? "," : "", hdp->hd_irq[i]);
877 printk("\n");
878
Clemens Ladisch318db8f2005-10-30 15:03:41 -0800879 printk(KERN_INFO "hpet%u: %u %d-bit timers, %Lu Hz\n",
880 hpetp->hp_which, hpetp->hp_ntimer,
881 cap & HPET_COUNTER_SIZE_MASK ? 64 : 32, hpetp->hp_tick_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 mcfg = readq(&hpet->hpet_config);
884 if ((mcfg & HPET_ENABLE_CNF_MASK) == 0) {
885 write_counter(0L, &hpet->hpet_mc);
886 mcfg |= HPET_ENABLE_CNF_MASK;
887 writeq(mcfg, &hpet->hpet_config);
888 }
889
Clemens Ladisch642d30b2005-10-30 15:03:31 -0800890 for (i = 0, devp = hpetp->hp_dev; i < hpetp->hp_ntimer; i++, devp++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 struct hpet_timer __iomem *timer;
892
893 timer = &hpet->hpet_timers[devp - hpetp->hp_dev];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 devp->hd_hpets = hpetp;
896 devp->hd_hpet = hpet;
897 devp->hd_timer = timer;
898
899 /*
900 * If the timer was reserved by platform code,
901 * then make timer unavailable for opens.
902 */
903 if (hdp->hd_state & (1 << i)) {
904 devp->hd_flags = HPET_OPEN;
905 continue;
906 }
907
908 init_waitqueue_head(&devp->hd_waitqueue);
909 }
910
911 hpetp->hp_delta = hpet_calibrate(hpetp);
912 hpet_register_interpolator(hpetp);
913
914 return 0;
915}
916
917static acpi_status hpet_resources(struct acpi_resource *res, void *data)
918{
919 struct hpet_data *hdp;
920 acpi_status status;
921 struct acpi_resource_address64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 hdp = data;
924
925 status = acpi_resource_to_address64(res, &addr);
926
927 if (ACPI_SUCCESS(status)) {
Bob Moore50eca3e2005-09-30 19:03:00 -0400928 hdp->hd_phys_address = addr.minimum;
Bjorn Helgaas9224a862006-03-28 17:04:00 -0500929 hdp->hd_address = ioremap(addr.minimum, addr.address_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Randy Dunlap3e6716e2005-10-30 15:03:44 -0800931 if (hpet_is_known(hdp)) {
932 printk(KERN_DEBUG "%s: 0x%lx is busy\n",
933 __FUNCTION__, hdp->hd_phys_address);
934 iounmap(hdp->hd_address);
935 return -EBUSY;
936 }
Bob Moore50eca3e2005-09-30 19:03:00 -0400937 } else if (res->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32) {
938 struct acpi_resource_fixed_memory32 *fixmem32;
Randy Dunlap757c4722005-10-30 15:03:42 -0800939
940 fixmem32 = &res->data.fixed_memory32;
941 if (!fixmem32)
942 return -EINVAL;
943
Bob Moore50eca3e2005-09-30 19:03:00 -0400944 hdp->hd_phys_address = fixmem32->address;
945 hdp->hd_address = ioremap(fixmem32->address,
Randy Dunlap757c4722005-10-30 15:03:42 -0800946 HPET_RANGE_SIZE);
947
Randy Dunlap3e6716e2005-10-30 15:03:44 -0800948 if (hpet_is_known(hdp)) {
949 printk(KERN_DEBUG "%s: 0x%lx is busy\n",
950 __FUNCTION__, hdp->hd_phys_address);
951 iounmap(hdp->hd_address);
952 return -EBUSY;
953 }
Bob Moore50eca3e2005-09-30 19:03:00 -0400954 } else if (res->type == ACPI_RESOURCE_TYPE_EXTENDED_IRQ) {
955 struct acpi_resource_extended_irq *irqp;
Bjorn Helgaasbe5efff2006-02-14 13:53:02 -0800956 int i, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 irqp = &res->data.extended_irq;
959
Bjorn Helgaasbe5efff2006-02-14 13:53:02 -0800960 for (i = 0; i < irqp->interrupt_count; i++) {
961 irq = acpi_register_gsi(irqp->interrupts[i],
962 irqp->triggering, irqp->polarity);
963 if (irq < 0)
964 return AE_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Bjorn Helgaasbe5efff2006-02-14 13:53:02 -0800966 hdp->hd_irq[hdp->hd_nirqs] = irq;
967 hdp->hd_nirqs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
969 }
970
971 return AE_OK;
972}
973
974static int hpet_acpi_add(struct acpi_device *device)
975{
976 acpi_status result;
977 struct hpet_data data;
978
979 memset(&data, 0, sizeof(data));
980
981 result =
982 acpi_walk_resources(device->handle, METHOD_NAME__CRS,
983 hpet_resources, &data);
984
985 if (ACPI_FAILURE(result))
986 return -ENODEV;
987
988 if (!data.hd_address || !data.hd_nirqs) {
989 printk("%s: no address or irqs in _CRS\n", __FUNCTION__);
990 return -ENODEV;
991 }
992
993 return hpet_alloc(&data);
994}
995
996static int hpet_acpi_remove(struct acpi_device *device, int type)
997{
998 /* XXX need to unregister interpolator, dealloc mem, etc */
999 return -EINVAL;
1000}
1001
1002static struct acpi_driver hpet_acpi_driver = {
1003 .name = "hpet",
1004 .ids = "PNP0103",
1005 .ops = {
1006 .add = hpet_acpi_add,
1007 .remove = hpet_acpi_remove,
1008 },
1009};
1010
1011static struct miscdevice hpet_misc = { HPET_MINOR, "hpet", &hpet_fops };
1012
1013static int __init hpet_init(void)
1014{
1015 int result;
1016
1017 result = misc_register(&hpet_misc);
1018 if (result < 0)
1019 return -ENODEV;
1020
1021 sysctl_header = register_sysctl_table(dev_root, 0);
1022
1023 result = acpi_bus_register_driver(&hpet_acpi_driver);
1024 if (result < 0) {
1025 if (sysctl_header)
1026 unregister_sysctl_table(sysctl_header);
1027 misc_deregister(&hpet_misc);
1028 return result;
1029 }
1030
1031 return 0;
1032}
1033
1034static void __exit hpet_exit(void)
1035{
1036 acpi_bus_unregister_driver(&hpet_acpi_driver);
1037
1038 if (sysctl_header)
1039 unregister_sysctl_table(sysctl_header);
1040 misc_deregister(&hpet_misc);
1041
1042 return;
1043}
1044
1045module_init(hpet_init);
1046module_exit(hpet_exit);
1047MODULE_AUTHOR("Bob Picco <Robert.Picco@hp.com>");
1048MODULE_LICENSE("GPL");