hpet_32.c 13 KB

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