time.c 6.0 KB

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