hpet.c 22 KB

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