hpet.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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/types.h>
  17. #include <linux/miscdevice.h>
  18. #include <linux/major.h>
  19. #include <linux/ioport.h>
  20. #include <linux/fcntl.h>
  21. #include <linux/init.h>
  22. #include <linux/poll.h>
  23. #include <linux/proc_fs.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/sysctl.h>
  26. #include <linux/wait.h>
  27. #include <linux/bcd.h>
  28. #include <linux/seq_file.h>
  29. #include <linux/bitops.h>
  30. #include <asm/current.h>
  31. #include <asm/uaccess.h>
  32. #include <asm/system.h>
  33. #include <asm/io.h>
  34. #include <asm/irq.h>
  35. #include <asm/div64.h>
  36. #include <linux/acpi.h>
  37. #include <acpi/acpi_bus.h>
  38. #include <linux/hpet.h>
  39. /*
  40. * The High Precision Event Timer driver.
  41. * This driver is closely modelled after the rtc.c driver.
  42. * http://www.intel.com/hardwaredesign/hpetspec.htm
  43. */
  44. #define HPET_USER_FREQ (64)
  45. #define HPET_DRIFT (500)
  46. #define HPET_RANGE_SIZE 1024 /* from HPET spec */
  47. static u32 hpet_nhpet, hpet_max_freq = HPET_USER_FREQ;
  48. /* A lock for concurrent access by app and isr hpet activity. */
  49. static DEFINE_SPINLOCK(hpet_lock);
  50. /* A lock for concurrent intermodule access to hpet and isr hpet activity. */
  51. static DEFINE_SPINLOCK(hpet_task_lock);
  52. #define HPET_DEV_NAME (7)
  53. struct hpet_dev {
  54. struct hpets *hd_hpets;
  55. struct hpet __iomem *hd_hpet;
  56. struct hpet_timer __iomem *hd_timer;
  57. unsigned long hd_ireqfreq;
  58. unsigned long hd_irqdata;
  59. wait_queue_head_t hd_waitqueue;
  60. struct fasync_struct *hd_async_queue;
  61. struct hpet_task *hd_task;
  62. unsigned int hd_flags;
  63. unsigned int hd_irq;
  64. unsigned int hd_hdwirq;
  65. char hd_name[HPET_DEV_NAME];
  66. };
  67. struct hpets {
  68. struct hpets *hp_next;
  69. struct hpet __iomem *hp_hpet;
  70. unsigned long hp_hpet_phys;
  71. struct time_interpolator *hp_interpolator;
  72. unsigned long long hp_tick_freq;
  73. unsigned long hp_delta;
  74. unsigned int hp_ntimer;
  75. unsigned int hp_which;
  76. struct hpet_dev hp_dev[1];
  77. };
  78. static struct hpets *hpets;
  79. #define HPET_OPEN 0x0001
  80. #define HPET_IE 0x0002 /* interrupt enabled */
  81. #define HPET_PERIODIC 0x0004
  82. #define HPET_SHARED_IRQ 0x0008
  83. #if BITS_PER_LONG == 64
  84. #define write_counter(V, MC) writeq(V, MC)
  85. #define read_counter(MC) readq(MC)
  86. #else
  87. #define write_counter(V, MC) writel(V, MC)
  88. #define read_counter(MC) readl(MC)
  89. #endif
  90. #ifndef readq
  91. static inline unsigned long long readq(void __iomem *addr)
  92. {
  93. return readl(addr) | (((unsigned long long)readl(addr + 4)) << 32LL);
  94. }
  95. #endif
  96. #ifndef writeq
  97. static inline void writeq(unsigned long long v, void __iomem *addr)
  98. {
  99. writel(v & 0xffffffff, addr);
  100. writel(v >> 32, addr + 4);
  101. }
  102. #endif
  103. static irqreturn_t hpet_interrupt(int irq, void *data)
  104. {
  105. struct hpet_dev *devp;
  106. unsigned long isr;
  107. devp = data;
  108. isr = 1 << (devp - devp->hd_hpets->hp_dev);
  109. if ((devp->hd_flags & HPET_SHARED_IRQ) &&
  110. !(isr & readl(&devp->hd_hpet->hpet_isr)))
  111. return IRQ_NONE;
  112. spin_lock(&hpet_lock);
  113. devp->hd_irqdata++;
  114. /*
  115. * For non-periodic timers, increment the accumulator.
  116. * This has the effect of treating non-periodic like periodic.
  117. */
  118. if ((devp->hd_flags & (HPET_IE | HPET_PERIODIC)) == HPET_IE) {
  119. unsigned long m, t;
  120. t = devp->hd_ireqfreq;
  121. m = read_counter(&devp->hd_hpet->hpet_mc);
  122. write_counter(t + m + devp->hd_hpets->hp_delta,
  123. &devp->hd_timer->hpet_compare);
  124. }
  125. if (devp->hd_flags & HPET_SHARED_IRQ)
  126. writel(isr, &devp->hd_hpet->hpet_isr);
  127. spin_unlock(&hpet_lock);
  128. spin_lock(&hpet_task_lock);
  129. if (devp->hd_task)
  130. devp->hd_task->ht_func(devp->hd_task->ht_data);
  131. spin_unlock(&hpet_task_lock);
  132. wake_up_interruptible(&devp->hd_waitqueue);
  133. kill_fasync(&devp->hd_async_queue, SIGIO, POLL_IN);
  134. return IRQ_HANDLED;
  135. }
  136. static int hpet_open(struct inode *inode, struct file *file)
  137. {
  138. struct hpet_dev *devp;
  139. struct hpets *hpetp;
  140. int i;
  141. if (file->f_mode & FMODE_WRITE)
  142. return -EINVAL;
  143. spin_lock_irq(&hpet_lock);
  144. for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next)
  145. for (i = 0; i < hpetp->hp_ntimer; i++)
  146. if (hpetp->hp_dev[i].hd_flags & HPET_OPEN
  147. || hpetp->hp_dev[i].hd_task)
  148. continue;
  149. else {
  150. devp = &hpetp->hp_dev[i];
  151. break;
  152. }
  153. if (!devp) {
  154. spin_unlock_irq(&hpet_lock);
  155. return -EBUSY;
  156. }
  157. file->private_data = devp;
  158. devp->hd_irqdata = 0;
  159. devp->hd_flags |= HPET_OPEN;
  160. spin_unlock_irq(&hpet_lock);
  161. return 0;
  162. }
  163. static ssize_t
  164. hpet_read(struct file *file, char __user *buf, size_t count, loff_t * ppos)
  165. {
  166. DECLARE_WAITQUEUE(wait, current);
  167. unsigned long data;
  168. ssize_t retval;
  169. struct hpet_dev *devp;
  170. devp = file->private_data;
  171. if (!devp->hd_ireqfreq)
  172. return -EIO;
  173. if (count < sizeof(unsigned long))
  174. return -EINVAL;
  175. add_wait_queue(&devp->hd_waitqueue, &wait);
  176. for ( ; ; ) {
  177. set_current_state(TASK_INTERRUPTIBLE);
  178. spin_lock_irq(&hpet_lock);
  179. data = devp->hd_irqdata;
  180. devp->hd_irqdata = 0;
  181. spin_unlock_irq(&hpet_lock);
  182. if (data)
  183. break;
  184. else if (file->f_flags & O_NONBLOCK) {
  185. retval = -EAGAIN;
  186. goto out;
  187. } else if (signal_pending(current)) {
  188. retval = -ERESTARTSYS;
  189. goto out;
  190. }
  191. schedule();
  192. }
  193. retval = put_user(data, (unsigned long __user *)buf);
  194. if (!retval)
  195. retval = sizeof(unsigned long);
  196. out:
  197. __set_current_state(TASK_RUNNING);
  198. remove_wait_queue(&devp->hd_waitqueue, &wait);
  199. return retval;
  200. }
  201. static unsigned int hpet_poll(struct file *file, poll_table * wait)
  202. {
  203. unsigned long v;
  204. struct hpet_dev *devp;
  205. devp = file->private_data;
  206. if (!devp->hd_ireqfreq)
  207. return 0;
  208. poll_wait(file, &devp->hd_waitqueue, wait);
  209. spin_lock_irq(&hpet_lock);
  210. v = devp->hd_irqdata;
  211. spin_unlock_irq(&hpet_lock);
  212. if (v != 0)
  213. return POLLIN | POLLRDNORM;
  214. return 0;
  215. }
  216. static int hpet_mmap(struct file *file, struct vm_area_struct *vma)
  217. {
  218. #ifdef CONFIG_HPET_MMAP
  219. struct hpet_dev *devp;
  220. unsigned long addr;
  221. if (((vma->vm_end - vma->vm_start) != PAGE_SIZE) || vma->vm_pgoff)
  222. return -EINVAL;
  223. devp = file->private_data;
  224. addr = devp->hd_hpets->hp_hpet_phys;
  225. if (addr & (PAGE_SIZE - 1))
  226. return -ENOSYS;
  227. vma->vm_flags |= VM_IO;
  228. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  229. if (io_remap_pfn_range(vma, vma->vm_start, addr >> PAGE_SHIFT,
  230. PAGE_SIZE, vma->vm_page_prot)) {
  231. printk(KERN_ERR "%s: io_remap_pfn_range failed\n",
  232. __FUNCTION__);
  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. ? IRQF_SHARED : IRQF_DISABLED;
  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 const 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. static int hpet_is_known(struct hpet_data *hdp)
  468. {
  469. struct hpets *hpetp;
  470. for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
  471. if (hpetp->hp_hpet_phys == hdp->hd_phys_address)
  472. return 1;
  473. return 0;
  474. }
  475. EXPORT_SYMBOL(hpet_alloc);
  476. EXPORT_SYMBOL(hpet_register);
  477. EXPORT_SYMBOL(hpet_unregister);
  478. EXPORT_SYMBOL(hpet_control);
  479. int hpet_register(struct hpet_task *tp, int periodic)
  480. {
  481. unsigned int i;
  482. u64 mask;
  483. struct hpet_timer __iomem *timer;
  484. struct hpet_dev *devp;
  485. struct hpets *hpetp;
  486. switch (periodic) {
  487. case 1:
  488. mask = Tn_PER_INT_CAP_MASK;
  489. break;
  490. case 0:
  491. mask = 0;
  492. break;
  493. default:
  494. return -EINVAL;
  495. }
  496. tp->ht_opaque = NULL;
  497. spin_lock_irq(&hpet_task_lock);
  498. spin_lock(&hpet_lock);
  499. for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next)
  500. for (timer = hpetp->hp_hpet->hpet_timers, i = 0;
  501. i < hpetp->hp_ntimer; i++, timer++) {
  502. if ((readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK)
  503. != mask)
  504. continue;
  505. devp = &hpetp->hp_dev[i];
  506. if (devp->hd_flags & HPET_OPEN || devp->hd_task) {
  507. devp = NULL;
  508. continue;
  509. }
  510. tp->ht_opaque = devp;
  511. devp->hd_task = tp;
  512. break;
  513. }
  514. spin_unlock(&hpet_lock);
  515. spin_unlock_irq(&hpet_task_lock);
  516. if (tp->ht_opaque)
  517. return 0;
  518. else
  519. return -EBUSY;
  520. }
  521. static inline int hpet_tpcheck(struct hpet_task *tp)
  522. {
  523. struct hpet_dev *devp;
  524. struct hpets *hpetp;
  525. devp = tp->ht_opaque;
  526. if (!devp)
  527. return -ENXIO;
  528. for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
  529. if (devp >= hpetp->hp_dev
  530. && devp < (hpetp->hp_dev + hpetp->hp_ntimer)
  531. && devp->hd_hpet == hpetp->hp_hpet)
  532. return 0;
  533. return -ENXIO;
  534. }
  535. int hpet_unregister(struct hpet_task *tp)
  536. {
  537. struct hpet_dev *devp;
  538. struct hpet_timer __iomem *timer;
  539. int err;
  540. if ((err = hpet_tpcheck(tp)))
  541. return err;
  542. spin_lock_irq(&hpet_task_lock);
  543. spin_lock(&hpet_lock);
  544. devp = tp->ht_opaque;
  545. if (devp->hd_task != tp) {
  546. spin_unlock(&hpet_lock);
  547. spin_unlock_irq(&hpet_task_lock);
  548. return -ENXIO;
  549. }
  550. timer = devp->hd_timer;
  551. writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK),
  552. &timer->hpet_config);
  553. devp->hd_flags &= ~(HPET_IE | HPET_PERIODIC);
  554. devp->hd_task = NULL;
  555. spin_unlock(&hpet_lock);
  556. spin_unlock_irq(&hpet_task_lock);
  557. return 0;
  558. }
  559. int hpet_control(struct hpet_task *tp, unsigned int cmd, unsigned long arg)
  560. {
  561. struct hpet_dev *devp;
  562. int err;
  563. if ((err = hpet_tpcheck(tp)))
  564. return err;
  565. spin_lock_irq(&hpet_lock);
  566. devp = tp->ht_opaque;
  567. if (devp->hd_task != tp) {
  568. spin_unlock_irq(&hpet_lock);
  569. return -ENXIO;
  570. }
  571. spin_unlock_irq(&hpet_lock);
  572. return hpet_ioctl_common(devp, cmd, arg, 1);
  573. }
  574. static ctl_table hpet_table[] = {
  575. {
  576. .ctl_name = 1,
  577. .procname = "max-user-freq",
  578. .data = &hpet_max_freq,
  579. .maxlen = sizeof(int),
  580. .mode = 0644,
  581. .proc_handler = &proc_dointvec,
  582. },
  583. {.ctl_name = 0}
  584. };
  585. static ctl_table hpet_root[] = {
  586. {
  587. .ctl_name = 1,
  588. .procname = "hpet",
  589. .maxlen = 0,
  590. .mode = 0555,
  591. .child = hpet_table,
  592. },
  593. {.ctl_name = 0}
  594. };
  595. static ctl_table dev_root[] = {
  596. {
  597. .ctl_name = CTL_DEV,
  598. .procname = "dev",
  599. .maxlen = 0,
  600. .mode = 0555,
  601. .child = hpet_root,
  602. },
  603. {.ctl_name = 0}
  604. };
  605. static struct ctl_table_header *sysctl_header;
  606. static void hpet_register_interpolator(struct hpets *hpetp)
  607. {
  608. #ifdef CONFIG_TIME_INTERPOLATION
  609. struct time_interpolator *ti;
  610. ti = kzalloc(sizeof(*ti), GFP_KERNEL);
  611. if (!ti)
  612. return;
  613. ti->source = TIME_SOURCE_MMIO64;
  614. ti->shift = 10;
  615. ti->addr = &hpetp->hp_hpet->hpet_mc;
  616. ti->frequency = hpetp->hp_tick_freq;
  617. ti->drift = HPET_DRIFT;
  618. ti->mask = -1;
  619. hpetp->hp_interpolator = ti;
  620. register_time_interpolator(ti);
  621. #endif
  622. }
  623. /*
  624. * Adjustment for when arming the timer with
  625. * initial conditions. That is, main counter
  626. * ticks expired before interrupts are enabled.
  627. */
  628. #define TICK_CALIBRATE (1000UL)
  629. static unsigned long hpet_calibrate(struct hpets *hpetp)
  630. {
  631. struct hpet_timer __iomem *timer = NULL;
  632. unsigned long t, m, count, i, flags, start;
  633. struct hpet_dev *devp;
  634. int j;
  635. struct hpet __iomem *hpet;
  636. for (j = 0, devp = hpetp->hp_dev; j < hpetp->hp_ntimer; j++, devp++)
  637. if ((devp->hd_flags & HPET_OPEN) == 0) {
  638. timer = devp->hd_timer;
  639. break;
  640. }
  641. if (!timer)
  642. return 0;
  643. hpet = hpetp->hp_hpet;
  644. t = read_counter(&timer->hpet_compare);
  645. i = 0;
  646. count = hpet_time_div(hpetp, TICK_CALIBRATE);
  647. local_irq_save(flags);
  648. start = read_counter(&hpet->hpet_mc);
  649. do {
  650. m = read_counter(&hpet->hpet_mc);
  651. write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
  652. } while (i++, (m - start) < count);
  653. local_irq_restore(flags);
  654. return (m - start) / i;
  655. }
  656. int hpet_alloc(struct hpet_data *hdp)
  657. {
  658. u64 cap, mcfg;
  659. struct hpet_dev *devp;
  660. u32 i, ntimer;
  661. struct hpets *hpetp;
  662. size_t siz;
  663. struct hpet __iomem *hpet;
  664. static struct hpets *last = NULL;
  665. unsigned long period;
  666. unsigned long long temp;
  667. /*
  668. * hpet_alloc can be called by platform dependent code.
  669. * If platform dependent code has allocated the hpet that
  670. * ACPI has also reported, then we catch it here.
  671. */
  672. if (hpet_is_known(hdp)) {
  673. printk(KERN_DEBUG "%s: duplicate HPET ignored\n",
  674. __FUNCTION__);
  675. return 0;
  676. }
  677. siz = sizeof(struct hpets) + ((hdp->hd_nirqs - 1) *
  678. sizeof(struct hpet_dev));
  679. hpetp = kzalloc(siz, GFP_KERNEL);
  680. if (!hpetp)
  681. return -ENOMEM;
  682. hpetp->hp_which = hpet_nhpet++;
  683. hpetp->hp_hpet = hdp->hd_address;
  684. hpetp->hp_hpet_phys = hdp->hd_phys_address;
  685. hpetp->hp_ntimer = hdp->hd_nirqs;
  686. for (i = 0; i < hdp->hd_nirqs; i++)
  687. hpetp->hp_dev[i].hd_hdwirq = hdp->hd_irq[i];
  688. hpet = hpetp->hp_hpet;
  689. cap = readq(&hpet->hpet_cap);
  690. ntimer = ((cap & HPET_NUM_TIM_CAP_MASK) >> HPET_NUM_TIM_CAP_SHIFT) + 1;
  691. if (hpetp->hp_ntimer != ntimer) {
  692. printk(KERN_WARNING "hpet: number irqs doesn't agree"
  693. " with number of timers\n");
  694. kfree(hpetp);
  695. return -ENODEV;
  696. }
  697. if (last)
  698. last->hp_next = hpetp;
  699. else
  700. hpets = hpetp;
  701. last = hpetp;
  702. period = (cap & HPET_COUNTER_CLK_PERIOD_MASK) >>
  703. HPET_COUNTER_CLK_PERIOD_SHIFT; /* fs, 10^-15 */
  704. temp = 1000000000000000uLL; /* 10^15 femtoseconds per second */
  705. temp += period >> 1; /* round */
  706. do_div(temp, period);
  707. hpetp->hp_tick_freq = temp; /* ticks per second */
  708. printk(KERN_INFO "hpet%d: at MMIO 0x%lx, IRQ%s",
  709. hpetp->hp_which, hdp->hd_phys_address,
  710. hpetp->hp_ntimer > 1 ? "s" : "");
  711. for (i = 0; i < hpetp->hp_ntimer; i++)
  712. printk("%s %d", i > 0 ? "," : "", hdp->hd_irq[i]);
  713. printk("\n");
  714. printk(KERN_INFO "hpet%u: %u %d-bit timers, %Lu Hz\n",
  715. hpetp->hp_which, hpetp->hp_ntimer,
  716. cap & HPET_COUNTER_SIZE_MASK ? 64 : 32, hpetp->hp_tick_freq);
  717. mcfg = readq(&hpet->hpet_config);
  718. if ((mcfg & HPET_ENABLE_CNF_MASK) == 0) {
  719. write_counter(0L, &hpet->hpet_mc);
  720. mcfg |= HPET_ENABLE_CNF_MASK;
  721. writeq(mcfg, &hpet->hpet_config);
  722. }
  723. for (i = 0, devp = hpetp->hp_dev; i < hpetp->hp_ntimer; i++, devp++) {
  724. struct hpet_timer __iomem *timer;
  725. timer = &hpet->hpet_timers[devp - hpetp->hp_dev];
  726. devp->hd_hpets = hpetp;
  727. devp->hd_hpet = hpet;
  728. devp->hd_timer = timer;
  729. /*
  730. * If the timer was reserved by platform code,
  731. * then make timer unavailable for opens.
  732. */
  733. if (hdp->hd_state & (1 << i)) {
  734. devp->hd_flags = HPET_OPEN;
  735. continue;
  736. }
  737. init_waitqueue_head(&devp->hd_waitqueue);
  738. }
  739. hpetp->hp_delta = hpet_calibrate(hpetp);
  740. hpet_register_interpolator(hpetp);
  741. return 0;
  742. }
  743. static acpi_status hpet_resources(struct acpi_resource *res, void *data)
  744. {
  745. struct hpet_data *hdp;
  746. acpi_status status;
  747. struct acpi_resource_address64 addr;
  748. hdp = data;
  749. status = acpi_resource_to_address64(res, &addr);
  750. if (ACPI_SUCCESS(status)) {
  751. hdp->hd_phys_address = addr.minimum;
  752. hdp->hd_address = ioremap(addr.minimum, addr.address_length);
  753. if (hpet_is_known(hdp)) {
  754. printk(KERN_DEBUG "%s: 0x%lx is busy\n",
  755. __FUNCTION__, hdp->hd_phys_address);
  756. iounmap(hdp->hd_address);
  757. return -EBUSY;
  758. }
  759. } else if (res->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32) {
  760. struct acpi_resource_fixed_memory32 *fixmem32;
  761. fixmem32 = &res->data.fixed_memory32;
  762. if (!fixmem32)
  763. return -EINVAL;
  764. hdp->hd_phys_address = fixmem32->address;
  765. hdp->hd_address = ioremap(fixmem32->address,
  766. HPET_RANGE_SIZE);
  767. if (hpet_is_known(hdp)) {
  768. printk(KERN_DEBUG "%s: 0x%lx is busy\n",
  769. __FUNCTION__, hdp->hd_phys_address);
  770. iounmap(hdp->hd_address);
  771. return -EBUSY;
  772. }
  773. } else if (res->type == ACPI_RESOURCE_TYPE_EXTENDED_IRQ) {
  774. struct acpi_resource_extended_irq *irqp;
  775. int i, irq;
  776. irqp = &res->data.extended_irq;
  777. for (i = 0; i < irqp->interrupt_count; i++) {
  778. irq = acpi_register_gsi(irqp->interrupts[i],
  779. irqp->triggering, irqp->polarity);
  780. if (irq < 0)
  781. return AE_ERROR;
  782. hdp->hd_irq[hdp->hd_nirqs] = irq;
  783. hdp->hd_nirqs++;
  784. }
  785. }
  786. return AE_OK;
  787. }
  788. static int hpet_acpi_add(struct acpi_device *device)
  789. {
  790. acpi_status result;
  791. struct hpet_data data;
  792. memset(&data, 0, sizeof(data));
  793. result =
  794. acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  795. hpet_resources, &data);
  796. if (ACPI_FAILURE(result))
  797. return -ENODEV;
  798. if (!data.hd_address || !data.hd_nirqs) {
  799. printk("%s: no address or irqs in _CRS\n", __FUNCTION__);
  800. return -ENODEV;
  801. }
  802. return hpet_alloc(&data);
  803. }
  804. static int hpet_acpi_remove(struct acpi_device *device, int type)
  805. {
  806. /* XXX need to unregister interpolator, dealloc mem, etc */
  807. return -EINVAL;
  808. }
  809. static struct acpi_driver hpet_acpi_driver = {
  810. .name = "hpet",
  811. .ids = "PNP0103",
  812. .ops = {
  813. .add = hpet_acpi_add,
  814. .remove = hpet_acpi_remove,
  815. },
  816. };
  817. static struct miscdevice hpet_misc = { HPET_MINOR, "hpet", &hpet_fops };
  818. static int __init hpet_init(void)
  819. {
  820. int result;
  821. result = misc_register(&hpet_misc);
  822. if (result < 0)
  823. return -ENODEV;
  824. sysctl_header = register_sysctl_table(dev_root, 0);
  825. result = acpi_bus_register_driver(&hpet_acpi_driver);
  826. if (result < 0) {
  827. if (sysctl_header)
  828. unregister_sysctl_table(sysctl_header);
  829. misc_deregister(&hpet_misc);
  830. return result;
  831. }
  832. return 0;
  833. }
  834. static void __exit hpet_exit(void)
  835. {
  836. acpi_bus_unregister_driver(&hpet_acpi_driver);
  837. if (sysctl_header)
  838. unregister_sysctl_table(sysctl_header);
  839. misc_deregister(&hpet_misc);
  840. return;
  841. }
  842. module_init(hpet_init);
  843. module_exit(hpet_exit);
  844. MODULE_AUTHOR("Bob Picco <Robert.Picco@hp.com>");
  845. MODULE_LICENSE("GPL");