hpet.c 13 KB

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