time.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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(ce->mode == CLOCK_EVT_MODE_PERIODIC))
  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 void
  85. rtc_ce_set_mode(enum clock_event_mode mode, struct clock_event_device *ce)
  86. {
  87. /* The mode member of CE is updated in generic code.
  88. Since we only support periodic events, nothing to do. */
  89. }
  90. static int
  91. rtc_ce_set_next_event(unsigned long evt, struct clock_event_device *ce)
  92. {
  93. /* This hook is for oneshot mode, which we don't support. */
  94. return -EINVAL;
  95. }
  96. static void __init
  97. init_rtc_clockevent(void)
  98. {
  99. int cpu = smp_processor_id();
  100. struct clock_event_device *ce = &per_cpu(cpu_ce, cpu);
  101. *ce = (struct clock_event_device){
  102. .name = "rtc",
  103. .features = CLOCK_EVT_FEAT_PERIODIC,
  104. .rating = 100,
  105. .cpumask = cpumask_of(cpu),
  106. .set_mode = rtc_ce_set_mode,
  107. .set_next_event = rtc_ce_set_next_event,
  108. };
  109. clockevents_config_and_register(ce, CONFIG_HZ, 0, 0);
  110. }
  111. /*
  112. * The QEMU clock as a clocksource primitive.
  113. */
  114. static cycle_t
  115. qemu_cs_read(struct clocksource *cs)
  116. {
  117. return qemu_get_vmtime();
  118. }
  119. static struct clocksource qemu_cs = {
  120. .name = "qemu",
  121. .rating = 400,
  122. .read = qemu_cs_read,
  123. .mask = CLOCKSOURCE_MASK(64),
  124. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  125. .max_idle_ns = LONG_MAX
  126. };
  127. /*
  128. * The QEMU alarm as a clock_event_device primitive.
  129. */
  130. static void
  131. qemu_ce_set_mode(enum clock_event_mode mode, struct clock_event_device *ce)
  132. {
  133. /* The mode member of CE is updated for us in generic code.
  134. Just make sure that the event is disabled. */
  135. qemu_set_alarm_abs(0);
  136. }
  137. static int
  138. qemu_ce_set_next_event(unsigned long evt, struct clock_event_device *ce)
  139. {
  140. qemu_set_alarm_rel(evt);
  141. return 0;
  142. }
  143. static irqreturn_t
  144. qemu_timer_interrupt(int irq, void *dev)
  145. {
  146. int cpu = smp_processor_id();
  147. struct clock_event_device *ce = &per_cpu(cpu_ce, cpu);
  148. ce->event_handler(ce);
  149. return IRQ_HANDLED;
  150. }
  151. static void __init
  152. init_qemu_clockevent(void)
  153. {
  154. int cpu = smp_processor_id();
  155. struct clock_event_device *ce = &per_cpu(cpu_ce, cpu);
  156. *ce = (struct clock_event_device){
  157. .name = "qemu",
  158. .features = CLOCK_EVT_FEAT_ONESHOT,
  159. .rating = 400,
  160. .cpumask = cpumask_of(cpu),
  161. .set_mode = qemu_ce_set_mode,
  162. .set_next_event = qemu_ce_set_next_event,
  163. };
  164. clockevents_config_and_register(ce, NSEC_PER_SEC, 1000, LONG_MAX);
  165. }
  166. void __init
  167. common_init_rtc(void)
  168. {
  169. unsigned char x, sel = 0;
  170. /* Reset periodic interrupt frequency. */
  171. #if CONFIG_HZ == 1024 || CONFIG_HZ == 1200
  172. x = CMOS_READ(RTC_FREQ_SELECT) & 0x3f;
  173. /* Test includes known working values on various platforms
  174. where 0x26 is wrong; we refuse to change those. */
  175. if (x != 0x26 && x != 0x25 && x != 0x19 && x != 0x06) {
  176. sel = RTC_REF_CLCK_32KHZ + 6;
  177. }
  178. #elif CONFIG_HZ == 256 || CONFIG_HZ == 128 || CONFIG_HZ == 64 || CONFIG_HZ == 32
  179. sel = RTC_REF_CLCK_32KHZ + __builtin_ffs(32768 / CONFIG_HZ);
  180. #else
  181. # error "Unknown HZ from arch/alpha/Kconfig"
  182. #endif
  183. if (sel) {
  184. printk(KERN_INFO "Setting RTC_FREQ to %d Hz (%x)\n",
  185. CONFIG_HZ, sel);
  186. CMOS_WRITE(sel, RTC_FREQ_SELECT);
  187. }
  188. /* Turn on periodic interrupts. */
  189. x = CMOS_READ(RTC_CONTROL);
  190. if (!(x & RTC_PIE)) {
  191. printk("Turning on RTC interrupts.\n");
  192. x |= RTC_PIE;
  193. x &= ~(RTC_AIE | RTC_UIE);
  194. CMOS_WRITE(x, RTC_CONTROL);
  195. }
  196. (void) CMOS_READ(RTC_INTR_FLAGS);
  197. outb(0x36, 0x43); /* pit counter 0: system timer */
  198. outb(0x00, 0x40);
  199. outb(0x00, 0x40);
  200. outb(0xb6, 0x43); /* pit counter 2: speaker */
  201. outb(0x31, 0x42);
  202. outb(0x13, 0x42);
  203. init_rtc_irq();
  204. }
  205. #ifndef CONFIG_ALPHA_WTINT
  206. /*
  207. * The RPCC as a clocksource primitive.
  208. *
  209. * While we have free-running timecounters running on all CPUs, and we make
  210. * a half-hearted attempt in init_rtc_rpcc_info to sync the timecounter
  211. * with the wall clock, that initialization isn't kept up-to-date across
  212. * different time counters in SMP mode. Therefore we can only use this
  213. * method when there's only one CPU enabled.
  214. *
  215. * When using the WTINT PALcall, the RPCC may shift to a lower frequency,
  216. * or stop altogether, while waiting for the interrupt. Therefore we cannot
  217. * use this method when WTINT is in use.
  218. */
  219. static cycle_t read_rpcc(struct clocksource *cs)
  220. {
  221. return rpcc();
  222. }
  223. static struct clocksource clocksource_rpcc = {
  224. .name = "rpcc",
  225. .rating = 300,
  226. .read = read_rpcc,
  227. .mask = CLOCKSOURCE_MASK(32),
  228. .flags = CLOCK_SOURCE_IS_CONTINUOUS
  229. };
  230. #endif /* ALPHA_WTINT */
  231. /* Validate a computed cycle counter result against the known bounds for
  232. the given processor core. There's too much brokenness in the way of
  233. timing hardware for any one method to work everywhere. :-(
  234. Return 0 if the result cannot be trusted, otherwise return the argument. */
  235. static unsigned long __init
  236. validate_cc_value(unsigned long cc)
  237. {
  238. static struct bounds {
  239. unsigned int min, max;
  240. } cpu_hz[] __initdata = {
  241. [EV3_CPU] = { 50000000, 200000000 }, /* guess */
  242. [EV4_CPU] = { 100000000, 300000000 },
  243. [LCA4_CPU] = { 100000000, 300000000 }, /* guess */
  244. [EV45_CPU] = { 200000000, 300000000 },
  245. [EV5_CPU] = { 250000000, 433000000 },
  246. [EV56_CPU] = { 333000000, 667000000 },
  247. [PCA56_CPU] = { 400000000, 600000000 }, /* guess */
  248. [PCA57_CPU] = { 500000000, 600000000 }, /* guess */
  249. [EV6_CPU] = { 466000000, 600000000 },
  250. [EV67_CPU] = { 600000000, 750000000 },
  251. [EV68AL_CPU] = { 750000000, 940000000 },
  252. [EV68CB_CPU] = { 1000000000, 1333333333 },
  253. /* None of the following are shipping as of 2001-11-01. */
  254. [EV68CX_CPU] = { 1000000000, 1700000000 }, /* guess */
  255. [EV69_CPU] = { 1000000000, 1700000000 }, /* guess */
  256. [EV7_CPU] = { 800000000, 1400000000 }, /* guess */
  257. [EV79_CPU] = { 1000000000, 2000000000 }, /* guess */
  258. };
  259. /* Allow for some drift in the crystal. 10MHz is more than enough. */
  260. const unsigned int deviation = 10000000;
  261. struct percpu_struct *cpu;
  262. unsigned int index;
  263. cpu = (struct percpu_struct *)((char*)hwrpb + hwrpb->processor_offset);
  264. index = cpu->type & 0xffffffff;
  265. /* If index out of bounds, no way to validate. */
  266. if (index >= ARRAY_SIZE(cpu_hz))
  267. return cc;
  268. /* If index contains no data, no way to validate. */
  269. if (cpu_hz[index].max == 0)
  270. return cc;
  271. if (cc < cpu_hz[index].min - deviation
  272. || cc > cpu_hz[index].max + deviation)
  273. return 0;
  274. return cc;
  275. }
  276. /*
  277. * Calibrate CPU clock using legacy 8254 timer/counter. Stolen from
  278. * arch/i386/time.c.
  279. */
  280. #define CALIBRATE_LATCH 0xffff
  281. #define TIMEOUT_COUNT 0x100000
  282. static unsigned long __init
  283. calibrate_cc_with_pit(void)
  284. {
  285. int cc, count = 0;
  286. /* Set the Gate high, disable speaker */
  287. outb((inb(0x61) & ~0x02) | 0x01, 0x61);
  288. /*
  289. * Now let's take care of CTC channel 2
  290. *
  291. * Set the Gate high, program CTC channel 2 for mode 0,
  292. * (interrupt on terminal count mode), binary count,
  293. * load 5 * LATCH count, (LSB and MSB) to begin countdown.
  294. */
  295. outb(0xb0, 0x43); /* binary, mode 0, LSB/MSB, Ch 2 */
  296. outb(CALIBRATE_LATCH & 0xff, 0x42); /* LSB of count */
  297. outb(CALIBRATE_LATCH >> 8, 0x42); /* MSB of count */
  298. cc = rpcc();
  299. do {
  300. count++;
  301. } while ((inb(0x61) & 0x20) == 0 && count < TIMEOUT_COUNT);
  302. cc = rpcc() - cc;
  303. /* Error: ECTCNEVERSET or ECPUTOOFAST. */
  304. if (count <= 1 || count == TIMEOUT_COUNT)
  305. return 0;
  306. return ((long)cc * PIT_TICK_RATE) / (CALIBRATE_LATCH + 1);
  307. }
  308. /* The Linux interpretation of the CMOS clock register contents:
  309. When the Update-In-Progress (UIP) flag goes from 1 to 0, the
  310. RTC registers show the second which has precisely just started.
  311. Let's hope other operating systems interpret the RTC the same way. */
  312. static unsigned long __init
  313. rpcc_after_update_in_progress(void)
  314. {
  315. do { } while (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP));
  316. do { } while (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
  317. return rpcc();
  318. }
  319. void __init
  320. time_init(void)
  321. {
  322. unsigned int cc1, cc2;
  323. unsigned long cycle_freq, tolerance;
  324. long diff;
  325. if (alpha_using_qemu) {
  326. clocksource_register_hz(&qemu_cs, NSEC_PER_SEC);
  327. init_qemu_clockevent();
  328. timer_irqaction.handler = qemu_timer_interrupt;
  329. init_rtc_irq();
  330. return;
  331. }
  332. /* Calibrate CPU clock -- attempt #1. */
  333. if (!est_cycle_freq)
  334. est_cycle_freq = validate_cc_value(calibrate_cc_with_pit());
  335. cc1 = rpcc();
  336. /* Calibrate CPU clock -- attempt #2. */
  337. if (!est_cycle_freq) {
  338. cc1 = rpcc_after_update_in_progress();
  339. cc2 = rpcc_after_update_in_progress();
  340. est_cycle_freq = validate_cc_value(cc2 - cc1);
  341. cc1 = cc2;
  342. }
  343. cycle_freq = hwrpb->cycle_freq;
  344. if (est_cycle_freq) {
  345. /* If the given value is within 250 PPM of what we calculated,
  346. accept it. Otherwise, use what we found. */
  347. tolerance = cycle_freq / 4000;
  348. diff = cycle_freq - est_cycle_freq;
  349. if (diff < 0)
  350. diff = -diff;
  351. if ((unsigned long)diff > tolerance) {
  352. cycle_freq = est_cycle_freq;
  353. printk("HWRPB cycle frequency bogus. "
  354. "Estimated %lu Hz\n", cycle_freq);
  355. } else {
  356. est_cycle_freq = 0;
  357. }
  358. } else if (! validate_cc_value (cycle_freq)) {
  359. printk("HWRPB cycle frequency bogus, "
  360. "and unable to estimate a proper value!\n");
  361. }
  362. /* See above for restrictions on using clocksource_rpcc. */
  363. #ifndef CONFIG_ALPHA_WTINT
  364. if (hwrpb->nr_processors == 1)
  365. clocksource_register_hz(&clocksource_rpcc, cycle_freq);
  366. #endif
  367. /* Startup the timer source. */
  368. alpha_mv.init_rtc();
  369. init_rtc_clockevent();
  370. }
  371. /* Initialize the clock_event_device for secondary cpus. */
  372. #ifdef CONFIG_SMP
  373. void __init
  374. init_clockevent(void)
  375. {
  376. if (alpha_using_qemu)
  377. init_qemu_clockevent();
  378. else
  379. init_rtc_clockevent();
  380. }
  381. #endif