time.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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/errno.h>
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/sched.h>
  22. #include <linux/kernel.h>
  23. #include <linux/param.h>
  24. #include <linux/string.h>
  25. #include <linux/mm.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/profile.h>
  28. #include <asm/io.h>
  29. #include <asm/m32r.h>
  30. #include <asm/hw_irq.h>
  31. #if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE)
  32. /* this needs a better home */
  33. DEFINE_SPINLOCK(rtc_lock);
  34. #ifdef CONFIG_RTC_DRV_CMOS_MODULE
  35. EXPORT_SYMBOL(rtc_lock);
  36. #endif
  37. #endif /* pc-style 'CMOS' RTC support */
  38. #ifdef CONFIG_SMP
  39. extern void smp_local_timer_interrupt(void);
  40. #endif
  41. #define TICK_SIZE (tick_nsec / 1000)
  42. /*
  43. * Change this if you have some constant time drift
  44. */
  45. /* This is for machines which generate the exact clock. */
  46. #define USECS_PER_JIFFY (1000000/HZ)
  47. static unsigned long latch;
  48. u32 arch_gettimeoffset(void)
  49. {
  50. unsigned long elapsed_time = 0; /* [us] */
  51. #if defined(CONFIG_CHIP_M32102) || defined(CONFIG_CHIP_XNUX2) \
  52. || defined(CONFIG_CHIP_VDEC2) || defined(CONFIG_CHIP_M32700) \
  53. || defined(CONFIG_CHIP_OPSP) || defined(CONFIG_CHIP_M32104)
  54. #ifndef CONFIG_SMP
  55. unsigned long count;
  56. /* timer count may underflow right here */
  57. count = inl(M32R_MFT2CUT_PORTL);
  58. if (inl(M32R_ICU_CR18_PORTL) & 0x00000100) /* underflow check */
  59. count = 0;
  60. count = (latch - count) * TICK_SIZE;
  61. elapsed_time = DIV_ROUND_CLOSEST(count, latch);
  62. /* NOTE: LATCH is equal to the "interval" value (= reload count). */
  63. #else /* CONFIG_SMP */
  64. unsigned long count;
  65. static unsigned long p_jiffies = -1;
  66. static unsigned long p_count = 0;
  67. /* timer count may underflow right here */
  68. count = inl(M32R_MFT2CUT_PORTL);
  69. if (jiffies == p_jiffies && count > p_count)
  70. count = 0;
  71. p_jiffies = jiffies;
  72. p_count = count;
  73. count = (latch - count) * TICK_SIZE;
  74. elapsed_time = DIV_ROUND_CLOSEST(count, latch);
  75. /* NOTE: LATCH is equal to the "interval" value (= reload count). */
  76. #endif /* CONFIG_SMP */
  77. #elif defined(CONFIG_CHIP_M32310)
  78. #warning do_gettimeoffse not implemented
  79. #else
  80. #error no chip configuration
  81. #endif
  82. return elapsed_time * 1000;
  83. }
  84. /*
  85. * In order to set the CMOS clock precisely, set_rtc_mmss has to be
  86. * called 500 ms after the second nowtime has started, because when
  87. * nowtime is written into the registers of the CMOS clock, it will
  88. * jump to the next second precisely 500 ms later. Check the Motorola
  89. * MC146818A or Dallas DS12887 data sheet for details.
  90. *
  91. * BUG: This routine does not handle hour overflow properly; it just
  92. * sets the minutes. Usually you won't notice until after reboot!
  93. */
  94. static inline int set_rtc_mmss(unsigned long nowtime)
  95. {
  96. return 0;
  97. }
  98. /* last time the cmos clock got updated */
  99. static long last_rtc_update = 0;
  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. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  105. {
  106. #ifndef CONFIG_SMP
  107. profile_tick(CPU_PROFILING);
  108. #endif
  109. /* XXX FIXME. Uh, the xtime_lock should be held here, no? */
  110. do_timer(1);
  111. #ifndef CONFIG_SMP
  112. update_process_times(user_mode(get_irq_regs()));
  113. #endif
  114. /*
  115. * If we have an externally synchronized Linux clock, then update
  116. * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
  117. * called as close as possible to 500 ms before the new second starts.
  118. */
  119. write_seqlock(&xtime_lock);
  120. if (ntp_synced()
  121. && xtime.tv_sec > last_rtc_update + 660
  122. && (xtime.tv_nsec / 1000) >= 500000 - ((unsigned)TICK_SIZE) / 2
  123. && (xtime.tv_nsec / 1000) <= 500000 + ((unsigned)TICK_SIZE) / 2)
  124. {
  125. if (set_rtc_mmss(xtime.tv_sec) == 0)
  126. last_rtc_update = xtime.tv_sec;
  127. else /* do it again in 60 s */
  128. last_rtc_update = xtime.tv_sec - 600;
  129. }
  130. write_sequnlock(&xtime_lock);
  131. /* As we return to user mode fire off the other CPU schedulers..
  132. this is basically because we don't yet share IRQ's around.
  133. This message is rigged to be safe on the 386 - basically it's
  134. a hack, so don't look closely for now.. */
  135. #ifdef CONFIG_SMP
  136. smp_local_timer_interrupt();
  137. smp_send_timer();
  138. #endif
  139. return IRQ_HANDLED;
  140. }
  141. static struct irqaction irq0 = {
  142. .handler = timer_interrupt,
  143. .flags = IRQF_DISABLED,
  144. .name = "MFT2",
  145. };
  146. void __init time_init(void)
  147. {
  148. unsigned int epoch, year, mon, day, hour, min, sec;
  149. sec = min = hour = day = mon = year = 0;
  150. epoch = 0;
  151. year = 23;
  152. mon = 4;
  153. day = 17;
  154. /* Attempt to guess the epoch. This is the same heuristic as in rtc.c
  155. so no stupid things will happen to timekeeping. Who knows, maybe
  156. Ultrix also uses 1952 as epoch ... */
  157. if (year > 10 && year < 44)
  158. epoch = 1980;
  159. else if (year < 96)
  160. epoch = 1952;
  161. year += epoch;
  162. xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
  163. xtime.tv_nsec = (INITIAL_JIFFIES % HZ) * (NSEC_PER_SEC / HZ);
  164. set_normalized_timespec(&wall_to_monotonic,
  165. -xtime.tv_sec, -xtime.tv_nsec);
  166. #if defined(CONFIG_CHIP_M32102) || defined(CONFIG_CHIP_XNUX2) \
  167. || defined(CONFIG_CHIP_VDEC2) || defined(CONFIG_CHIP_M32700) \
  168. || defined(CONFIG_CHIP_OPSP) || defined(CONFIG_CHIP_M32104)
  169. /* M32102 MFT setup */
  170. setup_irq(M32R_IRQ_MFT2, &irq0);
  171. {
  172. unsigned long bus_clock;
  173. unsigned short divide;
  174. bus_clock = boot_cpu_data.bus_clock;
  175. divide = boot_cpu_data.timer_divide;
  176. latch = DIV_ROUND_CLOSEST(bus_clock/divide, HZ);
  177. printk("Timer start : latch = %ld\n", latch);
  178. outl((M32R_MFTMOD_CC_MASK | M32R_MFTMOD_TCCR \
  179. |M32R_MFTMOD_CSSEL011), M32R_MFT2MOD_PORTL);
  180. outl(latch, M32R_MFT2RLD_PORTL);
  181. outl(latch, M32R_MFT2CUT_PORTL);
  182. outl(0, M32R_MFT2CMPRLD_PORTL);
  183. outl((M32R_MFTCR_MFT2MSK|M32R_MFTCR_MFT2EN), M32R_MFTCR_PORTL);
  184. }
  185. #elif defined(CONFIG_CHIP_M32310)
  186. #warning time_init not implemented
  187. #else
  188. #error no chip configuration
  189. #endif
  190. }