hpet.c 22 KB

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