hpet.c 14 KB

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