hpet.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. #include <linux/kernel.h>
  2. #include <linux/sched.h>
  3. #include <linux/init.h>
  4. #include <linux/mc146818rtc.h>
  5. #include <linux/time.h>
  6. #include <linux/clocksource.h>
  7. #include <linux/ioport.h>
  8. #include <linux/acpi.h>
  9. #include <linux/hpet.h>
  10. #include <asm/pgtable.h>
  11. #include <asm/vsyscall.h>
  12. #include <asm/timex.h>
  13. #include <asm/hpet.h>
  14. #define HPET_MASK 0xFFFFFFFF
  15. #define HPET_SHIFT 22
  16. /* FSEC = 10^-15 NSEC = 10^-9 */
  17. #define FSEC_PER_NSEC 1000000
  18. int nohpet __initdata;
  19. unsigned long hpet_address;
  20. unsigned long hpet_period; /* fsecs / HPET clock */
  21. unsigned long hpet_tick; /* HPET clocks / interrupt */
  22. int hpet_use_timer; /* Use counter of hpet for time keeping,
  23. * otherwise PIT
  24. */
  25. #ifdef CONFIG_HPET
  26. static __init int late_hpet_init(void)
  27. {
  28. struct hpet_data hd;
  29. unsigned int ntimer;
  30. if (!hpet_address)
  31. return 0;
  32. memset(&hd, 0, sizeof(hd));
  33. ntimer = hpet_readl(HPET_ID);
  34. ntimer = (ntimer & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT;
  35. ntimer++;
  36. /*
  37. * Register with driver.
  38. * Timer0 and Timer1 is used by platform.
  39. */
  40. hd.hd_phys_address = hpet_address;
  41. hd.hd_address = (void __iomem *)fix_to_virt(FIX_HPET_BASE);
  42. hd.hd_nirqs = ntimer;
  43. hd.hd_flags = HPET_DATA_PLATFORM;
  44. hpet_reserve_timer(&hd, 0);
  45. #ifdef CONFIG_HPET_EMULATE_RTC
  46. hpet_reserve_timer(&hd, 1);
  47. #endif
  48. hd.hd_irq[0] = HPET_LEGACY_8254;
  49. hd.hd_irq[1] = HPET_LEGACY_RTC;
  50. if (ntimer > 2) {
  51. struct hpet *hpet;
  52. struct hpet_timer *timer;
  53. int i;
  54. hpet = (struct hpet *) fix_to_virt(FIX_HPET_BASE);
  55. timer = &hpet->hpet_timers[2];
  56. for (i = 2; i < ntimer; timer++, i++)
  57. hd.hd_irq[i] = (timer->hpet_config &
  58. Tn_INT_ROUTE_CNF_MASK) >>
  59. Tn_INT_ROUTE_CNF_SHIFT;
  60. }
  61. hpet_alloc(&hd);
  62. return 0;
  63. }
  64. fs_initcall(late_hpet_init);
  65. #endif
  66. int hpet_timer_stop_set_go(unsigned long tick)
  67. {
  68. unsigned int cfg;
  69. /*
  70. * Stop the timers and reset the main counter.
  71. */
  72. cfg = hpet_readl(HPET_CFG);
  73. cfg &= ~(HPET_CFG_ENABLE | HPET_CFG_LEGACY);
  74. hpet_writel(cfg, HPET_CFG);
  75. hpet_writel(0, HPET_COUNTER);
  76. hpet_writel(0, HPET_COUNTER + 4);
  77. /*
  78. * Set up timer 0, as periodic with first interrupt to happen at hpet_tick,
  79. * and period also hpet_tick.
  80. */
  81. if (hpet_use_timer) {
  82. hpet_writel(HPET_TN_ENABLE | HPET_TN_PERIODIC | HPET_TN_SETVAL |
  83. HPET_TN_32BIT, HPET_T0_CFG);
  84. hpet_writel(hpet_tick, HPET_T0_CMP); /* next interrupt */
  85. hpet_writel(hpet_tick, HPET_T0_CMP); /* period */
  86. cfg |= HPET_CFG_LEGACY;
  87. }
  88. /*
  89. * Go!
  90. */
  91. cfg |= HPET_CFG_ENABLE;
  92. hpet_writel(cfg, HPET_CFG);
  93. return 0;
  94. }
  95. static cycle_t read_hpet(void)
  96. {
  97. return (cycle_t)hpet_readl(HPET_COUNTER);
  98. }
  99. static cycle_t __vsyscall_fn vread_hpet(void)
  100. {
  101. return readl((void __iomem *)fix_to_virt(VSYSCALL_HPET) + 0xf0);
  102. }
  103. struct clocksource clocksource_hpet = {
  104. .name = "hpet",
  105. .rating = 250,
  106. .read = read_hpet,
  107. .mask = (cycle_t)HPET_MASK,
  108. .mult = 0, /* set below */
  109. .shift = HPET_SHIFT,
  110. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  111. .vread = vread_hpet,
  112. };
  113. int __init hpet_arch_init(void)
  114. {
  115. unsigned int id;
  116. u64 tmp;
  117. if (!hpet_address)
  118. return -1;
  119. set_fixmap_nocache(FIX_HPET_BASE, hpet_address);
  120. __set_fixmap(VSYSCALL_HPET, hpet_address, PAGE_KERNEL_VSYSCALL_NOCACHE);
  121. /*
  122. * Read the period, compute tick and quotient.
  123. */
  124. id = hpet_readl(HPET_ID);
  125. if (!(id & HPET_ID_VENDOR) || !(id & HPET_ID_NUMBER))
  126. return -1;
  127. hpet_period = hpet_readl(HPET_PERIOD);
  128. if (hpet_period < 100000 || hpet_period > 100000000)
  129. return -1;
  130. hpet_tick = (FSEC_PER_TICK + hpet_period / 2) / hpet_period;
  131. hpet_use_timer = (id & HPET_ID_LEGSUP);
  132. /*
  133. * hpet period is in femto seconds per cycle
  134. * so we need to convert this to ns/cyc units
  135. * aproximated by mult/2^shift
  136. *
  137. * fsec/cyc * 1nsec/1000000fsec = nsec/cyc = mult/2^shift
  138. * fsec/cyc * 1ns/1000000fsec * 2^shift = mult
  139. * fsec/cyc * 2^shift * 1nsec/1000000fsec = mult
  140. * (fsec/cyc << shift)/1000000 = mult
  141. * (hpet_period << shift)/FSEC_PER_NSEC = mult
  142. */
  143. tmp = (u64)hpet_period << HPET_SHIFT;
  144. do_div(tmp, FSEC_PER_NSEC);
  145. clocksource_hpet.mult = (u32)tmp;
  146. clocksource_register(&clocksource_hpet);
  147. return hpet_timer_stop_set_go(hpet_tick);
  148. }
  149. int hpet_reenable(void)
  150. {
  151. return hpet_timer_stop_set_go(hpet_tick);
  152. }
  153. /*
  154. * calibrate_tsc() calibrates the processor TSC in a very simple way, comparing
  155. * it to the HPET timer of known frequency.
  156. */
  157. #define TICK_COUNT 100000000
  158. #define SMI_THRESHOLD 50000
  159. #define MAX_TRIES 5
  160. /*
  161. * Some platforms take periodic SMI interrupts with 5ms duration. Make sure none
  162. * occurs between the reads of the hpet & TSC.
  163. */
  164. static void __init read_hpet_tsc(int *hpet, int *tsc)
  165. {
  166. int tsc1, tsc2, hpet1, i;
  167. for (i = 0; i < MAX_TRIES; i++) {
  168. tsc1 = get_cycles_sync();
  169. hpet1 = hpet_readl(HPET_COUNTER);
  170. tsc2 = get_cycles_sync();
  171. if ((tsc2 - tsc1) < SMI_THRESHOLD)
  172. break;
  173. }
  174. *hpet = hpet1;
  175. *tsc = tsc2;
  176. }
  177. unsigned int __init hpet_calibrate_tsc(void)
  178. {
  179. int tsc_start, hpet_start;
  180. int tsc_now, hpet_now;
  181. unsigned long flags;
  182. local_irq_save(flags);
  183. read_hpet_tsc(&hpet_start, &tsc_start);
  184. do {
  185. local_irq_disable();
  186. read_hpet_tsc(&hpet_now, &tsc_now);
  187. local_irq_restore(flags);
  188. } while ((tsc_now - tsc_start) < TICK_COUNT &&
  189. (hpet_now - hpet_start) < TICK_COUNT);
  190. return (tsc_now - tsc_start) * 1000000000L
  191. / ((hpet_now - hpet_start) * hpet_period / 1000);
  192. }
  193. #ifdef CONFIG_HPET_EMULATE_RTC
  194. /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
  195. * is enabled, we support RTC interrupt functionality in software.
  196. * RTC has 3 kinds of interrupts:
  197. * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
  198. * is updated
  199. * 2) Alarm Interrupt - generate an interrupt at a specific time of day
  200. * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
  201. * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
  202. * (1) and (2) above are implemented using polling at a frequency of
  203. * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
  204. * overhead. (DEFAULT_RTC_INT_FREQ)
  205. * For (3), we use interrupts at 64Hz or user specified periodic
  206. * frequency, whichever is higher.
  207. */
  208. #include <linux/rtc.h>
  209. #define DEFAULT_RTC_INT_FREQ 64
  210. #define RTC_NUM_INTS 1
  211. static unsigned long UIE_on;
  212. static unsigned long prev_update_sec;
  213. static unsigned long AIE_on;
  214. static struct rtc_time alarm_time;
  215. static unsigned long PIE_on;
  216. static unsigned long PIE_freq = DEFAULT_RTC_INT_FREQ;
  217. static unsigned long PIE_count;
  218. static unsigned long hpet_rtc_int_freq; /* RTC interrupt frequency */
  219. static unsigned int hpet_t1_cmp; /* cached comparator register */
  220. int is_hpet_enabled(void)
  221. {
  222. return hpet_address != 0;
  223. }
  224. /*
  225. * Timer 1 for RTC, we do not use periodic interrupt feature,
  226. * even if HPET supports periodic interrupts on Timer 1.
  227. * The reason being, to set up a periodic interrupt in HPET, we need to
  228. * stop the main counter. And if we do that everytime someone diables/enables
  229. * RTC, we will have adverse effect on main kernel timer running on Timer 0.
  230. * So, for the time being, simulate the periodic interrupt in software.
  231. *
  232. * hpet_rtc_timer_init() is called for the first time and during subsequent
  233. * interuppts reinit happens through hpet_rtc_timer_reinit().
  234. */
  235. int hpet_rtc_timer_init(void)
  236. {
  237. unsigned int cfg, cnt;
  238. unsigned long flags;
  239. if (!is_hpet_enabled())
  240. return 0;
  241. /*
  242. * Set the counter 1 and enable the interrupts.
  243. */
  244. if (PIE_on && (PIE_freq > DEFAULT_RTC_INT_FREQ))
  245. hpet_rtc_int_freq = PIE_freq;
  246. else
  247. hpet_rtc_int_freq = DEFAULT_RTC_INT_FREQ;
  248. local_irq_save(flags);
  249. cnt = hpet_readl(HPET_COUNTER);
  250. cnt += ((hpet_tick*HZ)/hpet_rtc_int_freq);
  251. hpet_writel(cnt, HPET_T1_CMP);
  252. hpet_t1_cmp = cnt;
  253. cfg = hpet_readl(HPET_T1_CFG);
  254. cfg &= ~HPET_TN_PERIODIC;
  255. cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
  256. hpet_writel(cfg, HPET_T1_CFG);
  257. local_irq_restore(flags);
  258. return 1;
  259. }
  260. static void hpet_rtc_timer_reinit(void)
  261. {
  262. unsigned int cfg, cnt, ticks_per_int, lost_ints;
  263. if (unlikely(!(PIE_on | AIE_on | UIE_on))) {
  264. cfg = hpet_readl(HPET_T1_CFG);
  265. cfg &= ~HPET_TN_ENABLE;
  266. hpet_writel(cfg, HPET_T1_CFG);
  267. return;
  268. }
  269. if (PIE_on && (PIE_freq > DEFAULT_RTC_INT_FREQ))
  270. hpet_rtc_int_freq = PIE_freq;
  271. else
  272. hpet_rtc_int_freq = DEFAULT_RTC_INT_FREQ;
  273. /* It is more accurate to use the comparator value than current count.*/
  274. ticks_per_int = hpet_tick * HZ / hpet_rtc_int_freq;
  275. hpet_t1_cmp += ticks_per_int;
  276. hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
  277. /*
  278. * If the interrupt handler was delayed too long, the write above tries
  279. * to schedule the next interrupt in the past and the hardware would
  280. * not interrupt until the counter had wrapped around.
  281. * So we have to check that the comparator wasn't set to a past time.
  282. */
  283. cnt = hpet_readl(HPET_COUNTER);
  284. if (unlikely((int)(cnt - hpet_t1_cmp) > 0)) {
  285. lost_ints = (cnt - hpet_t1_cmp) / ticks_per_int + 1;
  286. /* Make sure that, even with the time needed to execute
  287. * this code, the next scheduled interrupt has been moved
  288. * back to the future: */
  289. lost_ints++;
  290. hpet_t1_cmp += lost_ints * ticks_per_int;
  291. hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
  292. if (PIE_on)
  293. PIE_count += lost_ints;
  294. if (printk_ratelimit())
  295. printk(KERN_WARNING "rtc: lost some interrupts at %ldHz.\n",
  296. hpet_rtc_int_freq);
  297. }
  298. }
  299. /*
  300. * The functions below are called from rtc driver.
  301. * Return 0 if HPET is not being used.
  302. * Otherwise do the necessary changes and return 1.
  303. */
  304. int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
  305. {
  306. if (!is_hpet_enabled())
  307. return 0;
  308. if (bit_mask & RTC_UIE)
  309. UIE_on = 0;
  310. if (bit_mask & RTC_PIE)
  311. PIE_on = 0;
  312. if (bit_mask & RTC_AIE)
  313. AIE_on = 0;
  314. return 1;
  315. }
  316. int hpet_set_rtc_irq_bit(unsigned long bit_mask)
  317. {
  318. int timer_init_reqd = 0;
  319. if (!is_hpet_enabled())
  320. return 0;
  321. if (!(PIE_on | AIE_on | UIE_on))
  322. timer_init_reqd = 1;
  323. if (bit_mask & RTC_UIE) {
  324. UIE_on = 1;
  325. }
  326. if (bit_mask & RTC_PIE) {
  327. PIE_on = 1;
  328. PIE_count = 0;
  329. }
  330. if (bit_mask & RTC_AIE) {
  331. AIE_on = 1;
  332. }
  333. if (timer_init_reqd)
  334. hpet_rtc_timer_init();
  335. return 1;
  336. }
  337. int hpet_set_alarm_time(unsigned char hrs, unsigned char min, unsigned char sec)
  338. {
  339. if (!is_hpet_enabled())
  340. return 0;
  341. alarm_time.tm_hour = hrs;
  342. alarm_time.tm_min = min;
  343. alarm_time.tm_sec = sec;
  344. return 1;
  345. }
  346. int hpet_set_periodic_freq(unsigned long freq)
  347. {
  348. if (!is_hpet_enabled())
  349. return 0;
  350. PIE_freq = freq;
  351. PIE_count = 0;
  352. return 1;
  353. }
  354. int hpet_rtc_dropped_irq(void)
  355. {
  356. if (!is_hpet_enabled())
  357. return 0;
  358. return 1;
  359. }
  360. irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
  361. {
  362. struct rtc_time curr_time;
  363. unsigned long rtc_int_flag = 0;
  364. int call_rtc_interrupt = 0;
  365. hpet_rtc_timer_reinit();
  366. if (UIE_on | AIE_on) {
  367. rtc_get_rtc_time(&curr_time);
  368. }
  369. if (UIE_on) {
  370. if (curr_time.tm_sec != prev_update_sec) {
  371. /* Set update int info, call real rtc int routine */
  372. call_rtc_interrupt = 1;
  373. rtc_int_flag = RTC_UF;
  374. prev_update_sec = curr_time.tm_sec;
  375. }
  376. }
  377. if (PIE_on) {
  378. PIE_count++;
  379. if (PIE_count >= hpet_rtc_int_freq/PIE_freq) {
  380. /* Set periodic int info, call real rtc int routine */
  381. call_rtc_interrupt = 1;
  382. rtc_int_flag |= RTC_PF;
  383. PIE_count = 0;
  384. }
  385. }
  386. if (AIE_on) {
  387. if ((curr_time.tm_sec == alarm_time.tm_sec) &&
  388. (curr_time.tm_min == alarm_time.tm_min) &&
  389. (curr_time.tm_hour == alarm_time.tm_hour)) {
  390. /* Set alarm int info, call real rtc int routine */
  391. call_rtc_interrupt = 1;
  392. rtc_int_flag |= RTC_AF;
  393. }
  394. }
  395. if (call_rtc_interrupt) {
  396. rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
  397. rtc_interrupt(rtc_int_flag, dev_id);
  398. }
  399. return IRQ_HANDLED;
  400. }
  401. #endif
  402. static int __init nohpet_setup(char *s)
  403. {
  404. nohpet = 1;
  405. return 1;
  406. }
  407. __setup("nohpet", nohpet_setup);