hpet.c 18 KB

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