hpet.c 22 KB

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