time.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * linux/arch/arm/kernel/time.c
  3. *
  4. * Copyright (C) 1991, 1992, 1995 Linus Torvalds
  5. * Modifications for ARM (C) 1994-2001 Russell King
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This file contains the ARM-specific time handling details:
  12. * reading the RTC at bootup, etc...
  13. */
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/time.h>
  18. #include <linux/init.h>
  19. #include <linux/sched.h>
  20. #include <linux/smp.h>
  21. #include <linux/timex.h>
  22. #include <linux/errno.h>
  23. #include <linux/profile.h>
  24. #include <linux/sysdev.h>
  25. #include <linux/timer.h>
  26. #include <linux/irq.h>
  27. #include <linux/mc146818rtc.h>
  28. #include <asm/leds.h>
  29. #include <asm/thread_info.h>
  30. #include <asm/stacktrace.h>
  31. #include <asm/mach/time.h>
  32. /*
  33. * Our system timer.
  34. */
  35. struct sys_timer *system_timer;
  36. #if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE)
  37. /* this needs a better home */
  38. DEFINE_SPINLOCK(rtc_lock);
  39. #ifdef CONFIG_RTC_DRV_CMOS_MODULE
  40. EXPORT_SYMBOL(rtc_lock);
  41. #endif
  42. #endif /* pc-style 'CMOS' RTC support */
  43. /* change this if you have some constant time drift */
  44. #define USECS_PER_JIFFY (1000000/HZ)
  45. #ifdef CONFIG_SMP
  46. unsigned long profile_pc(struct pt_regs *regs)
  47. {
  48. struct stackframe frame;
  49. if (!in_lock_functions(regs->ARM_pc))
  50. return regs->ARM_pc;
  51. frame.fp = regs->ARM_fp;
  52. frame.sp = regs->ARM_sp;
  53. frame.lr = regs->ARM_lr;
  54. frame.pc = regs->ARM_pc;
  55. do {
  56. int ret = unwind_frame(&frame);
  57. if (ret < 0)
  58. return 0;
  59. } while (in_lock_functions(frame.pc));
  60. return frame.pc;
  61. }
  62. EXPORT_SYMBOL(profile_pc);
  63. #endif
  64. #ifndef CONFIG_GENERIC_TIME
  65. static unsigned long dummy_gettimeoffset(void)
  66. {
  67. return 0;
  68. }
  69. #endif
  70. #ifdef CONFIG_LEDS_TIMER
  71. static inline void do_leds(void)
  72. {
  73. static unsigned int count = HZ/2;
  74. if (--count == 0) {
  75. count = HZ/2;
  76. leds_event(led_timer);
  77. }
  78. }
  79. #else
  80. #define do_leds()
  81. #endif
  82. #ifndef CONFIG_GENERIC_TIME
  83. void do_gettimeofday(struct timeval *tv)
  84. {
  85. unsigned long flags;
  86. unsigned long seq;
  87. unsigned long usec, sec;
  88. do {
  89. seq = read_seqbegin_irqsave(&xtime_lock, flags);
  90. usec = system_timer->offset();
  91. sec = xtime.tv_sec;
  92. usec += xtime.tv_nsec / 1000;
  93. } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
  94. /* usec may have gone up a lot: be safe */
  95. while (usec >= 1000000) {
  96. usec -= 1000000;
  97. sec++;
  98. }
  99. tv->tv_sec = sec;
  100. tv->tv_usec = usec;
  101. }
  102. EXPORT_SYMBOL(do_gettimeofday);
  103. int do_settimeofday(struct timespec *tv)
  104. {
  105. time_t wtm_sec, sec = tv->tv_sec;
  106. long wtm_nsec, nsec = tv->tv_nsec;
  107. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  108. return -EINVAL;
  109. write_seqlock_irq(&xtime_lock);
  110. /*
  111. * This is revolting. We need to set "xtime" correctly. However, the
  112. * value in this location is the value at the most recent update of
  113. * wall time. Discover what correction gettimeofday() would have
  114. * done, and then undo it!
  115. */
  116. nsec -= system_timer->offset() * NSEC_PER_USEC;
  117. wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
  118. wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
  119. set_normalized_timespec(&xtime, sec, nsec);
  120. set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
  121. ntp_clear();
  122. write_sequnlock_irq(&xtime_lock);
  123. clock_was_set();
  124. return 0;
  125. }
  126. EXPORT_SYMBOL(do_settimeofday);
  127. #endif /* !CONFIG_GENERIC_TIME */
  128. #ifndef CONFIG_GENERIC_CLOCKEVENTS
  129. /*
  130. * Kernel system timer support.
  131. */
  132. void timer_tick(void)
  133. {
  134. profile_tick(CPU_PROFILING);
  135. do_leds();
  136. write_seqlock(&xtime_lock);
  137. do_timer(1);
  138. write_sequnlock(&xtime_lock);
  139. #ifndef CONFIG_SMP
  140. update_process_times(user_mode(get_irq_regs()));
  141. #endif
  142. }
  143. #endif
  144. #if defined(CONFIG_PM) && !defined(CONFIG_GENERIC_CLOCKEVENTS)
  145. static int timer_suspend(struct sys_device *dev, pm_message_t state)
  146. {
  147. struct sys_timer *timer = container_of(dev, struct sys_timer, dev);
  148. if (timer->suspend != NULL)
  149. timer->suspend();
  150. return 0;
  151. }
  152. static int timer_resume(struct sys_device *dev)
  153. {
  154. struct sys_timer *timer = container_of(dev, struct sys_timer, dev);
  155. if (timer->resume != NULL)
  156. timer->resume();
  157. return 0;
  158. }
  159. #else
  160. #define timer_suspend NULL
  161. #define timer_resume NULL
  162. #endif
  163. static struct sysdev_class timer_sysclass = {
  164. .name = "timer",
  165. .suspend = timer_suspend,
  166. .resume = timer_resume,
  167. };
  168. static int __init timer_init_sysfs(void)
  169. {
  170. int ret = sysdev_class_register(&timer_sysclass);
  171. if (ret == 0) {
  172. system_timer->dev.cls = &timer_sysclass;
  173. ret = sysdev_register(&system_timer->dev);
  174. }
  175. return ret;
  176. }
  177. device_initcall(timer_init_sysfs);
  178. void __init time_init(void)
  179. {
  180. #ifndef CONFIG_GENERIC_TIME
  181. if (system_timer->offset == NULL)
  182. system_timer->offset = dummy_gettimeoffset;
  183. #endif
  184. system_timer->init();
  185. }