hpet_64.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. #ifdef CONFIG_HPET_EMULATE_RTC
  154. /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
  155. * is enabled, we support RTC interrupt functionality in software.
  156. * RTC has 3 kinds of interrupts:
  157. * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
  158. * is updated
  159. * 2) Alarm Interrupt - generate an interrupt at a specific time of day
  160. * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
  161. * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
  162. * (1) and (2) above are implemented using polling at a frequency of
  163. * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
  164. * overhead. (DEFAULT_RTC_INT_FREQ)
  165. * For (3), we use interrupts at 64Hz or user specified periodic
  166. * frequency, whichever is higher.
  167. */
  168. #include <linux/rtc.h>
  169. #define DEFAULT_RTC_INT_FREQ 64
  170. #define RTC_NUM_INTS 1
  171. static unsigned long UIE_on;
  172. static unsigned long prev_update_sec;
  173. static unsigned long AIE_on;
  174. static struct rtc_time alarm_time;
  175. static unsigned long PIE_on;
  176. static unsigned long PIE_freq = DEFAULT_RTC_INT_FREQ;
  177. static unsigned long PIE_count;
  178. static unsigned long hpet_rtc_int_freq; /* RTC interrupt frequency */
  179. static unsigned int hpet_t1_cmp; /* cached comparator register */
  180. int is_hpet_enabled(void)
  181. {
  182. return hpet_address != 0;
  183. }
  184. /*
  185. * Timer 1 for RTC, we do not use periodic interrupt feature,
  186. * even if HPET supports periodic interrupts on Timer 1.
  187. * The reason being, to set up a periodic interrupt in HPET, we need to
  188. * stop the main counter. And if we do that everytime someone diables/enables
  189. * RTC, we will have adverse effect on main kernel timer running on Timer 0.
  190. * So, for the time being, simulate the periodic interrupt in software.
  191. *
  192. * hpet_rtc_timer_init() is called for the first time and during subsequent
  193. * interuppts reinit happens through hpet_rtc_timer_reinit().
  194. */
  195. int hpet_rtc_timer_init(void)
  196. {
  197. unsigned int cfg, cnt;
  198. unsigned long flags;
  199. if (!is_hpet_enabled())
  200. return 0;
  201. /*
  202. * Set the counter 1 and enable the interrupts.
  203. */
  204. if (PIE_on && (PIE_freq > DEFAULT_RTC_INT_FREQ))
  205. hpet_rtc_int_freq = PIE_freq;
  206. else
  207. hpet_rtc_int_freq = DEFAULT_RTC_INT_FREQ;
  208. local_irq_save(flags);
  209. cnt = hpet_readl(HPET_COUNTER);
  210. cnt += ((hpet_tick*HZ)/hpet_rtc_int_freq);
  211. hpet_writel(cnt, HPET_T1_CMP);
  212. hpet_t1_cmp = cnt;
  213. cfg = hpet_readl(HPET_T1_CFG);
  214. cfg &= ~HPET_TN_PERIODIC;
  215. cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
  216. hpet_writel(cfg, HPET_T1_CFG);
  217. local_irq_restore(flags);
  218. return 1;
  219. }
  220. static void hpet_rtc_timer_reinit(void)
  221. {
  222. unsigned int cfg, cnt, ticks_per_int, lost_ints;
  223. if (unlikely(!(PIE_on | AIE_on | UIE_on))) {
  224. cfg = hpet_readl(HPET_T1_CFG);
  225. cfg &= ~HPET_TN_ENABLE;
  226. hpet_writel(cfg, HPET_T1_CFG);
  227. return;
  228. }
  229. if (PIE_on && (PIE_freq > DEFAULT_RTC_INT_FREQ))
  230. hpet_rtc_int_freq = PIE_freq;
  231. else
  232. hpet_rtc_int_freq = DEFAULT_RTC_INT_FREQ;
  233. /* It is more accurate to use the comparator value than current count.*/
  234. ticks_per_int = hpet_tick * HZ / hpet_rtc_int_freq;
  235. hpet_t1_cmp += ticks_per_int;
  236. hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
  237. /*
  238. * If the interrupt handler was delayed too long, the write above tries
  239. * to schedule the next interrupt in the past and the hardware would
  240. * not interrupt until the counter had wrapped around.
  241. * So we have to check that the comparator wasn't set to a past time.
  242. */
  243. cnt = hpet_readl(HPET_COUNTER);
  244. if (unlikely((int)(cnt - hpet_t1_cmp) > 0)) {
  245. lost_ints = (cnt - hpet_t1_cmp) / ticks_per_int + 1;
  246. /* Make sure that, even with the time needed to execute
  247. * this code, the next scheduled interrupt has been moved
  248. * back to the future: */
  249. lost_ints++;
  250. hpet_t1_cmp += lost_ints * ticks_per_int;
  251. hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
  252. if (PIE_on)
  253. PIE_count += lost_ints;
  254. if (printk_ratelimit())
  255. printk(KERN_WARNING "rtc: lost some interrupts at %ldHz.\n",
  256. hpet_rtc_int_freq);
  257. }
  258. }
  259. /*
  260. * The functions below are called from rtc driver.
  261. * Return 0 if HPET is not being used.
  262. * Otherwise do the necessary changes and return 1.
  263. */
  264. int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
  265. {
  266. if (!is_hpet_enabled())
  267. return 0;
  268. if (bit_mask & RTC_UIE)
  269. UIE_on = 0;
  270. if (bit_mask & RTC_PIE)
  271. PIE_on = 0;
  272. if (bit_mask & RTC_AIE)
  273. AIE_on = 0;
  274. return 1;
  275. }
  276. int hpet_set_rtc_irq_bit(unsigned long bit_mask)
  277. {
  278. int timer_init_reqd = 0;
  279. if (!is_hpet_enabled())
  280. return 0;
  281. if (!(PIE_on | AIE_on | UIE_on))
  282. timer_init_reqd = 1;
  283. if (bit_mask & RTC_UIE) {
  284. UIE_on = 1;
  285. }
  286. if (bit_mask & RTC_PIE) {
  287. PIE_on = 1;
  288. PIE_count = 0;
  289. }
  290. if (bit_mask & RTC_AIE) {
  291. AIE_on = 1;
  292. }
  293. if (timer_init_reqd)
  294. hpet_rtc_timer_init();
  295. return 1;
  296. }
  297. int hpet_set_alarm_time(unsigned char hrs, unsigned char min, unsigned char sec)
  298. {
  299. if (!is_hpet_enabled())
  300. return 0;
  301. alarm_time.tm_hour = hrs;
  302. alarm_time.tm_min = min;
  303. alarm_time.tm_sec = sec;
  304. return 1;
  305. }
  306. int hpet_set_periodic_freq(unsigned long freq)
  307. {
  308. if (!is_hpet_enabled())
  309. return 0;
  310. PIE_freq = freq;
  311. PIE_count = 0;
  312. return 1;
  313. }
  314. int hpet_rtc_dropped_irq(void)
  315. {
  316. if (!is_hpet_enabled())
  317. return 0;
  318. return 1;
  319. }
  320. irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
  321. {
  322. struct rtc_time curr_time;
  323. unsigned long rtc_int_flag = 0;
  324. int call_rtc_interrupt = 0;
  325. hpet_rtc_timer_reinit();
  326. if (UIE_on | AIE_on) {
  327. rtc_get_rtc_time(&curr_time);
  328. }
  329. if (UIE_on) {
  330. if (curr_time.tm_sec != prev_update_sec) {
  331. /* Set update int info, call real rtc int routine */
  332. call_rtc_interrupt = 1;
  333. rtc_int_flag = RTC_UF;
  334. prev_update_sec = curr_time.tm_sec;
  335. }
  336. }
  337. if (PIE_on) {
  338. PIE_count++;
  339. if (PIE_count >= hpet_rtc_int_freq/PIE_freq) {
  340. /* Set periodic int info, call real rtc int routine */
  341. call_rtc_interrupt = 1;
  342. rtc_int_flag |= RTC_PF;
  343. PIE_count = 0;
  344. }
  345. }
  346. if (AIE_on) {
  347. if ((curr_time.tm_sec == alarm_time.tm_sec) &&
  348. (curr_time.tm_min == alarm_time.tm_min) &&
  349. (curr_time.tm_hour == alarm_time.tm_hour)) {
  350. /* Set alarm int info, call real rtc int routine */
  351. call_rtc_interrupt = 1;
  352. rtc_int_flag |= RTC_AF;
  353. }
  354. }
  355. if (call_rtc_interrupt) {
  356. rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
  357. rtc_interrupt(rtc_int_flag, dev_id);
  358. }
  359. return IRQ_HANDLED;
  360. }
  361. #endif
  362. static int __init nohpet_setup(char *s)
  363. {
  364. nohpet = 1;
  365. return 1;
  366. }
  367. __setup("nohpet", nohpet_setup);