hpet.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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. static inline int hpet_tpcheck(struct hpet_task *tp)
  496. {
  497. struct hpet_dev *devp;
  498. struct hpets *hpetp;
  499. devp = tp->ht_opaque;
  500. if (!devp)
  501. return -ENXIO;
  502. for (hpetp = hpets; hpetp; hpetp = hpetp->hp_next)
  503. if (devp >= hpetp->hp_dev
  504. && devp < (hpetp->hp_dev + hpetp->hp_ntimer)
  505. && devp->hd_hpet == hpetp->hp_hpet)
  506. return 0;
  507. return -ENXIO;
  508. }
  509. int hpet_unregister(struct hpet_task *tp)
  510. {
  511. struct hpet_dev *devp;
  512. struct hpet_timer __iomem *timer;
  513. int err;
  514. if ((err = hpet_tpcheck(tp)))
  515. return err;
  516. spin_lock_irq(&hpet_task_lock);
  517. spin_lock(&hpet_lock);
  518. devp = tp->ht_opaque;
  519. if (devp->hd_task != tp) {
  520. spin_unlock(&hpet_lock);
  521. spin_unlock_irq(&hpet_task_lock);
  522. return -ENXIO;
  523. }
  524. timer = devp->hd_timer;
  525. writeq((readq(&timer->hpet_config) & ~Tn_INT_ENB_CNF_MASK),
  526. &timer->hpet_config);
  527. devp->hd_flags &= ~(HPET_IE | HPET_PERIODIC);
  528. devp->hd_task = NULL;
  529. spin_unlock(&hpet_lock);
  530. spin_unlock_irq(&hpet_task_lock);
  531. return 0;
  532. }
  533. static ctl_table hpet_table[] = {
  534. {
  535. .ctl_name = CTL_UNNUMBERED,
  536. .procname = "max-user-freq",
  537. .data = &hpet_max_freq,
  538. .maxlen = sizeof(int),
  539. .mode = 0644,
  540. .proc_handler = &proc_dointvec,
  541. },
  542. {.ctl_name = 0}
  543. };
  544. static ctl_table hpet_root[] = {
  545. {
  546. .ctl_name = CTL_UNNUMBERED,
  547. .procname = "hpet",
  548. .maxlen = 0,
  549. .mode = 0555,
  550. .child = hpet_table,
  551. },
  552. {.ctl_name = 0}
  553. };
  554. static ctl_table dev_root[] = {
  555. {
  556. .ctl_name = CTL_DEV,
  557. .procname = "dev",
  558. .maxlen = 0,
  559. .mode = 0555,
  560. .child = hpet_root,
  561. },
  562. {.ctl_name = 0}
  563. };
  564. static struct ctl_table_header *sysctl_header;
  565. /*
  566. * Adjustment for when arming the timer with
  567. * initial conditions. That is, main counter
  568. * ticks expired before interrupts are enabled.
  569. */
  570. #define TICK_CALIBRATE (1000UL)
  571. static unsigned long hpet_calibrate(struct hpets *hpetp)
  572. {
  573. struct hpet_timer __iomem *timer = NULL;
  574. unsigned long t, m, count, i, flags, start;
  575. struct hpet_dev *devp;
  576. int j;
  577. struct hpet __iomem *hpet;
  578. for (j = 0, devp = hpetp->hp_dev; j < hpetp->hp_ntimer; j++, devp++)
  579. if ((devp->hd_flags & HPET_OPEN) == 0) {
  580. timer = devp->hd_timer;
  581. break;
  582. }
  583. if (!timer)
  584. return 0;
  585. hpet = hpetp->hp_hpet;
  586. t = read_counter(&timer->hpet_compare);
  587. i = 0;
  588. count = hpet_time_div(hpetp, TICK_CALIBRATE);
  589. local_irq_save(flags);
  590. start = read_counter(&hpet->hpet_mc);
  591. do {
  592. m = read_counter(&hpet->hpet_mc);
  593. write_counter(t + m + hpetp->hp_delta, &timer->hpet_compare);
  594. } while (i++, (m - start) < count);
  595. local_irq_restore(flags);
  596. return (m - start) / i;
  597. }
  598. int hpet_alloc(struct hpet_data *hdp)
  599. {
  600. u64 cap, mcfg;
  601. struct hpet_dev *devp;
  602. u32 i, ntimer;
  603. struct hpets *hpetp;
  604. size_t siz;
  605. struct hpet __iomem *hpet;
  606. static struct hpets *last = NULL;
  607. unsigned long period;
  608. unsigned long long temp;
  609. /*
  610. * hpet_alloc can be called by platform dependent code.
  611. * If platform dependent code has allocated the hpet that
  612. * ACPI has also reported, then we catch it here.
  613. */
  614. if (hpet_is_known(hdp)) {
  615. printk(KERN_DEBUG "%s: duplicate HPET ignored\n",
  616. __FUNCTION__);
  617. return 0;
  618. }
  619. siz = sizeof(struct hpets) + ((hdp->hd_nirqs - 1) *
  620. sizeof(struct hpet_dev));
  621. hpetp = kzalloc(siz, GFP_KERNEL);
  622. if (!hpetp)
  623. return -ENOMEM;
  624. hpetp->hp_which = hpet_nhpet++;
  625. hpetp->hp_hpet = hdp->hd_address;
  626. hpetp->hp_hpet_phys = hdp->hd_phys_address;
  627. hpetp->hp_ntimer = hdp->hd_nirqs;
  628. for (i = 0; i < hdp->hd_nirqs; i++)
  629. hpetp->hp_dev[i].hd_hdwirq = hdp->hd_irq[i];
  630. hpet = hpetp->hp_hpet;
  631. cap = readq(&hpet->hpet_cap);
  632. ntimer = ((cap & HPET_NUM_TIM_CAP_MASK) >> HPET_NUM_TIM_CAP_SHIFT) + 1;
  633. if (hpetp->hp_ntimer != ntimer) {
  634. printk(KERN_WARNING "hpet: number irqs doesn't agree"
  635. " with number of timers\n");
  636. kfree(hpetp);
  637. return -ENODEV;
  638. }
  639. if (last)
  640. last->hp_next = hpetp;
  641. else
  642. hpets = hpetp;
  643. last = hpetp;
  644. period = (cap & HPET_COUNTER_CLK_PERIOD_MASK) >>
  645. HPET_COUNTER_CLK_PERIOD_SHIFT; /* fs, 10^-15 */
  646. temp = 1000000000000000uLL; /* 10^15 femtoseconds per second */
  647. temp += period >> 1; /* round */
  648. do_div(temp, period);
  649. hpetp->hp_tick_freq = temp; /* ticks per second */
  650. printk(KERN_INFO "hpet%d: at MMIO 0x%lx, IRQ%s",
  651. hpetp->hp_which, hdp->hd_phys_address,
  652. hpetp->hp_ntimer > 1 ? "s" : "");
  653. for (i = 0; i < hpetp->hp_ntimer; i++)
  654. printk("%s %d", i > 0 ? "," : "", hdp->hd_irq[i]);
  655. printk("\n");
  656. printk(KERN_INFO "hpet%u: %u %d-bit timers, %Lu Hz\n",
  657. hpetp->hp_which, hpetp->hp_ntimer,
  658. cap & HPET_COUNTER_SIZE_MASK ? 64 : 32, hpetp->hp_tick_freq);
  659. mcfg = readq(&hpet->hpet_config);
  660. if ((mcfg & HPET_ENABLE_CNF_MASK) == 0) {
  661. write_counter(0L, &hpet->hpet_mc);
  662. mcfg |= HPET_ENABLE_CNF_MASK;
  663. writeq(mcfg, &hpet->hpet_config);
  664. }
  665. for (i = 0, devp = hpetp->hp_dev; i < hpetp->hp_ntimer; i++, devp++) {
  666. struct hpet_timer __iomem *timer;
  667. timer = &hpet->hpet_timers[devp - hpetp->hp_dev];
  668. devp->hd_hpets = hpetp;
  669. devp->hd_hpet = hpet;
  670. devp->hd_timer = timer;
  671. /*
  672. * If the timer was reserved by platform code,
  673. * then make timer unavailable for opens.
  674. */
  675. if (hdp->hd_state & (1 << i)) {
  676. devp->hd_flags = HPET_OPEN;
  677. continue;
  678. }
  679. init_waitqueue_head(&devp->hd_waitqueue);
  680. }
  681. hpetp->hp_delta = hpet_calibrate(hpetp);
  682. /* This clocksource driver currently only works on ia64 */
  683. #ifdef CONFIG_IA64
  684. if (!hpet_clocksource) {
  685. hpet_mctr = (void __iomem *)&hpetp->hp_hpet->hpet_mc;
  686. CLKSRC_FSYS_MMIO_SET(clocksource_hpet.fsys_mmio, hpet_mctr);
  687. clocksource_hpet.mult = clocksource_hz2mult(hpetp->hp_tick_freq,
  688. clocksource_hpet.shift);
  689. clocksource_register(&clocksource_hpet);
  690. hpetp->hp_clocksource = &clocksource_hpet;
  691. hpet_clocksource = &clocksource_hpet;
  692. }
  693. #endif
  694. return 0;
  695. }
  696. static acpi_status hpet_resources(struct acpi_resource *res, void *data)
  697. {
  698. struct hpet_data *hdp;
  699. acpi_status status;
  700. struct acpi_resource_address64 addr;
  701. hdp = data;
  702. status = acpi_resource_to_address64(res, &addr);
  703. if (ACPI_SUCCESS(status)) {
  704. hdp->hd_phys_address = addr.minimum;
  705. hdp->hd_address = ioremap(addr.minimum, addr.address_length);
  706. if (hpet_is_known(hdp)) {
  707. printk(KERN_DEBUG "%s: 0x%lx is busy\n",
  708. __FUNCTION__, hdp->hd_phys_address);
  709. iounmap(hdp->hd_address);
  710. return AE_ALREADY_EXISTS;
  711. }
  712. } else if (res->type == ACPI_RESOURCE_TYPE_FIXED_MEMORY32) {
  713. struct acpi_resource_fixed_memory32 *fixmem32;
  714. fixmem32 = &res->data.fixed_memory32;
  715. if (!fixmem32)
  716. return AE_NO_MEMORY;
  717. hdp->hd_phys_address = fixmem32->address;
  718. hdp->hd_address = ioremap(fixmem32->address,
  719. HPET_RANGE_SIZE);
  720. if (hpet_is_known(hdp)) {
  721. printk(KERN_DEBUG "%s: 0x%lx is busy\n",
  722. __FUNCTION__, hdp->hd_phys_address);
  723. iounmap(hdp->hd_address);
  724. return AE_ALREADY_EXISTS;
  725. }
  726. } else if (res->type == ACPI_RESOURCE_TYPE_EXTENDED_IRQ) {
  727. struct acpi_resource_extended_irq *irqp;
  728. int i, irq;
  729. irqp = &res->data.extended_irq;
  730. for (i = 0; i < irqp->interrupt_count; i++) {
  731. irq = acpi_register_gsi(irqp->interrupts[i],
  732. irqp->triggering, irqp->polarity);
  733. if (irq < 0)
  734. return AE_ERROR;
  735. hdp->hd_irq[hdp->hd_nirqs] = irq;
  736. hdp->hd_nirqs++;
  737. }
  738. }
  739. return AE_OK;
  740. }
  741. static int hpet_acpi_add(struct acpi_device *device)
  742. {
  743. acpi_status result;
  744. struct hpet_data data;
  745. memset(&data, 0, sizeof(data));
  746. result =
  747. acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  748. hpet_resources, &data);
  749. if (ACPI_FAILURE(result))
  750. return -ENODEV;
  751. if (!data.hd_address || !data.hd_nirqs) {
  752. printk("%s: no address or irqs in _CRS\n", __FUNCTION__);
  753. return -ENODEV;
  754. }
  755. return hpet_alloc(&data);
  756. }
  757. static int hpet_acpi_remove(struct acpi_device *device, int type)
  758. {
  759. /* XXX need to unregister clocksource, dealloc mem, etc */
  760. return -EINVAL;
  761. }
  762. static const struct acpi_device_id hpet_device_ids[] = {
  763. {"PNP0103", 0},
  764. {"", 0},
  765. };
  766. MODULE_DEVICE_TABLE(acpi, hpet_device_ids);
  767. static struct acpi_driver hpet_acpi_driver = {
  768. .name = "hpet",
  769. .ids = hpet_device_ids,
  770. .ops = {
  771. .add = hpet_acpi_add,
  772. .remove = hpet_acpi_remove,
  773. },
  774. };
  775. static struct miscdevice hpet_misc = { HPET_MINOR, "hpet", &hpet_fops };
  776. static int __init hpet_init(void)
  777. {
  778. int result;
  779. result = misc_register(&hpet_misc);
  780. if (result < 0)
  781. return -ENODEV;
  782. sysctl_header = register_sysctl_table(dev_root);
  783. result = acpi_bus_register_driver(&hpet_acpi_driver);
  784. if (result < 0) {
  785. if (sysctl_header)
  786. unregister_sysctl_table(sysctl_header);
  787. misc_deregister(&hpet_misc);
  788. return result;
  789. }
  790. return 0;
  791. }
  792. static void __exit hpet_exit(void)
  793. {
  794. acpi_bus_unregister_driver(&hpet_acpi_driver);
  795. if (sysctl_header)
  796. unregister_sysctl_table(sysctl_header);
  797. misc_deregister(&hpet_misc);
  798. return;
  799. }
  800. module_init(hpet_init);
  801. module_exit(hpet_exit);
  802. MODULE_AUTHOR("Bob Picco <Robert.Picco@hp.com>");
  803. MODULE_LICENSE("GPL");