hpet.c 12 KB

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