time_64.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * arch/sh/kernel/time_64.c
  3. *
  4. * Copyright (C) 2000, 2001 Paolo Alberelli
  5. * Copyright (C) 2003 - 2007 Paul Mundt
  6. * Copyright (C) 2003 Richard Curnow
  7. *
  8. * Original TMU/RTC code taken from sh version.
  9. * Copyright (C) 1999 Tetsuya Okada & Niibe Yutaka
  10. * Some code taken from i386 version.
  11. * Copyright (C) 1991, 1992, 1995 Linus Torvalds
  12. *
  13. * This file is subject to the terms and conditions of the GNU General Public
  14. * License. See the file "COPYING" in the main directory of this archive
  15. * for more details.
  16. */
  17. #include <linux/errno.h>
  18. #include <linux/rwsem.h>
  19. #include <linux/sched.h>
  20. #include <linux/kernel.h>
  21. #include <linux/param.h>
  22. #include <linux/string.h>
  23. #include <linux/mm.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/time.h>
  26. #include <linux/delay.h>
  27. #include <linux/init.h>
  28. #include <linux/profile.h>
  29. #include <linux/smp.h>
  30. #include <linux/module.h>
  31. #include <linux/bcd.h>
  32. #include <linux/timex.h>
  33. #include <linux/irq.h>
  34. #include <linux/io.h>
  35. #include <linux/platform_device.h>
  36. #include <cpu/registers.h> /* required by inline __asm__ stmt. */
  37. #include <cpu/irq.h>
  38. #include <asm/addrspace.h>
  39. #include <asm/processor.h>
  40. #include <asm/uaccess.h>
  41. #include <asm/delay.h>
  42. #include <asm/clock.h>
  43. #define TMU_TOCR_INIT 0x00
  44. #define TMU0_TCR_INIT 0x0020
  45. #define TMU_TSTR_INIT 1
  46. #define TMU_TSTR_OFF 0
  47. /* Time Management Unit */
  48. #define TMU_BLOCK_OFF 0x01020000
  49. #define TMU_BASE PHYS_PERIPHERAL_BLOCK + TMU_BLOCK_OFF
  50. #define TMU0_BASE tmu_base + 0x8 + (0xc * 0x0)
  51. #define TMU1_BASE tmu_base + 0x8 + (0xc * 0x1)
  52. #define TMU2_BASE tmu_base + 0x8 + (0xc * 0x2)
  53. #define TMU_TOCR tmu_base+0x0 /* Byte access */
  54. #define TMU_TSTR tmu_base+0x4 /* Byte access */
  55. #define TMU0_TCOR TMU0_BASE+0x0 /* Long access */
  56. #define TMU0_TCNT TMU0_BASE+0x4 /* Long access */
  57. #define TMU0_TCR TMU0_BASE+0x8 /* Word access */
  58. #define TICK_SIZE (tick_nsec / 1000)
  59. static unsigned long tmu_base;
  60. /* Variables to allow interpolation of time of day to resolution better than a
  61. * jiffy. */
  62. /* This is effectively protected by xtime_lock */
  63. static unsigned long ctc_last_interrupt;
  64. static unsigned long long usecs_per_jiffy = 1000000/HZ; /* Approximation */
  65. #define CTC_JIFFY_SCALE_SHIFT 40
  66. /* 2**CTC_JIFFY_SCALE_SHIFT / ctc_ticks_per_jiffy */
  67. static unsigned long long scaled_recip_ctc_ticks_per_jiffy;
  68. /* Estimate number of microseconds that have elapsed since the last timer tick,
  69. by scaling the delta that has occurred in the CTC register.
  70. WARNING WARNING WARNING : This algorithm relies on the CTC decrementing at
  71. the CPU clock rate. If the CPU sleeps, the CTC stops counting. Bear this
  72. in mind if enabling SLEEP_WORKS in process.c. In that case, this algorithm
  73. probably needs to use TMU.TCNT0 instead. This will work even if the CPU is
  74. sleeping, though will be coarser.
  75. FIXME : What if usecs_per_tick is moving around too much, e.g. if an adjtime
  76. is running or if the freq or tick arguments of adjtimex are modified after
  77. we have calibrated the scaling factor? This will result in either a jump at
  78. the end of a tick period, or a wrap backwards at the start of the next one,
  79. if the application is reading the time of day often enough. I think we
  80. ought to do better than this. For this reason, usecs_per_jiffy is left
  81. separated out in the calculation below. This allows some future hook into
  82. the adjtime-related stuff in kernel/timer.c to remove this hazard.
  83. */
  84. static unsigned long usecs_since_tick(void)
  85. {
  86. unsigned long long current_ctc;
  87. long ctc_ticks_since_interrupt;
  88. unsigned long long ull_ctc_ticks_since_interrupt;
  89. unsigned long result;
  90. unsigned long long mul1_out;
  91. unsigned long long mul1_out_high;
  92. unsigned long long mul2_out_low, mul2_out_high;
  93. /* Read CTC register */
  94. asm ("getcon cr62, %0" : "=r" (current_ctc));
  95. /* Note, the CTC counts down on each CPU clock, not up.
  96. Note(2), use long type to get correct wraparound arithmetic when
  97. the counter crosses zero. */
  98. ctc_ticks_since_interrupt = (long) ctc_last_interrupt - (long) current_ctc;
  99. ull_ctc_ticks_since_interrupt = (unsigned long long) ctc_ticks_since_interrupt;
  100. /* Inline assembly to do 32x32x32->64 multiplier */
  101. asm volatile ("mulu.l %1, %2, %0" :
  102. "=r" (mul1_out) :
  103. "r" (ull_ctc_ticks_since_interrupt), "r" (usecs_per_jiffy));
  104. mul1_out_high = mul1_out >> 32;
  105. asm volatile ("mulu.l %1, %2, %0" :
  106. "=r" (mul2_out_low) :
  107. "r" (mul1_out), "r" (scaled_recip_ctc_ticks_per_jiffy));
  108. #if 1
  109. asm volatile ("mulu.l %1, %2, %0" :
  110. "=r" (mul2_out_high) :
  111. "r" (mul1_out_high), "r" (scaled_recip_ctc_ticks_per_jiffy));
  112. #endif
  113. result = (unsigned long) (((mul2_out_high << 32) + mul2_out_low) >> CTC_JIFFY_SCALE_SHIFT);
  114. return result;
  115. }
  116. u32 arch_gettimeoffset(void)
  117. {
  118. return usecs_since_tick() * 1000;
  119. }
  120. /* Dummy RTC ops */
  121. static void null_rtc_get_time(struct timespec *tv)
  122. {
  123. tv->tv_sec = mktime(2000, 1, 1, 0, 0, 0);
  124. tv->tv_nsec = 0;
  125. }
  126. static int null_rtc_set_time(const time_t secs)
  127. {
  128. return 0;
  129. }
  130. void (*rtc_sh_get_time)(struct timespec *) = null_rtc_get_time;
  131. int (*rtc_sh_set_time)(const time_t) = null_rtc_set_time;
  132. /* last time the RTC clock got updated */
  133. static long last_rtc_update;
  134. /*
  135. * timer_interrupt() needs to keep up the real-time clock,
  136. * as well as call the "do_timer()" routine every clocktick
  137. */
  138. static inline void do_timer_interrupt(void)
  139. {
  140. unsigned long long current_ctc;
  141. if (current->pid)
  142. profile_tick(CPU_PROFILING);
  143. /*
  144. * Here we are in the timer irq handler. We just have irqs locally
  145. * disabled but we don't know if the timer_bh is running on the other
  146. * CPU. We need to avoid to SMP race with it. NOTE: we don' t need
  147. * the irq version of write_lock because as just said we have irq
  148. * locally disabled. -arca
  149. */
  150. write_seqlock(&xtime_lock);
  151. asm ("getcon cr62, %0" : "=r" (current_ctc));
  152. ctc_last_interrupt = (unsigned long) current_ctc;
  153. do_timer(1);
  154. /*
  155. * If we have an externally synchronized Linux clock, then update
  156. * RTC clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
  157. * called as close as possible to 500 ms before the new second starts.
  158. */
  159. if (ntp_synced() &&
  160. xtime.tv_sec > last_rtc_update + 660 &&
  161. (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
  162. (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
  163. if (rtc_sh_set_time(xtime.tv_sec) == 0)
  164. last_rtc_update = xtime.tv_sec;
  165. else
  166. /* do it again in 60 s */
  167. last_rtc_update = xtime.tv_sec - 600;
  168. }
  169. write_sequnlock(&xtime_lock);
  170. #ifndef CONFIG_SMP
  171. update_process_times(user_mode(get_irq_regs()));
  172. #endif
  173. }
  174. /*
  175. * This is the same as the above, except we _also_ save the current
  176. * Time Stamp Counter value at the time of the timer interrupt, so that
  177. * we later on can estimate the time of day more exactly.
  178. */
  179. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  180. {
  181. unsigned long timer_status;
  182. /* Clear UNF bit */
  183. timer_status = ctrl_inw(TMU0_TCR);
  184. timer_status &= ~0x100;
  185. ctrl_outw(timer_status, TMU0_TCR);
  186. do_timer_interrupt();
  187. return IRQ_HANDLED;
  188. }
  189. static struct irqaction irq0 = {
  190. .handler = timer_interrupt,
  191. .flags = IRQF_DISABLED,
  192. .name = "timer",
  193. };
  194. void __init time_init(void)
  195. {
  196. unsigned long interval;
  197. struct clk *clk;
  198. tmu_base = (unsigned long)ioremap_nocache(TMU_BASE, 1024);
  199. if (!tmu_base) {
  200. panic("Unable to remap TMU\n");
  201. }
  202. clk = clk_get(NULL, "cpu_clk");
  203. scaled_recip_ctc_ticks_per_jiffy = ((1ULL << CTC_JIFFY_SCALE_SHIFT) /
  204. (unsigned long long)(clk_get_rate(clk) / HZ));
  205. rtc_sh_get_time(&xtime);
  206. setup_irq(TIMER_IRQ, &irq0);
  207. clk = clk_get(NULL, "module_clk");
  208. interval = (clk_get_rate(clk)/(HZ*4));
  209. printk("Interval = %ld\n", interval);
  210. /* Start TMU0 */
  211. ctrl_outb(TMU_TSTR_OFF, TMU_TSTR);
  212. ctrl_outb(TMU_TOCR_INIT, TMU_TOCR);
  213. ctrl_outw(TMU0_TCR_INIT, TMU0_TCR);
  214. ctrl_outl(interval, TMU0_TCOR);
  215. ctrl_outl(interval, TMU0_TCNT);
  216. ctrl_outb(TMU_TSTR_INIT, TMU_TSTR);
  217. }