time.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * File: arch/blackfin/kernel/time.c
  3. * Based on: none - original work
  4. * Author:
  5. *
  6. * Created:
  7. * Description: This file contains the bfin-specific time handling details.
  8. * Most of the stuff is located in the machine specific files.
  9. * FIXME: (This file is subject for removal)
  10. *
  11. * Modified:
  12. * Copyright 2004-2008 Analog Devices Inc.
  13. *
  14. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, see the file COPYING, or write
  28. * to the Free Software Foundation, Inc.,
  29. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  30. */
  31. #include <linux/module.h>
  32. #include <linux/profile.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/time.h>
  35. #include <linux/irq.h>
  36. #include <asm/blackfin.h>
  37. #include <asm/time.h>
  38. /* This is an NTP setting */
  39. #define TICK_SIZE (tick_nsec / 1000)
  40. static void time_sched_init(irq_handler_t timer_routine);
  41. static unsigned long gettimeoffset(void);
  42. static struct irqaction bfin_timer_irq = {
  43. .name = "BFIN Timer Tick",
  44. .flags = IRQF_DISABLED
  45. };
  46. static void
  47. time_sched_init(irq_handler_t timer_routine)
  48. {
  49. u32 tcount;
  50. /* power up the timer, but don't enable it just yet */
  51. bfin_write_TCNTL(1);
  52. CSYNC();
  53. /*
  54. * the TSCALE prescaler counter.
  55. */
  56. bfin_write_TSCALE((TIME_SCALE - 1));
  57. tcount = ((get_cclk() / (HZ * TIME_SCALE)) - 1);
  58. bfin_write_TPERIOD(tcount);
  59. bfin_write_TCOUNT(tcount);
  60. /* now enable the timer */
  61. CSYNC();
  62. bfin_write_TCNTL(7);
  63. bfin_timer_irq.handler = (irq_handler_t)timer_routine;
  64. /* call setup_irq instead of request_irq because request_irq calls
  65. * kmalloc which has not been initialized yet
  66. */
  67. setup_irq(IRQ_CORETMR, &bfin_timer_irq);
  68. }
  69. /*
  70. * Should return useconds since last timer tick
  71. */
  72. static unsigned long gettimeoffset(void)
  73. {
  74. unsigned long offset;
  75. unsigned long clocks_per_jiffy;
  76. clocks_per_jiffy = bfin_read_TPERIOD();
  77. offset =
  78. (clocks_per_jiffy -
  79. bfin_read_TCOUNT()) / (((clocks_per_jiffy + 1) * HZ) /
  80. USEC_PER_SEC);
  81. /* Check if we just wrapped the counters and maybe missed a tick */
  82. if ((bfin_read_ILAT() & (1 << IRQ_CORETMR))
  83. && (offset < (100000 / HZ / 2)))
  84. offset += (USEC_PER_SEC / HZ);
  85. return offset;
  86. }
  87. static inline int set_rtc_mmss(unsigned long nowtime)
  88. {
  89. return 0;
  90. }
  91. /*
  92. * timer_interrupt() needs to keep up the real-time clock,
  93. * as well as call the "do_timer()" routine every clocktick
  94. */
  95. #ifdef CONFIG_CORE_TIMER_IRQ_L1
  96. irqreturn_t timer_interrupt(int irq, void *dummy)__attribute__((l1_text));
  97. #endif
  98. irqreturn_t timer_interrupt(int irq, void *dummy)
  99. {
  100. /* last time the cmos clock got updated */
  101. static long last_rtc_update;
  102. write_seqlock(&xtime_lock);
  103. do_timer(1);
  104. profile_tick(CPU_PROFILING);
  105. /*
  106. * If we have an externally synchronized Linux clock, then update
  107. * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
  108. * called as close as possible to 500 ms before the new second starts.
  109. */
  110. if (ntp_synced() &&
  111. xtime.tv_sec > last_rtc_update + 660 &&
  112. (xtime.tv_nsec / NSEC_PER_USEC) >=
  113. 500000 - ((unsigned)TICK_SIZE) / 2
  114. && (xtime.tv_nsec / NSEC_PER_USEC) <=
  115. 500000 + ((unsigned)TICK_SIZE) / 2) {
  116. if (set_rtc_mmss(xtime.tv_sec) == 0)
  117. last_rtc_update = xtime.tv_sec;
  118. else
  119. /* Do it again in 60s. */
  120. last_rtc_update = xtime.tv_sec - 600;
  121. }
  122. write_sequnlock(&xtime_lock);
  123. #ifndef CONFIG_SMP
  124. update_process_times(user_mode(get_irq_regs()));
  125. #endif
  126. return IRQ_HANDLED;
  127. }
  128. void __init time_init(void)
  129. {
  130. time_t secs_since_1970 = (365 * 37 + 9) * 24 * 60 * 60; /* 1 Jan 2007 */
  131. #ifdef CONFIG_RTC_DRV_BFIN
  132. /* [#2663] hack to filter junk RTC values that would cause
  133. * userspace to have to deal with time values greater than
  134. * 2^31 seconds (which uClibc cannot cope with yet)
  135. */
  136. if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) {
  137. printk(KERN_NOTICE "bfin-rtc: invalid date; resetting\n");
  138. bfin_write_RTC_STAT(0);
  139. }
  140. #endif
  141. /* Initialize xtime. From now on, xtime is updated with timer interrupts */
  142. xtime.tv_sec = secs_since_1970;
  143. xtime.tv_nsec = 0;
  144. wall_to_monotonic.tv_sec = -xtime.tv_sec;
  145. time_sched_init(timer_interrupt);
  146. }
  147. #ifndef CONFIG_GENERIC_TIME
  148. void do_gettimeofday(struct timeval *tv)
  149. {
  150. unsigned long flags;
  151. unsigned long seq;
  152. unsigned long usec, sec;
  153. do {
  154. seq = read_seqbegin_irqsave(&xtime_lock, flags);
  155. usec = gettimeoffset();
  156. sec = xtime.tv_sec;
  157. usec += (xtime.tv_nsec / NSEC_PER_USEC);
  158. }
  159. while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
  160. while (usec >= USEC_PER_SEC) {
  161. usec -= USEC_PER_SEC;
  162. sec++;
  163. }
  164. tv->tv_sec = sec;
  165. tv->tv_usec = usec;
  166. }
  167. EXPORT_SYMBOL(do_gettimeofday);
  168. int do_settimeofday(struct timespec *tv)
  169. {
  170. time_t wtm_sec, sec = tv->tv_sec;
  171. long wtm_nsec, nsec = tv->tv_nsec;
  172. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  173. return -EINVAL;
  174. write_seqlock_irq(&xtime_lock);
  175. /*
  176. * This is revolting. We need to set the xtime.tv_usec
  177. * correctly. However, the value in this location is
  178. * is value at the last tick.
  179. * Discover what correction gettimeofday
  180. * would have done, and then undo it!
  181. */
  182. nsec -= (gettimeoffset() * NSEC_PER_USEC);
  183. wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
  184. wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
  185. set_normalized_timespec(&xtime, sec, nsec);
  186. set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
  187. ntp_clear();
  188. write_sequnlock_irq(&xtime_lock);
  189. clock_was_set();
  190. return 0;
  191. }
  192. EXPORT_SYMBOL(do_settimeofday);
  193. #endif /* !CONFIG_GENERIC_TIME */
  194. /*
  195. * Scheduler clock - returns current time in nanosec units.
  196. */
  197. unsigned long long sched_clock(void)
  198. {
  199. return (unsigned long long)jiffies *(NSEC_PER_SEC / HZ);
  200. }