time_hpet.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * linux/arch/i386/kernel/time_hpet.c
  3. * This code largely copied from arch/x86_64/kernel/time.c
  4. * See that file for credits.
  5. *
  6. * 2003-06-30 Venkatesh Pallipadi - Additional changes for HPET support
  7. */
  8. #include <linux/errno.h>
  9. #include <linux/kernel.h>
  10. #include <linux/param.h>
  11. #include <linux/string.h>
  12. #include <linux/init.h>
  13. #include <linux/smp.h>
  14. #include <asm/timer.h>
  15. #include <asm/fixmap.h>
  16. #include <asm/apic.h>
  17. #include <linux/timex.h>
  18. #include <asm/hpet.h>
  19. #include <linux/hpet.h>
  20. static unsigned long hpet_period; /* fsecs / HPET clock */
  21. unsigned long hpet_tick; /* hpet clks count per tick */
  22. unsigned long hpet_address; /* hpet memory map physical address */
  23. int hpet_use_timer;
  24. static int use_hpet; /* can be used for runtime check of hpet */
  25. static int boot_hpet_disable; /* boottime override for HPET timer */
  26. static void __iomem * hpet_virt_address; /* hpet kernel virtual address */
  27. #define FSEC_TO_USEC (1000000000UL)
  28. int hpet_readl(unsigned long a)
  29. {
  30. return readl(hpet_virt_address + a);
  31. }
  32. static void hpet_writel(unsigned long d, unsigned long a)
  33. {
  34. writel(d, hpet_virt_address + a);
  35. }
  36. #ifdef CONFIG_X86_LOCAL_APIC
  37. /*
  38. * HPET counters dont wrap around on every tick. They just change the
  39. * comparator value and continue. Next tick can be caught by checking
  40. * for a change in the comparator value. Used in apic.c.
  41. */
  42. static void __devinit wait_hpet_tick(void)
  43. {
  44. unsigned int start_cmp_val, end_cmp_val;
  45. start_cmp_val = hpet_readl(HPET_T0_CMP);
  46. do {
  47. end_cmp_val = hpet_readl(HPET_T0_CMP);
  48. } while (start_cmp_val == end_cmp_val);
  49. }
  50. #endif
  51. static int hpet_timer_stop_set_go(unsigned long tick)
  52. {
  53. unsigned int cfg;
  54. /*
  55. * Stop the timers and reset the main counter.
  56. */
  57. cfg = hpet_readl(HPET_CFG);
  58. cfg &= ~HPET_CFG_ENABLE;
  59. hpet_writel(cfg, HPET_CFG);
  60. hpet_writel(0, HPET_COUNTER);
  61. hpet_writel(0, HPET_COUNTER + 4);
  62. if (hpet_use_timer) {
  63. /*
  64. * Set up timer 0, as periodic with first interrupt to happen at
  65. * hpet_tick, and period also hpet_tick.
  66. */
  67. cfg = hpet_readl(HPET_T0_CFG);
  68. cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
  69. HPET_TN_SETVAL | HPET_TN_32BIT;
  70. hpet_writel(cfg, HPET_T0_CFG);
  71. /*
  72. * The first write after writing TN_SETVAL to the config register sets
  73. * the counter value, the second write sets the threshold.
  74. */
  75. hpet_writel(tick, HPET_T0_CMP);
  76. hpet_writel(tick, HPET_T0_CMP);
  77. }
  78. /*
  79. * Go!
  80. */
  81. cfg = hpet_readl(HPET_CFG);
  82. if (hpet_use_timer)
  83. cfg |= HPET_CFG_LEGACY;
  84. cfg |= HPET_CFG_ENABLE;
  85. hpet_writel(cfg, HPET_CFG);
  86. return 0;
  87. }
  88. /*
  89. * Check whether HPET was found by ACPI boot parse. If yes setup HPET
  90. * counter 0 for kernel base timer.
  91. */
  92. int __init hpet_enable(void)
  93. {
  94. unsigned int id;
  95. unsigned long tick_fsec_low, tick_fsec_high; /* tick in femto sec */
  96. unsigned long hpet_tick_rem;
  97. if (boot_hpet_disable)
  98. return -1;
  99. if (!hpet_address) {
  100. return -1;
  101. }
  102. hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
  103. /*
  104. * Read the period, compute tick and quotient.
  105. */
  106. id = hpet_readl(HPET_ID);
  107. /*
  108. * We are checking for value '1' or more in number field if
  109. * CONFIG_HPET_EMULATE_RTC is set because we will need an
  110. * additional timer for RTC emulation.
  111. * However, we can do with one timer otherwise using the
  112. * the single HPET timer for system time.
  113. */
  114. #ifdef CONFIG_HPET_EMULATE_RTC
  115. if (!(id & HPET_ID_NUMBER)) {
  116. iounmap(hpet_virt_address);
  117. hpet_virt_address = NULL;
  118. return -1;
  119. }
  120. #endif
  121. hpet_period = hpet_readl(HPET_PERIOD);
  122. if ((hpet_period < HPET_MIN_PERIOD) || (hpet_period > HPET_MAX_PERIOD)) {
  123. iounmap(hpet_virt_address);
  124. hpet_virt_address = NULL;
  125. return -1;
  126. }
  127. /*
  128. * 64 bit math
  129. * First changing tick into fsec
  130. * Then 64 bit div to find number of hpet clk per tick
  131. */
  132. ASM_MUL64_REG(tick_fsec_low, tick_fsec_high,
  133. KERNEL_TICK_USEC, FSEC_TO_USEC);
  134. ASM_DIV64_REG(hpet_tick, hpet_tick_rem,
  135. hpet_period, tick_fsec_low, tick_fsec_high);
  136. if (hpet_tick_rem > (hpet_period >> 1))
  137. hpet_tick++; /* rounding the result */
  138. hpet_use_timer = id & HPET_ID_LEGSUP;
  139. if (hpet_timer_stop_set_go(hpet_tick)) {
  140. iounmap(hpet_virt_address);
  141. hpet_virt_address = NULL;
  142. return -1;
  143. }
  144. use_hpet = 1;
  145. #ifdef CONFIG_HPET
  146. {
  147. struct hpet_data hd;
  148. unsigned int ntimer;
  149. memset(&hd, 0, sizeof (hd));
  150. ntimer = hpet_readl(HPET_ID);
  151. ntimer = (ntimer & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT;
  152. ntimer++;
  153. /*
  154. * Register with driver.
  155. * Timer0 and Timer1 is used by platform.
  156. */
  157. hd.hd_phys_address = hpet_address;
  158. hd.hd_address = hpet_virt_address;
  159. hd.hd_nirqs = ntimer;
  160. hd.hd_flags = HPET_DATA_PLATFORM;
  161. hpet_reserve_timer(&hd, 0);
  162. #ifdef CONFIG_HPET_EMULATE_RTC
  163. hpet_reserve_timer(&hd, 1);
  164. #endif
  165. hd.hd_irq[0] = HPET_LEGACY_8254;
  166. hd.hd_irq[1] = HPET_LEGACY_RTC;
  167. if (ntimer > 2) {
  168. struct hpet __iomem *hpet;
  169. struct hpet_timer __iomem *timer;
  170. int i;
  171. hpet = hpet_virt_address;
  172. for (i = 2, timer = &hpet->hpet_timers[2]; i < ntimer;
  173. timer++, i++)
  174. hd.hd_irq[i] = (timer->hpet_config &
  175. Tn_INT_ROUTE_CNF_MASK) >>
  176. Tn_INT_ROUTE_CNF_SHIFT;
  177. }
  178. hpet_alloc(&hd);
  179. }
  180. #endif
  181. #ifdef CONFIG_X86_LOCAL_APIC
  182. if (hpet_use_timer)
  183. wait_timer_tick = wait_hpet_tick;
  184. #endif
  185. return 0;
  186. }
  187. int hpet_reenable(void)
  188. {
  189. return hpet_timer_stop_set_go(hpet_tick);
  190. }
  191. int is_hpet_enabled(void)
  192. {
  193. return use_hpet;
  194. }
  195. int is_hpet_capable(void)
  196. {
  197. if (!boot_hpet_disable && hpet_address)
  198. return 1;
  199. return 0;
  200. }
  201. static int __init hpet_setup(char* str)
  202. {
  203. if (str) {
  204. if (!strncmp("disable", str, 7))
  205. boot_hpet_disable = 1;
  206. }
  207. return 1;
  208. }
  209. __setup("hpet=", hpet_setup);
  210. #ifdef CONFIG_HPET_EMULATE_RTC
  211. /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
  212. * is enabled, we support RTC interrupt functionality in software.
  213. * RTC has 3 kinds of interrupts:
  214. * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
  215. * is updated
  216. * 2) Alarm Interrupt - generate an interrupt at a specific time of day
  217. * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
  218. * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
  219. * (1) and (2) above are implemented using polling at a frequency of
  220. * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
  221. * overhead. (DEFAULT_RTC_INT_FREQ)
  222. * For (3), we use interrupts at 64Hz or user specified periodic
  223. * frequency, whichever is higher.
  224. */
  225. #include <linux/mc146818rtc.h>
  226. #include <linux/rtc.h>
  227. #define DEFAULT_RTC_INT_FREQ 64
  228. #define RTC_NUM_INTS 1
  229. static unsigned long UIE_on;
  230. static unsigned long prev_update_sec;
  231. static unsigned long AIE_on;
  232. static struct rtc_time alarm_time;
  233. static unsigned long PIE_on;
  234. static unsigned long PIE_freq = DEFAULT_RTC_INT_FREQ;
  235. static unsigned long PIE_count;
  236. static unsigned long hpet_rtc_int_freq; /* RTC interrupt frequency */
  237. static unsigned int hpet_t1_cmp; /* cached comparator register */
  238. /*
  239. * Timer 1 for RTC, we do not use periodic interrupt feature,
  240. * even if HPET supports periodic interrupts on Timer 1.
  241. * The reason being, to set up a periodic interrupt in HPET, we need to
  242. * stop the main counter. And if we do that everytime someone diables/enables
  243. * RTC, we will have adverse effect on main kernel timer running on Timer 0.
  244. * So, for the time being, simulate the periodic interrupt in software.
  245. *
  246. * hpet_rtc_timer_init() is called for the first time and during subsequent
  247. * interuppts reinit happens through hpet_rtc_timer_reinit().
  248. */
  249. int hpet_rtc_timer_init(void)
  250. {
  251. unsigned int cfg, cnt;
  252. unsigned long flags;
  253. if (!is_hpet_enabled())
  254. return 0;
  255. /*
  256. * Set the counter 1 and enable the interrupts.
  257. */
  258. if (PIE_on && (PIE_freq > DEFAULT_RTC_INT_FREQ))
  259. hpet_rtc_int_freq = PIE_freq;
  260. else
  261. hpet_rtc_int_freq = DEFAULT_RTC_INT_FREQ;
  262. local_irq_save(flags);
  263. cnt = hpet_readl(HPET_COUNTER);
  264. cnt += ((hpet_tick*HZ)/hpet_rtc_int_freq);
  265. hpet_writel(cnt, HPET_T1_CMP);
  266. hpet_t1_cmp = cnt;
  267. cfg = hpet_readl(HPET_T1_CFG);
  268. cfg &= ~HPET_TN_PERIODIC;
  269. cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
  270. hpet_writel(cfg, HPET_T1_CFG);
  271. local_irq_restore(flags);
  272. return 1;
  273. }
  274. static void hpet_rtc_timer_reinit(void)
  275. {
  276. unsigned int cfg, cnt, ticks_per_int, lost_ints;
  277. if (unlikely(!(PIE_on | AIE_on | UIE_on))) {
  278. cfg = hpet_readl(HPET_T1_CFG);
  279. cfg &= ~HPET_TN_ENABLE;
  280. hpet_writel(cfg, HPET_T1_CFG);
  281. return;
  282. }
  283. if (PIE_on && (PIE_freq > DEFAULT_RTC_INT_FREQ))
  284. hpet_rtc_int_freq = PIE_freq;
  285. else
  286. hpet_rtc_int_freq = DEFAULT_RTC_INT_FREQ;
  287. /* It is more accurate to use the comparator value than current count.*/
  288. ticks_per_int = hpet_tick * HZ / hpet_rtc_int_freq;
  289. hpet_t1_cmp += ticks_per_int;
  290. hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
  291. /*
  292. * If the interrupt handler was delayed too long, the write above tries
  293. * to schedule the next interrupt in the past and the hardware would
  294. * not interrupt until the counter had wrapped around.
  295. * So we have to check that the comparator wasn't set to a past time.
  296. */
  297. cnt = hpet_readl(HPET_COUNTER);
  298. if (unlikely((int)(cnt - hpet_t1_cmp) > 0)) {
  299. lost_ints = (cnt - hpet_t1_cmp) / ticks_per_int + 1;
  300. /* Make sure that, even with the time needed to execute
  301. * this code, the next scheduled interrupt has been moved
  302. * back to the future: */
  303. lost_ints++;
  304. hpet_t1_cmp += lost_ints * ticks_per_int;
  305. hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
  306. if (PIE_on)
  307. PIE_count += lost_ints;
  308. printk(KERN_WARNING "rtc: lost some interrupts at %ldHz.\n",
  309. hpet_rtc_int_freq);
  310. }
  311. }
  312. /*
  313. * The functions below are called from rtc driver.
  314. * Return 0 if HPET is not being used.
  315. * Otherwise do the necessary changes and return 1.
  316. */
  317. int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
  318. {
  319. if (!is_hpet_enabled())
  320. return 0;
  321. if (bit_mask & RTC_UIE)
  322. UIE_on = 0;
  323. if (bit_mask & RTC_PIE)
  324. PIE_on = 0;
  325. if (bit_mask & RTC_AIE)
  326. AIE_on = 0;
  327. return 1;
  328. }
  329. int hpet_set_rtc_irq_bit(unsigned long bit_mask)
  330. {
  331. int timer_init_reqd = 0;
  332. if (!is_hpet_enabled())
  333. return 0;
  334. if (!(PIE_on | AIE_on | UIE_on))
  335. timer_init_reqd = 1;
  336. if (bit_mask & RTC_UIE) {
  337. UIE_on = 1;
  338. }
  339. if (bit_mask & RTC_PIE) {
  340. PIE_on = 1;
  341. PIE_count = 0;
  342. }
  343. if (bit_mask & RTC_AIE) {
  344. AIE_on = 1;
  345. }
  346. if (timer_init_reqd)
  347. hpet_rtc_timer_init();
  348. return 1;
  349. }
  350. int hpet_set_alarm_time(unsigned char hrs, unsigned char min, unsigned char sec)
  351. {
  352. if (!is_hpet_enabled())
  353. return 0;
  354. alarm_time.tm_hour = hrs;
  355. alarm_time.tm_min = min;
  356. alarm_time.tm_sec = sec;
  357. return 1;
  358. }
  359. int hpet_set_periodic_freq(unsigned long freq)
  360. {
  361. if (!is_hpet_enabled())
  362. return 0;
  363. PIE_freq = freq;
  364. PIE_count = 0;
  365. return 1;
  366. }
  367. int hpet_rtc_dropped_irq(void)
  368. {
  369. if (!is_hpet_enabled())
  370. return 0;
  371. return 1;
  372. }
  373. irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
  374. {
  375. struct rtc_time curr_time;
  376. unsigned long rtc_int_flag = 0;
  377. int call_rtc_interrupt = 0;
  378. hpet_rtc_timer_reinit();
  379. if (UIE_on | AIE_on) {
  380. rtc_get_rtc_time(&curr_time);
  381. }
  382. if (UIE_on) {
  383. if (curr_time.tm_sec != prev_update_sec) {
  384. /* Set update int info, call real rtc int routine */
  385. call_rtc_interrupt = 1;
  386. rtc_int_flag = RTC_UF;
  387. prev_update_sec = curr_time.tm_sec;
  388. }
  389. }
  390. if (PIE_on) {
  391. PIE_count++;
  392. if (PIE_count >= hpet_rtc_int_freq/PIE_freq) {
  393. /* Set periodic int info, call real rtc int routine */
  394. call_rtc_interrupt = 1;
  395. rtc_int_flag |= RTC_PF;
  396. PIE_count = 0;
  397. }
  398. }
  399. if (AIE_on) {
  400. if ((curr_time.tm_sec == alarm_time.tm_sec) &&
  401. (curr_time.tm_min == alarm_time.tm_min) &&
  402. (curr_time.tm_hour == alarm_time.tm_hour)) {
  403. /* Set alarm int info, call real rtc int routine */
  404. call_rtc_interrupt = 1;
  405. rtc_int_flag |= RTC_AF;
  406. }
  407. }
  408. if (call_rtc_interrupt) {
  409. rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
  410. rtc_interrupt(rtc_int_flag, dev_id);
  411. }
  412. return IRQ_HANDLED;
  413. }
  414. #endif