time.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 <linux/sched.h>
  17. #include <asm/blackfin.h>
  18. #include <asm/time.h>
  19. #include <asm/gptimers.h>
  20. /* This is an NTP setting */
  21. #define TICK_SIZE (tick_nsec / 1000)
  22. static struct irqaction bfin_timer_irq = {
  23. .name = "Blackfin Timer Tick",
  24. .flags = IRQF_DISABLED
  25. };
  26. #if defined(CONFIG_IPIPE)
  27. void __init setup_system_timer0(void)
  28. {
  29. /* Power down the core timer, just to play safe. */
  30. bfin_write_TCNTL(0);
  31. disable_gptimers(TIMER0bit);
  32. set_gptimer_status(0, TIMER_STATUS_TRUN0);
  33. while (get_gptimer_status(0) & TIMER_STATUS_TRUN0)
  34. udelay(10);
  35. set_gptimer_config(0, 0x59); /* IRQ enable, periodic, PWM_OUT, SCLKed, OUT PAD disabled */
  36. set_gptimer_period(TIMER0_id, get_sclk() / HZ);
  37. set_gptimer_pwidth(TIMER0_id, 1);
  38. SSYNC();
  39. enable_gptimers(TIMER0bit);
  40. }
  41. #else
  42. void __init setup_core_timer(void)
  43. {
  44. u32 tcount;
  45. /* power up the timer, but don't enable it just yet */
  46. bfin_write_TCNTL(1);
  47. CSYNC();
  48. /* the TSCALE prescaler counter */
  49. bfin_write_TSCALE(TIME_SCALE - 1);
  50. tcount = ((get_cclk() / (HZ * TIME_SCALE)) - 1);
  51. bfin_write_TPERIOD(tcount);
  52. bfin_write_TCOUNT(tcount);
  53. /* now enable the timer */
  54. CSYNC();
  55. bfin_write_TCNTL(7);
  56. }
  57. #endif
  58. static void __init
  59. time_sched_init(irqreturn_t(*timer_routine) (int, void *))
  60. {
  61. #if defined(CONFIG_IPIPE)
  62. setup_system_timer0();
  63. bfin_timer_irq.handler = timer_routine;
  64. setup_irq(IRQ_TIMER0, &bfin_timer_irq);
  65. #else
  66. setup_core_timer();
  67. bfin_timer_irq.handler = timer_routine;
  68. setup_irq(IRQ_CORETMR, &bfin_timer_irq);
  69. #endif
  70. }
  71. #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
  72. /*
  73. * Should return useconds since last timer tick
  74. */
  75. u32 arch_gettimeoffset(void)
  76. {
  77. unsigned long offset;
  78. unsigned long clocks_per_jiffy;
  79. #if defined(CONFIG_IPIPE)
  80. clocks_per_jiffy = bfin_read_TIMER0_PERIOD();
  81. offset = bfin_read_TIMER0_COUNTER() / \
  82. (((clocks_per_jiffy + 1) * HZ) / USEC_PER_SEC);
  83. if ((get_gptimer_status(0) & TIMER_STATUS_TIMIL0) && offset < (100000 / HZ / 2))
  84. offset += (USEC_PER_SEC / HZ);
  85. #else
  86. clocks_per_jiffy = bfin_read_TPERIOD();
  87. offset = (clocks_per_jiffy - bfin_read_TCOUNT()) / \
  88. (((clocks_per_jiffy + 1) * HZ) / USEC_PER_SEC);
  89. /* Check if we just wrapped the counters and maybe missed a tick */
  90. if ((bfin_read_ILAT() & (1 << IRQ_CORETMR))
  91. && (offset < (100000 / HZ / 2)))
  92. offset += (USEC_PER_SEC / HZ);
  93. #endif
  94. return offset;
  95. }
  96. #endif
  97. static inline int set_rtc_mmss(unsigned long nowtime)
  98. {
  99. return 0;
  100. }
  101. /*
  102. * timer_interrupt() needs to keep up the real-time clock,
  103. * as well as call the "do_timer()" routine every clocktick
  104. */
  105. #ifdef CONFIG_CORE_TIMER_IRQ_L1
  106. __attribute__((l1_text))
  107. #endif
  108. irqreturn_t timer_interrupt(int irq, void *dummy)
  109. {
  110. /* last time the cmos clock got updated */
  111. static long last_rtc_update;
  112. write_seqlock(&xtime_lock);
  113. do_timer(1);
  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. if (ntp_synced() &&
  120. xtime.tv_sec > last_rtc_update + 660 &&
  121. (xtime.tv_nsec / NSEC_PER_USEC) >=
  122. 500000 - ((unsigned)TICK_SIZE) / 2
  123. && (xtime.tv_nsec / NSEC_PER_USEC) <=
  124. 500000 + ((unsigned)TICK_SIZE) / 2) {
  125. if (set_rtc_mmss(xtime.tv_sec) == 0)
  126. last_rtc_update = xtime.tv_sec;
  127. else
  128. /* Do it again in 60s. */
  129. last_rtc_update = xtime.tv_sec - 600;
  130. }
  131. write_sequnlock(&xtime_lock);
  132. #ifdef CONFIG_IPIPE
  133. update_root_process_times(get_irq_regs());
  134. #else
  135. update_process_times(user_mode(get_irq_regs()));
  136. #endif
  137. profile_tick(CPU_PROFILING);
  138. return IRQ_HANDLED;
  139. }
  140. void __init time_init(void)
  141. {
  142. time_t secs_since_1970 = (365 * 37 + 9) * 24 * 60 * 60; /* 1 Jan 2007 */
  143. #ifdef CONFIG_RTC_DRV_BFIN
  144. /* [#2663] hack to filter junk RTC values that would cause
  145. * userspace to have to deal with time values greater than
  146. * 2^31 seconds (which uClibc cannot cope with yet)
  147. */
  148. if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) {
  149. printk(KERN_NOTICE "bfin-rtc: invalid date; resetting\n");
  150. bfin_write_RTC_STAT(0);
  151. }
  152. #endif
  153. /* Initialize xtime. From now on, xtime is updated with timer interrupts */
  154. xtime.tv_sec = secs_since_1970;
  155. xtime.tv_nsec = 0;
  156. wall_to_monotonic.tv_sec = -xtime.tv_sec;
  157. time_sched_init(timer_interrupt);
  158. }
  159. /*
  160. * Scheduler clock - returns current time in nanosec units.
  161. */
  162. unsigned long long sched_clock(void)
  163. {
  164. return (unsigned long long)jiffies *(NSEC_PER_SEC / HZ);
  165. }