hpet.c 23 KB

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