time.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. };
  25. #if defined(CONFIG_IPIPE)
  26. void __init setup_system_timer0(void)
  27. {
  28. /* Power down the core timer, just to play safe. */
  29. bfin_write_TCNTL(0);
  30. disable_gptimers(TIMER0bit);
  31. set_gptimer_status(0, TIMER_STATUS_TRUN0);
  32. while (get_gptimer_status(0) & TIMER_STATUS_TRUN0)
  33. udelay(10);
  34. set_gptimer_config(0, 0x59); /* IRQ enable, periodic, PWM_OUT, SCLKed, OUT PAD disabled */
  35. set_gptimer_period(TIMER0_id, get_sclk() / HZ);
  36. set_gptimer_pwidth(TIMER0_id, 1);
  37. SSYNC();
  38. enable_gptimers(TIMER0bit);
  39. }
  40. #else
  41. void __init setup_core_timer(void)
  42. {
  43. u32 tcount;
  44. /* power up the timer, but don't enable it just yet */
  45. bfin_write_TCNTL(TMPWR);
  46. CSYNC();
  47. /* the TSCALE prescaler counter */
  48. bfin_write_TSCALE(TIME_SCALE - 1);
  49. tcount = ((get_cclk() / (HZ * TIME_SCALE)) - 1);
  50. bfin_write_TPERIOD(tcount);
  51. bfin_write_TCOUNT(tcount);
  52. /* now enable the timer */
  53. CSYNC();
  54. bfin_write_TCNTL(TAUTORLD | TMREN | TMPWR);
  55. }
  56. #endif
  57. static void __init
  58. time_sched_init(irqreturn_t(*timer_routine) (int, void *))
  59. {
  60. #if defined(CONFIG_IPIPE)
  61. setup_system_timer0();
  62. bfin_timer_irq.handler = timer_routine;
  63. setup_irq(IRQ_TIMER0, &bfin_timer_irq);
  64. #else
  65. setup_core_timer();
  66. bfin_timer_irq.handler = timer_routine;
  67. setup_irq(IRQ_CORETMR, &bfin_timer_irq);
  68. #endif
  69. }
  70. #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
  71. /*
  72. * Should return useconds since last timer tick
  73. */
  74. static u32 blackfin_gettimeoffset(void)
  75. {
  76. unsigned long offset;
  77. unsigned long clocks_per_jiffy;
  78. #if defined(CONFIG_IPIPE)
  79. clocks_per_jiffy = bfin_read_TIMER0_PERIOD();
  80. offset = bfin_read_TIMER0_COUNTER() / \
  81. (((clocks_per_jiffy + 1) * HZ) / USEC_PER_SEC);
  82. if ((get_gptimer_status(0) & TIMER_STATUS_TIMIL0) && offset < (100000 / HZ / 2))
  83. offset += (USEC_PER_SEC / HZ);
  84. #else
  85. clocks_per_jiffy = bfin_read_TPERIOD();
  86. offset = (clocks_per_jiffy - bfin_read_TCOUNT()) / \
  87. (((clocks_per_jiffy + 1) * HZ) / USEC_PER_SEC);
  88. /* Check if we just wrapped the counters and maybe missed a tick */
  89. if ((bfin_read_ILAT() & (1 << IRQ_CORETMR))
  90. && (offset < (100000 / HZ / 2)))
  91. offset += (USEC_PER_SEC / HZ);
  92. #endif
  93. return offset;
  94. }
  95. #endif
  96. /*
  97. * timer_interrupt() needs to keep up the real-time clock,
  98. * as well as call the "xtime_update()" routine every clocktick
  99. */
  100. #ifdef CONFIG_CORE_TIMER_IRQ_L1
  101. __attribute__((l1_text))
  102. #endif
  103. irqreturn_t timer_interrupt(int irq, void *dummy)
  104. {
  105. xtime_update(1);
  106. #ifdef CONFIG_IPIPE
  107. update_root_process_times(get_irq_regs());
  108. #else
  109. update_process_times(user_mode(get_irq_regs()));
  110. #endif
  111. profile_tick(CPU_PROFILING);
  112. return IRQ_HANDLED;
  113. }
  114. void read_persistent_clock(struct timespec *ts)
  115. {
  116. time_t secs_since_1970 = (365 * 37 + 9) * 24 * 60 * 60; /* 1 Jan 2007 */
  117. ts->tv_sec = secs_since_1970;
  118. ts->tv_nsec = 0;
  119. }
  120. void __init time_init(void)
  121. {
  122. #ifdef CONFIG_ARCH_USES_GETTIMEOFFSET
  123. arch_gettimeoffset = blackfin_gettimeoffset;
  124. #endif
  125. #ifdef CONFIG_RTC_DRV_BFIN
  126. /* [#2663] hack to filter junk RTC values that would cause
  127. * userspace to have to deal with time values greater than
  128. * 2^31 seconds (which uClibc cannot cope with yet)
  129. */
  130. if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) {
  131. printk(KERN_NOTICE "bfin-rtc: invalid date; resetting\n");
  132. bfin_write_RTC_STAT(0);
  133. }
  134. #endif
  135. time_sched_init(timer_interrupt);
  136. }