time-ts.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * linux/arch/kernel/time-ts.c
  3. *
  4. * Based on arm clockevents implementation and old bfin time tick.
  5. *
  6. * Copyright(C) 2008, GeoTechnologies, Vitja Makarov
  7. *
  8. * This code is licenced under the GPL version 2. For details see
  9. * kernel-base/COPYING.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/profile.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/time.h>
  15. #include <linux/timex.h>
  16. #include <linux/irq.h>
  17. #include <linux/clocksource.h>
  18. #include <linux/clockchips.h>
  19. #include <asm/blackfin.h>
  20. #ifdef CONFIG_CYCLES_CLOCKSOURCE
  21. static unsigned long cyc2ns_scale;
  22. #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
  23. static inline void set_cyc2ns_scale(unsigned long cpu_khz)
  24. {
  25. cyc2ns_scale = (1000000 << CYC2NS_SCALE_FACTOR) / cpu_khz;
  26. }
  27. static inline unsigned long long cycles_2_ns(cycle_t cyc)
  28. {
  29. return (cyc * cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
  30. }
  31. static cycle_t read_cycles(void)
  32. {
  33. return get_cycles();
  34. }
  35. unsigned long long sched_clock(void)
  36. {
  37. return cycles_2_ns(read_cycles());
  38. }
  39. static struct clocksource clocksource_bfin = {
  40. .name = "bfin_cycles",
  41. .rating = 350,
  42. .read = read_cycles,
  43. .mask = CLOCKSOURCE_MASK(64),
  44. .shift = 22,
  45. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  46. };
  47. static int __init bfin_clocksource_init(void)
  48. {
  49. set_cyc2ns_scale(get_cclk() / 1000);
  50. clocksource_bfin.mult = clocksource_hz2mult(get_cclk(), clocksource_bfin.shift);
  51. if (clocksource_register(&clocksource_bfin))
  52. panic("failed to register clocksource");
  53. return 0;
  54. }
  55. #else
  56. # define bfin_clocksource_init()
  57. #endif
  58. static int bfin_timer_set_next_event(unsigned long cycles,
  59. struct clock_event_device *evt)
  60. {
  61. bfin_write_TCOUNT(cycles);
  62. CSYNC();
  63. return 0;
  64. }
  65. static void bfin_timer_set_mode(enum clock_event_mode mode,
  66. struct clock_event_device *evt)
  67. {
  68. switch (mode) {
  69. case CLOCK_EVT_MODE_PERIODIC: {
  70. unsigned long tcount = ((get_cclk() / (HZ * 1)) - 1);
  71. bfin_write_TCNTL(TMPWR);
  72. CSYNC();
  73. bfin_write_TPERIOD(tcount);
  74. bfin_write_TCOUNT(tcount);
  75. bfin_write_TCNTL(TMPWR | TMREN | TAUTORLD);
  76. CSYNC();
  77. break;
  78. }
  79. case CLOCK_EVT_MODE_ONESHOT:
  80. bfin_write_TCOUNT(0);
  81. bfin_write_TCNTL(TMPWR | TMREN);
  82. CSYNC();
  83. break;
  84. case CLOCK_EVT_MODE_UNUSED:
  85. case CLOCK_EVT_MODE_SHUTDOWN:
  86. bfin_write_TCNTL(0);
  87. CSYNC();
  88. break;
  89. case CLOCK_EVT_MODE_RESUME:
  90. break;
  91. }
  92. }
  93. static void __init bfin_timer_init(void)
  94. {
  95. /* power up the timer, but don't enable it just yet */
  96. bfin_write_TCNTL(TMPWR);
  97. CSYNC();
  98. /*
  99. * the TSCALE prescaler counter.
  100. */
  101. bfin_write_TSCALE(0);
  102. bfin_write_TPERIOD(0);
  103. bfin_write_TCOUNT(0);
  104. /* now enable the timer */
  105. CSYNC();
  106. }
  107. /*
  108. * timer_interrupt() needs to keep up the real-time clock,
  109. * as well as call the "do_timer()" routine every clocktick
  110. */
  111. #ifdef CONFIG_CORE_TIMER_IRQ_L1
  112. __attribute__((l1_text))
  113. #endif
  114. irqreturn_t timer_interrupt(int irq, void *dev_id);
  115. static struct clock_event_device clockevent_bfin = {
  116. .name = "bfin_core_timer",
  117. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  118. .shift = 32,
  119. .cpumask = CPU_MASK_CPU0,
  120. .set_next_event = bfin_timer_set_next_event,
  121. .set_mode = bfin_timer_set_mode,
  122. };
  123. static struct irqaction bfin_timer_irq = {
  124. .name = "Blackfin Core Timer",
  125. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  126. .handler = timer_interrupt,
  127. .dev_id = &clockevent_bfin,
  128. };
  129. irqreturn_t timer_interrupt(int irq, void *dev_id)
  130. {
  131. struct clock_event_device *evt = dev_id;
  132. evt->event_handler(evt);
  133. return IRQ_HANDLED;
  134. }
  135. static int __init bfin_clockevent_init(void)
  136. {
  137. setup_irq(IRQ_CORETMR, &bfin_timer_irq);
  138. bfin_timer_init();
  139. clockevent_bfin.mult = div_sc(get_cclk(), NSEC_PER_SEC, clockevent_bfin.shift);
  140. clockevent_bfin.max_delta_ns = clockevent_delta2ns(-1, &clockevent_bfin);
  141. clockevent_bfin.min_delta_ns = clockevent_delta2ns(100, &clockevent_bfin);
  142. clockevents_register_device(&clockevent_bfin);
  143. return 0;
  144. }
  145. void __init time_init(void)
  146. {
  147. time_t secs_since_1970 = (365 * 37 + 9) * 24 * 60 * 60; /* 1 Jan 2007 */
  148. #ifdef CONFIG_RTC_DRV_BFIN
  149. /* [#2663] hack to filter junk RTC values that would cause
  150. * userspace to have to deal with time values greater than
  151. * 2^31 seconds (which uClibc cannot cope with yet)
  152. */
  153. if ((bfin_read_RTC_STAT() & 0xC0000000) == 0xC0000000) {
  154. printk(KERN_NOTICE "bfin-rtc: invalid date; resetting\n");
  155. bfin_write_RTC_STAT(0);
  156. }
  157. #endif
  158. /* Initialize xtime. From now on, xtime is updated with timer interrupts */
  159. xtime.tv_sec = secs_since_1970;
  160. xtime.tv_nsec = 0;
  161. set_normalized_timespec(&wall_to_monotonic, -xtime.tv_sec, -xtime.tv_nsec);
  162. bfin_clocksource_init();
  163. bfin_clockevent_init();
  164. }