hpet.c 23 KB

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