time.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 PC-specific time handling details:
  7. * reading the RTC at bootup, etc..
  8. * 1994-07-02 Alan Modra
  9. * fixed set_rtc_mmss, fixed time.year for >= 2000, new mktime
  10. * 1995-03-26 Markus Kuhn
  11. * fixed 500 ms bug at call to set_rtc_mmss, fixed DS12887
  12. * precision CMOS clock update
  13. * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
  14. * "A Kernel Model for Precision Timekeeping" by Dave Mills
  15. * 1997-01-09 Adrian Sun
  16. * use interval timer if CONFIG_RTC=y
  17. * 1997-10-29 John Bowman (bowman@math.ualberta.ca)
  18. * fixed tick loss calculation in timer_interrupt
  19. * (round system clock to nearest tick instead of truncating)
  20. * fixed algorithm in time_init for getting time from CMOS clock
  21. * 1999-04-16 Thorsten Kranzkowski (dl8bcu@gmx.net)
  22. * fixed algorithm in do_gettimeofday() for calculating the precise time
  23. * from processor cycle counter (now taking lost_ticks into account)
  24. * 2000-08-13 Jan-Benedict Glaw <jbglaw@lug-owl.de>
  25. * Fixed time_init to be aware of epoches != 1900. This prevents
  26. * booting up in 2048 for me;) Code is stolen from rtc.c.
  27. * 2003-06-03 R. Scott Bailey <scott.bailey@eds.com>
  28. * Tighten sanity in time_init from 1% (10,000 PPM) to 250 PPM
  29. */
  30. #include <linux/errno.h>
  31. #include <linux/module.h>
  32. #include <linux/sched.h>
  33. #include <linux/kernel.h>
  34. #include <linux/param.h>
  35. #include <linux/string.h>
  36. #include <linux/mm.h>
  37. #include <linux/delay.h>
  38. #include <linux/ioport.h>
  39. #include <linux/irq.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/init.h>
  42. #include <linux/bcd.h>
  43. #include <linux/profile.h>
  44. #include <linux/irq_work.h>
  45. #include <asm/uaccess.h>
  46. #include <asm/io.h>
  47. #include <asm/hwrpb.h>
  48. #include <asm/8253pit.h>
  49. #include <asm/rtc.h>
  50. #include <linux/mc146818rtc.h>
  51. #include <linux/time.h>
  52. #include <linux/timex.h>
  53. #include <linux/clocksource.h>
  54. #include "proto.h"
  55. #include "irq_impl.h"
  56. static int set_rtc_mmss(unsigned long);
  57. DEFINE_SPINLOCK(rtc_lock);
  58. EXPORT_SYMBOL(rtc_lock);
  59. #define TICK_SIZE (tick_nsec / 1000)
  60. /*
  61. * Shift amount by which scaled_ticks_per_cycle is scaled. Shifting
  62. * by 48 gives us 16 bits for HZ while keeping the accuracy good even
  63. * for large CPU clock rates.
  64. */
  65. #define FIX_SHIFT 48
  66. /* lump static variables together for more efficient access: */
  67. static struct {
  68. /* cycle counter last time it got invoked */
  69. __u32 last_time;
  70. /* ticks/cycle * 2^48 */
  71. unsigned long scaled_ticks_per_cycle;
  72. /* partial unused tick */
  73. unsigned long partial_tick;
  74. } state;
  75. unsigned long est_cycle_freq;
  76. #ifdef CONFIG_IRQ_WORK
  77. DEFINE_PER_CPU(u8, irq_work_pending);
  78. #define set_irq_work_pending_flag() __get_cpu_var(irq_work_pending) = 1
  79. #define test_irq_work_pending() __get_cpu_var(irq_work_pending)
  80. #define clear_irq_work_pending() __get_cpu_var(irq_work_pending) = 0
  81. void arch_irq_work_raise(void)
  82. {
  83. set_irq_work_pending_flag();
  84. }
  85. #else /* CONFIG_IRQ_WORK */
  86. #define test_irq_work_pending() 0
  87. #define clear_irq_work_pending()
  88. #endif /* CONFIG_IRQ_WORK */
  89. static inline __u32 rpcc(void)
  90. {
  91. __u32 result;
  92. asm volatile ("rpcc %0" : "=r"(result));
  93. return result;
  94. }
  95. int update_persistent_clock(struct timespec now)
  96. {
  97. return set_rtc_mmss(now.tv_sec);
  98. }
  99. void read_persistent_clock(struct timespec *ts)
  100. {
  101. unsigned int year, mon, day, hour, min, sec, epoch;
  102. sec = CMOS_READ(RTC_SECONDS);
  103. min = CMOS_READ(RTC_MINUTES);
  104. hour = CMOS_READ(RTC_HOURS);
  105. day = CMOS_READ(RTC_DAY_OF_MONTH);
  106. mon = CMOS_READ(RTC_MONTH);
  107. year = CMOS_READ(RTC_YEAR);
  108. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  109. sec = bcd2bin(sec);
  110. min = bcd2bin(min);
  111. hour = bcd2bin(hour);
  112. day = bcd2bin(day);
  113. mon = bcd2bin(mon);
  114. year = bcd2bin(year);
  115. }
  116. /* PC-like is standard; used for year >= 70 */
  117. epoch = 1900;
  118. if (year < 20)
  119. epoch = 2000;
  120. else if (year >= 20 && year < 48)
  121. /* NT epoch */
  122. epoch = 1980;
  123. else if (year >= 48 && year < 70)
  124. /* Digital UNIX epoch */
  125. epoch = 1952;
  126. printk(KERN_INFO "Using epoch = %d\n", epoch);
  127. if ((year += epoch) < 1970)
  128. year += 100;
  129. ts->tv_sec = mktime(year, mon, day, hour, min, sec);
  130. ts->tv_nsec = 0;
  131. }
  132. /*
  133. * timer_interrupt() needs to keep up the real-time clock,
  134. * as well as call the "xtime_update()" routine every clocktick
  135. */
  136. irqreturn_t timer_interrupt(int irq, void *dev)
  137. {
  138. unsigned long delta;
  139. __u32 now;
  140. long nticks;
  141. #ifndef CONFIG_SMP
  142. /* Not SMP, do kernel PC profiling here. */
  143. profile_tick(CPU_PROFILING);
  144. #endif
  145. /*
  146. * Calculate how many ticks have passed since the last update,
  147. * including any previous partial leftover. Save any resulting
  148. * fraction for the next pass.
  149. */
  150. now = rpcc();
  151. delta = now - state.last_time;
  152. state.last_time = now;
  153. delta = delta * state.scaled_ticks_per_cycle + state.partial_tick;
  154. state.partial_tick = delta & ((1UL << FIX_SHIFT) - 1);
  155. nticks = delta >> FIX_SHIFT;
  156. if (nticks)
  157. xtime_update(nticks);
  158. if (test_irq_work_pending()) {
  159. clear_irq_work_pending();
  160. irq_work_run();
  161. }
  162. #ifndef CONFIG_SMP
  163. while (nticks--)
  164. update_process_times(user_mode(get_irq_regs()));
  165. #endif
  166. return IRQ_HANDLED;
  167. }
  168. void __init
  169. common_init_rtc(void)
  170. {
  171. unsigned char x;
  172. /* Reset periodic interrupt frequency. */
  173. x = CMOS_READ(RTC_FREQ_SELECT) & 0x3f;
  174. /* Test includes known working values on various platforms
  175. where 0x26 is wrong; we refuse to change those. */
  176. if (x != 0x26 && x != 0x25 && x != 0x19 && x != 0x06) {
  177. printk("Setting RTC_FREQ to 1024 Hz (%x)\n", x);
  178. CMOS_WRITE(0x26, RTC_FREQ_SELECT);
  179. }
  180. /* Turn on periodic interrupts. */
  181. x = CMOS_READ(RTC_CONTROL);
  182. if (!(x & RTC_PIE)) {
  183. printk("Turning on RTC interrupts.\n");
  184. x |= RTC_PIE;
  185. x &= ~(RTC_AIE | RTC_UIE);
  186. CMOS_WRITE(x, RTC_CONTROL);
  187. }
  188. (void) CMOS_READ(RTC_INTR_FLAGS);
  189. outb(0x36, 0x43); /* pit counter 0: system timer */
  190. outb(0x00, 0x40);
  191. outb(0x00, 0x40);
  192. outb(0xb6, 0x43); /* pit counter 2: speaker */
  193. outb(0x31, 0x42);
  194. outb(0x13, 0x42);
  195. init_rtc_irq();
  196. }
  197. unsigned int common_get_rtc_time(struct rtc_time *time)
  198. {
  199. return __get_rtc_time(time);
  200. }
  201. int common_set_rtc_time(struct rtc_time *time)
  202. {
  203. return __set_rtc_time(time);
  204. }
  205. /* Validate a computed cycle counter result against the known bounds for
  206. the given processor core. There's too much brokenness in the way of
  207. timing hardware for any one method to work everywhere. :-(
  208. Return 0 if the result cannot be trusted, otherwise return the argument. */
  209. static unsigned long __init
  210. validate_cc_value(unsigned long cc)
  211. {
  212. static struct bounds {
  213. unsigned int min, max;
  214. } cpu_hz[] __initdata = {
  215. [EV3_CPU] = { 50000000, 200000000 }, /* guess */
  216. [EV4_CPU] = { 100000000, 300000000 },
  217. [LCA4_CPU] = { 100000000, 300000000 }, /* guess */
  218. [EV45_CPU] = { 200000000, 300000000 },
  219. [EV5_CPU] = { 250000000, 433000000 },
  220. [EV56_CPU] = { 333000000, 667000000 },
  221. [PCA56_CPU] = { 400000000, 600000000 }, /* guess */
  222. [PCA57_CPU] = { 500000000, 600000000 }, /* guess */
  223. [EV6_CPU] = { 466000000, 600000000 },
  224. [EV67_CPU] = { 600000000, 750000000 },
  225. [EV68AL_CPU] = { 750000000, 940000000 },
  226. [EV68CB_CPU] = { 1000000000, 1333333333 },
  227. /* None of the following are shipping as of 2001-11-01. */
  228. [EV68CX_CPU] = { 1000000000, 1700000000 }, /* guess */
  229. [EV69_CPU] = { 1000000000, 1700000000 }, /* guess */
  230. [EV7_CPU] = { 800000000, 1400000000 }, /* guess */
  231. [EV79_CPU] = { 1000000000, 2000000000 }, /* guess */
  232. };
  233. /* Allow for some drift in the crystal. 10MHz is more than enough. */
  234. const unsigned int deviation = 10000000;
  235. struct percpu_struct *cpu;
  236. unsigned int index;
  237. cpu = (struct percpu_struct *)((char*)hwrpb + hwrpb->processor_offset);
  238. index = cpu->type & 0xffffffff;
  239. /* If index out of bounds, no way to validate. */
  240. if (index >= ARRAY_SIZE(cpu_hz))
  241. return cc;
  242. /* If index contains no data, no way to validate. */
  243. if (cpu_hz[index].max == 0)
  244. return cc;
  245. if (cc < cpu_hz[index].min - deviation
  246. || cc > cpu_hz[index].max + deviation)
  247. return 0;
  248. return cc;
  249. }
  250. /*
  251. * Calibrate CPU clock using legacy 8254 timer/counter. Stolen from
  252. * arch/i386/time.c.
  253. */
  254. #define CALIBRATE_LATCH 0xffff
  255. #define TIMEOUT_COUNT 0x100000
  256. static unsigned long __init
  257. calibrate_cc_with_pit(void)
  258. {
  259. int cc, count = 0;
  260. /* Set the Gate high, disable speaker */
  261. outb((inb(0x61) & ~0x02) | 0x01, 0x61);
  262. /*
  263. * Now let's take care of CTC channel 2
  264. *
  265. * Set the Gate high, program CTC channel 2 for mode 0,
  266. * (interrupt on terminal count mode), binary count,
  267. * load 5 * LATCH count, (LSB and MSB) to begin countdown.
  268. */
  269. outb(0xb0, 0x43); /* binary, mode 0, LSB/MSB, Ch 2 */
  270. outb(CALIBRATE_LATCH & 0xff, 0x42); /* LSB of count */
  271. outb(CALIBRATE_LATCH >> 8, 0x42); /* MSB of count */
  272. cc = rpcc();
  273. do {
  274. count++;
  275. } while ((inb(0x61) & 0x20) == 0 && count < TIMEOUT_COUNT);
  276. cc = rpcc() - cc;
  277. /* Error: ECTCNEVERSET or ECPUTOOFAST. */
  278. if (count <= 1 || count == TIMEOUT_COUNT)
  279. return 0;
  280. return ((long)cc * PIT_TICK_RATE) / (CALIBRATE_LATCH + 1);
  281. }
  282. /* The Linux interpretation of the CMOS clock register contents:
  283. When the Update-In-Progress (UIP) flag goes from 1 to 0, the
  284. RTC registers show the second which has precisely just started.
  285. Let's hope other operating systems interpret the RTC the same way. */
  286. static unsigned long __init
  287. rpcc_after_update_in_progress(void)
  288. {
  289. do { } while (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP));
  290. do { } while (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
  291. return rpcc();
  292. }
  293. #ifndef CONFIG_SMP
  294. /* Until and unless we figure out how to get cpu cycle counters
  295. in sync and keep them there, we can't use the rpcc. */
  296. static cycle_t read_rpcc(struct clocksource *cs)
  297. {
  298. cycle_t ret = (cycle_t)rpcc();
  299. return ret;
  300. }
  301. static struct clocksource clocksource_rpcc = {
  302. .name = "rpcc",
  303. .rating = 300,
  304. .read = read_rpcc,
  305. .mask = CLOCKSOURCE_MASK(32),
  306. .flags = CLOCK_SOURCE_IS_CONTINUOUS
  307. };
  308. static inline void register_rpcc_clocksource(long cycle_freq)
  309. {
  310. clocksource_register_hz(&clocksource_rpcc, cycle_freq);
  311. }
  312. #else /* !CONFIG_SMP */
  313. static inline void register_rpcc_clocksource(long cycle_freq)
  314. {
  315. }
  316. #endif /* !CONFIG_SMP */
  317. void __init
  318. time_init(void)
  319. {
  320. unsigned int cc1, cc2;
  321. unsigned long cycle_freq, tolerance;
  322. long diff;
  323. /* Calibrate CPU clock -- attempt #1. */
  324. if (!est_cycle_freq)
  325. est_cycle_freq = validate_cc_value(calibrate_cc_with_pit());
  326. cc1 = rpcc();
  327. /* Calibrate CPU clock -- attempt #2. */
  328. if (!est_cycle_freq) {
  329. cc1 = rpcc_after_update_in_progress();
  330. cc2 = rpcc_after_update_in_progress();
  331. est_cycle_freq = validate_cc_value(cc2 - cc1);
  332. cc1 = cc2;
  333. }
  334. cycle_freq = hwrpb->cycle_freq;
  335. if (est_cycle_freq) {
  336. /* If the given value is within 250 PPM of what we calculated,
  337. accept it. Otherwise, use what we found. */
  338. tolerance = cycle_freq / 4000;
  339. diff = cycle_freq - est_cycle_freq;
  340. if (diff < 0)
  341. diff = -diff;
  342. if ((unsigned long)diff > tolerance) {
  343. cycle_freq = est_cycle_freq;
  344. printk("HWRPB cycle frequency bogus. "
  345. "Estimated %lu Hz\n", cycle_freq);
  346. } else {
  347. est_cycle_freq = 0;
  348. }
  349. } else if (! validate_cc_value (cycle_freq)) {
  350. printk("HWRPB cycle frequency bogus, "
  351. "and unable to estimate a proper value!\n");
  352. }
  353. /* From John Bowman <bowman@math.ualberta.ca>: allow the values
  354. to settle, as the Update-In-Progress bit going low isn't good
  355. enough on some hardware. 2ms is our guess; we haven't found
  356. bogomips yet, but this is close on a 500Mhz box. */
  357. __delay(1000000);
  358. if (HZ > (1<<16)) {
  359. extern void __you_loose (void);
  360. __you_loose();
  361. }
  362. register_rpcc_clocksource(cycle_freq);
  363. state.last_time = cc1;
  364. state.scaled_ticks_per_cycle
  365. = ((unsigned long) HZ << FIX_SHIFT) / cycle_freq;
  366. state.partial_tick = 0L;
  367. /* Startup the timer source. */
  368. alpha_mv.init_rtc();
  369. }
  370. /*
  371. * In order to set the CMOS clock precisely, set_rtc_mmss has to be
  372. * called 500 ms after the second nowtime has started, because when
  373. * nowtime is written into the registers of the CMOS clock, it will
  374. * jump to the next second precisely 500 ms later. Check the Motorola
  375. * MC146818A or Dallas DS12887 data sheet for details.
  376. *
  377. * BUG: This routine does not handle hour overflow properly; it just
  378. * sets the minutes. Usually you won't notice until after reboot!
  379. */
  380. static int
  381. set_rtc_mmss(unsigned long nowtime)
  382. {
  383. int retval = 0;
  384. int real_seconds, real_minutes, cmos_minutes;
  385. unsigned char save_control, save_freq_select;
  386. /* irq are locally disabled here */
  387. spin_lock(&rtc_lock);
  388. /* Tell the clock it's being set */
  389. save_control = CMOS_READ(RTC_CONTROL);
  390. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  391. /* Stop and reset prescaler */
  392. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  393. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  394. cmos_minutes = CMOS_READ(RTC_MINUTES);
  395. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  396. cmos_minutes = bcd2bin(cmos_minutes);
  397. /*
  398. * since we're only adjusting minutes and seconds,
  399. * don't interfere with hour overflow. This avoids
  400. * messing with unknown time zones but requires your
  401. * RTC not to be off by more than 15 minutes
  402. */
  403. real_seconds = nowtime % 60;
  404. real_minutes = nowtime / 60;
  405. if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1) {
  406. /* correct for half hour time zone */
  407. real_minutes += 30;
  408. }
  409. real_minutes %= 60;
  410. if (abs(real_minutes - cmos_minutes) < 30) {
  411. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  412. real_seconds = bin2bcd(real_seconds);
  413. real_minutes = bin2bcd(real_minutes);
  414. }
  415. CMOS_WRITE(real_seconds,RTC_SECONDS);
  416. CMOS_WRITE(real_minutes,RTC_MINUTES);
  417. } else {
  418. printk_once(KERN_NOTICE
  419. "set_rtc_mmss: can't update from %d to %d\n",
  420. cmos_minutes, real_minutes);
  421. retval = -1;
  422. }
  423. /* The following flags have to be released exactly in this order,
  424. * otherwise the DS12887 (popular MC146818A clone with integrated
  425. * battery and quartz) will not reset the oscillator and will not
  426. * update precisely 500 ms later. You won't find this mentioned in
  427. * the Dallas Semiconductor data sheets, but who believes data
  428. * sheets anyway ... -- Markus Kuhn
  429. */
  430. CMOS_WRITE(save_control, RTC_CONTROL);
  431. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  432. spin_unlock(&rtc_lock);
  433. return retval;
  434. }