hpet.c 17 KB

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