hpet.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. #include <linux/clocksource.h>
  2. #include <linux/clockchips.h>
  3. #include <linux/delay.h>
  4. #include <linux/errno.h>
  5. #include <linux/hpet.h>
  6. #include <linux/init.h>
  7. #include <linux/sysdev.h>
  8. #include <linux/pm.h>
  9. #include <asm/fixmap.h>
  10. #include <asm/hpet.h>
  11. #include <asm/i8253.h>
  12. #include <asm/io.h>
  13. #define HPET_MASK CLOCKSOURCE_MASK(32)
  14. #define HPET_SHIFT 22
  15. /* FSEC = 10^-15
  16. NSEC = 10^-9 */
  17. #define FSEC_PER_NSEC 1000000L
  18. /*
  19. * HPET address is set in acpi/boot.c, when an ACPI entry exists
  20. */
  21. unsigned long hpet_address;
  22. static void __iomem *hpet_virt_address;
  23. unsigned long hpet_readl(unsigned long a)
  24. {
  25. return readl(hpet_virt_address + a);
  26. }
  27. static inline void hpet_writel(unsigned long d, unsigned long a)
  28. {
  29. writel(d, hpet_virt_address + a);
  30. }
  31. #ifdef CONFIG_X86_64
  32. #include <asm/pgtable.h>
  33. #endif
  34. static inline void hpet_set_mapping(void)
  35. {
  36. hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
  37. #ifdef CONFIG_X86_64
  38. __set_fixmap(VSYSCALL_HPET, hpet_address, PAGE_KERNEL_VSYSCALL_NOCACHE);
  39. #endif
  40. }
  41. static inline void hpet_clear_mapping(void)
  42. {
  43. iounmap(hpet_virt_address);
  44. hpet_virt_address = NULL;
  45. }
  46. /*
  47. * HPET command line enable / disable
  48. */
  49. static int boot_hpet_disable;
  50. int hpet_force_user;
  51. static int __init hpet_setup(char* str)
  52. {
  53. if (str) {
  54. if (!strncmp("disable", str, 7))
  55. boot_hpet_disable = 1;
  56. if (!strncmp("force", str, 5))
  57. hpet_force_user = 1;
  58. }
  59. return 1;
  60. }
  61. __setup("hpet=", hpet_setup);
  62. static int __init disable_hpet(char *str)
  63. {
  64. boot_hpet_disable = 1;
  65. return 1;
  66. }
  67. __setup("nohpet", disable_hpet);
  68. static inline int is_hpet_capable(void)
  69. {
  70. return (!boot_hpet_disable && hpet_address);
  71. }
  72. /*
  73. * HPET timer interrupt enable / disable
  74. */
  75. static int hpet_legacy_int_enabled;
  76. /**
  77. * is_hpet_enabled - check whether the hpet timer interrupt is enabled
  78. */
  79. int is_hpet_enabled(void)
  80. {
  81. return is_hpet_capable() && hpet_legacy_int_enabled;
  82. }
  83. EXPORT_SYMBOL_GPL(is_hpet_enabled);
  84. /*
  85. * When the hpet driver (/dev/hpet) is enabled, we need to reserve
  86. * timer 0 and timer 1 in case of RTC emulation.
  87. */
  88. #ifdef CONFIG_HPET
  89. static void hpet_reserve_platform_timers(unsigned long id)
  90. {
  91. struct hpet __iomem *hpet = hpet_virt_address;
  92. struct hpet_timer __iomem *timer = &hpet->hpet_timers[2];
  93. unsigned int nrtimers, i;
  94. struct hpet_data hd;
  95. nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
  96. memset(&hd, 0, sizeof (hd));
  97. hd.hd_phys_address = hpet_address;
  98. hd.hd_address = hpet;
  99. hd.hd_nirqs = nrtimers;
  100. hpet_reserve_timer(&hd, 0);
  101. #ifdef CONFIG_HPET_EMULATE_RTC
  102. hpet_reserve_timer(&hd, 1);
  103. #endif
  104. /*
  105. * NOTE that hd_irq[] reflects IOAPIC input pins (LEGACY_8254
  106. * is wrong for i8259!) not the output IRQ. Many BIOS writers
  107. * don't bother configuring *any* comparator interrupts.
  108. */
  109. hd.hd_irq[0] = HPET_LEGACY_8254;
  110. hd.hd_irq[1] = HPET_LEGACY_RTC;
  111. for (i = 2; i < nrtimers; timer++, i++) {
  112. hd.hd_irq[i] = (readl(&timer->hpet_config) & Tn_INT_ROUTE_CNF_MASK) >>
  113. Tn_INT_ROUTE_CNF_SHIFT;
  114. }
  115. hpet_alloc(&hd);
  116. }
  117. #else
  118. static void hpet_reserve_platform_timers(unsigned long id) { }
  119. #endif
  120. /*
  121. * Common hpet info
  122. */
  123. static unsigned long hpet_period;
  124. static void hpet_legacy_set_mode(enum clock_event_mode mode,
  125. struct clock_event_device *evt);
  126. static int hpet_legacy_next_event(unsigned long delta,
  127. struct clock_event_device *evt);
  128. /*
  129. * The hpet clock event device
  130. */
  131. static struct clock_event_device hpet_clockevent = {
  132. .name = "hpet",
  133. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  134. .set_mode = hpet_legacy_set_mode,
  135. .set_next_event = hpet_legacy_next_event,
  136. .shift = 32,
  137. .irq = 0,
  138. .rating = 50,
  139. };
  140. static void hpet_start_counter(void)
  141. {
  142. unsigned long cfg = hpet_readl(HPET_CFG);
  143. cfg &= ~HPET_CFG_ENABLE;
  144. hpet_writel(cfg, HPET_CFG);
  145. hpet_writel(0, HPET_COUNTER);
  146. hpet_writel(0, HPET_COUNTER + 4);
  147. cfg |= HPET_CFG_ENABLE;
  148. hpet_writel(cfg, HPET_CFG);
  149. }
  150. static void hpet_resume_device(void)
  151. {
  152. force_hpet_resume();
  153. }
  154. static void hpet_restart_counter(void)
  155. {
  156. hpet_resume_device();
  157. hpet_start_counter();
  158. }
  159. static void hpet_enable_legacy_int(void)
  160. {
  161. unsigned long cfg = hpet_readl(HPET_CFG);
  162. cfg |= HPET_CFG_LEGACY;
  163. hpet_writel(cfg, HPET_CFG);
  164. hpet_legacy_int_enabled = 1;
  165. }
  166. static void hpet_legacy_clockevent_register(void)
  167. {
  168. /* Start HPET legacy interrupts */
  169. hpet_enable_legacy_int();
  170. /*
  171. * The mult factor is defined as (include/linux/clockchips.h)
  172. * mult/2^shift = cyc/ns (in contrast to ns/cyc in clocksource.h)
  173. * hpet_period is in units of femtoseconds (per cycle), so
  174. * mult/2^shift = cyc/ns = 10^6/hpet_period
  175. * mult = (10^6 * 2^shift)/hpet_period
  176. * mult = (FSEC_PER_NSEC << hpet_clockevent.shift)/hpet_period
  177. */
  178. hpet_clockevent.mult = div_sc((unsigned long) FSEC_PER_NSEC,
  179. hpet_period, hpet_clockevent.shift);
  180. /* Calculate the min / max delta */
  181. hpet_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF,
  182. &hpet_clockevent);
  183. /* 5 usec minimum reprogramming delta. */
  184. hpet_clockevent.min_delta_ns = 5000;
  185. /*
  186. * Start hpet with the boot cpu mask and make it
  187. * global after the IO_APIC has been initialized.
  188. */
  189. hpet_clockevent.cpumask = cpumask_of_cpu(smp_processor_id());
  190. clockevents_register_device(&hpet_clockevent);
  191. global_clock_event = &hpet_clockevent;
  192. printk(KERN_DEBUG "hpet clockevent registered\n");
  193. }
  194. static void hpet_set_mode(enum clock_event_mode mode,
  195. struct clock_event_device *evt, int timer)
  196. {
  197. unsigned long cfg, cmp, now;
  198. uint64_t delta;
  199. switch(mode) {
  200. case CLOCK_EVT_MODE_PERIODIC:
  201. delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * evt->mult;
  202. delta >>= evt->shift;
  203. now = hpet_readl(HPET_COUNTER);
  204. cmp = now + (unsigned long) delta;
  205. cfg = hpet_readl(HPET_Tn_CFG(timer));
  206. cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
  207. HPET_TN_SETVAL | HPET_TN_32BIT;
  208. hpet_writel(cfg, HPET_Tn_CFG(timer));
  209. /*
  210. * The first write after writing TN_SETVAL to the
  211. * config register sets the counter value, the second
  212. * write sets the period.
  213. */
  214. hpet_writel(cmp, HPET_Tn_CMP(timer));
  215. udelay(1);
  216. hpet_writel((unsigned long) delta, HPET_Tn_CMP(timer));
  217. break;
  218. case CLOCK_EVT_MODE_ONESHOT:
  219. cfg = hpet_readl(HPET_Tn_CFG(timer));
  220. cfg &= ~HPET_TN_PERIODIC;
  221. cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
  222. hpet_writel(cfg, HPET_Tn_CFG(timer));
  223. break;
  224. case CLOCK_EVT_MODE_UNUSED:
  225. case CLOCK_EVT_MODE_SHUTDOWN:
  226. cfg = hpet_readl(HPET_Tn_CFG(timer));
  227. cfg &= ~HPET_TN_ENABLE;
  228. hpet_writel(cfg, HPET_Tn_CFG(timer));
  229. break;
  230. case CLOCK_EVT_MODE_RESUME:
  231. hpet_enable_legacy_int();
  232. break;
  233. }
  234. }
  235. static int hpet_next_event(unsigned long delta,
  236. struct clock_event_device *evt, int timer)
  237. {
  238. u32 cnt;
  239. cnt = hpet_readl(HPET_COUNTER);
  240. cnt += (u32) delta;
  241. hpet_writel(cnt, HPET_Tn_CMP(timer));
  242. /*
  243. * We need to read back the CMP register to make sure that
  244. * what we wrote hit the chip before we compare it to the
  245. * counter.
  246. */
  247. WARN_ON((u32)hpet_readl(HPET_T0_CMP) != cnt);
  248. return (s32)((u32)hpet_readl(HPET_COUNTER) - cnt) >= 0 ? -ETIME : 0;
  249. }
  250. static void hpet_legacy_set_mode(enum clock_event_mode mode,
  251. struct clock_event_device *evt)
  252. {
  253. hpet_set_mode(mode, evt, 0);
  254. }
  255. static int hpet_legacy_next_event(unsigned long delta,
  256. struct clock_event_device *evt)
  257. {
  258. return hpet_next_event(delta, evt, 0);
  259. }
  260. /*
  261. * Clock source related code
  262. */
  263. static cycle_t read_hpet(void)
  264. {
  265. return (cycle_t)hpet_readl(HPET_COUNTER);
  266. }
  267. #ifdef CONFIG_X86_64
  268. static cycle_t __vsyscall_fn vread_hpet(void)
  269. {
  270. return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
  271. }
  272. #endif
  273. static struct clocksource clocksource_hpet = {
  274. .name = "hpet",
  275. .rating = 250,
  276. .read = read_hpet,
  277. .mask = HPET_MASK,
  278. .shift = HPET_SHIFT,
  279. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  280. .resume = hpet_restart_counter,
  281. #ifdef CONFIG_X86_64
  282. .vread = vread_hpet,
  283. #endif
  284. };
  285. static int hpet_clocksource_register(void)
  286. {
  287. u64 start, now;
  288. cycle_t t1;
  289. /* Start the counter */
  290. hpet_start_counter();
  291. /* Verify whether hpet counter works */
  292. t1 = read_hpet();
  293. rdtscll(start);
  294. /*
  295. * We don't know the TSC frequency yet, but waiting for
  296. * 200000 TSC cycles is safe:
  297. * 4 GHz == 50us
  298. * 1 GHz == 200us
  299. */
  300. do {
  301. rep_nop();
  302. rdtscll(now);
  303. } while ((now - start) < 200000UL);
  304. if (t1 == read_hpet()) {
  305. printk(KERN_WARNING
  306. "HPET counter not counting. HPET disabled\n");
  307. return -ENODEV;
  308. }
  309. /*
  310. * The definition of mult is (include/linux/clocksource.h)
  311. * mult/2^shift = ns/cyc and hpet_period is in units of fsec/cyc
  312. * so we first need to convert hpet_period to ns/cyc units:
  313. * mult/2^shift = ns/cyc = hpet_period/10^6
  314. * mult = (hpet_period * 2^shift)/10^6
  315. * mult = (hpet_period << shift)/FSEC_PER_NSEC
  316. */
  317. clocksource_hpet.mult = div_sc(hpet_period, FSEC_PER_NSEC, HPET_SHIFT);
  318. clocksource_register(&clocksource_hpet);
  319. return 0;
  320. }
  321. /**
  322. * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
  323. */
  324. int __init hpet_enable(void)
  325. {
  326. unsigned long id;
  327. int i;
  328. if (!is_hpet_capable())
  329. return 0;
  330. hpet_set_mapping();
  331. /*
  332. * Read the period and check for a sane value:
  333. */
  334. hpet_period = hpet_readl(HPET_PERIOD);
  335. /*
  336. * AMD SB700 based systems with spread spectrum enabled use a
  337. * SMM based HPET emulation to provide proper frequency
  338. * setting. The SMM code is initialized with the first HPET
  339. * register access and takes some time to complete. During
  340. * this time the config register reads 0xffffffff. We check
  341. * for max. 1000 loops whether the config register reads a non
  342. * 0xffffffff value to make sure that HPET is up and running
  343. * before we go further. A counting loop is safe, as the HPET
  344. * access takes thousands of CPU cycles. On non SB700 based
  345. * machines this check is only done once and has no side
  346. * effects.
  347. */
  348. for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) {
  349. if (i == 1000) {
  350. printk(KERN_WARNING
  351. "HPET config register value = 0xFFFFFFFF. "
  352. "Disabling HPET\n");
  353. goto out_nohpet;
  354. }
  355. }
  356. if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
  357. goto out_nohpet;
  358. /*
  359. * Read the HPET ID register to retrieve the IRQ routing
  360. * information and the number of channels
  361. */
  362. id = hpet_readl(HPET_ID);
  363. #ifdef CONFIG_HPET_EMULATE_RTC
  364. /*
  365. * The legacy routing mode needs at least two channels, tick timer
  366. * and the rtc emulation channel.
  367. */
  368. if (!(id & HPET_ID_NUMBER))
  369. goto out_nohpet;
  370. #endif
  371. if (hpet_clocksource_register())
  372. goto out_nohpet;
  373. if (id & HPET_ID_LEGSUP) {
  374. hpet_legacy_clockevent_register();
  375. return 1;
  376. }
  377. return 0;
  378. out_nohpet:
  379. hpet_clear_mapping();
  380. boot_hpet_disable = 1;
  381. return 0;
  382. }
  383. /*
  384. * Needs to be late, as the reserve_timer code calls kalloc !
  385. *
  386. * Not a problem on i386 as hpet_enable is called from late_time_init,
  387. * but on x86_64 it is necessary !
  388. */
  389. static __init int hpet_late_init(void)
  390. {
  391. if (boot_hpet_disable)
  392. return -ENODEV;
  393. if (!hpet_address) {
  394. if (!force_hpet_address)
  395. return -ENODEV;
  396. hpet_address = force_hpet_address;
  397. hpet_enable();
  398. if (!hpet_virt_address)
  399. return -ENODEV;
  400. }
  401. hpet_reserve_platform_timers(hpet_readl(HPET_ID));
  402. return 0;
  403. }
  404. fs_initcall(hpet_late_init);
  405. void hpet_disable(void)
  406. {
  407. if (is_hpet_capable()) {
  408. unsigned long cfg = hpet_readl(HPET_CFG);
  409. if (hpet_legacy_int_enabled) {
  410. cfg &= ~HPET_CFG_LEGACY;
  411. hpet_legacy_int_enabled = 0;
  412. }
  413. cfg &= ~HPET_CFG_ENABLE;
  414. hpet_writel(cfg, HPET_CFG);
  415. }
  416. }
  417. #ifdef CONFIG_HPET_EMULATE_RTC
  418. /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
  419. * is enabled, we support RTC interrupt functionality in software.
  420. * RTC has 3 kinds of interrupts:
  421. * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
  422. * is updated
  423. * 2) Alarm Interrupt - generate an interrupt at a specific time of day
  424. * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
  425. * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
  426. * (1) and (2) above are implemented using polling at a frequency of
  427. * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
  428. * overhead. (DEFAULT_RTC_INT_FREQ)
  429. * For (3), we use interrupts at 64Hz or user specified periodic
  430. * frequency, whichever is higher.
  431. */
  432. #include <linux/mc146818rtc.h>
  433. #include <linux/rtc.h>
  434. #include <asm/rtc.h>
  435. #define DEFAULT_RTC_INT_FREQ 64
  436. #define DEFAULT_RTC_SHIFT 6
  437. #define RTC_NUM_INTS 1
  438. static unsigned long hpet_rtc_flags;
  439. static int hpet_prev_update_sec;
  440. static struct rtc_time hpet_alarm_time;
  441. static unsigned long hpet_pie_count;
  442. static unsigned long hpet_t1_cmp;
  443. static unsigned long hpet_default_delta;
  444. static unsigned long hpet_pie_delta;
  445. static unsigned long hpet_pie_limit;
  446. static rtc_irq_handler irq_handler;
  447. /*
  448. * Registers a IRQ handler.
  449. */
  450. int hpet_register_irq_handler(rtc_irq_handler handler)
  451. {
  452. if (!is_hpet_enabled())
  453. return -ENODEV;
  454. if (irq_handler)
  455. return -EBUSY;
  456. irq_handler = handler;
  457. return 0;
  458. }
  459. EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
  460. /*
  461. * Deregisters the IRQ handler registered with hpet_register_irq_handler()
  462. * and does cleanup.
  463. */
  464. void hpet_unregister_irq_handler(rtc_irq_handler handler)
  465. {
  466. if (!is_hpet_enabled())
  467. return;
  468. irq_handler = NULL;
  469. hpet_rtc_flags = 0;
  470. }
  471. EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
  472. /*
  473. * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
  474. * is not supported by all HPET implementations for timer 1.
  475. *
  476. * hpet_rtc_timer_init() is called when the rtc is initialized.
  477. */
  478. int hpet_rtc_timer_init(void)
  479. {
  480. unsigned long cfg, cnt, delta, flags;
  481. if (!is_hpet_enabled())
  482. return 0;
  483. if (!hpet_default_delta) {
  484. uint64_t clc;
  485. clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
  486. clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
  487. hpet_default_delta = (unsigned long) clc;
  488. }
  489. if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
  490. delta = hpet_default_delta;
  491. else
  492. delta = hpet_pie_delta;
  493. local_irq_save(flags);
  494. cnt = delta + hpet_readl(HPET_COUNTER);
  495. hpet_writel(cnt, HPET_T1_CMP);
  496. hpet_t1_cmp = cnt;
  497. cfg = hpet_readl(HPET_T1_CFG);
  498. cfg &= ~HPET_TN_PERIODIC;
  499. cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
  500. hpet_writel(cfg, HPET_T1_CFG);
  501. local_irq_restore(flags);
  502. return 1;
  503. }
  504. EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
  505. /*
  506. * The functions below are called from rtc driver.
  507. * Return 0 if HPET is not being used.
  508. * Otherwise do the necessary changes and return 1.
  509. */
  510. int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
  511. {
  512. if (!is_hpet_enabled())
  513. return 0;
  514. hpet_rtc_flags &= ~bit_mask;
  515. return 1;
  516. }
  517. EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
  518. int hpet_set_rtc_irq_bit(unsigned long bit_mask)
  519. {
  520. unsigned long oldbits = hpet_rtc_flags;
  521. if (!is_hpet_enabled())
  522. return 0;
  523. hpet_rtc_flags |= bit_mask;
  524. if ((bit_mask & RTC_UIE) && !(oldbits & RTC_UIE))
  525. hpet_prev_update_sec = -1;
  526. if (!oldbits)
  527. hpet_rtc_timer_init();
  528. return 1;
  529. }
  530. EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
  531. int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
  532. unsigned char sec)
  533. {
  534. if (!is_hpet_enabled())
  535. return 0;
  536. hpet_alarm_time.tm_hour = hrs;
  537. hpet_alarm_time.tm_min = min;
  538. hpet_alarm_time.tm_sec = sec;
  539. return 1;
  540. }
  541. EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
  542. int hpet_set_periodic_freq(unsigned long freq)
  543. {
  544. uint64_t clc;
  545. if (!is_hpet_enabled())
  546. return 0;
  547. if (freq <= DEFAULT_RTC_INT_FREQ)
  548. hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
  549. else {
  550. clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
  551. do_div(clc, freq);
  552. clc >>= hpet_clockevent.shift;
  553. hpet_pie_delta = (unsigned long) clc;
  554. }
  555. return 1;
  556. }
  557. EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
  558. int hpet_rtc_dropped_irq(void)
  559. {
  560. return is_hpet_enabled();
  561. }
  562. EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
  563. static void hpet_rtc_timer_reinit(void)
  564. {
  565. unsigned long cfg, delta;
  566. int lost_ints = -1;
  567. if (unlikely(!hpet_rtc_flags)) {
  568. cfg = hpet_readl(HPET_T1_CFG);
  569. cfg &= ~HPET_TN_ENABLE;
  570. hpet_writel(cfg, HPET_T1_CFG);
  571. return;
  572. }
  573. if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
  574. delta = hpet_default_delta;
  575. else
  576. delta = hpet_pie_delta;
  577. /*
  578. * Increment the comparator value until we are ahead of the
  579. * current count.
  580. */
  581. do {
  582. hpet_t1_cmp += delta;
  583. hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
  584. lost_ints++;
  585. } while ((long)(hpet_readl(HPET_COUNTER) - hpet_t1_cmp) > 0);
  586. if (lost_ints) {
  587. if (hpet_rtc_flags & RTC_PIE)
  588. hpet_pie_count += lost_ints;
  589. if (printk_ratelimit())
  590. printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n",
  591. lost_ints);
  592. }
  593. }
  594. irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
  595. {
  596. struct rtc_time curr_time;
  597. unsigned long rtc_int_flag = 0;
  598. hpet_rtc_timer_reinit();
  599. memset(&curr_time, 0, sizeof(struct rtc_time));
  600. if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
  601. get_rtc_time(&curr_time);
  602. if (hpet_rtc_flags & RTC_UIE &&
  603. curr_time.tm_sec != hpet_prev_update_sec) {
  604. if (hpet_prev_update_sec >= 0)
  605. rtc_int_flag = RTC_UF;
  606. hpet_prev_update_sec = curr_time.tm_sec;
  607. }
  608. if (hpet_rtc_flags & RTC_PIE &&
  609. ++hpet_pie_count >= hpet_pie_limit) {
  610. rtc_int_flag |= RTC_PF;
  611. hpet_pie_count = 0;
  612. }
  613. if (hpet_rtc_flags & RTC_AIE &&
  614. (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
  615. (curr_time.tm_min == hpet_alarm_time.tm_min) &&
  616. (curr_time.tm_hour == hpet_alarm_time.tm_hour))
  617. rtc_int_flag |= RTC_AF;
  618. if (rtc_int_flag) {
  619. rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
  620. if (irq_handler)
  621. irq_handler(rtc_int_flag, dev_id);
  622. }
  623. return IRQ_HANDLED;
  624. }
  625. EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);
  626. #endif