hpet.c 22 KB

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