time.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #ifdef CONFIG_TICK_SOURCE_SYSTMR0
  30. void 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 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
  62. time_sched_init(irqreturn_t(*timer_routine) (int, void *))
  63. {
  64. #ifdef CONFIG_TICK_SOURCE_SYSTMR0
  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. #ifdef CONFIG_TICK_SOURCE_SYSTMR0
  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. #ifdef CONFIG_TICK_SOURCE_SYSTMR0
  117. if (get_gptimer_status(0) & TIMER_STATUS_TIMIL0) {
  118. #endif
  119. do_timer(1);
  120. /*
  121. * If we have an externally synchronized Linux clock, then update
  122. * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
  123. * called as close as possible to 500 ms before the new second starts.
  124. */
  125. if (ntp_synced() &&
  126. xtime.tv_sec > last_rtc_update + 660 &&
  127. (xtime.tv_nsec / NSEC_PER_USEC) >=
  128. 500000 - ((unsigned)TICK_SIZE) / 2
  129. && (xtime.tv_nsec / NSEC_PER_USEC) <=
  130. 500000 + ((unsigned)TICK_SIZE) / 2) {
  131. if (set_rtc_mmss(xtime.tv_sec) == 0)
  132. last_rtc_update = xtime.tv_sec;
  133. else
  134. /* Do it again in 60s. */
  135. last_rtc_update = xtime.tv_sec - 600;
  136. }
  137. #ifdef CONFIG_TICK_SOURCE_SYSTMR0
  138. set_gptimer_status(0, TIMER_STATUS_TIMIL0);
  139. }
  140. #endif
  141. write_sequnlock(&xtime_lock);
  142. update_process_times(user_mode(get_irq_regs()));
  143. profile_tick(CPU_PROFILING);
  144. return IRQ_HANDLED;
  145. }
  146. void __init time_init(void)
  147. {
  148. time_t secs_since_1970 = (365 * 37 + 9) * 24 * 60 * 60; /* 1 Jan 2007 */
  149. #ifdef CONFIG_RTC_DRV_BFIN
  150. /* [#2663] hack to filter junk RTC values that would cause
  151. * userspace to have to deal with time values greater than
  152. * 2^31 seconds (which uClibc cannot cope with yet)
  153. */
  154. if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) {
  155. printk(KERN_NOTICE "bfin-rtc: invalid date; resetting\n");
  156. bfin_write_RTC_STAT(0);
  157. }
  158. #endif
  159. /* Initialize xtime. From now on, xtime is updated with timer interrupts */
  160. xtime.tv_sec = secs_since_1970;
  161. xtime.tv_nsec = 0;
  162. wall_to_monotonic.tv_sec = -xtime.tv_sec;
  163. time_sched_init(timer_interrupt);
  164. }
  165. #ifndef CONFIG_GENERIC_TIME
  166. void do_gettimeofday(struct timeval *tv)
  167. {
  168. unsigned long flags;
  169. unsigned long seq;
  170. unsigned long usec, sec;
  171. do {
  172. seq = read_seqbegin_irqsave(&xtime_lock, flags);
  173. usec = gettimeoffset();
  174. sec = xtime.tv_sec;
  175. usec += (xtime.tv_nsec / NSEC_PER_USEC);
  176. }
  177. while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
  178. while (usec >= USEC_PER_SEC) {
  179. usec -= USEC_PER_SEC;
  180. sec++;
  181. }
  182. tv->tv_sec = sec;
  183. tv->tv_usec = usec;
  184. }
  185. EXPORT_SYMBOL(do_gettimeofday);
  186. int do_settimeofday(struct timespec *tv)
  187. {
  188. time_t wtm_sec, sec = tv->tv_sec;
  189. long wtm_nsec, nsec = tv->tv_nsec;
  190. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  191. return -EINVAL;
  192. write_seqlock_irq(&xtime_lock);
  193. /*
  194. * This is revolting. We need to set the xtime.tv_usec
  195. * correctly. However, the value in this location is
  196. * is value at the last tick.
  197. * Discover what correction gettimeofday
  198. * would have done, and then undo it!
  199. */
  200. nsec -= (gettimeoffset() * NSEC_PER_USEC);
  201. wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
  202. wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
  203. set_normalized_timespec(&xtime, sec, nsec);
  204. set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
  205. ntp_clear();
  206. write_sequnlock_irq(&xtime_lock);
  207. clock_was_set();
  208. return 0;
  209. }
  210. EXPORT_SYMBOL(do_settimeofday);
  211. #endif /* !CONFIG_GENERIC_TIME */
  212. /*
  213. * Scheduler clock - returns current time in nanosec units.
  214. */
  215. unsigned long long sched_clock(void)
  216. {
  217. return (unsigned long long)jiffies *(NSEC_PER_SEC / HZ);
  218. }