hpet.c 22 KB

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