time.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * linux/arch/alpha/kernel/time.c
  3. *
  4. * Copyright (C) 1991, 1992, 1995, 1999, 2000 Linus Torvalds
  5. *
  6. * This file contains the clocksource time handling.
  7. * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
  8. * "A Kernel Model for Precision Timekeeping" by Dave Mills
  9. * 1997-01-09 Adrian Sun
  10. * use interval timer if CONFIG_RTC=y
  11. * 1997-10-29 John Bowman (bowman@math.ualberta.ca)
  12. * fixed tick loss calculation in timer_interrupt
  13. * (round system clock to nearest tick instead of truncating)
  14. * fixed algorithm in time_init for getting time from CMOS clock
  15. * 1999-04-16 Thorsten Kranzkowski (dl8bcu@gmx.net)
  16. * fixed algorithm in do_gettimeofday() for calculating the precise time
  17. * from processor cycle counter (now taking lost_ticks into account)
  18. * 2003-06-03 R. Scott Bailey <scott.bailey@eds.com>
  19. * Tighten sanity in time_init from 1% (10,000 PPM) to 250 PPM
  20. */
  21. #include <linux/errno.h>
  22. #include <linux/module.h>
  23. #include <linux/sched.h>
  24. #include <linux/kernel.h>
  25. #include <linux/param.h>
  26. #include <linux/string.h>
  27. #include <linux/mm.h>
  28. #include <linux/delay.h>
  29. #include <linux/ioport.h>
  30. #include <linux/irq.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/init.h>
  33. #include <linux/bcd.h>
  34. #include <linux/profile.h>
  35. #include <linux/irq_work.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/io.h>
  38. #include <asm/hwrpb.h>
  39. #include <linux/mc146818rtc.h>
  40. #include <linux/time.h>
  41. #include <linux/timex.h>
  42. #include <linux/clocksource.h>
  43. #include <linux/clockchips.h>
  44. #include "proto.h"
  45. #include "irq_impl.h"
  46. DEFINE_SPINLOCK(rtc_lock);
  47. EXPORT_SYMBOL(rtc_lock);
  48. unsigned long est_cycle_freq;
  49. #ifdef CONFIG_IRQ_WORK
  50. DEFINE_PER_CPU(u8, irq_work_pending);
  51. #define set_irq_work_pending_flag() __this_cpu_write(irq_work_pending, 1)
  52. #define test_irq_work_pending() __this_cpu_read(irq_work_pending)
  53. #define clear_irq_work_pending() __this_cpu_write(irq_work_pending, 0)
  54. void arch_irq_work_raise(void)
  55. {
  56. set_irq_work_pending_flag();
  57. }
  58. #else /* CONFIG_IRQ_WORK */
  59. #define test_irq_work_pending() 0
  60. #define clear_irq_work_pending()
  61. #endif /* CONFIG_IRQ_WORK */
  62. static inline __u32 rpcc(void)
  63. {
  64. return __builtin_alpha_rpcc();
  65. }
  66. /*
  67. * The RTC as a clock_event_device primitive.
  68. */
  69. static DEFINE_PER_CPU(struct clock_event_device, cpu_ce);
  70. irqreturn_t
  71. rtc_timer_interrupt(int irq, void *dev)
  72. {
  73. int cpu = smp_processor_id();
  74. struct clock_event_device *ce = &per_cpu(cpu_ce, cpu);
  75. /* Don't run the hook for UNUSED or SHUTDOWN. */
  76. if (likely(clockevent_state_periodic(ce)))
  77. ce->event_handler(ce);
  78. if (test_irq_work_pending()) {
  79. clear_irq_work_pending();
  80. irq_work_run();
  81. }
  82. return IRQ_HANDLED;
  83. }
  84. static int
  85. rtc_ce_set_next_event(unsigned long evt, struct clock_event_device *ce)
  86. {
  87. /* This hook is for oneshot mode, which we don't support. */
  88. return -EINVAL;
  89. }
  90. static void __init
  91. init_rtc_clockevent(void)
  92. {
  93. int cpu = smp_processor_id();
  94. struct clock_event_device *ce = &per_cpu(cpu_ce, cpu);
  95. *ce = (struct clock_event_device){
  96. .name = "rtc",
  97. .features = CLOCK_EVT_FEAT_PERIODIC,
  98. .rating = 100,
  99. .cpumask = cpumask_of(cpu),
  100. .set_next_event = rtc_ce_set_next_event,
  101. };
  102. clockevents_config_and_register(ce, CONFIG_HZ, 0, 0);
  103. }
  104. /*
  105. * The QEMU clock as a clocksource primitive.
  106. */
  107. static cycle_t
  108. qemu_cs_read(struct clocksource *cs)
  109. {
  110. return qemu_get_vmtime();
  111. }
  112. static struct clocksource qemu_cs = {
  113. .name = "qemu",
  114. .rating = 400,
  115. .read = qemu_cs_read,
  116. .mask = CLOCKSOURCE_MASK(64),
  117. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  118. .max_idle_ns = LONG_MAX
  119. };
  120. /*
  121. * The QEMU alarm as a clock_event_device primitive.
  122. */
  123. static int qemu_ce_shutdown(struct clock_event_device *ce)
  124. {
  125. /* The mode member of CE is updated for us in generic code.
  126. Just make sure that the event is disabled. */
  127. qemu_set_alarm_abs(0);
  128. return 0;
  129. }
  130. static int
  131. qemu_ce_set_next_event(unsigned long evt, struct clock_event_device *ce)
  132. {
  133. qemu_set_alarm_rel(evt);
  134. return 0;
  135. }
  136. static irqreturn_t
  137. qemu_timer_interrupt(int irq, void *dev)
  138. {
  139. int cpu = smp_processor_id();
  140. struct clock_event_device *ce = &per_cpu(cpu_ce, cpu);
  141. ce->event_handler(ce);
  142. return IRQ_HANDLED;
  143. }
  144. static void __init
  145. init_qemu_clockevent(void)
  146. {
  147. int cpu = smp_processor_id();
  148. struct clock_event_device *ce = &per_cpu(cpu_ce, cpu);
  149. *ce = (struct clock_event_device){
  150. .name = "qemu",
  151. .features = CLOCK_EVT_FEAT_ONESHOT,
  152. .rating = 400,
  153. .cpumask = cpumask_of(cpu),
  154. .set_state_shutdown = qemu_ce_shutdown,
  155. .set_state_oneshot = qemu_ce_shutdown,
  156. .tick_resume = qemu_ce_shutdown,
  157. .set_next_event = qemu_ce_set_next_event,
  158. };
  159. clockevents_config_and_register(ce, NSEC_PER_SEC, 1000, LONG_MAX);
  160. }
  161. void __init
  162. common_init_rtc(void)
  163. {
  164. unsigned char x, sel = 0;
  165. /* Reset periodic interrupt frequency. */
  166. #if CONFIG_HZ == 1024 || CONFIG_HZ == 1200
  167. x = CMOS_READ(RTC_FREQ_SELECT) & 0x3f;
  168. /* Test includes known working values on various platforms
  169. where 0x26 is wrong; we refuse to change those. */
  170. if (x != 0x26 && x != 0x25 && x != 0x19 && x != 0x06) {
  171. sel = RTC_REF_CLCK_32KHZ + 6;
  172. }
  173. #elif CONFIG_HZ == 256 || CONFIG_HZ == 128 || CONFIG_HZ == 64 || CONFIG_HZ == 32
  174. sel = RTC_REF_CLCK_32KHZ + __builtin_ffs(32768 / CONFIG_HZ);
  175. #else
  176. # error "Unknown HZ from arch/alpha/Kconfig"
  177. #endif
  178. if (sel) {
  179. printk(KERN_INFO "Setting RTC_FREQ to %d Hz (%x)\n",
  180. CONFIG_HZ, sel);
  181. CMOS_WRITE(sel, RTC_FREQ_SELECT);
  182. }
  183. /* Turn on periodic interrupts. */
  184. x = CMOS_READ(RTC_CONTROL);
  185. if (!(x & RTC_PIE)) {
  186. printk("Turning on RTC interrupts.\n");
  187. x |= RTC_PIE;
  188. x &= ~(RTC_AIE | RTC_UIE);
  189. CMOS_WRITE(x, RTC_CONTROL);
  190. }
  191. (void) CMOS_READ(RTC_INTR_FLAGS);
  192. outb(0x36, 0x43); /* pit counter 0: system timer */
  193. outb(0x00, 0x40);
  194. outb(0x00, 0x40);
  195. outb(0xb6, 0x43); /* pit counter 2: speaker */
  196. outb(0x31, 0x42);
  197. outb(0x13, 0x42);
  198. init_rtc_irq();
  199. }
  200. #ifndef CONFIG_ALPHA_WTINT
  201. /*
  202. * The RPCC as a clocksource primitive.
  203. *
  204. * While we have free-running timecounters running on all CPUs, and we make
  205. * a half-hearted attempt in init_rtc_rpcc_info to sync the timecounter
  206. * with the wall clock, that initialization isn't kept up-to-date across
  207. * different time counters in SMP mode. Therefore we can only use this
  208. * method when there's only one CPU enabled.
  209. *
  210. * When using the WTINT PALcall, the RPCC may shift to a lower frequency,
  211. * or stop altogether, while waiting for the interrupt. Therefore we cannot
  212. * use this method when WTINT is in use.
  213. */
  214. static cycle_t read_rpcc(struct clocksource *cs)
  215. {
  216. return rpcc();
  217. }
  218. static struct clocksource clocksource_rpcc = {
  219. .name = "rpcc",
  220. .rating = 300,
  221. .read = read_rpcc,
  222. .mask = CLOCKSOURCE_MASK(32),
  223. .flags = CLOCK_SOURCE_IS_CONTINUOUS
  224. };
  225. #endif /* ALPHA_WTINT */
  226. /* Validate a computed cycle counter result against the known bounds for
  227. the given processor core. There's too much brokenness in the way of
  228. timing hardware for any one method to work everywhere. :-(
  229. Return 0 if the result cannot be trusted, otherwise return the argument. */
  230. static unsigned long __init
  231. validate_cc_value(unsigned long cc)
  232. {
  233. static struct bounds {
  234. unsigned int min, max;
  235. } cpu_hz[] __initdata = {
  236. [EV3_CPU] = { 50000000, 200000000 }, /* guess */
  237. [EV4_CPU] = { 100000000, 300000000 },
  238. [LCA4_CPU] = { 100000000, 300000000 }, /* guess */
  239. [EV45_CPU] = { 200000000, 300000000 },
  240. [EV5_CPU] = { 250000000, 433000000 },
  241. [EV56_CPU] = { 333000000, 667000000 },
  242. [PCA56_CPU] = { 400000000, 600000000 }, /* guess */
  243. [PCA57_CPU] = { 500000000, 600000000 }, /* guess */
  244. [EV6_CPU] = { 466000000, 600000000 },
  245. [EV67_CPU] = { 600000000, 750000000 },
  246. [EV68AL_CPU] = { 750000000, 940000000 },
  247. [EV68CB_CPU] = { 1000000000, 1333333333 },
  248. /* None of the following are shipping as of 2001-11-01. */
  249. [EV68CX_CPU] = { 1000000000, 1700000000 }, /* guess */
  250. [EV69_CPU] = { 1000000000, 1700000000 }, /* guess */
  251. [EV7_CPU] = { 800000000, 1400000000 }, /* guess */
  252. [EV79_CPU] = { 1000000000, 2000000000 }, /* guess */
  253. };
  254. /* Allow for some drift in the crystal. 10MHz is more than enough. */
  255. const unsigned int deviation = 10000000;
  256. struct percpu_struct *cpu;
  257. unsigned int index;
  258. cpu = (struct percpu_struct *)((char*)hwrpb + hwrpb->processor_offset);
  259. index = cpu->type & 0xffffffff;
  260. /* If index out of bounds, no way to validate. */
  261. if (index >= ARRAY_SIZE(cpu_hz))
  262. return cc;
  263. /* If index contains no data, no way to validate. */
  264. if (cpu_hz[index].max == 0)
  265. return cc;
  266. if (cc < cpu_hz[index].min - deviation
  267. || cc > cpu_hz[index].max + deviation)
  268. return 0;
  269. return cc;
  270. }
  271. /*
  272. * Calibrate CPU clock using legacy 8254 timer/counter. Stolen from
  273. * arch/i386/time.c.
  274. */
  275. #define CALIBRATE_LATCH 0xffff
  276. #define TIMEOUT_COUNT 0x100000
  277. static unsigned long __init
  278. calibrate_cc_with_pit(void)
  279. {
  280. int cc, count = 0;
  281. /* Set the Gate high, disable speaker */
  282. outb((inb(0x61) & ~0x02) | 0x01, 0x61);
  283. /*
  284. * Now let's take care of CTC channel 2
  285. *
  286. * Set the Gate high, program CTC channel 2 for mode 0,
  287. * (interrupt on terminal count mode), binary count,
  288. * load 5 * LATCH count, (LSB and MSB) to begin countdown.
  289. */
  290. outb(0xb0, 0x43); /* binary, mode 0, LSB/MSB, Ch 2 */
  291. outb(CALIBRATE_LATCH & 0xff, 0x42); /* LSB of count */
  292. outb(CALIBRATE_LATCH >> 8, 0x42); /* MSB of count */
  293. cc = rpcc();
  294. do {
  295. count++;
  296. } while ((inb(0x61) & 0x20) == 0 && count < TIMEOUT_COUNT);
  297. cc = rpcc() - cc;
  298. /* Error: ECTCNEVERSET or ECPUTOOFAST. */
  299. if (count <= 1 || count == TIMEOUT_COUNT)
  300. return 0;
  301. return ((long)cc * PIT_TICK_RATE) / (CALIBRATE_LATCH + 1);
  302. }
  303. /* The Linux interpretation of the CMOS clock register contents:
  304. When the Update-In-Progress (UIP) flag goes from 1 to 0, the
  305. RTC registers show the second which has precisely just started.
  306. Let's hope other operating systems interpret the RTC the same way. */
  307. static unsigned long __init
  308. rpcc_after_update_in_progress(void)
  309. {
  310. do { } while (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP));
  311. do { } while (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
  312. return rpcc();
  313. }
  314. void __init
  315. time_init(void)
  316. {
  317. unsigned int cc1, cc2;
  318. unsigned long cycle_freq, tolerance;
  319. long diff;
  320. if (alpha_using_qemu) {
  321. clocksource_register_hz(&qemu_cs, NSEC_PER_SEC);
  322. init_qemu_clockevent();
  323. timer_irqaction.handler = qemu_timer_interrupt;
  324. init_rtc_irq();
  325. return;
  326. }
  327. /* Calibrate CPU clock -- attempt #1. */
  328. if (!est_cycle_freq)
  329. est_cycle_freq = validate_cc_value(calibrate_cc_with_pit());
  330. cc1 = rpcc();
  331. /* Calibrate CPU clock -- attempt #2. */
  332. if (!est_cycle_freq) {
  333. cc1 = rpcc_after_update_in_progress();
  334. cc2 = rpcc_after_update_in_progress();
  335. est_cycle_freq = validate_cc_value(cc2 - cc1);
  336. cc1 = cc2;
  337. }
  338. cycle_freq = hwrpb->cycle_freq;
  339. if (est_cycle_freq) {
  340. /* If the given value is within 250 PPM of what we calculated,
  341. accept it. Otherwise, use what we found. */
  342. tolerance = cycle_freq / 4000;
  343. diff = cycle_freq - est_cycle_freq;
  344. if (diff < 0)
  345. diff = -diff;
  346. if ((unsigned long)diff > tolerance) {
  347. cycle_freq = est_cycle_freq;
  348. printk("HWRPB cycle frequency bogus. "
  349. "Estimated %lu Hz\n", cycle_freq);
  350. } else {
  351. est_cycle_freq = 0;
  352. }
  353. } else if (! validate_cc_value (cycle_freq)) {
  354. printk("HWRPB cycle frequency bogus, "
  355. "and unable to estimate a proper value!\n");
  356. }
  357. /* See above for restrictions on using clocksource_rpcc. */
  358. #ifndef CONFIG_ALPHA_WTINT
  359. if (hwrpb->nr_processors == 1)
  360. clocksource_register_hz(&clocksource_rpcc, cycle_freq);
  361. #endif
  362. /* Startup the timer source. */
  363. alpha_mv.init_rtc();
  364. init_rtc_clockevent();
  365. }
  366. /* Initialize the clock_event_device for secondary cpus. */
  367. #ifdef CONFIG_SMP
  368. void __init
  369. init_clockevent(void)
  370. {
  371. if (alpha_using_qemu)
  372. init_qemu_clockevent();
  373. else
  374. init_rtc_clockevent();
  375. }
  376. #endif