hpet.c 22 KB

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