hpet.c 14 KB

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