hpet.c 22 KB

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