time.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * arch/sh/kernel/time.c
  3. *
  4. * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka
  5. * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
  6. * Copyright (C) 2002 - 2006 Paul Mundt
  7. * Copyright (C) 2002 M. R. Brown <mrbrown@linux-sh.org>
  8. *
  9. * Some code taken from i386 version.
  10. * Copyright (C) 1991, 1992, 1995 Linus Torvalds
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/profile.h>
  16. #include <linux/timex.h>
  17. #include <linux/sched.h>
  18. #include <asm/clock.h>
  19. #include <asm/rtc.h>
  20. #include <asm/timer.h>
  21. #include <asm/kgdb.h>
  22. struct sys_timer *sys_timer;
  23. /* Move this somewhere more sensible.. */
  24. DEFINE_SPINLOCK(rtc_lock);
  25. EXPORT_SYMBOL(rtc_lock);
  26. /* Dummy RTC ops */
  27. static void null_rtc_get_time(struct timespec *tv)
  28. {
  29. tv->tv_sec = mktime(2000, 1, 1, 0, 0, 0);
  30. tv->tv_nsec = 0;
  31. }
  32. static int null_rtc_set_time(const time_t secs)
  33. {
  34. return 0;
  35. }
  36. void (*rtc_sh_get_time)(struct timespec *) = null_rtc_get_time;
  37. int (*rtc_sh_set_time)(const time_t) = null_rtc_set_time;
  38. /*
  39. * Scheduler clock - returns current time in nanosec units.
  40. */
  41. unsigned long long __attribute__ ((weak)) sched_clock(void)
  42. {
  43. return (unsigned long long)jiffies * (1000000000 / HZ);
  44. }
  45. #ifndef CONFIG_GENERIC_TIME
  46. void do_gettimeofday(struct timeval *tv)
  47. {
  48. unsigned long flags;
  49. unsigned long seq;
  50. unsigned long usec, sec;
  51. do {
  52. /*
  53. * Turn off IRQs when grabbing xtime_lock, so that
  54. * the sys_timer get_offset code doesn't have to handle it.
  55. */
  56. seq = read_seqbegin_irqsave(&xtime_lock, flags);
  57. usec = get_timer_offset();
  58. sec = xtime.tv_sec;
  59. usec += xtime.tv_nsec / NSEC_PER_USEC;
  60. } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
  61. while (usec >= 1000000) {
  62. usec -= 1000000;
  63. sec++;
  64. }
  65. tv->tv_sec = sec;
  66. tv->tv_usec = usec;
  67. }
  68. EXPORT_SYMBOL(do_gettimeofday);
  69. int do_settimeofday(struct timespec *tv)
  70. {
  71. time_t wtm_sec, sec = tv->tv_sec;
  72. long wtm_nsec, nsec = tv->tv_nsec;
  73. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  74. return -EINVAL;
  75. write_seqlock_irq(&xtime_lock);
  76. /*
  77. * This is revolting. We need to set "xtime" correctly. However, the
  78. * value in this location is the value at the most recent update of
  79. * wall time. Discover what correction gettimeofday() would have
  80. * made, and then undo it!
  81. */
  82. nsec -= get_timer_offset() * NSEC_PER_USEC;
  83. wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
  84. wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
  85. set_normalized_timespec(&xtime, sec, nsec);
  86. set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
  87. ntp_clear();
  88. write_sequnlock_irq(&xtime_lock);
  89. clock_was_set();
  90. return 0;
  91. }
  92. EXPORT_SYMBOL(do_settimeofday);
  93. #endif /* !CONFIG_GENERIC_TIME */
  94. /* last time the RTC clock got updated */
  95. static long last_rtc_update;
  96. /*
  97. * handle_timer_tick() needs to keep up the real-time clock,
  98. * as well as call the "do_timer()" routine every clocktick
  99. */
  100. void handle_timer_tick(void)
  101. {
  102. do_timer(1);
  103. #ifndef CONFIG_SMP
  104. update_process_times(user_mode(get_irq_regs()));
  105. #endif
  106. if (current->pid)
  107. profile_tick(CPU_PROFILING);
  108. #ifdef CONFIG_HEARTBEAT
  109. if (sh_mv.mv_heartbeat != NULL)
  110. sh_mv.mv_heartbeat();
  111. #endif
  112. /*
  113. * If we have an externally synchronized Linux clock, then update
  114. * RTC clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
  115. * called as close as possible to 500 ms before the new second starts.
  116. */
  117. if (ntp_synced() &&
  118. xtime.tv_sec > last_rtc_update + 660 &&
  119. (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
  120. (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
  121. if (rtc_sh_set_time(xtime.tv_sec) == 0)
  122. last_rtc_update = xtime.tv_sec;
  123. else
  124. /* do it again in 60s */
  125. last_rtc_update = xtime.tv_sec - 600;
  126. }
  127. }
  128. #ifdef CONFIG_PM
  129. int timer_suspend(struct sys_device *dev, pm_message_t state)
  130. {
  131. struct sys_timer *sys_timer = container_of(dev, struct sys_timer, dev);
  132. sys_timer->ops->stop();
  133. return 0;
  134. }
  135. int timer_resume(struct sys_device *dev)
  136. {
  137. struct sys_timer *sys_timer = container_of(dev, struct sys_timer, dev);
  138. sys_timer->ops->start();
  139. return 0;
  140. }
  141. #else
  142. #define timer_suspend NULL
  143. #define timer_resume NULL
  144. #endif
  145. static struct sysdev_class timer_sysclass = {
  146. set_kset_name("timer"),
  147. .suspend = timer_suspend,
  148. .resume = timer_resume,
  149. };
  150. #ifdef CONFIG_NO_IDLE_HZ
  151. static int timer_dyn_tick_enable(void)
  152. {
  153. struct dyn_tick_timer *dyn_tick = sys_timer->dyn_tick;
  154. unsigned long flags;
  155. int ret = -ENODEV;
  156. if (dyn_tick) {
  157. spin_lock_irqsave(&dyn_tick->lock, flags);
  158. ret = 0;
  159. if (!(dyn_tick->state & DYN_TICK_ENABLED)) {
  160. ret = dyn_tick->enable();
  161. if (ret == 0)
  162. dyn_tick->state |= DYN_TICK_ENABLED;
  163. }
  164. spin_unlock_irqrestore(&dyn_tick->lock, flags);
  165. }
  166. return ret;
  167. }
  168. static int timer_dyn_tick_disable(void)
  169. {
  170. struct dyn_tick_timer *dyn_tick = sys_timer->dyn_tick;
  171. unsigned long flags;
  172. int ret = -ENODEV;
  173. if (dyn_tick) {
  174. spin_lock_irqsave(&dyn_tick->lock, flags);
  175. ret = 0;
  176. if (dyn_tick->state & DYN_TICK_ENABLED) {
  177. ret = dyn_tick->disable();
  178. if (ret == 0)
  179. dyn_tick->state &= ~DYN_TICK_ENABLED;
  180. }
  181. spin_unlock_irqrestore(&dyn_tick->lock, flags);
  182. }
  183. return ret;
  184. }
  185. /*
  186. * Reprogram the system timer for at least the calculated time interval.
  187. * This function should be called from the idle thread with IRQs disabled,
  188. * immediately before sleeping.
  189. */
  190. void timer_dyn_reprogram(void)
  191. {
  192. struct dyn_tick_timer *dyn_tick = sys_timer->dyn_tick;
  193. unsigned long next, seq, flags;
  194. if (!dyn_tick)
  195. return;
  196. spin_lock_irqsave(&dyn_tick->lock, flags);
  197. if (dyn_tick->state & DYN_TICK_ENABLED) {
  198. next = next_timer_interrupt();
  199. do {
  200. seq = read_seqbegin(&xtime_lock);
  201. dyn_tick->reprogram(next - jiffies);
  202. } while (read_seqretry(&xtime_lock, seq));
  203. }
  204. spin_unlock_irqrestore(&dyn_tick->lock, flags);
  205. }
  206. static ssize_t timer_show_dyn_tick(struct sys_device *dev, char *buf)
  207. {
  208. return sprintf(buf, "%i\n",
  209. (sys_timer->dyn_tick->state & DYN_TICK_ENABLED) >> 1);
  210. }
  211. static ssize_t timer_set_dyn_tick(struct sys_device *dev, const char *buf,
  212. size_t count)
  213. {
  214. unsigned int enable = simple_strtoul(buf, NULL, 2);
  215. if (enable)
  216. timer_dyn_tick_enable();
  217. else
  218. timer_dyn_tick_disable();
  219. return count;
  220. }
  221. static SYSDEV_ATTR(dyn_tick, 0644, timer_show_dyn_tick, timer_set_dyn_tick);
  222. /*
  223. * dyntick=enable|disable
  224. */
  225. static char dyntick_str[4] __initdata = "";
  226. static int __init dyntick_setup(char *str)
  227. {
  228. if (str)
  229. strlcpy(dyntick_str, str, sizeof(dyntick_str));
  230. return 1;
  231. }
  232. __setup("dyntick=", dyntick_setup);
  233. #endif
  234. static int __init timer_init_sysfs(void)
  235. {
  236. int ret = sysdev_class_register(&timer_sysclass);
  237. if (ret != 0)
  238. return ret;
  239. sys_timer->dev.cls = &timer_sysclass;
  240. ret = sysdev_register(&sys_timer->dev);
  241. #ifdef CONFIG_NO_IDLE_HZ
  242. if (ret == 0 && sys_timer->dyn_tick) {
  243. ret = sysdev_create_file(&sys_timer->dev, &attr_dyn_tick);
  244. /*
  245. * Turn on dynamic tick after calibrate delay
  246. * for correct bogomips
  247. */
  248. if (ret == 0 && dyntick_str[0] == 'e')
  249. ret = timer_dyn_tick_enable();
  250. }
  251. #endif
  252. return ret;
  253. }
  254. device_initcall(timer_init_sysfs);
  255. void (*board_time_init)(void);
  256. void __init time_init(void)
  257. {
  258. if (board_time_init)
  259. board_time_init();
  260. clk_init();
  261. rtc_sh_get_time(&xtime);
  262. set_normalized_timespec(&wall_to_monotonic,
  263. -xtime.tv_sec, -xtime.tv_nsec);
  264. /*
  265. * Find the timer to use as the system timer, it will be
  266. * initialized for us.
  267. */
  268. sys_timer = get_sys_timer();
  269. printk(KERN_INFO "Using %s for system timer\n", sys_timer->name);
  270. #ifdef CONFIG_NO_IDLE_HZ
  271. if (sys_timer->dyn_tick)
  272. spin_lock_init(&sys_timer->dyn_tick->lock);
  273. #endif
  274. #if defined(CONFIG_SH_KGDB)
  275. /*
  276. * Set up kgdb as requested. We do it here because the serial
  277. * init uses the timer vars we just set up for figuring baud.
  278. */
  279. kgdb_init();
  280. #endif
  281. }