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