time.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * arch/blackfin/kernel/time.c
  3. *
  4. * This file contains the Blackfin-specific time handling details.
  5. * Most of the stuff is located in the machine specific files.
  6. *
  7. * Copyright 2004-2008 Analog Devices Inc.
  8. * Licensed under the GPL-2 or later.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/profile.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/time.h>
  14. #include <linux/irq.h>
  15. #include <linux/delay.h>
  16. #include <asm/blackfin.h>
  17. #include <asm/time.h>
  18. #include <asm/gptimers.h>
  19. /* This is an NTP setting */
  20. #define TICK_SIZE (tick_nsec / 1000)
  21. static struct irqaction bfin_timer_irq = {
  22. .name = "Blackfin Timer Tick",
  23. #ifdef CONFIG_IRQ_PER_CPU
  24. .flags = IRQF_DISABLED | IRQF_PERCPU,
  25. #else
  26. .flags = IRQF_DISABLED
  27. #endif
  28. };
  29. #if defined(CONFIG_TICK_SOURCE_SYSTMR0) || defined(CONFIG_IPIPE)
  30. void __init setup_system_timer0(void)
  31. {
  32. /* Power down the core timer, just to play safe. */
  33. bfin_write_TCNTL(0);
  34. disable_gptimers(TIMER0bit);
  35. set_gptimer_status(0, TIMER_STATUS_TRUN0);
  36. while (get_gptimer_status(0) & TIMER_STATUS_TRUN0)
  37. udelay(10);
  38. set_gptimer_config(0, 0x59); /* IRQ enable, periodic, PWM_OUT, SCLKed, OUT PAD disabled */
  39. set_gptimer_period(TIMER0_id, get_sclk() / HZ);
  40. set_gptimer_pwidth(TIMER0_id, 1);
  41. SSYNC();
  42. enable_gptimers(TIMER0bit);
  43. }
  44. #else
  45. void __init setup_core_timer(void)
  46. {
  47. u32 tcount;
  48. /* power up the timer, but don't enable it just yet */
  49. bfin_write_TCNTL(1);
  50. CSYNC();
  51. /* the TSCALE prescaler counter */
  52. bfin_write_TSCALE(TIME_SCALE - 1);
  53. tcount = ((get_cclk() / (HZ * TIME_SCALE)) - 1);
  54. bfin_write_TPERIOD(tcount);
  55. bfin_write_TCOUNT(tcount);
  56. /* now enable the timer */
  57. CSYNC();
  58. bfin_write_TCNTL(7);
  59. }
  60. #endif
  61. static void __init
  62. time_sched_init(irqreturn_t(*timer_routine) (int, void *))
  63. {
  64. #if defined(CONFIG_TICK_SOURCE_SYSTMR0) || defined(CONFIG_IPIPE)
  65. setup_system_timer0();
  66. bfin_timer_irq.handler = timer_routine;
  67. setup_irq(IRQ_TIMER0, &bfin_timer_irq);
  68. #else
  69. setup_core_timer();
  70. bfin_timer_irq.handler = timer_routine;
  71. setup_irq(IRQ_CORETMR, &bfin_timer_irq);
  72. #endif
  73. }
  74. /*
  75. * Should return useconds since last timer tick
  76. */
  77. #ifndef CONFIG_GENERIC_TIME
  78. static unsigned long gettimeoffset(void)
  79. {
  80. unsigned long offset;
  81. unsigned long clocks_per_jiffy;
  82. #if defined(CONFIG_TICK_SOURCE_SYSTMR0) || defined(CONFIG_IPIPE)
  83. clocks_per_jiffy = bfin_read_TIMER0_PERIOD();
  84. offset = bfin_read_TIMER0_COUNTER() / \
  85. (((clocks_per_jiffy + 1) * HZ) / USEC_PER_SEC);
  86. if ((get_gptimer_status(0) & TIMER_STATUS_TIMIL0) && offset < (100000 / HZ / 2))
  87. offset += (USEC_PER_SEC / HZ);
  88. #else
  89. clocks_per_jiffy = bfin_read_TPERIOD();
  90. offset = (clocks_per_jiffy - bfin_read_TCOUNT()) / \
  91. (((clocks_per_jiffy + 1) * HZ) / USEC_PER_SEC);
  92. /* Check if we just wrapped the counters and maybe missed a tick */
  93. if ((bfin_read_ILAT() & (1 << IRQ_CORETMR))
  94. && (offset < (100000 / HZ / 2)))
  95. offset += (USEC_PER_SEC / HZ);
  96. #endif
  97. return offset;
  98. }
  99. #endif
  100. static inline int set_rtc_mmss(unsigned long nowtime)
  101. {
  102. return 0;
  103. }
  104. /*
  105. * timer_interrupt() needs to keep up the real-time clock,
  106. * as well as call the "do_timer()" routine every clocktick
  107. */
  108. #ifdef CONFIG_CORE_TIMER_IRQ_L1
  109. __attribute__((l1_text))
  110. #endif
  111. irqreturn_t timer_interrupt(int irq, void *dummy)
  112. {
  113. /* last time the cmos clock got updated */
  114. static long last_rtc_update;
  115. write_seqlock(&xtime_lock);
  116. #if defined(CONFIG_TICK_SOURCE_SYSTMR0) && !defined(CONFIG_IPIPE)
  117. /*
  118. * TIMIL0 is latched in __ipipe_grab_irq() when the I-Pipe is
  119. * enabled.
  120. */
  121. if (get_gptimer_status(0) & TIMER_STATUS_TIMIL0) {
  122. #endif
  123. do_timer(1);
  124. /*
  125. * If we have an externally synchronized Linux clock, then update
  126. * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
  127. * called as close as possible to 500 ms before the new second starts.
  128. */
  129. if (ntp_synced() &&
  130. xtime.tv_sec > last_rtc_update + 660 &&
  131. (xtime.tv_nsec / NSEC_PER_USEC) >=
  132. 500000 - ((unsigned)TICK_SIZE) / 2
  133. && (xtime.tv_nsec / NSEC_PER_USEC) <=
  134. 500000 + ((unsigned)TICK_SIZE) / 2) {
  135. if (set_rtc_mmss(xtime.tv_sec) == 0)
  136. last_rtc_update = xtime.tv_sec;
  137. else
  138. /* Do it again in 60s. */
  139. last_rtc_update = xtime.tv_sec - 600;
  140. }
  141. #if defined(CONFIG_TICK_SOURCE_SYSTMR0) && !defined(CONFIG_IPIPE)
  142. set_gptimer_status(0, TIMER_STATUS_TIMIL0);
  143. }
  144. #endif
  145. write_sequnlock(&xtime_lock);
  146. #ifdef CONFIG_IPIPE
  147. update_root_process_times(get_irq_regs());
  148. #else
  149. update_process_times(user_mode(get_irq_regs()));
  150. #endif
  151. profile_tick(CPU_PROFILING);
  152. return IRQ_HANDLED;
  153. }
  154. void __init time_init(void)
  155. {
  156. time_t secs_since_1970 = (365 * 37 + 9) * 24 * 60 * 60; /* 1 Jan 2007 */
  157. #ifdef CONFIG_RTC_DRV_BFIN
  158. /* [#2663] hack to filter junk RTC values that would cause
  159. * userspace to have to deal with time values greater than
  160. * 2^31 seconds (which uClibc cannot cope with yet)
  161. */
  162. if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) {
  163. printk(KERN_NOTICE "bfin-rtc: invalid date; resetting\n");
  164. bfin_write_RTC_STAT(0);
  165. }
  166. #endif
  167. /* Initialize xtime. From now on, xtime is updated with timer interrupts */
  168. xtime.tv_sec = secs_since_1970;
  169. xtime.tv_nsec = 0;
  170. wall_to_monotonic.tv_sec = -xtime.tv_sec;
  171. time_sched_init(timer_interrupt);
  172. }
  173. #ifndef CONFIG_GENERIC_TIME
  174. void do_gettimeofday(struct timeval *tv)
  175. {
  176. unsigned long flags;
  177. unsigned long seq;
  178. unsigned long usec, sec;
  179. do {
  180. seq = read_seqbegin_irqsave(&xtime_lock, flags);
  181. usec = gettimeoffset();
  182. sec = xtime.tv_sec;
  183. usec += (xtime.tv_nsec / NSEC_PER_USEC);
  184. }
  185. while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
  186. while (usec >= USEC_PER_SEC) {
  187. usec -= USEC_PER_SEC;
  188. sec++;
  189. }
  190. tv->tv_sec = sec;
  191. tv->tv_usec = usec;
  192. }
  193. EXPORT_SYMBOL(do_gettimeofday);
  194. int do_settimeofday(struct timespec *tv)
  195. {
  196. time_t wtm_sec, sec = tv->tv_sec;
  197. long wtm_nsec, nsec = tv->tv_nsec;
  198. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  199. return -EINVAL;
  200. write_seqlock_irq(&xtime_lock);
  201. /*
  202. * This is revolting. We need to set the xtime.tv_usec
  203. * correctly. However, the value in this location is
  204. * is value at the last tick.
  205. * Discover what correction gettimeofday
  206. * would have done, and then undo it!
  207. */
  208. nsec -= (gettimeoffset() * NSEC_PER_USEC);
  209. wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
  210. wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
  211. set_normalized_timespec(&xtime, sec, nsec);
  212. set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
  213. ntp_clear();
  214. write_sequnlock_irq(&xtime_lock);
  215. clock_was_set();
  216. return 0;
  217. }
  218. EXPORT_SYMBOL(do_settimeofday);
  219. #endif /* !CONFIG_GENERIC_TIME */
  220. /*
  221. * Scheduler clock - returns current time in nanosec units.
  222. */
  223. unsigned long long sched_clock(void)
  224. {
  225. return (unsigned long long)jiffies *(NSEC_PER_SEC / HZ);
  226. }