hpet.c 22 KB

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