hpet.c 21 KB

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