hpet.c 13 KB

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