time.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * linux/arch/h8300/kernel/time.c
  3. *
  4. * Yoshinori Sato <ysato@users.sourceforge.jp>
  5. *
  6. * Copied/hacked from:
  7. *
  8. * linux/arch/m68k/kernel/time.c
  9. *
  10. * Copyright (C) 1991, 1992, 1995 Linus Torvalds
  11. *
  12. * This file contains the m68k-specific time handling details.
  13. * Most of the stuff is located in the machine specific files.
  14. *
  15. * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
  16. * "A Kernel Model for Precision Timekeeping" by Dave Mills
  17. */
  18. #include <linux/config.h> /* CONFIG_HEARTBEAT */
  19. #include <linux/errno.h>
  20. #include <linux/module.h>
  21. #include <linux/sched.h>
  22. #include <linux/kernel.h>
  23. #include <linux/param.h>
  24. #include <linux/string.h>
  25. #include <linux/mm.h>
  26. #include <linux/timex.h>
  27. #include <linux/profile.h>
  28. #include <asm/io.h>
  29. #include <asm/target_time.h>
  30. #define TICK_SIZE (tick_nsec / 1000)
  31. u64 jiffies_64;
  32. EXPORT_SYMBOL(jiffies_64);
  33. /*
  34. * timer_interrupt() needs to keep up the real-time clock,
  35. * as well as call the "do_timer()" routine every clocktick
  36. */
  37. static void timer_interrupt(int irq, void *dummy, struct pt_regs * regs)
  38. {
  39. /* may need to kick the hardware timer */
  40. platform_timer_eoi();
  41. do_timer(regs);
  42. #ifndef CONFIG_SMP
  43. update_process_times(user_mode(regs));
  44. #endif
  45. profile_tick(CPU_PROFILING, regs);
  46. }
  47. void time_init(void)
  48. {
  49. unsigned int year, mon, day, hour, min, sec;
  50. /* FIX by dqg : Set to zero for platforms that don't have tod */
  51. /* without this time is undefined and can overflow time_t, causing */
  52. /* very stange errors */
  53. year = 1980;
  54. mon = day = 1;
  55. hour = min = sec = 0;
  56. platform_gettod (&year, &mon, &day, &hour, &min, &sec);
  57. if ((year += 1900) < 1970)
  58. year += 100;
  59. xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
  60. xtime.tv_nsec = 0;
  61. platform_timer_setup(timer_interrupt);
  62. }
  63. /*
  64. * This version of gettimeofday has near microsecond resolution.
  65. */
  66. void do_gettimeofday(struct timeval *tv)
  67. {
  68. unsigned long flags;
  69. unsigned long usec, sec;
  70. read_lock_irqsave(&xtime_lock, flags);
  71. usec = 0;
  72. sec = xtime.tv_sec;
  73. usec += (xtime.tv_nsec / 1000);
  74. read_unlock_irqrestore(&xtime_lock, flags);
  75. while (usec >= 1000000) {
  76. usec -= 1000000;
  77. sec++;
  78. }
  79. tv->tv_sec = sec;
  80. tv->tv_usec = usec;
  81. }
  82. EXPORT_SYMBOL(do_gettimeofday);
  83. int do_settimeofday(struct timespec *tv)
  84. {
  85. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  86. return -EINVAL;
  87. write_lock_irq(&xtime_lock);
  88. /* This is revolting. We need to set the xtime.tv_usec
  89. * correctly. However, the value in this location is
  90. * is value at the last tick.
  91. * Discover what correction gettimeofday
  92. * would have done, and then undo it!
  93. */
  94. while (tv->tv_nsec < 0) {
  95. tv->tv_nsec += NSEC_PER_SEC;
  96. tv->tv_sec--;
  97. }
  98. xtime.tv_sec = tv->tv_sec;
  99. xtime.tv_nsec = tv->tv_nsec;
  100. ntp_clear();
  101. write_sequnlock_irq(&xtime_lock);
  102. clock_was_set();
  103. return 0;
  104. }
  105. EXPORT_SYMBOL(do_settimeofday);
  106. unsigned long long sched_clock(void)
  107. {
  108. return (unsigned long long)jiffies * (1000000000 / HZ);
  109. }