hpet.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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_legacy_set_mode(enum clock_event_mode mode,
  195. struct clock_event_device *evt)
  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)) * hpet_clockevent.mult;
  202. delta >>= hpet_clockevent.shift;
  203. now = hpet_readl(HPET_COUNTER);
  204. cmp = now + (unsigned long) delta;
  205. cfg = hpet_readl(HPET_T0_CFG);
  206. cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
  207. HPET_TN_SETVAL | HPET_TN_32BIT;
  208. hpet_writel(cfg, HPET_T0_CFG);
  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_T0_CMP);
  215. udelay(1);
  216. hpet_writel((unsigned long) delta, HPET_T0_CMP);
  217. break;
  218. case CLOCK_EVT_MODE_ONESHOT:
  219. cfg = hpet_readl(HPET_T0_CFG);
  220. cfg &= ~HPET_TN_PERIODIC;
  221. cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
  222. hpet_writel(cfg, HPET_T0_CFG);
  223. break;
  224. case CLOCK_EVT_MODE_UNUSED:
  225. case CLOCK_EVT_MODE_SHUTDOWN:
  226. cfg = hpet_readl(HPET_T0_CFG);
  227. cfg &= ~HPET_TN_ENABLE;
  228. hpet_writel(cfg, HPET_T0_CFG);
  229. break;
  230. case CLOCK_EVT_MODE_RESUME:
  231. hpet_enable_legacy_int();
  232. break;
  233. }
  234. }
  235. static int hpet_legacy_next_event(unsigned long delta,
  236. struct clock_event_device *evt)
  237. {
  238. u32 cnt;
  239. cnt = hpet_readl(HPET_COUNTER);
  240. cnt += (u32) delta;
  241. hpet_writel(cnt, HPET_T0_CMP);
  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. /*
  251. * Clock source related code
  252. */
  253. static cycle_t read_hpet(void)
  254. {
  255. return (cycle_t)hpet_readl(HPET_COUNTER);
  256. }
  257. #ifdef CONFIG_X86_64
  258. static cycle_t __vsyscall_fn vread_hpet(void)
  259. {
  260. return readl((const void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
  261. }
  262. #endif
  263. static struct clocksource clocksource_hpet = {
  264. .name = "hpet",
  265. .rating = 250,
  266. .read = read_hpet,
  267. .mask = HPET_MASK,
  268. .shift = HPET_SHIFT,
  269. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  270. .resume = hpet_restart_counter,
  271. #ifdef CONFIG_X86_64
  272. .vread = vread_hpet,
  273. #endif
  274. };
  275. static int hpet_clocksource_register(void)
  276. {
  277. u64 start, now;
  278. cycle_t t1;
  279. /* Start the counter */
  280. hpet_start_counter();
  281. /* Verify whether hpet counter works */
  282. t1 = read_hpet();
  283. rdtscll(start);
  284. /*
  285. * We don't know the TSC frequency yet, but waiting for
  286. * 200000 TSC cycles is safe:
  287. * 4 GHz == 50us
  288. * 1 GHz == 200us
  289. */
  290. do {
  291. rep_nop();
  292. rdtscll(now);
  293. } while ((now - start) < 200000UL);
  294. if (t1 == read_hpet()) {
  295. printk(KERN_WARNING
  296. "HPET counter not counting. HPET disabled\n");
  297. return -ENODEV;
  298. }
  299. /*
  300. * The definition of mult is (include/linux/clocksource.h)
  301. * mult/2^shift = ns/cyc and hpet_period is in units of fsec/cyc
  302. * so we first need to convert hpet_period to ns/cyc units:
  303. * mult/2^shift = ns/cyc = hpet_period/10^6
  304. * mult = (hpet_period * 2^shift)/10^6
  305. * mult = (hpet_period << shift)/FSEC_PER_NSEC
  306. */
  307. clocksource_hpet.mult = div_sc(hpet_period, FSEC_PER_NSEC, HPET_SHIFT);
  308. clocksource_register(&clocksource_hpet);
  309. return 0;
  310. }
  311. /**
  312. * hpet_enable - Try to setup the HPET timer. Returns 1 on success.
  313. */
  314. int __init hpet_enable(void)
  315. {
  316. unsigned long id;
  317. int i;
  318. if (!is_hpet_capable())
  319. return 0;
  320. hpet_set_mapping();
  321. /*
  322. * Read the period and check for a sane value:
  323. */
  324. hpet_period = hpet_readl(HPET_PERIOD);
  325. /*
  326. * AMD SB700 based systems with spread spectrum enabled use a
  327. * SMM based HPET emulation to provide proper frequency
  328. * setting. The SMM code is initialized with the first HPET
  329. * register access and takes some time to complete. During
  330. * this time the config register reads 0xffffffff. We check
  331. * for max. 1000 loops whether the config register reads a non
  332. * 0xffffffff value to make sure that HPET is up and running
  333. * before we go further. A counting loop is safe, as the HPET
  334. * access takes thousands of CPU cycles. On non SB700 based
  335. * machines this check is only done once and has no side
  336. * effects.
  337. */
  338. for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) {
  339. if (i == 1000) {
  340. printk(KERN_WARNING
  341. "HPET config register value = 0xFFFFFFFF. "
  342. "Disabling HPET\n");
  343. goto out_nohpet;
  344. }
  345. }
  346. if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
  347. goto out_nohpet;
  348. /*
  349. * Read the HPET ID register to retrieve the IRQ routing
  350. * information and the number of channels
  351. */
  352. id = hpet_readl(HPET_ID);
  353. #ifdef CONFIG_HPET_EMULATE_RTC
  354. /*
  355. * The legacy routing mode needs at least two channels, tick timer
  356. * and the rtc emulation channel.
  357. */
  358. if (!(id & HPET_ID_NUMBER))
  359. goto out_nohpet;
  360. #endif
  361. if (hpet_clocksource_register())
  362. goto out_nohpet;
  363. if (id & HPET_ID_LEGSUP) {
  364. hpet_legacy_clockevent_register();
  365. return 1;
  366. }
  367. return 0;
  368. out_nohpet:
  369. hpet_clear_mapping();
  370. boot_hpet_disable = 1;
  371. return 0;
  372. }
  373. /*
  374. * Needs to be late, as the reserve_timer code calls kalloc !
  375. *
  376. * Not a problem on i386 as hpet_enable is called from late_time_init,
  377. * but on x86_64 it is necessary !
  378. */
  379. static __init int hpet_late_init(void)
  380. {
  381. if (boot_hpet_disable)
  382. return -ENODEV;
  383. if (!hpet_address) {
  384. if (!force_hpet_address)
  385. return -ENODEV;
  386. hpet_address = force_hpet_address;
  387. hpet_enable();
  388. if (!hpet_virt_address)
  389. return -ENODEV;
  390. }
  391. hpet_reserve_platform_timers(hpet_readl(HPET_ID));
  392. return 0;
  393. }
  394. fs_initcall(hpet_late_init);
  395. void hpet_disable(void)
  396. {
  397. if (is_hpet_capable()) {
  398. unsigned long cfg = hpet_readl(HPET_CFG);
  399. if (hpet_legacy_int_enabled) {
  400. cfg &= ~HPET_CFG_LEGACY;
  401. hpet_legacy_int_enabled = 0;
  402. }
  403. cfg &= ~HPET_CFG_ENABLE;
  404. hpet_writel(cfg, HPET_CFG);
  405. }
  406. }
  407. #ifdef CONFIG_HPET_EMULATE_RTC
  408. /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
  409. * is enabled, we support RTC interrupt functionality in software.
  410. * RTC has 3 kinds of interrupts:
  411. * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
  412. * is updated
  413. * 2) Alarm Interrupt - generate an interrupt at a specific time of day
  414. * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
  415. * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
  416. * (1) and (2) above are implemented using polling at a frequency of
  417. * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
  418. * overhead. (DEFAULT_RTC_INT_FREQ)
  419. * For (3), we use interrupts at 64Hz or user specified periodic
  420. * frequency, whichever is higher.
  421. */
  422. #include <linux/mc146818rtc.h>
  423. #include <linux/rtc.h>
  424. #include <asm/rtc.h>
  425. #define DEFAULT_RTC_INT_FREQ 64
  426. #define DEFAULT_RTC_SHIFT 6
  427. #define RTC_NUM_INTS 1
  428. static unsigned long hpet_rtc_flags;
  429. static int hpet_prev_update_sec;
  430. static struct rtc_time hpet_alarm_time;
  431. static unsigned long hpet_pie_count;
  432. static unsigned long hpet_t1_cmp;
  433. static unsigned long hpet_default_delta;
  434. static unsigned long hpet_pie_delta;
  435. static unsigned long hpet_pie_limit;
  436. static rtc_irq_handler irq_handler;
  437. /*
  438. * Registers a IRQ handler.
  439. */
  440. int hpet_register_irq_handler(rtc_irq_handler handler)
  441. {
  442. if (!is_hpet_enabled())
  443. return -ENODEV;
  444. if (irq_handler)
  445. return -EBUSY;
  446. irq_handler = handler;
  447. return 0;
  448. }
  449. EXPORT_SYMBOL_GPL(hpet_register_irq_handler);
  450. /*
  451. * Deregisters the IRQ handler registered with hpet_register_irq_handler()
  452. * and does cleanup.
  453. */
  454. void hpet_unregister_irq_handler(rtc_irq_handler handler)
  455. {
  456. if (!is_hpet_enabled())
  457. return;
  458. irq_handler = NULL;
  459. hpet_rtc_flags = 0;
  460. }
  461. EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler);
  462. /*
  463. * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
  464. * is not supported by all HPET implementations for timer 1.
  465. *
  466. * hpet_rtc_timer_init() is called when the rtc is initialized.
  467. */
  468. int hpet_rtc_timer_init(void)
  469. {
  470. unsigned long cfg, cnt, delta, flags;
  471. if (!is_hpet_enabled())
  472. return 0;
  473. if (!hpet_default_delta) {
  474. uint64_t clc;
  475. clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
  476. clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
  477. hpet_default_delta = (unsigned long) clc;
  478. }
  479. if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
  480. delta = hpet_default_delta;
  481. else
  482. delta = hpet_pie_delta;
  483. local_irq_save(flags);
  484. cnt = delta + hpet_readl(HPET_COUNTER);
  485. hpet_writel(cnt, HPET_T1_CMP);
  486. hpet_t1_cmp = cnt;
  487. cfg = hpet_readl(HPET_T1_CFG);
  488. cfg &= ~HPET_TN_PERIODIC;
  489. cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
  490. hpet_writel(cfg, HPET_T1_CFG);
  491. local_irq_restore(flags);
  492. return 1;
  493. }
  494. EXPORT_SYMBOL_GPL(hpet_rtc_timer_init);
  495. /*
  496. * The functions below are called from rtc driver.
  497. * Return 0 if HPET is not being used.
  498. * Otherwise do the necessary changes and return 1.
  499. */
  500. int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
  501. {
  502. if (!is_hpet_enabled())
  503. return 0;
  504. hpet_rtc_flags &= ~bit_mask;
  505. return 1;
  506. }
  507. EXPORT_SYMBOL_GPL(hpet_mask_rtc_irq_bit);
  508. int hpet_set_rtc_irq_bit(unsigned long bit_mask)
  509. {
  510. unsigned long oldbits = hpet_rtc_flags;
  511. if (!is_hpet_enabled())
  512. return 0;
  513. hpet_rtc_flags |= bit_mask;
  514. if ((bit_mask & RTC_UIE) && !(oldbits & RTC_UIE))
  515. hpet_prev_update_sec = -1;
  516. if (!oldbits)
  517. hpet_rtc_timer_init();
  518. return 1;
  519. }
  520. EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit);
  521. int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
  522. unsigned char sec)
  523. {
  524. if (!is_hpet_enabled())
  525. return 0;
  526. hpet_alarm_time.tm_hour = hrs;
  527. hpet_alarm_time.tm_min = min;
  528. hpet_alarm_time.tm_sec = sec;
  529. return 1;
  530. }
  531. EXPORT_SYMBOL_GPL(hpet_set_alarm_time);
  532. int hpet_set_periodic_freq(unsigned long freq)
  533. {
  534. uint64_t clc;
  535. if (!is_hpet_enabled())
  536. return 0;
  537. if (freq <= DEFAULT_RTC_INT_FREQ)
  538. hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
  539. else {
  540. clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
  541. do_div(clc, freq);
  542. clc >>= hpet_clockevent.shift;
  543. hpet_pie_delta = (unsigned long) clc;
  544. }
  545. return 1;
  546. }
  547. EXPORT_SYMBOL_GPL(hpet_set_periodic_freq);
  548. int hpet_rtc_dropped_irq(void)
  549. {
  550. return is_hpet_enabled();
  551. }
  552. EXPORT_SYMBOL_GPL(hpet_rtc_dropped_irq);
  553. static void hpet_rtc_timer_reinit(void)
  554. {
  555. unsigned long cfg, delta;
  556. int lost_ints = -1;
  557. if (unlikely(!hpet_rtc_flags)) {
  558. cfg = hpet_readl(HPET_T1_CFG);
  559. cfg &= ~HPET_TN_ENABLE;
  560. hpet_writel(cfg, HPET_T1_CFG);
  561. return;
  562. }
  563. if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
  564. delta = hpet_default_delta;
  565. else
  566. delta = hpet_pie_delta;
  567. /*
  568. * Increment the comparator value until we are ahead of the
  569. * current count.
  570. */
  571. do {
  572. hpet_t1_cmp += delta;
  573. hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
  574. lost_ints++;
  575. } while ((long)(hpet_readl(HPET_COUNTER) - hpet_t1_cmp) > 0);
  576. if (lost_ints) {
  577. if (hpet_rtc_flags & RTC_PIE)
  578. hpet_pie_count += lost_ints;
  579. if (printk_ratelimit())
  580. printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n",
  581. lost_ints);
  582. }
  583. }
  584. irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
  585. {
  586. struct rtc_time curr_time;
  587. unsigned long rtc_int_flag = 0;
  588. hpet_rtc_timer_reinit();
  589. memset(&curr_time, 0, sizeof(struct rtc_time));
  590. if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
  591. get_rtc_time(&curr_time);
  592. if (hpet_rtc_flags & RTC_UIE &&
  593. curr_time.tm_sec != hpet_prev_update_sec) {
  594. if (hpet_prev_update_sec >= 0)
  595. rtc_int_flag = RTC_UF;
  596. hpet_prev_update_sec = curr_time.tm_sec;
  597. }
  598. if (hpet_rtc_flags & RTC_PIE &&
  599. ++hpet_pie_count >= hpet_pie_limit) {
  600. rtc_int_flag |= RTC_PF;
  601. hpet_pie_count = 0;
  602. }
  603. if (hpet_rtc_flags & RTC_AIE &&
  604. (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
  605. (curr_time.tm_min == hpet_alarm_time.tm_min) &&
  606. (curr_time.tm_hour == hpet_alarm_time.tm_hour))
  607. rtc_int_flag |= RTC_AF;
  608. if (rtc_int_flag) {
  609. rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
  610. if (irq_handler)
  611. irq_handler(rtc_int_flag, dev_id);
  612. }
  613. return IRQ_HANDLED;
  614. }
  615. EXPORT_SYMBOL_GPL(hpet_rtc_interrupt);
  616. #endif