time.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * linux/arch/m32r/kernel/time.c
  3. *
  4. * Copyright (c) 2001, 2002 Hiroyuki Kondo, Hirokazu Takata,
  5. * Hitoshi Yamamoto
  6. * Taken from i386 version.
  7. * Copyright (C) 1991, 1992, 1995 Linus Torvalds
  8. * Copyright (C) 1996, 1997, 1998 Ralf Baechle
  9. *
  10. * This file contains the time handling details for PC-style clocks as
  11. * found in some MIPS systems.
  12. *
  13. * Some code taken from sh version.
  14. * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka
  15. * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
  16. */
  17. #undef DEBUG_TIMER
  18. #include <linux/config.h>
  19. #include <linux/errno.h>
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/sched.h>
  23. #include <linux/kernel.h>
  24. #include <linux/param.h>
  25. #include <linux/string.h>
  26. #include <linux/mm.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/profile.h>
  29. #include <asm/io.h>
  30. #include <asm/m32r.h>
  31. #include <asm/hw_irq.h>
  32. #ifdef CONFIG_SMP
  33. extern void send_IPI_allbutself(int, int);
  34. extern void smp_local_timer_interrupt(struct pt_regs *);
  35. #endif
  36. extern unsigned long wall_jiffies;
  37. #define TICK_SIZE (tick_nsec / 1000)
  38. /*
  39. * Change this if you have some constant time drift
  40. */
  41. /* This is for machines which generate the exact clock. */
  42. #define USECS_PER_JIFFY (1000000/HZ)
  43. static unsigned long latch;
  44. static unsigned long do_gettimeoffset(void)
  45. {
  46. unsigned long elapsed_time = 0; /* [us] */
  47. #if defined(CONFIG_CHIP_M32102) || defined(CONFIG_CHIP_XNUX2) \
  48. || defined(CONFIG_CHIP_VDEC2) || defined(CONFIG_CHIP_M32700) \
  49. || defined(CONFIG_CHIP_OPSP) || defined(CONFIG_CHIP_M32104)
  50. #ifndef CONFIG_SMP
  51. unsigned long count;
  52. /* timer count may underflow right here */
  53. count = inl(M32R_MFT2CUT_PORTL);
  54. if (inl(M32R_ICU_CR18_PORTL) & 0x00000100) /* underflow check */
  55. count = 0;
  56. count = (latch - count) * TICK_SIZE;
  57. elapsed_time = (count + latch / 2) / latch;
  58. /* NOTE: LATCH is equal to the "interval" value (= reload count). */
  59. #else /* CONFIG_SMP */
  60. unsigned long count;
  61. static unsigned long p_jiffies = -1;
  62. static unsigned long p_count = 0;
  63. /* timer count may underflow right here */
  64. count = inl(M32R_MFT2CUT_PORTL);
  65. if (jiffies == p_jiffies && count > p_count)
  66. count = 0;
  67. p_jiffies = jiffies;
  68. p_count = count;
  69. count = (latch - count) * TICK_SIZE;
  70. elapsed_time = (count + latch / 2) / latch;
  71. /* NOTE: LATCH is equal to the "interval" value (= reload count). */
  72. #endif /* CONFIG_SMP */
  73. #elif defined(CONFIG_CHIP_M32310)
  74. #warning do_gettimeoffse not implemented
  75. #else
  76. #error no chip configuration
  77. #endif
  78. return elapsed_time;
  79. }
  80. /*
  81. * This version of gettimeofday has near microsecond resolution.
  82. */
  83. void do_gettimeofday(struct timeval *tv)
  84. {
  85. unsigned long seq;
  86. unsigned long usec, sec;
  87. unsigned long max_ntp_tick = tick_usec - tickadj;
  88. do {
  89. unsigned long lost;
  90. seq = read_seqbegin(&xtime_lock);
  91. usec = do_gettimeoffset();
  92. lost = jiffies - wall_jiffies;
  93. /*
  94. * If time_adjust is negative then NTP is slowing the clock
  95. * so make sure not to go into next possible interval.
  96. * Better to lose some accuracy than have time go backwards..
  97. */
  98. if (unlikely(time_adjust < 0)) {
  99. usec = min(usec, max_ntp_tick);
  100. if (lost)
  101. usec += lost * max_ntp_tick;
  102. } else if (unlikely(lost))
  103. usec += lost * tick_usec;
  104. sec = xtime.tv_sec;
  105. usec += (xtime.tv_nsec / 1000);
  106. } while (read_seqretry(&xtime_lock, seq));
  107. while (usec >= 1000000) {
  108. usec -= 1000000;
  109. sec++;
  110. }
  111. tv->tv_sec = sec;
  112. tv->tv_usec = usec;
  113. }
  114. EXPORT_SYMBOL(do_gettimeofday);
  115. int do_settimeofday(struct timespec *tv)
  116. {
  117. time_t wtm_sec, sec = tv->tv_sec;
  118. long wtm_nsec, nsec = tv->tv_nsec;
  119. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  120. return -EINVAL;
  121. write_seqlock_irq(&xtime_lock);
  122. /*
  123. * This is revolting. We need to set "xtime" correctly. However, the
  124. * value in this location is the value at the most recent update of
  125. * wall time. Discover what correction gettimeofday() would have
  126. * made, and then undo it!
  127. */
  128. nsec -= do_gettimeoffset() * NSEC_PER_USEC;
  129. nsec -= (jiffies - wall_jiffies) * TICK_NSEC;
  130. wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
  131. wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
  132. set_normalized_timespec(&xtime, sec, nsec);
  133. set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
  134. ntp_clear();
  135. write_sequnlock_irq(&xtime_lock);
  136. clock_was_set();
  137. return 0;
  138. }
  139. EXPORT_SYMBOL(do_settimeofday);
  140. /*
  141. * In order to set the CMOS clock precisely, set_rtc_mmss has to be
  142. * called 500 ms after the second nowtime has started, because when
  143. * nowtime is written into the registers of the CMOS clock, it will
  144. * jump to the next second precisely 500 ms later. Check the Motorola
  145. * MC146818A or Dallas DS12887 data sheet for details.
  146. *
  147. * BUG: This routine does not handle hour overflow properly; it just
  148. * sets the minutes. Usually you won't notice until after reboot!
  149. */
  150. static inline int set_rtc_mmss(unsigned long nowtime)
  151. {
  152. return 0;
  153. }
  154. /* last time the cmos clock got updated */
  155. static long last_rtc_update = 0;
  156. /*
  157. * timer_interrupt() needs to keep up the real-time clock,
  158. * as well as call the "do_timer()" routine every clocktick
  159. */
  160. irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  161. {
  162. #ifndef CONFIG_SMP
  163. profile_tick(CPU_PROFILING, regs);
  164. #endif
  165. do_timer(regs);
  166. #ifndef CONFIG_SMP
  167. update_process_times(user_mode(regs));
  168. #endif
  169. /*
  170. * If we have an externally synchronized Linux clock, then update
  171. * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
  172. * called as close as possible to 500 ms before the new second starts.
  173. */
  174. write_seqlock(&xtime_lock);
  175. if (ntp_synced()
  176. && xtime.tv_sec > last_rtc_update + 660
  177. && (xtime.tv_nsec / 1000) >= 500000 - ((unsigned)TICK_SIZE) / 2
  178. && (xtime.tv_nsec / 1000) <= 500000 + ((unsigned)TICK_SIZE) / 2)
  179. {
  180. if (set_rtc_mmss(xtime.tv_sec) == 0)
  181. last_rtc_update = xtime.tv_sec;
  182. else /* do it again in 60 s */
  183. last_rtc_update = xtime.tv_sec - 600;
  184. }
  185. write_sequnlock(&xtime_lock);
  186. /* As we return to user mode fire off the other CPU schedulers..
  187. this is basically because we don't yet share IRQ's around.
  188. This message is rigged to be safe on the 386 - basically it's
  189. a hack, so don't look closely for now.. */
  190. #ifdef CONFIG_SMP
  191. smp_local_timer_interrupt(regs);
  192. smp_send_timer();
  193. #endif
  194. return IRQ_HANDLED;
  195. }
  196. struct irqaction irq0 = { timer_interrupt, SA_INTERRUPT, CPU_MASK_NONE,
  197. "MFT2", NULL, NULL };
  198. void __init time_init(void)
  199. {
  200. unsigned int epoch, year, mon, day, hour, min, sec;
  201. sec = min = hour = day = mon = year = 0;
  202. epoch = 0;
  203. year = 23;
  204. mon = 4;
  205. day = 17;
  206. /* Attempt to guess the epoch. This is the same heuristic as in rtc.c
  207. so no stupid things will happen to timekeeping. Who knows, maybe
  208. Ultrix also uses 1952 as epoch ... */
  209. if (year > 10 && year < 44)
  210. epoch = 1980;
  211. else if (year < 96)
  212. epoch = 1952;
  213. year += epoch;
  214. xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
  215. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  216. set_normalized_timespec(&wall_to_monotonic,
  217. -xtime.tv_sec, -xtime.tv_nsec);
  218. #if defined(CONFIG_CHIP_M32102) || defined(CONFIG_CHIP_XNUX2) \
  219. || defined(CONFIG_CHIP_VDEC2) || defined(CONFIG_CHIP_M32700) \
  220. || defined(CONFIG_CHIP_OPSP) || defined(CONFIG_CHIP_M32104)
  221. /* M32102 MFT setup */
  222. setup_irq(M32R_IRQ_MFT2, &irq0);
  223. {
  224. unsigned long bus_clock;
  225. unsigned short divide;
  226. bus_clock = boot_cpu_data.bus_clock;
  227. divide = boot_cpu_data.timer_divide;
  228. latch = (bus_clock/divide + HZ / 2) / HZ;
  229. printk("Timer start : latch = %ld\n", latch);
  230. outl((M32R_MFTMOD_CC_MASK | M32R_MFTMOD_TCCR \
  231. |M32R_MFTMOD_CSSEL011), M32R_MFT2MOD_PORTL);
  232. outl(latch, M32R_MFT2RLD_PORTL);
  233. outl(latch, M32R_MFT2CUT_PORTL);
  234. outl(0, M32R_MFT2CMPRLD_PORTL);
  235. outl((M32R_MFTCR_MFT2MSK|M32R_MFTCR_MFT2EN), M32R_MFTCR_PORTL);
  236. }
  237. #elif defined(CONFIG_CHIP_M32310)
  238. #warning time_init not implemented
  239. #else
  240. #error no chip configuration
  241. #endif
  242. }
  243. /*
  244. * Scheduler clock - returns current time in nanosec units.
  245. */
  246. unsigned long long sched_clock(void)
  247. {
  248. return (unsigned long long)jiffies * (1000000000 / HZ);
  249. }