hpet.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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.htm
  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. if (file->f_flags & FASYNC)
  345. hpet_fasync(-1, file, 0);
  346. file->private_data = NULL;
  347. return 0;
  348. }
  349. static int hpet_ioctl_common(struct hpet_dev *, int, unsigned long, int);
  350. static int
  351. hpet_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  352. unsigned long arg)
  353. {
  354. struct hpet_dev *devp;
  355. devp = file->private_data;
  356. return hpet_ioctl_common(devp, cmd, arg, 0);
  357. }
  358. static int hpet_ioctl_ieon(struct hpet_dev *devp)
  359. {
  360. struct hpet_timer __iomem *timer;
  361. struct hpet __iomem *hpet;
  362. struct hpets *hpetp;
  363. int irq;
  364. unsigned long g, v, t, m;
  365. unsigned long flags, isr;
  366. timer = devp->hd_timer;
  367. hpet = devp->hd_hpet;
  368. hpetp = devp->hd_hpets;
  369. if (!devp->hd_ireqfreq)
  370. return -EIO;
  371. spin_lock_irq(&hpet_lock);
  372. if (devp->hd_flags & HPET_IE) {
  373. spin_unlock_irq(&hpet_lock);
  374. return -EBUSY;
  375. }
  376. devp->hd_flags |= HPET_IE;
  377. if (readl(&timer->hpet_config) & Tn_INT_TYPE_CNF_MASK)
  378. devp->hd_flags |= HPET_SHARED_IRQ;
  379. spin_unlock_irq(&hpet_lock);
  380. irq = devp->hd_hdwirq;
  381. if (irq) {
  382. unsigned long irq_flags;
  383. sprintf(devp->hd_name, "hpet%d", (int)(devp - hpetp->hp_dev));
  384. irq_flags = devp->hd_flags & HPET_SHARED_IRQ
  385. ? IRQF_SHARED : IRQF_DISABLED;
  386. if (request_irq(irq, hpet_interrupt, irq_flags,
  387. devp->hd_name, (void *)devp)) {
  388. printk(KERN_ERR "hpet: IRQ %d is not free\n", irq);
  389. irq = 0;
  390. }
  391. }
  392. if (irq == 0) {
  393. spin_lock_irq(&hpet_lock);
  394. devp->hd_flags ^= HPET_IE;
  395. spin_unlock_irq(&hpet_lock);
  396. return -EIO;
  397. }
  398. devp->hd_irq = irq;
  399. t = devp->hd_ireqfreq;
  400. v = readq(&timer->hpet_config);
  401. /* 64-bit comparators are not yet supported through the ioctls,
  402. * so force this into 32-bit mode if it supports both modes
  403. */
  404. g = v | Tn_32MODE_CNF_MASK | Tn_INT_ENB_CNF_MASK;
  405. if (devp->hd_flags & HPET_PERIODIC) {
  406. write_counter(t, &timer->hpet_compare);
  407. g |= Tn_TYPE_CNF_MASK;
  408. v |= Tn_TYPE_CNF_MASK;
  409. writeq(v, &timer->hpet_config);
  410. v |= Tn_VAL_SET_CNF_MASK;
  411. writeq(v, &timer->hpet_config);
  412. local_irq_save(flags);
  413. /* NOTE: what we modify here is a hidden accumulator
  414. * register supported by periodic-capable comparators.
  415. * We never want to modify the (single) counter; that
  416. * would affect all the comparators.
  417. */
  418. m = read_counter(&hpet->hpet_mc);
  419. write_counter(t + m + hpetp->hp_delta, &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. .ctl_name = CTL_UNNUMBERED,
  557. .procname = "max-user-freq",
  558. .data = &hpet_max_freq,
  559. .maxlen = sizeof(int),
  560. .mode = 0644,
  561. .proc_handler = &proc_dointvec,
  562. },
  563. {.ctl_name = 0}
  564. };
  565. static ctl_table hpet_root[] = {
  566. {
  567. .ctl_name = CTL_UNNUMBERED,
  568. .procname = "hpet",
  569. .maxlen = 0,
  570. .mode = 0555,
  571. .child = hpet_table,
  572. },
  573. {.ctl_name = 0}
  574. };
  575. static ctl_table dev_root[] = {
  576. {
  577. .ctl_name = CTL_DEV,
  578. .procname = "dev",
  579. .maxlen = 0,
  580. .mode = 0555,
  581. .child = hpet_root,
  582. },
  583. {.ctl_name = 0}
  584. };
  585. static struct ctl_table_header *sysctl_header;
  586. /*
  587. * Adjustment for when arming the timer with
  588. * initial conditions. That is, main counter
  589. * ticks expired before interrupts are enabled.
  590. */
  591. #define TICK_CALIBRATE (1000UL)
  592. static unsigned long hpet_calibrate(struct hpets *hpetp)
  593. {
  594. struct hpet_timer __iomem *timer = NULL;
  595. unsigned long t, m, count, i, flags, start;
  596. struct hpet_dev *devp;
  597. int j;
  598. struct hpet __iomem *hpet;
  599. for (j = 0, devp = hpetp->hp_dev; j < hpetp->hp_ntimer; j++, devp++)
  600. if ((devp->hd_flags & HPET_OPEN) == 0) {
  601. timer = devp->hd_timer;
  602. break;
  603. }
  604. if (!timer)
  605. return 0;
  606. hpet = hpetp->hp_hpet;
  607. t = read_counter(&timer->hpet_compare);
  608. i = 0;
  609. count = hpet_time_div(hpetp, TICK_CALIBRATE);
  610. local_irq_save(flags);
  611. start = read_counter(&hpet->hpet_mc);
  612. do {
  613. m = read_counter(&hpet->hpet_mc);
  614. write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
  615. } while (i++, (m - start) < count);
  616. local_irq_restore(flags);
  617. return (m - start) / i;
  618. }
  619. int hpet_alloc(struct hpet_data *hdp)
  620. {
  621. u64 cap, mcfg;
  622. struct hpet_dev *devp;
  623. u32 i, ntimer;
  624. struct hpets *hpetp;
  625. size_t siz;
  626. struct hpet __iomem *hpet;
  627. static struct hpets *last = NULL;
  628. unsigned long period;
  629. unsigned long long temp;
  630. u32 remainder;
  631. /*
  632. * hpet_alloc can be called by platform dependent code.
  633. * If platform dependent code has allocated the hpet that
  634. * ACPI has also reported, then we catch it here.
  635. */
  636. if (hpet_is_known(hdp)) {
  637. printk(KERN_DEBUG "%s: duplicate HPET ignored\n",
  638. __func__);
  639. return 0;
  640. }
  641. siz = sizeof(struct hpets) + ((hdp->hd_nirqs - 1) *
  642. sizeof(struct hpet_dev));
  643. hpetp = kzalloc(siz, GFP_KERNEL);
  644. if (!hpetp)
  645. return -ENOMEM;
  646. hpetp->hp_which = hpet_nhpet++;
  647. hpetp->hp_hpet = hdp->hd_address;
  648. hpetp->hp_hpet_phys = hdp->hd_phys_address;
  649. hpetp->hp_ntimer = hdp->hd_nirqs;
  650. for (i = 0; i < hdp->hd_nirqs; i++)
  651. hpetp->hp_dev[i].hd_hdwirq = hdp->hd_irq[i];
  652. hpet = hpetp->hp_hpet;
  653. cap = readq(&hpet->hpet_cap);
  654. ntimer = ((cap & HPET_NUM_TIM_CAP_MASK) >> HPET_NUM_TIM_CAP_SHIFT) + 1;
  655. if (hpetp->hp_ntimer != ntimer) {
  656. printk(KERN_WARNING "hpet: number irqs doesn't agree"
  657. " with number of timers\n");
  658. kfree(hpetp);
  659. return -ENODEV;
  660. }
  661. if (last)
  662. last->hp_next = hpetp;
  663. else
  664. hpets = hpetp;
  665. last = hpetp;
  666. period = (cap & HPET_COUNTER_CLK_PERIOD_MASK) >>
  667. HPET_COUNTER_CLK_PERIOD_SHIFT; /* fs, 10^-15 */
  668. temp = 1000000000000000uLL; /* 10^15 femtoseconds per second */
  669. temp += period >> 1; /* round */
  670. do_div(temp, period);
  671. hpetp->hp_tick_freq = temp; /* ticks per second */
  672. printk(KERN_INFO "hpet%d: at MMIO 0x%lx, IRQ%s",
  673. hpetp->hp_which, hdp->hd_phys_address,
  674. hpetp->hp_ntimer > 1 ? "s" : "");
  675. for (i = 0; i < hpetp->hp_ntimer; i++)
  676. printk("%s %d", i > 0 ? "," : "", hdp->hd_irq[i]);
  677. printk("\n");
  678. temp = hpetp->hp_tick_freq;
  679. remainder = do_div(temp, 1000000);
  680. printk(KERN_INFO
  681. "hpet%u: %u comparators, %d-bit %u.%06u MHz counter\n",
  682. hpetp->hp_which, hpetp->hp_ntimer,
  683. cap & HPET_COUNTER_SIZE_MASK ? 64 : 32,
  684. (unsigned) temp, remainder);
  685. mcfg = readq(&hpet->hpet_config);
  686. if ((mcfg & HPET_ENABLE_CNF_MASK) == 0) {
  687. write_counter(0L, &hpet->hpet_mc);
  688. mcfg |= HPET_ENABLE_CNF_MASK;
  689. writeq(mcfg, &hpet->hpet_config);
  690. }
  691. for (i = 0, devp = hpetp->hp_dev; i < hpetp->hp_ntimer; i++, devp++) {
  692. struct hpet_timer __iomem *timer;
  693. timer = &hpet->hpet_timers[devp - hpetp->hp_dev];
  694. devp->hd_hpets = hpetp;
  695. devp->hd_hpet = hpet;
  696. devp->hd_timer = timer;
  697. /*
  698. * If the timer was reserved by platform code,
  699. * then make timer unavailable for opens.
  700. */
  701. if (hdp->hd_state & (1 << i)) {
  702. devp->hd_flags = HPET_OPEN;
  703. continue;
  704. }
  705. init_waitqueue_head(&devp->hd_waitqueue);
  706. }
  707. hpetp->hp_delta = hpet_calibrate(hpetp);
  708. /* This clocksource driver currently only works on ia64 */
  709. #ifdef CONFIG_IA64
  710. if (!hpet_clocksource) {
  711. hpet_mctr = (void __iomem *)&hpetp->hp_hpet->hpet_mc;
  712. CLKSRC_FSYS_MMIO_SET(clocksource_hpet.fsys_mmio, hpet_mctr);
  713. clocksource_hpet.mult = clocksource_hz2mult(hpetp->hp_tick_freq,
  714. clocksource_hpet.shift);
  715. clocksource_register(&clocksource_hpet);
  716. hpetp->hp_clocksource = &clocksource_hpet;
  717. hpet_clocksource = &clocksource_hpet;
  718. }
  719. #endif
  720. return 0;
  721. }
  722. static acpi_status hpet_resources(struct acpi_resource *res, void *data)
  723. {
  724. struct hpet_data *hdp;
  725. acpi_status status;
  726. struct acpi_resource_address64 addr;
  727. hdp = data;
  728. status = acpi_resource_to_address64(res, &addr);
  729. if (ACPI_SUCCESS(status)) {
  730. hdp->hd_phys_address = addr.minimum;
  731. hdp->hd_address = ioremap(addr.minimum, addr.address_length);
  732. if (hpet_is_known(hdp)) {
  733. printk(KERN_DEBUG "%s: 0x%lx is busy\n",
  734. __func__, hdp->hd_phys_address);
  735. iounmap(hdp->hd_address);
  736. return AE_ALREADY_EXISTS;
  737. }
  738. } else if (res->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32) {
  739. struct acpi_resource_fixed_memory32 *fixmem32;
  740. fixmem32 = &res->data.fixed_memory32;
  741. if (!fixmem32)
  742. return AE_NO_MEMORY;
  743. hdp->hd_phys_address = fixmem32->address;
  744. hdp->hd_address = ioremap(fixmem32->address,
  745. HPET_RANGE_SIZE);
  746. if (hpet_is_known(hdp)) {
  747. printk(KERN_DEBUG "%s: 0x%lx is busy\n",
  748. __func__, hdp->hd_phys_address);
  749. iounmap(hdp->hd_address);
  750. return AE_ALREADY_EXISTS;
  751. }
  752. } else if (res->type == ACPI_RESOURCE_TYPE_EXTENDED_IRQ) {
  753. struct acpi_resource_extended_irq *irqp;
  754. int i, irq;
  755. irqp = &res->data.extended_irq;
  756. for (i = 0; i < irqp->interrupt_count; i++) {
  757. irq = acpi_register_gsi(irqp->interrupts[i],
  758. irqp->triggering, irqp->polarity);
  759. if (irq < 0)
  760. return AE_ERROR;
  761. hdp->hd_irq[hdp->hd_nirqs] = irq;
  762. hdp->hd_nirqs++;
  763. }
  764. }
  765. return AE_OK;
  766. }
  767. static int hpet_acpi_add(struct acpi_device *device)
  768. {
  769. acpi_status result;
  770. struct hpet_data data;
  771. memset(&data, 0, sizeof(data));
  772. result =
  773. acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  774. hpet_resources, &data);
  775. if (ACPI_FAILURE(result))
  776. return -ENODEV;
  777. if (!data.hd_address || !data.hd_nirqs) {
  778. printk("%s: no address or irqs in _CRS\n", __func__);
  779. return -ENODEV;
  780. }
  781. return hpet_alloc(&data);
  782. }
  783. static int hpet_acpi_remove(struct acpi_device *device, int type)
  784. {
  785. /* XXX need to unregister clocksource, dealloc mem, etc */
  786. return -EINVAL;
  787. }
  788. static const struct acpi_device_id hpet_device_ids[] = {
  789. {"PNP0103", 0},
  790. {"", 0},
  791. };
  792. MODULE_DEVICE_TABLE(acpi, hpet_device_ids);
  793. static struct acpi_driver hpet_acpi_driver = {
  794. .name = "hpet",
  795. .ids = hpet_device_ids,
  796. .ops = {
  797. .add = hpet_acpi_add,
  798. .remove = hpet_acpi_remove,
  799. },
  800. };
  801. static struct miscdevice hpet_misc = { HPET_MINOR, "hpet", &hpet_fops };
  802. static int __init hpet_init(void)
  803. {
  804. int result;
  805. result = misc_register(&hpet_misc);
  806. if (result < 0)
  807. return -ENODEV;
  808. sysctl_header = register_sysctl_table(dev_root);
  809. result = acpi_bus_register_driver(&hpet_acpi_driver);
  810. if (result < 0) {
  811. if (sysctl_header)
  812. unregister_sysctl_table(sysctl_header);
  813. misc_deregister(&hpet_misc);
  814. return result;
  815. }
  816. return 0;
  817. }
  818. static void __exit hpet_exit(void)
  819. {
  820. acpi_bus_unregister_driver(&hpet_acpi_driver);
  821. if (sysctl_header)
  822. unregister_sysctl_table(sysctl_header);
  823. misc_deregister(&hpet_misc);
  824. return;
  825. }
  826. module_init(hpet_init);
  827. module_exit(hpet_exit);
  828. MODULE_AUTHOR("Bob Picco <Robert.Picco@hp.com>");
  829. MODULE_LICENSE("GPL");