time.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * linux/arch/cris/kernel/time.c
  3. *
  4. * Copyright (C) 1991, 1992, 1995 Linus Torvalds
  5. * Copyright (C) 1999, 2000, 2001 Axis Communications AB
  6. *
  7. * 1994-07-02 Alan Modra
  8. * fixed set_rtc_mmss, fixed time.year for >= 2000, new mktime
  9. * 1995-03-26 Markus Kuhn
  10. * fixed 500 ms bug at call to set_rtc_mmss, fixed DS12887
  11. * precision CMOS clock update
  12. * 1996-05-03 Ingo Molnar
  13. * fixed time warps in do_[slow|fast]_gettimeoffset()
  14. * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
  15. * "A Kernel Model for Precision Timekeeping" by Dave Mills
  16. *
  17. * Linux/CRIS specific code:
  18. *
  19. * Authors: Bjorn Wesen
  20. * Johan Adolfsson
  21. *
  22. */
  23. #include <asm/rtc.h>
  24. #include <linux/errno.h>
  25. #include <linux/module.h>
  26. #include <linux/param.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/bcd.h>
  29. #include <linux/timex.h>
  30. #include <linux/init.h>
  31. #include <linux/profile.h>
  32. #include <linux/sched.h> /* just for sched_clock() - funny that */
  33. int have_rtc; /* used to remember if we have an RTC or not */;
  34. #define TICK_SIZE tick
  35. extern unsigned long loops_per_jiffy; /* init/main.c */
  36. unsigned long loops_per_usec;
  37. #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
  38. extern unsigned long do_slow_gettimeoffset(void);
  39. static unsigned long (*do_gettimeoffset)(void) = do_slow_gettimeoffset;
  40. u32 arch_gettimeoffset(void)
  41. {
  42. return do_gettimeoffset() * 1000;
  43. }
  44. #endif
  45. /*
  46. * BUG: This routine does not handle hour overflow properly; it just
  47. * sets the minutes. Usually you'll only notice that after reboot!
  48. */
  49. int set_rtc_mmss(unsigned long nowtime)
  50. {
  51. int retval = 0;
  52. int real_seconds, real_minutes, cmos_minutes;
  53. printk(KERN_DEBUG "set_rtc_mmss(%lu)\n", nowtime);
  54. if(!have_rtc)
  55. return 0;
  56. cmos_minutes = CMOS_READ(RTC_MINUTES);
  57. cmos_minutes = bcd2bin(cmos_minutes);
  58. /*
  59. * since we're only adjusting minutes and seconds,
  60. * don't interfere with hour overflow. This avoids
  61. * messing with unknown time zones but requires your
  62. * RTC not to be off by more than 15 minutes
  63. */
  64. real_seconds = nowtime % 60;
  65. real_minutes = nowtime / 60;
  66. if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
  67. real_minutes += 30; /* correct for half hour time zone */
  68. real_minutes %= 60;
  69. if (abs(real_minutes - cmos_minutes) < 30) {
  70. real_seconds = bin2bcd(real_seconds);
  71. real_minutes = bin2bcd(real_minutes);
  72. CMOS_WRITE(real_seconds,RTC_SECONDS);
  73. CMOS_WRITE(real_minutes,RTC_MINUTES);
  74. } else {
  75. printk_once(KERN_NOTICE
  76. "set_rtc_mmss: can't update from %d to %d\n",
  77. cmos_minutes, real_minutes);
  78. retval = -1;
  79. }
  80. return retval;
  81. }
  82. /* grab the time from the RTC chip */
  83. unsigned long
  84. get_cmos_time(void)
  85. {
  86. unsigned int year, mon, day, hour, min, sec;
  87. if(!have_rtc)
  88. return 0;
  89. sec = CMOS_READ(RTC_SECONDS);
  90. min = CMOS_READ(RTC_MINUTES);
  91. hour = CMOS_READ(RTC_HOURS);
  92. day = CMOS_READ(RTC_DAY_OF_MONTH);
  93. mon = CMOS_READ(RTC_MONTH);
  94. year = CMOS_READ(RTC_YEAR);
  95. sec = bcd2bin(sec);
  96. min = bcd2bin(min);
  97. hour = bcd2bin(hour);
  98. day = bcd2bin(day);
  99. mon = bcd2bin(mon);
  100. year = bcd2bin(year);
  101. if ((year += 1900) < 1970)
  102. year += 100;
  103. return mktime(year, mon, day, hour, min, sec);
  104. }
  105. int update_persistent_clock(struct timespec now)
  106. {
  107. return set_rtc_mmss(now.tv_sec);
  108. }
  109. void read_persistent_clock(struct timespec *ts)
  110. {
  111. ts->tv_sec = get_cmos_time();
  112. ts->tv_nsec = 0;
  113. }
  114. extern void cris_profile_sample(struct pt_regs* regs);
  115. void
  116. cris_do_profile(struct pt_regs* regs)
  117. {
  118. #ifdef CONFIG_SYSTEM_PROFILER
  119. cris_profile_sample(regs);
  120. #endif
  121. #ifdef CONFIG_PROFILING
  122. profile_tick(CPU_PROFILING);
  123. #endif
  124. }
  125. unsigned long long sched_clock(void)
  126. {
  127. return (unsigned long long)jiffies * (NSEC_PER_SEC / HZ) +
  128. get_ns_in_jiffie();
  129. }
  130. static int
  131. __init init_udelay(void)
  132. {
  133. loops_per_usec = (loops_per_jiffy * HZ) / 1000000;
  134. return 0;
  135. }
  136. __initcall(init_udelay);