hpet.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. #include <linux/clocksource.h>
  2. #include <linux/clockchips.h>
  3. #include <linux/errno.h>
  4. #include <linux/hpet.h>
  5. #include <linux/init.h>
  6. #include <asm/hpet.h>
  7. #include <asm/io.h>
  8. extern struct clock_event_device *global_clock_event;
  9. #define HPET_MASK CLOCKSOURCE_MASK(32)
  10. #define HPET_SHIFT 22
  11. /* FSEC = 10^-15 NSEC = 10^-9 */
  12. #define FSEC_PER_NSEC 1000000
  13. /*
  14. * HPET address is set in acpi/boot.c, when an ACPI entry exists
  15. */
  16. unsigned long hpet_address;
  17. static void __iomem * hpet_virt_address;
  18. static inline unsigned long hpet_readl(unsigned long a)
  19. {
  20. return readl(hpet_virt_address + a);
  21. }
  22. static inline void hpet_writel(unsigned long d, unsigned long a)
  23. {
  24. writel(d, hpet_virt_address + a);
  25. }
  26. /*
  27. * HPET command line enable / disable
  28. */
  29. static int boot_hpet_disable;
  30. static int __init hpet_setup(char* str)
  31. {
  32. if (str) {
  33. if (!strncmp("disable", str, 7))
  34. boot_hpet_disable = 1;
  35. }
  36. return 1;
  37. }
  38. __setup("hpet=", hpet_setup);
  39. static inline int is_hpet_capable(void)
  40. {
  41. return (!boot_hpet_disable && hpet_address);
  42. }
  43. /*
  44. * HPET timer interrupt enable / disable
  45. */
  46. static int hpet_legacy_int_enabled;
  47. /**
  48. * is_hpet_enabled - check whether the hpet timer interrupt is enabled
  49. */
  50. int is_hpet_enabled(void)
  51. {
  52. return is_hpet_capable() && hpet_legacy_int_enabled;
  53. }
  54. /*
  55. * When the hpet driver (/dev/hpet) is enabled, we need to reserve
  56. * timer 0 and timer 1 in case of RTC emulation.
  57. */
  58. #ifdef CONFIG_HPET
  59. static void hpet_reserve_platform_timers(unsigned long id)
  60. {
  61. struct hpet __iomem *hpet = hpet_virt_address;
  62. struct hpet_timer __iomem *timer = &hpet->hpet_timers[2];
  63. unsigned int nrtimers, i;
  64. struct hpet_data hd;
  65. nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1;
  66. memset(&hd, 0, sizeof (hd));
  67. hd.hd_phys_address = hpet_address;
  68. hd.hd_address = hpet_virt_address;
  69. hd.hd_nirqs = nrtimers;
  70. hd.hd_flags = HPET_DATA_PLATFORM;
  71. hpet_reserve_timer(&hd, 0);
  72. #ifdef CONFIG_HPET_EMULATE_RTC
  73. hpet_reserve_timer(&hd, 1);
  74. #endif
  75. hd.hd_irq[0] = HPET_LEGACY_8254;
  76. hd.hd_irq[1] = HPET_LEGACY_RTC;
  77. for (i = 2; i < nrtimers; timer++, i++)
  78. hd.hd_irq[i] = (timer->hpet_config & Tn_INT_ROUTE_CNF_MASK) >>
  79. Tn_INT_ROUTE_CNF_SHIFT;
  80. hpet_alloc(&hd);
  81. }
  82. #else
  83. static void hpet_reserve_platform_timers(unsigned long id) { }
  84. #endif
  85. /*
  86. * Common hpet info
  87. */
  88. static unsigned long hpet_period;
  89. static void hpet_set_mode(enum clock_event_mode mode,
  90. struct clock_event_device *evt);
  91. static int hpet_next_event(unsigned long delta,
  92. struct clock_event_device *evt);
  93. /*
  94. * The hpet clock event device
  95. */
  96. static struct clock_event_device hpet_clockevent = {
  97. .name = "hpet",
  98. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  99. .set_mode = hpet_set_mode,
  100. .set_next_event = hpet_next_event,
  101. .shift = 32,
  102. .irq = 0,
  103. };
  104. static void hpet_start_counter(void)
  105. {
  106. unsigned long cfg = hpet_readl(HPET_CFG);
  107. cfg &= ~HPET_CFG_ENABLE;
  108. hpet_writel(cfg, HPET_CFG);
  109. hpet_writel(0, HPET_COUNTER);
  110. hpet_writel(0, HPET_COUNTER + 4);
  111. cfg |= HPET_CFG_ENABLE;
  112. hpet_writel(cfg, HPET_CFG);
  113. }
  114. static void hpet_enable_int(void)
  115. {
  116. unsigned long cfg = hpet_readl(HPET_CFG);
  117. cfg |= HPET_CFG_LEGACY;
  118. hpet_writel(cfg, HPET_CFG);
  119. hpet_legacy_int_enabled = 1;
  120. }
  121. static void hpet_set_mode(enum clock_event_mode mode,
  122. struct clock_event_device *evt)
  123. {
  124. unsigned long cfg, cmp, now;
  125. uint64_t delta;
  126. switch(mode) {
  127. case CLOCK_EVT_MODE_PERIODIC:
  128. delta = ((uint64_t)(NSEC_PER_SEC/HZ)) * hpet_clockevent.mult;
  129. delta >>= hpet_clockevent.shift;
  130. now = hpet_readl(HPET_COUNTER);
  131. cmp = now + (unsigned long) delta;
  132. cfg = hpet_readl(HPET_T0_CFG);
  133. cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC |
  134. HPET_TN_SETVAL | HPET_TN_32BIT;
  135. hpet_writel(cfg, HPET_T0_CFG);
  136. /*
  137. * The first write after writing TN_SETVAL to the
  138. * config register sets the counter value, the second
  139. * write sets the period.
  140. */
  141. hpet_writel(cmp, HPET_T0_CMP);
  142. udelay(1);
  143. hpet_writel((unsigned long) delta, HPET_T0_CMP);
  144. break;
  145. case CLOCK_EVT_MODE_ONESHOT:
  146. cfg = hpet_readl(HPET_T0_CFG);
  147. cfg &= ~HPET_TN_PERIODIC;
  148. cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
  149. hpet_writel(cfg, HPET_T0_CFG);
  150. break;
  151. case CLOCK_EVT_MODE_UNUSED:
  152. case CLOCK_EVT_MODE_SHUTDOWN:
  153. cfg = hpet_readl(HPET_T0_CFG);
  154. cfg &= ~HPET_TN_ENABLE;
  155. hpet_writel(cfg, HPET_T0_CFG);
  156. break;
  157. }
  158. }
  159. static int hpet_next_event(unsigned long delta,
  160. struct clock_event_device *evt)
  161. {
  162. unsigned long cnt;
  163. cnt = hpet_readl(HPET_COUNTER);
  164. cnt += delta;
  165. hpet_writel(cnt, HPET_T0_CMP);
  166. return ((long)(hpet_readl(HPET_COUNTER) - cnt ) > 0);
  167. }
  168. /*
  169. * Clock source related code
  170. */
  171. static cycle_t read_hpet(void)
  172. {
  173. return (cycle_t)hpet_readl(HPET_COUNTER);
  174. }
  175. static struct clocksource clocksource_hpet = {
  176. .name = "hpet",
  177. .rating = 250,
  178. .read = read_hpet,
  179. .mask = HPET_MASK,
  180. .shift = HPET_SHIFT,
  181. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  182. };
  183. /*
  184. * Try to setup the HPET timer
  185. */
  186. int __init hpet_enable(void)
  187. {
  188. unsigned long id;
  189. uint64_t hpet_freq;
  190. u64 tmp;
  191. if (!is_hpet_capable())
  192. return 0;
  193. hpet_virt_address = ioremap_nocache(hpet_address, HPET_MMAP_SIZE);
  194. /*
  195. * Read the period and check for a sane value:
  196. */
  197. hpet_period = hpet_readl(HPET_PERIOD);
  198. if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD)
  199. goto out_nohpet;
  200. /*
  201. * The period is a femto seconds value. We need to calculate the
  202. * scaled math multiplication factor for nanosecond to hpet tick
  203. * conversion.
  204. */
  205. hpet_freq = 1000000000000000ULL;
  206. do_div(hpet_freq, hpet_period);
  207. hpet_clockevent.mult = div_sc((unsigned long) hpet_freq,
  208. NSEC_PER_SEC, 32);
  209. /* Calculate the min / max delta */
  210. hpet_clockevent.max_delta_ns = clockevent_delta2ns(0x7FFFFFFF,
  211. &hpet_clockevent);
  212. hpet_clockevent.min_delta_ns = clockevent_delta2ns(0x30,
  213. &hpet_clockevent);
  214. /*
  215. * Read the HPET ID register to retrieve the IRQ routing
  216. * information and the number of channels
  217. */
  218. id = hpet_readl(HPET_ID);
  219. #ifdef CONFIG_HPET_EMULATE_RTC
  220. /*
  221. * The legacy routing mode needs at least two channels, tick timer
  222. * and the rtc emulation channel.
  223. */
  224. if (!(id & HPET_ID_NUMBER))
  225. goto out_nohpet;
  226. #endif
  227. /* Start the counter */
  228. hpet_start_counter();
  229. /* Initialize and register HPET clocksource
  230. *
  231. * hpet period is in femto seconds per cycle
  232. * so we need to convert this to ns/cyc units
  233. * aproximated by mult/2^shift
  234. *
  235. * fsec/cyc * 1nsec/1000000fsec = nsec/cyc = mult/2^shift
  236. * fsec/cyc * 1ns/1000000fsec * 2^shift = mult
  237. * fsec/cyc * 2^shift * 1nsec/1000000fsec = mult
  238. * (fsec/cyc << shift)/1000000 = mult
  239. * (hpet_period << shift)/FSEC_PER_NSEC = mult
  240. */
  241. tmp = (u64)hpet_period << HPET_SHIFT;
  242. do_div(tmp, FSEC_PER_NSEC);
  243. clocksource_hpet.mult = (u32)tmp;
  244. clocksource_register(&clocksource_hpet);
  245. if (id & HPET_ID_LEGSUP) {
  246. hpet_enable_int();
  247. hpet_reserve_platform_timers(id);
  248. /*
  249. * Start hpet with the boot cpu mask and make it
  250. * global after the IO_APIC has been initialized.
  251. */
  252. hpet_clockevent.cpumask =cpumask_of_cpu(0);
  253. clockevents_register_device(&hpet_clockevent);
  254. global_clock_event = &hpet_clockevent;
  255. return 1;
  256. }
  257. return 0;
  258. out_nohpet:
  259. iounmap(hpet_virt_address);
  260. hpet_virt_address = NULL;
  261. return 0;
  262. }
  263. #ifdef CONFIG_HPET_EMULATE_RTC
  264. /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
  265. * is enabled, we support RTC interrupt functionality in software.
  266. * RTC has 3 kinds of interrupts:
  267. * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
  268. * is updated
  269. * 2) Alarm Interrupt - generate an interrupt at a specific time of day
  270. * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
  271. * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
  272. * (1) and (2) above are implemented using polling at a frequency of
  273. * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
  274. * overhead. (DEFAULT_RTC_INT_FREQ)
  275. * For (3), we use interrupts at 64Hz or user specified periodic
  276. * frequency, whichever is higher.
  277. */
  278. #include <linux/mc146818rtc.h>
  279. #include <linux/rtc.h>
  280. #define DEFAULT_RTC_INT_FREQ 64
  281. #define DEFAULT_RTC_SHIFT 6
  282. #define RTC_NUM_INTS 1
  283. static unsigned long hpet_rtc_flags;
  284. static unsigned long hpet_prev_update_sec;
  285. static struct rtc_time hpet_alarm_time;
  286. static unsigned long hpet_pie_count;
  287. static unsigned long hpet_t1_cmp;
  288. static unsigned long hpet_default_delta;
  289. static unsigned long hpet_pie_delta;
  290. static unsigned long hpet_pie_limit;
  291. /*
  292. * Timer 1 for RTC emulation. We use one shot mode, as periodic mode
  293. * is not supported by all HPET implementations for timer 1.
  294. *
  295. * hpet_rtc_timer_init() is called when the rtc is initialized.
  296. */
  297. int hpet_rtc_timer_init(void)
  298. {
  299. unsigned long cfg, cnt, delta, flags;
  300. if (!is_hpet_enabled())
  301. return 0;
  302. if (!hpet_default_delta) {
  303. uint64_t clc;
  304. clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
  305. clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT;
  306. hpet_default_delta = (unsigned long) clc;
  307. }
  308. if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
  309. delta = hpet_default_delta;
  310. else
  311. delta = hpet_pie_delta;
  312. local_irq_save(flags);
  313. cnt = delta + hpet_readl(HPET_COUNTER);
  314. hpet_writel(cnt, HPET_T1_CMP);
  315. hpet_t1_cmp = cnt;
  316. cfg = hpet_readl(HPET_T1_CFG);
  317. cfg &= ~HPET_TN_PERIODIC;
  318. cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
  319. hpet_writel(cfg, HPET_T1_CFG);
  320. local_irq_restore(flags);
  321. return 1;
  322. }
  323. /*
  324. * The functions below are called from rtc driver.
  325. * Return 0 if HPET is not being used.
  326. * Otherwise do the necessary changes and return 1.
  327. */
  328. int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
  329. {
  330. if (!is_hpet_enabled())
  331. return 0;
  332. hpet_rtc_flags &= ~bit_mask;
  333. return 1;
  334. }
  335. int hpet_set_rtc_irq_bit(unsigned long bit_mask)
  336. {
  337. unsigned long oldbits = hpet_rtc_flags;
  338. if (!is_hpet_enabled())
  339. return 0;
  340. hpet_rtc_flags |= bit_mask;
  341. if (!oldbits)
  342. hpet_rtc_timer_init();
  343. return 1;
  344. }
  345. int hpet_set_alarm_time(unsigned char hrs, unsigned char min,
  346. unsigned char sec)
  347. {
  348. if (!is_hpet_enabled())
  349. return 0;
  350. hpet_alarm_time.tm_hour = hrs;
  351. hpet_alarm_time.tm_min = min;
  352. hpet_alarm_time.tm_sec = sec;
  353. return 1;
  354. }
  355. int hpet_set_periodic_freq(unsigned long freq)
  356. {
  357. uint64_t clc;
  358. if (!is_hpet_enabled())
  359. return 0;
  360. if (freq <= DEFAULT_RTC_INT_FREQ)
  361. hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq;
  362. else {
  363. clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC;
  364. do_div(clc, freq);
  365. clc >>= hpet_clockevent.shift;
  366. hpet_pie_delta = (unsigned long) clc;
  367. }
  368. return 1;
  369. }
  370. int hpet_rtc_dropped_irq(void)
  371. {
  372. return is_hpet_enabled();
  373. }
  374. static void hpet_rtc_timer_reinit(void)
  375. {
  376. unsigned long cfg, delta;
  377. int lost_ints = -1;
  378. if (unlikely(!hpet_rtc_flags)) {
  379. cfg = hpet_readl(HPET_T1_CFG);
  380. cfg &= ~HPET_TN_ENABLE;
  381. hpet_writel(cfg, HPET_T1_CFG);
  382. return;
  383. }
  384. if (!(hpet_rtc_flags & RTC_PIE) || hpet_pie_limit)
  385. delta = hpet_default_delta;
  386. else
  387. delta = hpet_pie_delta;
  388. /*
  389. * Increment the comparator value until we are ahead of the
  390. * current count.
  391. */
  392. do {
  393. hpet_t1_cmp += delta;
  394. hpet_writel(hpet_t1_cmp, HPET_T1_CMP);
  395. lost_ints++;
  396. } while ((long)(hpet_readl(HPET_COUNTER) - hpet_t1_cmp) > 0);
  397. if (lost_ints) {
  398. if (hpet_rtc_flags & RTC_PIE)
  399. hpet_pie_count += lost_ints;
  400. if (printk_ratelimit())
  401. printk(KERN_WARNING "rtc: lost %d interrupts\n",
  402. lost_ints);
  403. }
  404. }
  405. irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id)
  406. {
  407. struct rtc_time curr_time;
  408. unsigned long rtc_int_flag = 0;
  409. hpet_rtc_timer_reinit();
  410. if (hpet_rtc_flags & (RTC_UIE | RTC_AIE))
  411. rtc_get_rtc_time(&curr_time);
  412. if (hpet_rtc_flags & RTC_UIE &&
  413. curr_time.tm_sec != hpet_prev_update_sec) {
  414. rtc_int_flag = RTC_UF;
  415. hpet_prev_update_sec = curr_time.tm_sec;
  416. }
  417. if (hpet_rtc_flags & RTC_PIE &&
  418. ++hpet_pie_count >= hpet_pie_limit) {
  419. rtc_int_flag |= RTC_PF;
  420. hpet_pie_count = 0;
  421. }
  422. if (hpet_rtc_flags & RTC_PIE &&
  423. (curr_time.tm_sec == hpet_alarm_time.tm_sec) &&
  424. (curr_time.tm_min == hpet_alarm_time.tm_min) &&
  425. (curr_time.tm_hour == hpet_alarm_time.tm_hour))
  426. rtc_int_flag |= RTC_AF;
  427. if (rtc_int_flag) {
  428. rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
  429. rtc_interrupt(rtc_int_flag, dev_id);
  430. }
  431. return IRQ_HANDLED;
  432. }
  433. #endif